Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 402
z0d14c Avatar asked Sep 20 '25 03:09

z0d14c


2 Answers

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

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!

like image 42
z0d14c Avatar answered Sep 22 '25 16:09

z0d14c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!