How do merge commits affect the blame implementation in git

intro

diff is a handy tool to diff files on unix system. Intuitively, the same algorithm can be applied in git blame against commits to display differences, which is pretty straightforward. But taking git merger into consideration, which could have more than one parents and shouldn't take any blames itself, we may find that a simple diff doesn't work out in this specific scenario.

why diff only is not enough

At first, it's tempting to use `diff`` directly as the underlying tool to generate the blames as diff can show the exact changes already: both the deletions on the left and additions on the riht. This means that we can get what have changed in both sides with exact locus.

In the following test, we can see that lines from 3 to 5 are added in the right-hand side file, so we can say 222.txt is the scapegoat for these three lines.

tsecer@harry: cat 111.txt 
i111111
i111111
i111111
i111111
i111111
i111111
i111111
i111111
i111111
i111111
i111111
tsecer@harry: cat 222.txt 
i111111
i111111
2222222
2222222
2222222
i111111
i111111
i111111
i111111
i111111
i111111
i111111
i111111
tsecer@harry: diff 111.txt 222.txt 
2a3,5
> 2222222
> 2222222
> 2222222
11d13
< i111111
tsecer@harry: 

the crux

But the previous method doesn't work out when git merge meddles in: the additions may come from other parents or the merge commit itself. A difference against one single parent does't necessarily mean it's due to the merge commit itself.

This may sound confusing, so here is an example:

tsecer@harry: git log --graph --oneline
*   2833046 (HEAD -> master) Merge branch '111'
|\  
| * e9c2868 (111) 111
* | 98600cb 222
|/  
* 40cd85b 000
tsecer@harry: git blamce 000.txt 
git: 'blamce' is not a git command. See 'git --help'.

The most similar command is
	blame
tsecer@harry: 
tsecer@harry: 
tsecer@harry: git blame 000.txt 
^40cd85b (harry once upon a time +0800  1) 0
e9c28685 (harry once upon a time +0800  2) 1
^40cd85b (harry once upon a time +0800  3) 0
^40cd85b (harry once upon a time +0800  4) 0
^40cd85b (harry once upon a time +0800  5) 0
^40cd85b (harry once upon a time +0800  6) 0
98600cbb (harry once upon a time +0800  7) 2
^40cd85b (harry once upon a time +0800  8) 0
^40cd85b (harry once upon a time +0800  9) 0
28330464 (harry once upon a time +0800 10) 3
tsecer@harry: 

In file 000.txt from the merged commit, line 2 comes from parent branch 111; line 7 comes from parent branch 222; the merge commit itself also modifies line 10.
The question is how the blames can be tracked with the premise that diff can only be applied against two files at one time. Obviously,

the catch

So the diff parts can't be relied on to generate the scapegoats, in stead, the same parts should be exonerate the culprit: all modifications are assumed to be made by current commit, unless there are evidence showing that they are the same with one of the commit's parent.

The core code in git lives in this function(Given it's so important, I exerpt the whole function):

///@file: git/blame.c
/*
 * Process one hunk from the patch between the current suspect for
 * blame_entry e and its parent.  This first blames any unfinished
 * entries before the chunk (which is where target and parent start
 * differing) on the parent, and then splits blame entries at the
 * start and at the end of the difference region.  Since use of -M and
 * -C options may lead to overlapping/duplicate source line number
 * ranges, all we can rely on from sorting/merging is the order of the
 * first suspect line number.
 *
 * tlno: line number in the target where this chunk begins
 * same: line number in the target where this chunk ends
 * offset: add to tlno to get the chunk starting point in the parent
 * parent_len: number of lines in the parent chunk
 */
static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
			int tlno, int offset, int same, int parent_len,
			struct blame_origin *parent,
			struct blame_origin *target, int ignore_diffs)
{
	struct blame_entry *e = **srcq;
	struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL;
	struct blame_line_tracker *line_blames = NULL;

