gitpython and git diff gitpython and git diff git git

gitpython and git diff


If you want to access the contents of the diff, try this:

repo = git.Repo(repo_root.as_posix())commit_dev = repo.commit("dev")commit_origin_dev = repo.commit("origin/dev")diff_index = commit_origin_dev.diff(commit_dev)for diff_item in diff_index.iter_change_type('M'):    print("A blob:\n{}".format(diff_item.a_blob.data_stream.read().decode('utf-8')))    print("B blob:\n{}".format(diff_item.b_blob.data_stream.read().decode('utf-8'))) 

This will print the contents of each file.


You can use GitPython with the git command "diff", just need to use the "tree" object of each commit or the branch for that you want to see the diffs, for example:

repo = Repo('/git/repository')t = repo.head.commit.treerepo.git.diff(t)

This will print "all" the diffs for all files included in this commit, so if you want each one you must iterate over them.

With the actual branch it's:

repo.git.diff('HEAD~1')

Hope this help, regards.


Git does not store the diffs, as you have noticed. Given two blobs (before and after a change), you can use Python's difflib module to compare the data.