Skip to content

Commit c197702

Browse files
wpalmergitster
authored andcommitted
pretty: Respect --abbrev option
Prior to this, the output of git log -1 --format=%h was always 7 characters long, without regard to whether --abbrev had been passed. Signed-off-by: Will Palmer <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6003724 commit c197702

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

builtin-shortlog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
162162
sha1_to_hex(commit->object.sha1));
163163
if (log->user_format) {
164164
struct pretty_print_context ctx = {0};
165-
ctx.abbrev = DEFAULT_ABBREV;
165+
ctx.abbrev = log->abbrev;
166166
ctx.subject = "";
167167
ctx.after_subject = "";
168168
ctx.date_mode = DATE_NORMAL;
@@ -290,6 +290,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
290290
}
291291

292292
log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
293+
log.abbrev = rev.abbrev;
293294

294295
/* assume HEAD if from a tty */
295296
if (!nongit && !rev.pending.nr && isatty(0))

pretty.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
716716
if (add_again(sb, &c->abbrev_commit_hash))
717717
return 1;
718718
strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
719-
DEFAULT_ABBREV));
719+
c->pretty_ctx->abbrev));
720720
c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
721721
return 1;
722722
case 'T': /* tree hash */
@@ -726,7 +726,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
726726
if (add_again(sb, &c->abbrev_tree_hash))
727727
return 1;
728728
strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
729-
DEFAULT_ABBREV));
729+
c->pretty_ctx->abbrev));
730730
c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
731731
return 1;
732732
case 'P': /* parent hashes */
@@ -743,7 +743,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
743743
if (p != commit->parents)
744744
strbuf_addch(sb, ' ');
745745
strbuf_addstr(sb, find_unique_abbrev(
746-
p->item->object.sha1, DEFAULT_ABBREV));
746+
p->item->object.sha1,
747+
c->pretty_ctx->abbrev));
747748
}
748749
c->abbrev_parent_hashes.len = sb->len -
749750
c->abbrev_parent_hashes.off;

shortlog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct shortlog {
1212
int in1;
1313
int in2;
1414
int user_format;
15+
int abbrev;
1516

1617
char *common_repo_prefix;
1718
int email;

t/t4201-shortlog.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test_expect_success 'pretty format' '
7979
test_cmp expect log.predictable
8080
'
8181

82-
test_expect_failure '--abbrev' '
82+
test_expect_success '--abbrev' '
8383
sed s/SUBJECT/OBJID/ expect.template >expect &&
8484
git shortlog --format="%h" --abbrev=5 HEAD >log &&
8585
fuzz log >log.predictable &&

t/t6006-rev-list-format.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ test_expect_success 'add LF before non-empty (2)' '
191191
grep "^$" actual
192192
'
193193

194+
test_expect_success '--abbrev' '
195+
echo SHORT SHORT SHORT >expect2 &&
196+
echo LONG LONG LONG >expect3 &&
197+
git log -1 --format="%h %h %h" HEAD >actual1 &&
198+
git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
199+
git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
200+
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
201+
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
202+
test_cmp expect2 fuzzy2 &&
203+
test_cmp expect3 fuzzy3 &&
204+
! test_cmp actual1 actual2
205+
'
206+
207+
test_expect_success '%H is not affected by --abbrev-commit' '
208+
git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
209+
len=$(wc -c <actual) &&
210+
test $len = 41
211+
'
212+
213+
test_expect_success '%h is not affected by --abbrev-commit' '
214+
git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
215+
len=$(wc -c <actual) &&
216+
test $len = 21
217+
'
218+
194219
test_expect_success '"%h %gD: %gs" is same as git-reflog' '
195220
git reflog >expect &&
196221
git log -g --format="%h %gD: %gs" >actual &&
@@ -203,6 +228,12 @@ test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
203228
test_cmp expect actual
204229
'
205230

231+
test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
232+
git reflog --abbrev=13 --date=raw >expect &&
233+
git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
234+
test_cmp expect actual
235+
'
236+
206237
test_expect_success '%gd shortens ref name' '
207238
echo "master@{0}" >expect.gd-short &&
208239
git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&

0 commit comments

Comments
 (0)