Skip to content

Commit 9122983

Browse files
peffgitster
authored andcommitted
blame: fix alignment with --abbrev=40
The blame command internally adds 1 to any requested sha1 abbreviation length, and then subtracts it when outputting a boundary commit. This lets regular and boundary sha1s line up visually, but it misses one corner case. When the requested length is 40, we bump the value to 41. But since we only have 40 characters, that's all we can show (fortunately the truncation is done by a printf precision field, so it never tries to read past the end of the buffer). So a normal sha1 shows 40 hex characters, and a boundary sha1 shows "^" plus 40 hex characters. The result is misaligned. The "-l" option to show long sha1s gets around this by skipping the "abbrev" variable entirely and just always using GIT_SHA1_HEXSZ. This avoids the "+1" issue, but it does mean that boundary commits only have 39 characters printed. This is somewhat odd, but it does look good visually: the results are aligned and left-justified. The alternative would be to allocate an extra column that would contain either an extra space or the "^" boundary marker. As this is by definition the human-readable view, it's probably not that big a deal either way (and of course --porcelain, etc, correctly produce correct 40-hex sha1s). But for consistency, this patch teaches --abbrev=40 to produce the same output as "-l" (always left-aligned, with 40-hex for normal sha1s, and "^" plus 39-hex for boundaries). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3808ca commit 9122983

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
26062606
} else if (show_progress < 0)
26072607
show_progress = isatty(2);
26082608

2609-
if (0 < abbrev)
2609+
if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
26102610
/* one more abbrev length is needed for the boundary commit */
26112611
abbrev++;
26122612

t/t8002-blame.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,32 @@ test_expect_success 'blame with showEmail config true' '
8686
test_cmp expected_n result
8787
'
8888

89+
test_expect_success 'set up abbrev tests' '
90+
test_commit abbrev &&
91+
sha1=$(git rev-parse --verify HEAD) &&
92+
check_abbrev () {
93+
expect=$1; shift
94+
echo $sha1 | cut -c 1-$expect >expect &&
95+
git blame "$@" abbrev.t >actual &&
96+
perl -lne "/[0-9a-f]+/ and print \$&" <actual >actual.sha &&
97+
test_cmp expect actual.sha
98+
}
99+
'
100+
101+
test_expect_success 'blame --abbrev=<n> works' '
102+
# non-boundary commits get +1 for alignment
103+
check_abbrev 31 --abbrev=30 HEAD &&
104+
check_abbrev 30 --abbrev=30 ^HEAD
105+
'
106+
107+
test_expect_success 'blame -l aligns regular and boundary commits' '
108+
check_abbrev 40 -l HEAD &&
109+
check_abbrev 39 -l ^HEAD
110+
'
111+
112+
test_expect_success 'blame --abbrev=40 behaves like -l' '
113+
check_abbrev 40 --abbrev=40 HEAD &&
114+
check_abbrev 39 --abbrev=40 ^HEAD
115+
'
116+
89117
test_done

0 commit comments

Comments
 (0)