简体   繁体   中英

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?

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM