Cmag
Cmag

Reputation: 15750

List file that have changed since last commit with GitPython

I need to have the Python script read in the files that have changed since the last Git commit. Using GitPython, how would I get the same output as running from cli:

$ git diff --name-only HEAD~1 HEAD

I can do something like the following, however, I only need the file names:

hcommit = repo.head.commit
for diff_added in hcommit.diff('HEAD~1').iter_change_type('A'):
    print(diff_added)    

Upvotes: 27

Views: 9961

Answers (1)

alecxe
alecxe

Reputation: 473843

You need to pass the name_only keyword argument - it would automatically be used as --name-only command-line option when a git command would be issued.

The following is the equivalent of git diff --name-only HEAD~1..HEAD:

diff = repo.git.diff('HEAD~1..HEAD', name_only=True)
print(diff)

Upvotes: 28

Related Questions