how to do a git diff of current commit with last commit using gitpython?
Asked Answered
L

5

6

I am trying to grasp gitpython module,

hcommit = repo.head.commit
tdiff = hcommit.diff('HEAD~1')

but tdiff = hcommit.diff('HEAD^ HEAD') doesn't work !! neither does ('HEAD~ HEAD').,

I am trying to get the diff output !

Liquefacient answered 25/2, 2014 at 17:34 Comment(1)
I admit to never having used the gitpython code, but it seems obvious that if hcommit is repo.head.commit, it's bound to that particular commit, and thus hcommit.diff means "diff that particular commit, against something else". To get diffs of two arbitrary commits, you'd have to choose some other starting-point.Amorphism
L
1

I figured out how to get the git diff using gitPython.

import git
repo = git.Repo("path/of/repo/")

# the below gives us all commits
repo.commits()

# take the first and last commit

a_commit = repo.commits()[0]
b_commit = repo.commits()[1]

# now get the diff
repo.diff(a_commit,b_commit)

Voila !!!

Liquefacient answered 26/2, 2014 at 7:33 Comment(2)
I get AttributeError: 'Repo' object has no attribute 'diff', and Repo.diff is not mentioned in the API doc. Does this answer need to be updated?Coagulate
I haven't found commits() method for repo I have done : import git repo_url = "<REPO URL>" repo = git.Repo(repo_url) commit_list = list(repo.iter_commits())[0]Heterography
C
8
import git

repo = git.Repo('repo_path')
commits_list = list(repo.iter_commits())

# --- To compare the current HEAD against the bare init commit
a_commit = commits_list[0]
b_commit = commits_list[-1]

a_commit.diff(b_commit)

This will return a diff object for the commits. There are other ways to achieve this as well. For example (this is copy/pasted from http://gitpython.readthedocs.io/en/stable/tutorial.html#obtaining-diff-information):

```

    hcommit = repo.head.commit
    hcommit.diff()                  # diff tree against index
    hcommit.diff('HEAD~1')          # diff tree against previous tree
    hcommit.diff(None)              # diff tree against working tree

    index = repo.index
    index.diff()                    # diff index against itself yielding empty diff
    index.diff(None)                # diff index against working copy
    index.diff('HEAD')              # diff index against current HEAD tree

```

Conversational answered 22/9, 2015 at 16:37 Comment(0)
H
2

To get the contents of the diff:

import git
repo = git.Repo("path/of/repo/")

# define a new git object of the desired repo
gitt = repo.git
diff_st = gitt.diff("commitID_A", "commitID_B")
Hewett answered 31/3, 2016 at 18:19 Comment(0)
L
1

I figured out how to get the git diff using gitPython.

import git
repo = git.Repo("path/of/repo/")

# the below gives us all commits
repo.commits()

# take the first and last commit

a_commit = repo.commits()[0]
b_commit = repo.commits()[1]

# now get the diff
repo.diff(a_commit,b_commit)

Voila !!!

Liquefacient answered 26/2, 2014 at 7:33 Comment(2)
I get AttributeError: 'Repo' object has no attribute 'diff', and Repo.diff is not mentioned in the API doc. Does this answer need to be updated?Coagulate
I haven't found commits() method for repo I have done : import git repo_url = "<REPO URL>" repo = git.Repo(repo_url) commit_list = list(repo.iter_commits())[0]Heterography
G
1

For a proper solution (without using git command callback), you have to use create_patch option.

To compare current index with previous commit:

diff_as_patch = repo.index.diff(repo.commit('HEAD~1'), create_patch=True)
print(diff_as_patch)
Gluck answered 20/1, 2016 at 16:23 Comment(0)
B
1

Sorry to dig up an old thread... If you need to find out which files changed, you can use the .a_path or .b_path attribute of a diff object depending on which one you feed it first. In the example below I'm using the head commit as a so I look at a.

e.g:

# this will compare the most recent commit to the one prior to find out what changed.

from git import repo

repo = git.Repo("path/to/.git directory")
repoDiffs = repo.head.commit.diff('HEAD~1')

for item in repoDiffs:
    print(item.a_path)
Braze answered 25/10, 2019 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.