Search code examples
gitgithubgitpython

What's the difference between gitpython's diff and git log's diff?


I don't understand the difference between the output of git log -p and that of gitpython in terms of diffs.

For example, for some merge commit f534e1..., git log -p gives:

commit f534e1...
Merge: ....
Author: ....
Date:   ...

Merge pull request ... from ...

with no diff, which I assume to be correct (the diff comes with the next log entry - one of the parents of f534e1...).

I would like to achieve the same effect with gitpython. I'm trying:

repo = Repo("...")
for c in repo.iter_commits():
  print c.hexsha
  print c.summary
  print c.diff()

I get:

f534e1...
Merge pull request ... from ...
[<git.diff.Diff object at 0x102cd3490>]

with some diff.

What diff is this? Why do I get it here? How can I mimic the behavior of git log -p?


Solution

  • According to the documentation, c.diff() will compare the commit against the index, i.e. staged changes.

    It appears that git log -p will produce a special-format diff against all of the given commit's parents. Something roughly comparable can be achieved with the following code (based on your example).

    repo = Repo("...")
    for c in repo.iter_commits():
      print c.hexsha
      print c.summary
      for p in c.parents:
        handle_diff(c.diff(p))
    

    The latter would yield a Diff object with all relevant information.

    If what you really want is the exact format produced by git log -p, you may also call repo.git.log(p=True) and parse the output yourself.