Using, pygit2, I can get the total number of files changed, total insertions, total deletions, and the relative path of the files. See below code. However, I can't find a way to get the stats on lines changed for each modified files like git diff --stats shows. Can I do it in pygit2?
def get_diff_stats(repo_path, commit_a, commit_b):
repo = Repository("{}/.git".format(repo_path))
diff = repo.diff(commit_a, commit_b)
print(diff.stats.files_changed)
print(diff.stats.insertions)
print(diff.stats.deletions)
for delta in diff.deltas:
print(delta.new_file.path, delta.status_char())
Not entirely sure if you still care but I found your question and needed the answer myself. Ended up figuring it out by reading some rather dry documentation.
As you can see, a diff can contain multiple Patches and Diffs. Because of this, we need to loop over them. The Diff object behaves as a collection (this is not really clear from the documentation). Patches contain the info we need. The actual lines changed can be found in the Hunks. This is a term from the GNU diff utils documentation and describes the changes + some context.