From 7904af1c10d2feb2f6d1dcb22454d93168a88c49 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 4 May 2012 01:23:14 -0400 Subject: [PATCH 1/5] t1411: add more selector index/date tests We already check that @{now} and "--date" cause the displayed selector to use the date for both the multiline and oneline formats. However, we miss several cases: 1. The --format=%gd selector is not tested at all. 2. We do not check how the log.date config interacts with the "--date" magic (according to f4ea32f, it should not impact the output). Doing so reveals that the combination of both (log.date combined with the %gd format) does not behave as expected. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/t1411-reflog-show.sh | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh index caa687b5b4..4706f4c50c 100755 --- a/t/t1411-reflog-show.sh +++ b/t/t1411-reflog-show.sh @@ -64,6 +64,14 @@ test_expect_success 'using @{now} syntax shows reflog date (oneline)' ' test_cmp expect actual ' +cat >expect <<'EOF' +HEAD@{Thu Apr 7 15:13:13 2005 -0700} +EOF +test_expect_success 'using @{now} syntax shows reflog date (format=%gd)' ' + git log -g -1 --format=%gd HEAD@{now} >actual && + test_cmp expect actual +' + cat >expect <<'EOF' Reflog: HEAD@{1112911993 -0700} (C O Mitter ) Reflog message: commit (initial): one @@ -82,6 +90,43 @@ test_expect_success 'using --date= shows reflog date (oneline)' ' test_cmp expect actual ' +cat >expect <<'EOF' +HEAD@{1112911993 -0700} +EOF +test_expect_success 'using --date= shows reflog date (format=%gd)' ' + git log -g -1 --format=%gd --date=raw >actual && + test_cmp expect actual +' + +cat >expect <<'EOF' +Reflog: HEAD@{0} (C O Mitter ) +Reflog message: commit (initial): one +EOF +test_expect_success 'log.date does not invoke "--date" magic (multiline)' ' + test_config log.date raw && + git log -g -1 >tmp && + grep ^Reflog actual && + test_cmp expect actual +' + +cat >expect <<'EOF' +e46513e HEAD@{0}: commit (initial): one +EOF +test_expect_success 'log.date does not invoke "--date" magic (oneline)' ' + test_config log.date raw && + git log -g -1 --oneline >actual && + test_cmp expect actual +' + +cat >expect <<'EOF' +HEAD@{0} +EOF +test_expect_failure 'log.date does not invoke "--date" magic (format=%gd)' ' + test_config log.date raw && + git log -g -1 --format=%gd >actual && + test_cmp expect actual +' + : >expect test_expect_success 'empty reflog file' ' git branch empty && From f026c7563a249da9279e664fed16fcd5f55c62db Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 4 May 2012 01:25:18 -0400 Subject: [PATCH 2/5] log: respect date_mode_explicit with --format:%gd When we show a reflog selector (e.g., via "git log -g"), we perform some DWIM magic: while we normally show the entry's index (e.g., HEAD@{1}), if the user has given us a date with "--date", then we show a date-based select (e.g., HEAD@{yesterday}). However, we don't want to trigger this magic if the alternate date format we got was from the "log.date" configuration; that is not sufficiently strong context for us to invoke this particular magic. To fix this, commit f4ea32f (improve reflog date/number heuristic, 2009-09-24) introduced a "date_mode_explicit" flag in rev_info. This flag is set only when we see a "--date" option on the command line, and we a vanilla date to the reflog code if the date was not explicit. Later, commit 8f8f547 (Introduce new pretty formats %g[sdD] for reflog information, 2009-10-19) added another way to show selectors, and it did not respect the date_mode_explicit flag from f4ea32f. This patch propagates the date_mode_explicit flag to the pretty-print code, which can then use it to pass the appropriate date field to the reflog code. This brings the behavior of "%gd" in line with the other formats, and means that its output is independent of any user configuration. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/rev-list.c | 1 + commit.h | 1 + log-tree.c | 1 + pretty.c | 4 +++- t/t1411-reflog-show.sh | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/builtin/rev-list.c b/builtin/rev-list.c index 56727e8c1d..fe0fb20d2d 100644 --- a/builtin/rev-list.c +++ b/builtin/rev-list.c @@ -104,6 +104,7 @@ static void show_commit(struct commit *commit, void *data) struct pretty_print_context ctx = {0}; ctx.abbrev = revs->abbrev; ctx.date_mode = revs->date_mode; + ctx.date_mode_explicit = revs->date_mode_explicit; ctx.fmt = revs->commit_format; pretty_print_commit(&ctx, commit, &buf); if (revs->graph) { diff --git a/commit.h b/commit.h index 14f6a5a2ed..048f31ed93 100644 --- a/commit.h +++ b/commit.h @@ -82,6 +82,7 @@ struct pretty_print_context { const char *after_subject; int preserve_subject; enum date_mode date_mode; + unsigned date_mode_explicit:1; int need_8bit_cte; int show_notes; struct reflog_walk_info *reflog_info; diff --git a/log-tree.c b/log-tree.c index 24c295ea1d..5f9e59a10c 100644 --- a/log-tree.c +++ b/log-tree.c @@ -511,6 +511,7 @@ void show_log(struct rev_info *opt) if (ctx.need_8bit_cte >= 0) ctx.need_8bit_cte = has_non_ascii(opt->add_signoff); ctx.date_mode = opt->date_mode; + ctx.date_mode_explicit = opt->date_mode_explicit; ctx.abbrev = opt->diffopt.abbrev; ctx.after_subject = extra_headers; ctx.preserve_subject = opt->preserve_subject; diff --git a/pretty.c b/pretty.c index f45eb54e4c..efd62e8ae7 100644 --- a/pretty.c +++ b/pretty.c @@ -956,7 +956,9 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder, if (c->pretty_ctx->reflog_info) get_reflog_selector(sb, c->pretty_ctx->reflog_info, - c->pretty_ctx->date_mode, + c->pretty_ctx->date_mode_explicit ? + c->pretty_ctx->date_mode : + DATE_NORMAL, (placeholder[1] == 'd')); return 2; case 's': /* reflog message */ diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh index 4706f4c50c..88247f874e 100755 --- a/t/t1411-reflog-show.sh +++ b/t/t1411-reflog-show.sh @@ -121,7 +121,7 @@ test_expect_success 'log.date does not invoke "--date" magic (oneline)' ' cat >expect <<'EOF' HEAD@{0} EOF -test_expect_failure 'log.date does not invoke "--date" magic (format=%gd)' ' +test_expect_success 'log.date does not invoke "--date" magic (format=%gd)' ' test_config log.date raw && git log -g -1 --format=%gd >actual && test_cmp expect actual From a763126b5c0120057908e939a0ff7cc95f899f69 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 4 May 2012 01:26:26 -0400 Subject: [PATCH 3/5] reflog-walk: clean up "flag" field of commit_reflog struct When we prepare to walk a reflog, we parse the specification and pull some information from it, such as which reflog to look in (e.g., HEAD), and where to start (e.g., HEAD@{10} or HEAD@{yesterday}). The resulting struct has a "recno" field to show where in the reflog we are starting. It also has a "flag" field; if true, it means the recno field came from parsing a date like HEAD@{yesterday}. There are two problems with this: 1. "flag" is an absolutely terrible name, as it conveys nothing about the meaning 2. you can tell "HEAD" from "HEAD@{yesterday}", but you can't differentiate "HEAD" from "HEAD{0}" This patch converts the flag into a tri-state (and gives it a better name!). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- reflog-walk.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/reflog-walk.c b/reflog-walk.c index 5d81d39a52..80bffb0a00 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -122,7 +122,12 @@ static void add_commit_info(struct commit *commit, void *util, } struct commit_reflog { - int flag, recno; + int recno; + enum selector_type { + SELECTOR_NONE, + SELECTOR_INDEX, + SELECTOR_DATE + } selector; struct complete_reflogs *reflogs; }; @@ -146,6 +151,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info, struct complete_reflogs *reflogs; char *branch, *at = strchr(name, '@'); struct commit_reflog *commit_reflog; + enum selector_type selector = SELECTOR_NONE; if (commit->object.flags & UNINTERESTING) die ("Cannot walk reflogs for %s", name); @@ -158,7 +164,10 @@ int add_reflog_for_walk(struct reflog_walk_info *info, if (*ep != '}') { recno = -1; timestamp = approxidate(at + 2); + selector = SELECTOR_DATE; } + else + selector = SELECTOR_INDEX; } else recno = 0; @@ -196,7 +205,6 @@ int add_reflog_for_walk(struct reflog_walk_info *info, commit_reflog = xcalloc(sizeof(struct commit_reflog), 1); if (recno < 0) { - commit_reflog->flag = 1; commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); if (commit_reflog->recno < 0) { free(branch); @@ -205,6 +213,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info, } } else commit_reflog->recno = reflogs->nr - recno - 1; + commit_reflog->selector = selector; commit_reflog->reflogs = reflogs; add_commit_info(commit, commit_reflog, &info->reflogs); @@ -263,7 +272,7 @@ void get_reflog_selector(struct strbuf *sb, } strbuf_addf(sb, "%s@{", printed_ref); - if (commit_reflog->flag || dmode) { + if (commit_reflog->selector == SELECTOR_DATE || dmode) { info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); } else { From 794151e9b595ddc2700c0801caabfd27be763e12 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 4 May 2012 01:27:25 -0400 Subject: [PATCH 4/5] reflog-walk: always make HEAD@{0} show indexed selectors When we are showing reflog selectors during a walk, we infer from context whether the user wanted to see the index in each selector, or the reflog date. The current rules are: 1. if the user asked for an explicit date format in the output, show the date 2. if the user asked for ref@{now}, show the date 3. if neither is true, show the index However, if we see "ref@{0}", that should be a strong clue that the user wants to see the counted version. In fact, it should be much stronger than the date format in (1). The user may have been setting the date format to use in another part of the output (e.g., in --format="%gd (%ad)", they may have wanted to influence the author date). This patch flips the rules to: 1. if the user asked for ref@{0}, always show the index 2. if the user asked for ref@{now}, always show the date 3. otherwise, we have just "ref"; show them counted by default, but respect the presence of "--date" as a clue that the user wanted them date-based Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- reflog-walk.c | 3 ++- t/t1411-reflog-show.sh | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/reflog-walk.c b/reflog-walk.c index 80bffb0a00..b84e80f2ca 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -272,7 +272,8 @@ void get_reflog_selector(struct strbuf *sb, } strbuf_addf(sb, "%s@{", printed_ref); - if (commit_reflog->selector == SELECTOR_DATE || dmode) { + if (commit_reflog->selector == SELECTOR_DATE || + (commit_reflog->selector == SELECTOR_NONE && dmode)) { info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); } else { diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh index 88247f874e..7d9b5e33df 100755 --- a/t/t1411-reflog-show.sh +++ b/t/t1411-reflog-show.sh @@ -127,6 +127,14 @@ test_expect_success 'log.date does not invoke "--date" magic (format=%gd)' ' test_cmp expect actual ' +cat >expect <<'EOF' +HEAD@{0} +EOF +test_expect_success '--date magic does not override explicit @{0} syntax' ' + git log -g -1 --format=%gd --date=raw HEAD@{0} >actual && + test_cmp expect actual +' + : >expect test_expect_success 'empty reflog file' ' git branch empty && From 55ccf85a524f6a2d8cb5582d80a2927ff11dfb64 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 7 May 2012 14:11:32 -0700 Subject: [PATCH 5/5] reflog-walk: tell explicit --date=default from not having --date at all Introduction of opt->date_mode_explicit was a step in the right direction, but lost that crucial bit at the very end of the callchain, and the callee could not tell an explicitly specified "I want *date* but in default format" from the built-in default value passed when there was no --date specified. Signed-off-by: Junio C Hamano --- log-tree.c | 7 +++---- pretty.c | 5 ++--- reflog-walk.c | 8 ++++---- reflog-walk.h | 4 ++-- t/t1411-reflog-show.sh | 8 ++++---- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/log-tree.c b/log-tree.c index 5f9e59a10c..588117e013 100644 --- a/log-tree.c +++ b/log-tree.c @@ -493,10 +493,9 @@ void show_log(struct rev_info *opt) * graph info here. */ show_reflog_message(opt->reflog_info, - opt->commit_format == CMIT_FMT_ONELINE, - opt->date_mode_explicit ? - opt->date_mode : - DATE_NORMAL); + opt->commit_format == CMIT_FMT_ONELINE, + opt->date_mode, + opt->date_mode_explicit); if (opt->commit_format == CMIT_FMT_ONELINE) return; } diff --git a/pretty.c b/pretty.c index efd62e8ae7..25944de034 100644 --- a/pretty.c +++ b/pretty.c @@ -956,9 +956,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder, if (c->pretty_ctx->reflog_info) get_reflog_selector(sb, c->pretty_ctx->reflog_info, - c->pretty_ctx->date_mode_explicit ? - c->pretty_ctx->date_mode : - DATE_NORMAL, + c->pretty_ctx->date_mode, + c->pretty_ctx->date_mode_explicit, (placeholder[1] == 'd')); return 2; case 's': /* reflog message */ diff --git a/reflog-walk.c b/reflog-walk.c index b84e80f2ca..0c904fb2d1 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -252,7 +252,7 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit) void get_reflog_selector(struct strbuf *sb, struct reflog_walk_info *reflog_info, - enum date_mode dmode, + enum date_mode dmode, int force_date, int shorten) { struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; @@ -273,7 +273,7 @@ void get_reflog_selector(struct strbuf *sb, strbuf_addf(sb, "%s@{", printed_ref); if (commit_reflog->selector == SELECTOR_DATE || - (commit_reflog->selector == SELECTOR_NONE && dmode)) { + (commit_reflog->selector == SELECTOR_NONE && force_date)) { info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); } else { @@ -302,7 +302,7 @@ void get_reflog_message(struct strbuf *sb, } void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, - enum date_mode dmode) + enum date_mode dmode, int force_date) { if (reflog_info && reflog_info->last_commit_reflog) { struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; @@ -310,7 +310,7 @@ void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, struct strbuf selector = STRBUF_INIT; info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; - get_reflog_selector(&selector, reflog_info, dmode, 0); + get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); if (oneline) { printf("%s: %s", selector.buf, info->message); } diff --git a/reflog-walk.h b/reflog-walk.h index 7bd2cd4c4e..3adccb018b 100644 --- a/reflog-walk.h +++ b/reflog-walk.h @@ -11,12 +11,12 @@ extern int add_reflog_for_walk(struct reflog_walk_info *info, extern void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit); extern void show_reflog_message(struct reflog_walk_info *info, int, - enum date_mode); + enum date_mode, int force_date); extern void get_reflog_message(struct strbuf *sb, struct reflog_walk_info *reflog_info); extern void get_reflog_selector(struct strbuf *sb, struct reflog_walk_info *reflog_info, - enum date_mode dmode, + enum date_mode dmode, int force_date, int shorten); #endif diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh index 7d9b5e33df..9a105fe21f 100755 --- a/t/t1411-reflog-show.sh +++ b/t/t1411-reflog-show.sh @@ -73,20 +73,20 @@ test_expect_success 'using @{now} syntax shows reflog date (format=%gd)' ' ' cat >expect <<'EOF' -Reflog: HEAD@{1112911993 -0700} (C O Mitter ) +Reflog: HEAD@{Thu Apr 7 15:13:13 2005 -0700} (C O Mitter ) Reflog message: commit (initial): one EOF test_expect_success 'using --date= shows reflog date (multiline)' ' - git log -g -1 --date=raw >tmp && + git log -g -1 --date=default >tmp && grep ^Reflog actual && test_cmp expect actual ' cat >expect <<'EOF' -e46513e HEAD@{1112911993 -0700}: commit (initial): one +e46513e HEAD@{Thu Apr 7 15:13:13 2005 -0700}: commit (initial): one EOF test_expect_success 'using --date= shows reflog date (oneline)' ' - git log -g -1 --oneline --date=raw >actual && + git log -g -1 --oneline --date=default >actual && test_cmp expect actual '