Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <wmpalmer@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Will Palmer authored and Junio C Hamano committed May 4, 2010
1 parent 6003724 commit c197702
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion builtin-shortlog.c
Expand Up @@ -162,7 +162,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
sha1_to_hex(commit->object.sha1));
if (log->user_format) {
struct pretty_print_context ctx = {0};
ctx.abbrev = DEFAULT_ABBREV;
ctx.abbrev = log->abbrev;
ctx.subject = "";
ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL;
Expand Down Expand Up @@ -290,6 +290,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
}

log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
log.abbrev = rev.abbrev;

/* assume HEAD if from a tty */
if (!nongit && !rev.pending.nr && isatty(0))
Expand Down
7 changes: 4 additions & 3 deletions pretty.c
Expand Up @@ -716,7 +716,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
if (add_again(sb, &c->abbrev_commit_hash))
return 1;
strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
DEFAULT_ABBREV));
c->pretty_ctx->abbrev));
c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
return 1;
case 'T': /* tree hash */
Expand All @@ -726,7 +726,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
if (add_again(sb, &c->abbrev_tree_hash))
return 1;
strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
DEFAULT_ABBREV));
c->pretty_ctx->abbrev));
c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
return 1;
case 'P': /* parent hashes */
Expand All @@ -743,7 +743,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
if (p != commit->parents)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, find_unique_abbrev(
p->item->object.sha1, DEFAULT_ABBREV));
p->item->object.sha1,
c->pretty_ctx->abbrev));
}
c->abbrev_parent_hashes.len = sb->len -
c->abbrev_parent_hashes.off;
Expand Down
1 change: 1 addition & 0 deletions shortlog.h
Expand Up @@ -12,6 +12,7 @@ struct shortlog {
int in1;
int in2;
int user_format;
int abbrev;

char *common_repo_prefix;
int email;
Expand Down
2 changes: 1 addition & 1 deletion t/t4201-shortlog.sh
Expand Up @@ -79,7 +79,7 @@ test_expect_success 'pretty format' '
test_cmp expect log.predictable
'

test_expect_failure '--abbrev' '
test_expect_success '--abbrev' '
sed s/SUBJECT/OBJID/ expect.template >expect &&
git shortlog --format="%h" --abbrev=5 HEAD >log &&
fuzz log >log.predictable &&
Expand Down
31 changes: 31 additions & 0 deletions t/t6006-rev-list-format.sh
Expand Up @@ -191,6 +191,31 @@ test_expect_success 'add LF before non-empty (2)' '
grep "^$" actual
'

test_expect_success '--abbrev' '
echo SHORT SHORT SHORT >expect2 &&
echo LONG LONG LONG >expect3 &&
git log -1 --format="%h %h %h" HEAD >actual1 &&
git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
test_cmp expect2 fuzzy2 &&
test_cmp expect3 fuzzy3 &&
! test_cmp actual1 actual2
'

test_expect_success '%H is not affected by --abbrev-commit' '
git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
len=$(wc -c <actual) &&
test $len = 41
'

test_expect_success '%h is not affected by --abbrev-commit' '
git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
len=$(wc -c <actual) &&
test $len = 21
'

test_expect_success '"%h %gD: %gs" is same as git-reflog' '
git reflog >expect &&
git log -g --format="%h %gD: %gs" >actual &&
Expand All @@ -203,6 +228,12 @@ test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
test_cmp expect actual
'

test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
git reflog --abbrev=13 --date=raw >expect &&
git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
test_cmp expect actual
'

test_expect_success '%gd shortens ref name' '
echo "master@{0}" >expect.gd-short &&
git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
Expand Down

0 comments on commit c197702

Please sign in to comment.