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>
maint
Jeff King 2008-08-28 20:54:59 -04:00 committed by Junio C Hamano
parent 0cfeed2e1d
commit d36f8679e9
6 changed files with 22 additions and 10 deletions

View File

@ -103,7 +103,7 @@ The placeholders are:
- '%an': author name - '%an': author name
- '%aN': author name (respecting .mailmap) - '%aN': author name (respecting .mailmap)
- '%ae': author email - '%ae': author email
- '%ad': author date - '%ad': author date (format respects --date= option)
- '%aD': author date, RFC2822 style - '%aD': author date, RFC2822 style
- '%ar': author date, relative - '%ar': author date, relative
- '%at': author date, UNIX timestamp - '%at': author date, UNIX timestamp

View File

@ -48,7 +48,7 @@ static void format_subst(const struct commit *commit,
strbuf_add(&fmt, b + 8, c - b - 8); strbuf_add(&fmt, b + 8, c - b - 8);


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

View File

@ -882,7 +882,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)


if (!log_tree_commit(&rev, commit)) { if (!log_tree_commit(&rev, commit)) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
format_commit_message(commit, "%h: %s", &buf); format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
printf("%s\n", buf.buf); printf("%s\n", buf.buf);
strbuf_release(&buf); strbuf_release(&buf);
} }

View File

@ -67,7 +67,8 @@ extern int non_ascii(int);
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */ struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
extern void get_commit_format(const char *arg, struct rev_info *); extern void get_commit_format(const char *arg, struct rev_info *);
extern void format_commit_message(const struct commit *commit, extern void format_commit_message(const struct commit *commit,
const void *format, struct strbuf *sb); const void *format, struct strbuf *sb,
enum date_mode dmode);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*, extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
struct strbuf *, struct strbuf *,
int abbrev, const char *subject, int abbrev, const char *subject,

View File

@ -310,7 +310,7 @@ static int mailmap_name(struct strbuf *sb, const char *email)
} }


static size_t format_person_part(struct strbuf *sb, char part, static size_t format_person_part(struct strbuf *sb, char part,
const char *msg, int len) const char *msg, int len, enum date_mode dmode)
{ {
/* currently all placeholders have same length */ /* currently all placeholders have same length */
const int placeholder_len = 2; const int placeholder_len = 2;
@ -377,7 +377,7 @@ static size_t format_person_part(struct strbuf *sb, char part,


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


struct format_commit_context { struct format_commit_context {
const struct commit *commit; const struct commit *commit;
enum date_mode dmode;


/* These offsets are relative to the start of the commit message. */ /* These offsets are relative to the start of the commit message. */
int commit_header_parsed; int commit_header_parsed;
@ -584,10 +585,12 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
return 1; return 1;
case 'a': /* author ... */ case 'a': /* author ... */
return format_person_part(sb, placeholder[1], return format_person_part(sb, placeholder[1],
msg + c->author.off, c->author.len); msg + c->author.off, c->author.len,
c->dmode);
case 'c': /* committer ... */ case 'c': /* committer ... */
return format_person_part(sb, placeholder[1], return format_person_part(sb, placeholder[1],
msg + c->committer.off, c->committer.len); msg + c->committer.off, c->committer.len,
c->dmode);
case 'e': /* encoding */ case 'e': /* encoding */
strbuf_add(sb, msg + c->encoding.off, c->encoding.len); strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
return 1; return 1;
@ -599,12 +602,14 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
} }


void format_commit_message(const struct commit *commit, void format_commit_message(const struct commit *commit,
const void *format, struct strbuf *sb) const void *format, struct strbuf *sb,
enum date_mode dmode)
{ {
struct format_commit_context context; struct format_commit_context context;


memset(&context, 0, sizeof(context)); memset(&context, 0, sizeof(context));
context.commit = commit; context.commit = commit;
context.dmode = dmode;
strbuf_expand(sb, format, format_commit_item, &context); strbuf_expand(sb, format, format_commit_item, &context);
} }


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


if (fmt == CMIT_FMT_USERFORMAT) { if (fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb); format_commit_message(commit, user_format, sb, dmode);
return; return;
} }



View File

@ -139,6 +139,12 @@ commit 131a310eb913d107dd3c09a65d1651175898735d
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
EOF EOF


test_expect_success '%ad respects --date=' '
echo 2005-04-07 >expect.ad-short &&
git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
test_cmp expect.ad-short output.ad-short
'

test_expect_success 'empty email' ' test_expect_success 'empty email' '
test_tick && test_tick &&
C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) && C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&