Skip to content

Commit d36f867

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cfeed2 commit d36f867

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The placeholders are:
103103
- '%an': author name
104104
- '%aN': author name (respecting .mailmap)
105105
- '%ae': author email
106-
- '%ad': author date
106+
- '%ad': author date (format respects --date= option)
107107
- '%aD': author date, RFC2822 style
108108
- '%ar': author date, relative
109109
- '%at': author date, UNIX timestamp

archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void format_subst(const struct commit *commit,
4848
strbuf_add(&fmt, b + 8, c - b - 8);
4949

5050
strbuf_add(buf, src, b - src);
51-
format_commit_message(commit, fmt.buf, buf);
51+
format_commit_message(commit, fmt.buf, buf, DATE_NORMAL);
5252
len -= c + 1 - src;
5353
src = c + 1;
5454
}

builtin-commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
882882

883883
if (!log_tree_commit(&rev, commit)) {
884884
struct strbuf buf = STRBUF_INIT;
885-
format_commit_message(commit, "%h: %s", &buf);
885+
format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
886886
printf("%s\n", buf.buf);
887887
strbuf_release(&buf);
888888
}

commit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ extern int non_ascii(int);
6767
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
6868
extern void get_commit_format(const char *arg, struct rev_info *);
6969
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);
7172
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
7273
struct strbuf *,
7374
int abbrev, const char *subject,

pretty.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static int mailmap_name(struct strbuf *sb, const char *email)
310310
}
311311

312312
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)
314314
{
315315
/* currently all placeholders have same length */
316316
const int placeholder_len = 2;
@@ -377,7 +377,7 @@ static size_t format_person_part(struct strbuf *sb, char part,
377377

378378
switch (part) {
379379
case 'd': /* date */
380-
strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
380+
strbuf_addstr(sb, show_date(date, tz, dmode));
381381
return placeholder_len;
382382
case 'D': /* date, RFC2822 style */
383383
strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
@@ -409,6 +409,7 @@ struct chunk {
409409

410410
struct format_commit_context {
411411
const struct commit *commit;
412+
enum date_mode dmode;
412413

413414
/* These offsets are relative to the start of the commit message. */
414415
int commit_header_parsed;
@@ -584,10 +585,12 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
584585
return 1;
585586
case 'a': /* author ... */
586587
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);
588590
case 'c': /* committer ... */
589591
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);
591594
case 'e': /* encoding */
592595
strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
593596
return 1;
@@ -599,12 +602,14 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
599602
}
600603

601604
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)
603607
{
604608
struct format_commit_context context;
605609

606610
memset(&context, 0, sizeof(context));
607611
context.commit = commit;
612+
context.dmode = dmode;
608613
strbuf_expand(sb, format, format_commit_item, &context);
609614
}
610615

@@ -770,7 +775,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
770775
const char *encoding;
771776

772777
if (fmt == CMIT_FMT_USERFORMAT) {
773-
format_commit_message(commit, user_format, sb);
778+
format_commit_message(commit, user_format, sb, dmode);
774779
return;
775780
}
776781

t/t6006-rev-list-format.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ commit 131a310eb913d107dd3c09a65d1651175898735d
139139
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
140140
EOF
141141

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+
142148
test_expect_success 'empty email' '
143149
test_tick &&
144150
C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&

0 commit comments

Comments
 (0)