0

I am looking for a way to show only the conflicting part of merge commits in a git log, preferably along with all the other (non-merge) diffs from a history I get with git log --numstat. There is a question similar to mine that was already answered: Show conflict diff part of a merge. I can amend one answer to

git diff hash hash^1 hash^2 --numstat

but this does only allow me to show the diff for one commit at the time and not embedded in the history. Ultimately, I want to reconstruct the number of lines in each file from a log (cumulative sum of insertions - deletions), which works just fine for now as long as there were no merge conflicts.

If I use the full diff for merges (e.g. with git log --numstat -m), I don't get the diff to the immediate parent, but a diff including some changes that are reported in earlier non-merge commits already, which messes up my counting of insertions/deletions.

I know I may ask too much from git log with such a specific use case. I can work around with with the answer linked above, but it's just much more work.

Any help appreciated.

Lorenz Walthert
  • 4,414
  • 1
  • 18
  • 24
  • Untested, but logically should work: `git log --merges -p --cc --numstat`. (Be sure you know what `git diff --cc` is showing as this includes non-conflicting file changes as well as conflicting ones; it simply throws out some guaranteed-completely-unconflicted *files*.) – torek Feb 02 '18 at 16:05

1 Answers1

1

Ultimately, I want to reconstruct the number of lines in each file from a log (cumulative sum of insertions - deletions), which works just fine for now as long as there were no merge conflicts.

If I understand correctly that the goal is to find the number of lines of all files for a certain commit, there's a way that doesn't require reconstruction at all:

git diff-tree --numstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904..$COMMIT

Where $COMMIT would be the desired commit hash.

This uses diff-tree to compare the empty tree with the tree of the desired commit and produces a direct summarized stat.

This can also be put into a loop to get the line count of all files along the full history:

$ for COMMIT in $(git rev-list master)
do
    git diff-tree --numstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904..$COMMIT
done

It can also be limited to specific files using the usual -- file1.txt file2.txt ... suffix.

For reference, the empty tree hash is what you get when hashing an empty string as a tree object:

$ echo -n | git hash-object -t tree --stdin
4b825dc642cb6eb9a060e54bf8d69288fbee4904
mmlr
  • 1,895
  • 11
  • 17
  • Thanks @mml. The thing is that the [R package](https://github.com/lorenzwalthert/gitsum) I develop has the primary goal of parsing a log. From there, it seemed to be a natural extension to also report the changed lines, but with name changes (which I managed to sort out) and merge conflicts, it seemed not quite as straight forward as I thought initially. So I wonder whether I should go all the way to do what you outlined above (for every file for every commit, first in bash, pipe to R) or whether I can "enrich" the log with some diff information. I will see going forward what's more suitable. – Lorenz Walthert Feb 03 '18 at 21:39