4

I am trying to use GitPython to get me diffs in the format I want.

For every file I would like 3 lists. A list of lines that were changed, added, and removed.

I've been looking online and at the documentation and can't seem to figure out how to do this.

3 Answers 3

4

As far as I can tell, there is actually no way to do this.

You'd think they'd implement this functionality, but as far as I can tell file-level diffs are not implemented directly in GitPython.

The only way to retrieve changed lines within GitPython is using the git command directly, like so:

from git import Repo
repo = Repo('path/to/repo')
filename = 'path/to/repo/subdir/myfile'
diff_output = repo.git.diff(filename)

With diff_output now containing the same content you'd see if you did:

$ cd path/to/repo
$ git diff subdir/myfile

So it's not very useful. You're going to probably have to write your own code to parse the diff file to get the information you need.

Frustrating, because I'd like a similar functionality and it looks like my only option really is manually parsing git diffs.

I have to assume that GitPython is primarily used for managing changes to branches, etc and not inspecting or dealing with source code changes on the file level.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I ended up writing my own thing like you said. If I recall correctly, it was a combination of the --word-diff flag of the git diff command and some regular expressions to pull the additions and deletions out.
1

See this documentation. It could be useful.

2 Comments

ive seen it. this is the documentation i was referring to in my question.
The documentation isn't actually very useful in this case. OP is asking about getting file-level differences which as far as I can tell GitPython does not implement.
0

While the top answer lists a way to get text output, there is a way to get diff objects for further evaluation:

from git import Repo
repo = Repo('path/to/repo')
changes = repo.head.commit.diff(None)

returns a list of git.diff.Diff objects for further evalution.

This is documented in https://gitpython.readthedocs.io/en/stable/tutorial.html#obtaining-diff-information:

hcommit = repo.head.commit

hcommit.diff(None) # diff tree against working tree.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.