问题
I'm a Python/Git newb but I'm trying to write a script that takes two branches or commits as parameters and shows a list of changed files between the two, not all the extraneous info that comes with a regular diff.
This was accomplished in bash scripting by using
git diff --name-only FIRSTBRANCH...SECONDBRANCH
but it isn't translating as easily to Python scripting using gitpython. If anyone has any idea how to do this, that'd be great.
edit: heres some code
user = str(sys.argv[1])
password = str(sys.argv[2])
currentBranch = str(sys.argv[3])
compBranch = str(sys.argv[4])
repo = Repo(directory)
currentCommit = repo.commit(currentBranch)
compCommit = repo.commit(compBranch)
diffed = repo.diff(currentBranch, compBranch)
print diff will return all the diff details when I only want a list of changed files
回答1:
Here is a way to do this in python.
#Dif two branches, returns list
import git
def gitDiff(branch1, branch2):
format = '--name-only'
commits = []
g = git.Git('/path/to/git/repo')
differ = g.diff('%s..%s' % (branch1, branch2), format).split("\n")
for line in differ:
if len(line):
commits.append(line)
#for commit in commits:
# print '*%s' % (commit)
return commits
回答2:
Fixed or at least on the right track with the following (inspired by someone who deleted their answer... thanks, guy)
subprocess.check_output(['git', 'diff', '--name-only', currentBranch + '..' + compBranch])
This basically does what I need it to, although if there is a more elegant solution I'd love to hear it!
来源:https://stackoverflow.com/questions/25556696/get-a-list-of-changed-files-between-two-commits-or-branches