@matheusbsbs / matheus-bagliano / commits / c197702156
Fork 0

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>

Will Palmer committed May 3, 2010 at 22:18 UTC c197702156e2164074327f5e870ab0d3021977fc
5 files changed +39 -5
builtin-shortlog.c
+2 -1
@@ -162,7 +162,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
162 sha1_to_hex(commit->object.sha1));
163 if (log->user_format) {
164 struct pretty_print_context ctx = {0};
165 - ctx.abbrev = DEFAULT_ABBREV;
165 + ctx.abbrev = log->abbrev;
166 ctx.subject = "";
167 ctx.after_subject = "";
168 ctx.date_mode = DATE_NORMAL;
@@ -290,6 +290,7 @@ parse_done:
290 }
291
292 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
293 + log.abbrev = rev.abbrev;
294
295 /* assume HEAD if from a tty */
296 if (!nongit && !rev.pending.nr && isatty(0))
pretty.c
+4 -3
@@ -716,7 +716,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
716 if (add_again(sb, &c->abbrev_commit_hash))
717 return 1;
718 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
719 - DEFAULT_ABBREV));
719 + c->pretty_ctx->abbrev));
720 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
721 return 1;
722 case 'T': /* tree hash */
@@ -726,7 +726,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
726 if (add_again(sb, &c->abbrev_tree_hash))
727 return 1;
728 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
729 - DEFAULT_ABBREV));
729 + c->pretty_ctx->abbrev));
730 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
731 return 1;
732 case 'P': /* parent hashes */
@@ -743,7 +743,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
743 if (p != commit->parents)
744 strbuf_addch(sb, ' ');
745 strbuf_addstr(sb, find_unique_abbrev(
746 - p->item->object.sha1, DEFAULT_ABBREV));
746 + p->item->object.sha1,
747 + c->pretty_ctx->abbrev));
748 }
749 c->abbrev_parent_hashes.len = sb->len -
750 c->abbrev_parent_hashes.off;
shortlog.h
+1
@@ -12,6 +12,7 @@ struct shortlog {
12 int in1;
13 int in2;
14 int user_format;
15 + int abbrev;
16
17 char *common_repo_prefix;
18 int email;
t/t4201-shortlog.sh
+1 -1
@@ -79,7 +79,7 @@ test_expect_success 'pretty format' '
79 test_cmp expect log.predictable
80 '
81
82 -test_expect_failure '--abbrev' '
82 +test_expect_success '--abbrev' '
83 sed s/SUBJECT/OBJID/ expect.template >expect &&
84 git shortlog --format="%h" --abbrev=5 HEAD >log &&
85 fuzz log >log.predictable &&
t/t6006-rev-list-format.sh
+31
@@ -191,6 +191,31 @@ test_expect_success 'add LF before non-empty (2)' '
191 grep "^$" actual
192 '
193
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 +
219 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
220 git reflog >expect &&
221 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)' '
228 test_cmp expect actual
229 '
230
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 +
237 test_expect_success '%gd shortens ref name' '
238 echo "master@{0}" >expect.gd-short &&
239 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&