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

pretty=format: respect date format options

When running a command like: git log --pretty=format:%ad --date=short the date option was ignored. This patch causes it to use whatever format was specified by --date (or by --relative-date, etc), just as the non-user formats would do. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 28, 2008 at 20:54 UTC d36f8679e94c2a0d4d15d6adcea434634af6d627
6 files changed +22 -10
Documentation/pretty-formats.txt
+1 -1
@@ -103,7 +103,7 @@ The placeholders are:
103 - '%an': author name
104 - '%aN': author name (respecting .mailmap)
105 - '%ae': author email
106 -- '%ad': author date
106 +- '%ad': author date (format respects --date= option)
107 - '%aD': author date, RFC2822 style
108 - '%ar': author date, relative
109 - '%at': author date, UNIX timestamp
archive.c
+1 -1
@@ -48,7 +48,7 @@ static void format_subst(const struct commit *commit,
48 strbuf_add(&fmt, b + 8, c - b - 8);
49
50 strbuf_add(buf, src, b - src);
51 - format_commit_message(commit, fmt.buf, buf);
51 + format_commit_message(commit, fmt.buf, buf, DATE_NORMAL);
52 len -= c + 1 - src;
53 src = c + 1;
54 }
builtin-commit.c
+1 -1
@@ -882,7 +882,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
882
883 if (!log_tree_commit(&rev, commit)) {
884 struct strbuf buf = STRBUF_INIT;
885 - format_commit_message(commit, "%h: %s", &buf);
885 + format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
886 printf("%s\n", buf.buf);
887 strbuf_release(&buf);
888 }
commit.h
+2 -1
@@ -67,7 +67,8 @@ extern int non_ascii(int);
67 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
68 extern void get_commit_format(const char *arg, struct rev_info *);
69 extern void format_commit_message(const struct commit *commit,
70 - const void *format, struct strbuf *sb);
70 + const void *format, struct strbuf *sb,
71 + enum date_mode dmode);
72 extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
73 struct strbuf *,
74 int abbrev, const char *subject,
pretty.c
+11 -6
@@ -310,7 +310,7 @@ static int mailmap_name(struct strbuf *sb, const char *email)
310 }
311
312 static size_t format_person_part(struct strbuf *sb, char part,
313 - const char *msg, int len)
313 + const char *msg, int len, enum date_mode dmode)
314 {
315 /* currently all placeholders have same length */
316 const int placeholder_len = 2;
@@ -377,7 +377,7 @@ static size_t format_person_part(struct strbuf *sb, char part,
377
378 switch (part) {
379 case 'd': /* date */
380 - strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
380 + strbuf_addstr(sb, show_date(date, tz, dmode));
381 return placeholder_len;
382 case 'D': /* date, RFC2822 style */
383 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
@@ -409,6 +409,7 @@ struct chunk {
409
410 struct format_commit_context {
411 const struct commit *commit;
412 + enum date_mode dmode;
413
414 /* These offsets are relative to the start of the commit message. */
415 int commit_header_parsed;
@@ -584,10 +585,12 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
585 return 1;
586 case 'a': /* author ... */
587 return format_person_part(sb, placeholder[1],
587 - msg + c->author.off, c->author.len);
588 + msg + c->author.off, c->author.len,
589 + c->dmode);
590 case 'c': /* committer ... */
591 return format_person_part(sb, placeholder[1],
590 - msg + c->committer.off, c->committer.len);
592 + msg + c->committer.off, c->committer.len,
593 + c->dmode);
594 case 'e': /* encoding */
595 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
596 return 1;
@@ -599,12 +602,14 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
602 }
603
604 void format_commit_message(const struct commit *commit,
602 - const void *format, struct strbuf *sb)
605 + const void *format, struct strbuf *sb,
606 + enum date_mode dmode)
607 {
608 struct format_commit_context context;
609
610 memset(&context, 0, sizeof(context));
611 context.commit = commit;
612 + context.dmode = dmode;
613 strbuf_expand(sb, format, format_commit_item, &context);
614 }
615
@@ -770,7 +775,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
775 const char *encoding;
776
777 if (fmt == CMIT_FMT_USERFORMAT) {
773 - format_commit_message(commit, user_format, sb);
778 + format_commit_message(commit, user_format, sb, dmode);
779 return;
780 }
781
t/t6006-rev-list-format.sh
+6
@@ -139,6 +139,12 @@ commit 131a310eb913d107dd3c09a65d1651175898735d
139 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
140 EOF
141
142 +test_expect_success '%ad respects --date=' '
143 + echo 2005-04-07 >expect.ad-short &&
144 + git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
145 + test_cmp expect.ad-short output.ad-short
146 +'
147 +
148 test_expect_success 'empty email' '
149 test_tick &&
150 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&