Skip to content

Commit 2b15969

Browse files
dyronegitster
authored andcommitted
range-diff: let '--abbrev' option takes effect
As mentioned in 'git-range-diff.txt': "`git range-diff` also accepts the regular diff options (see linkgit:git-diff[1])...", but '--abbrev' is not in the "regular" scope. In Git, the "abbrev" of an object may not be a fixed value in different repositories, depending on the needs of the them(Linus mentioned in e6c587c in 2016: "the Linux kernel project needs 11 to 12 hexdigits" at that time ), that's why a user may want to display abbrev according to a specified length. Although a similar effect can be achieved through configuration (like: git -c core.abbrev=<abbrev>), but based on ease of use (many users may not know that the -c option can be specified) and the description in existing document, supporting users to directly use '--abbrev', could be a good way. Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9d677b commit 2b15969

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

range-diff.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,14 @@ static void output_pair_header(struct diff_options *diffopt,
383383
const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
384384
const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
385385
const char *color;
386+
char abbrev = diffopt->abbrev;
387+
388+
if (abbrev < 0)
389+
abbrev = DEFAULT_ABBREV;
386390

387391
if (!dashes->len)
388392
strbuf_addchars(dashes, '-',
389-
strlen(find_unique_abbrev(oid,
390-
DEFAULT_ABBREV)));
393+
strlen(find_unique_abbrev(oid, abbrev)));
391394

392395
if (!b_util) {
393396
color = color_old;
@@ -409,7 +412,7 @@ static void output_pair_header(struct diff_options *diffopt,
409412
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
410413
else
411414
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
412-
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
415+
find_unique_abbrev(&a_util->oid, abbrev));
413416

414417
if (status == '!')
415418
strbuf_addf(buf, "%s%s", color_reset, color);
@@ -421,7 +424,7 @@ static void output_pair_header(struct diff_options *diffopt,
421424
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
422425
else
423426
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
424-
find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
427+
find_unique_abbrev(&b_util->oid, abbrev));
425428

426429
commit = lookup_commit_reference(the_repository, oid);
427430
if (commit) {

t/t3206-range-diff.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ test_expect_success 'setup' '
3333
u3 sha256:736c4bc
3434
u4 sha256:673e77d
3535
36+
# topic (abbrev=10)
37+
t1_abbrev sha1:4de457d2c0
38+
t2_abbrev sha1:fccce22f8c
39+
t3_abbrev sha1:147e64ef53
40+
t4_abbrev sha1:a63e992599
41+
t1_abbrev sha256:b89f8b9092
42+
t2_abbrev sha256:5f12aadf34
43+
t3_abbrev sha256:ea8b273a6c
44+
t4_abbrev sha256:14b73361fc
45+
46+
# unmodified (abbrev=10)
47+
u1_abbrev sha1:35b9b25f76
48+
u2_abbrev sha1:de345ab3de
49+
u3_abbrev sha1:9af6654000
50+
u4_abbrev sha1:2901f773f3
51+
u1_abbrev sha256:e3731be242
52+
u2_abbrev sha256:14fadf8cee
53+
u3_abbrev sha256:736c4bcb44
54+
u4_abbrev sha256:673e77d589
55+
3656
# reordered
3757
r1 sha1:aca177a
3858
r2 sha1:14ad629
@@ -153,6 +173,18 @@ test_expect_success 'simple A B C (unmodified)' '
153173
test_cmp expect actual
154174
'
155175

176+
test_expect_success 'simple A..B A..C (unmodified) with --abbrev' '
177+
git range-diff --no-color --abbrev=10 main..topic main..unmodified \
178+
>actual &&
179+
cat >expect <<-EOF &&
180+
1: $(test_oid t1_abbrev) = 1: $(test_oid u1_abbrev) s/5/A/
181+
2: $(test_oid t2_abbrev) = 2: $(test_oid u2_abbrev) s/4/A/
182+
3: $(test_oid t3_abbrev) = 3: $(test_oid u3_abbrev) s/11/B/
183+
4: $(test_oid t4_abbrev) = 4: $(test_oid u4_abbrev) s/12/B/
184+
EOF
185+
test_cmp expect actual
186+
'
187+
156188
test_expect_success 'A^! and A^-<n> (unmodified)' '
157189
git range-diff --no-color topic^! unmodified^-1 >actual &&
158190
cat >expect <<-EOF &&

0 commit comments

Comments
 (0)