Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 427
Ciasto piekarz Avatar asked Feb 25 '14 17:02

Ciasto piekarz


People also ask

How do you compare the differences between two commits?

The git diff command is commonly used to get the unstaged changes between the index and working directory. It can be also used to show changes between two arbitrary commits. To view the changes between two commits, you can provide the commit hashes.

What is Pygit?

Pygit2 is a set of Python bindings to the libgit2 shared library, libgit2 implements the core of Git. Pygit2 works with Python 2.7, 3.3, 3.4, 3.5, 3.6 and pypy. It is likely to work with Python 2.6 and 3.1, but these versions are not officially supported.

How does git diff implemented?

In Git, there are four diff algorithms, namely Myers, Minimal, Patience, and Histogram, which are utilized to obtain the differences of the two same files located in two different commits. The Minimal and the Histogram algorithms are the improved versions of the Myers and the Patience respectively.

What does git diff head do?

The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository. The HEAD in the git command refers to the remote repository.


Video Answer


2 Answers

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

```

like image 99
Jon Sonesen Avatar answered Oct 23 '22 13:10

Jon Sonesen


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

like image 32
Ciasto piekarz Avatar answered Oct 23 '22 14:10

Ciasto piekarz