Search code examples
pythongitbashshellgitpython

Get a list of changed files between two commits or branches


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


Solution

  • 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!