Skip to content

Commit c2ebaa2

Browse files
peffgitster
authored andcommitted
blame: only coalesce lines that are adjacent in result
After blame has finished but before we produce any output, we coalesce groups of lines that were adjacent in the original suspect (which may have been split apart by lines in intermediate commits which went away). However, this can cause incorrect output if the lines are not also adjacent in the result. For instance, the case in t8003 has: ABC DEF which becomes ABC SPLIT DEF Blaming only lines 1 and 3 in the result yields two blame groups (one for each line) that were adjacent in the original. That's enough for us to coalesce them into a single group, but that loses information: our output routines assume they're adjacent in the result as well, and we output: <oid> 1) ABC <oid> 2) SPLIT This is nonsense for two reasons: - we were asked about line 3, not line 2; we should not output the SPLIT line at all - commit <oid> did not touch the SPLIT line at all! We found the correct blame for line 3, but the bug is actually in the output stage, which is showing the wrong line number and content from the final file. We can fix this by only coalescing when both the suspect and result lines are adjacent. That fixes this bug, but keeps coalescing in cases where want it (e.g., the existing test in t8003 where SPLIT goes away, and the lines really are adjacent in the result). Reported-by: Nuthan Munaiah <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dd7c611 commit c2ebaa2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

blame.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ void blame_coalesce(struct blame_scoreboard *sb)
11841184
for (ent = sb->ent; ent && (next = ent->next); ent = next) {
11851185
if (ent->suspect == next->suspect &&
11861186
ent->s_lno + ent->num_lines == next->s_lno &&
1187+
ent->lno + ent->num_lines == next->lno &&
11871188
ent->ignored == next->ignored &&
11881189
ent->unblamable == next->unblamable) {
11891190
ent->num_lines += next->num_lines;

t/t8003-blame-corner-cases.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,13 @@ test_expect_success 'blame coalesce' '
311311
test_cmp expect actual
312312
'
313313

314+
test_expect_success 'blame does not coalesce non-adjacent result lines' '
315+
cat >expect <<-EOF &&
316+
$orig 1) ABC
317+
$orig 3) DEF
318+
EOF
319+
git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
320+
test_cmp expect actual
321+
'
322+
314323
test_done

0 commit comments

Comments
 (0)