	while (e && e->s_lno < tlno) {
		struct blame_entry *next = e->next;
		/*
		 * current record starts before differing portion.  If
		 * it reaches into it, we need to split it up and
		 * examine the second part separately.
		 */
		if (e->s_lno + e->num_lines > tlno) {
			/* Move second half to a new record */
			struct blame_entry *n;

			n = split_blame_at(e, tlno - e->s_lno, e->suspect);
			/* Push new record to diffp */
			n->next = diffp;
			diffp = n;
		} else
			blame_origin_decref(e->suspect);
		/* Pass blame for everything before the differing
		 * chunk to the parent */
		e->suspect = blame_origin_incref(parent);
		e->s_lno += offset;
		e->next = samep;
		samep = e;
		e = next;
	}
	/*
	 * As we don't know how much of a common stretch after this
	 * diff will occur, the currently blamed parts are all that we
	 * can assign to the parent for now.
	 */

	if (samep) {
		**dstq = reverse_blame(samep, **dstq);
		*dstq = &samep->next;
	}
	/*
	 * Prepend the split off portions: everything after e starts
	 * after the blameable portion.
	 */
	e = reverse_blame(diffp, e);

	/*
	 * Now retain records on the target while parts are different
	 * from the parent.
	 */
	samep = NULL;
	diffp = NULL;

	if (ignore_diffs && same - tlno > 0) {
		CALLOC_ARRAY(line_blames, same - tlno);
		guess_line_blames(parent, target, tlno, offset, same,
				  parent_len, line_blames);
	}

	while (e && e->s_lno < same) {
		struct blame_entry *next = e->next;

		/*
		 * If current record extends into sameness, need to split.
		 */
		if (e->s_lno + e->num_lines > same) {
			/*
			 * Move second half to a new record to be
			 * processed by later chunks
			 */
			struct blame_entry *n;

			n = split_blame_at(e, same - e->s_lno,
					   blame_origin_incref(e->suspect));
			/* Push new record to samep */
			n->next = samep;
			samep = n;
		}
		if (ignore_diffs) {
			ignore_blame_entry(e, parent, &diffp, &ignoredp,
					   line_blames + e->s_lno - tlno);
		} else {
			e->next = diffp;
			diffp = e;
		}
		e = next;
	}
	free(line_blames);
	if (ignoredp) {
		/*
		 * Note ignoredp is not sorted yet, and thus neither is dstq.
		 * That list must be sorted before we queue_blames().  We defer
		 * sorting until after all diff hunks are processed, so that
		 * guess_line_blames() can pick *any* line in the parent.  The
		 * slight drawback is that we end up sorting all blame entries
		 * passed to the parent, including those that are unrelated to
		 * changes made by the ignored commit.
		 */
		**dstq = reverse_blame(ignoredp, **dstq);
		*dstq = &ignoredp->next;
	}
	**srcq = reverse_blame(diffp, reverse_blame(samep, e));
	/* Move across elements that are in the unblamable portion */
	if (diffp)
		*srcq = &diffp->next;
}

Artificial chunk

Once you've unerstood the main idea, the implementation should be much easier to comprehend, which is the missing step that puzzled me quite a while.

Then code under the comment "The rest are the same as the parent" literally tells blame_chunk that the next diff start from INT_MAX``, which makes the remaining part in the file identical to it's parent, assuming the file line never exceeding INT_MAX`.

static void pass_blame_to_parent(struct blame_scoreboard *sb,
				 struct blame_origin *target,
				 struct blame_origin *parent, int ignore_diffs)
{
///...

	if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
		die("unable to generate diff (%s -> %s)",
		    oid_to_hex(&parent->commit->object.oid),
		    oid_to_hex(&target->commit->object.oid));
	/* The rest are the same as the parent */
	blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
		    parent, target, 0);
///...
}

outro

I know this article reads pretty abstruse, but the whole idea is that there is something that I didn't realize before. The answer itself even doesn't matter that much as it's always been there and you can always found it if you want to.

posted on 2026-11-29 11:55  tsecer  阅读(7)  评论(0)    收藏  举报

导航