Leopoldo
Leopoldo

Reputation: 825

how to construct git diff query in python

I'm using the recommended GitPython module, but I cannot figure out how to construct the following command:

git diff --name-status ec04352 b945e6c 

I would like to get information about all modified files between two commits, and this command does exactly what I want to do. Could you comment on it?

Upvotes: 1

Views: 2063

Answers (2)

mhawke
mhawke

Reputation: 87074

This is one way to do it:

import git

repo = git.Repo('path/to/your/repo')
print repo.git.diff('ec04352', 'b945e6c', **{'name-status': True})

It is, however, going through the backdoor.

You should be able to do something like this:

a = repo.commit('ec04352')
b = repo.commit('b945e6c')
diffs = a.diff(b)

>>> a
<git.Commit "ec04352">
>>> b
<git.Commit "b945e6c">
>>> print diffs[0]
zip/JSONzip.java
=======================================================
lhs: 100644 | d8e3ac652a5a5158692fa5fc131340c03dffd08e
rhs: 100644 | 220686de3dcb0dd17a54cbc5f8e44df261b664d5
>>> 

You'll need to play with the Diff object to figure out the difference.

Upvotes: 6

MatsLindh
MatsLindh

Reputation: 52802

See Obtaining Diff Information in the GitPython manual for a few example on how to get the diff information between two commits.

hcommit = repo.head.commit
idiff = hcommit.diff()          # diff tree against index
tdiff = hcommit.diff('HEAD~1')  # diff tree against previous tree
wdiff = hcommit.diff(None)      # diff tree against working tree

These commands return a DiffIndex, which contain iter_change_type which you can call with each of the four different change types ('A', 'D', 'R', 'M') to get the paths that have been changed (added, deleted, renamed, modified).

Upvotes: 2

Related Questions