commit: print "Date" line when the user has set date

When we make a commit and the author is not the same as the committer (e.g., because you used "-c $commit" or "--author=$somebody"), we print the author's name and email in both the commit-message template and as part of the commit summary. This is a safety check to give the user a chance to confirm that we are doing what they expect. This patch brings the same safety for the "date" field, which may be set by "-c" or by using "--date". Note that we explicitly do not set it for $GIT_AUTHOR_DATE, as it is probably not of interest when "git commit" is being fed its parameters by a script. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 1, 2014 at 21:10 UTC b7242b8c9e4b3c57a07c2a76d0337389605aadcc
4 files changed +37
builtin/commit.c
+19
@@ -592,6 +592,11 @@ static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
592 die(_("Malformed ident string: '%s'"), buf->buf);
593 }
594
595 +static int author_date_is_interesting(void)
596 +{
597 + return author_message || force_date;
598 +}
599 +
600 static int prepare_to_commit(const char *index_file, const char *prefix,
601 struct commit *current_head,
602 struct wt_status *s,
@@ -805,6 +810,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
810 (int)(ai.name_end - ai.name_begin), ai.name_begin,
811 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
812
813 + if (author_date_is_interesting())
814 + status_printf_ln(s, GIT_COLOR_NORMAL,
815 + _("%s"
816 + "Date: %s"),
817 + ident_shown++ ? "" : "\n",
818 + show_ident_date(&ai, DATE_NORMAL));
819 +
820 if (!committer_ident_sufficiently_given())
821 status_printf_ln(s, GIT_COLOR_NORMAL,
822 _("%s"
@@ -1355,6 +1367,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
1367 strbuf_addstr(&format, "\n Author: ");
1368 strbuf_addbuf_percentquote(&format, &author_ident);
1369 }
1370 + if (author_date_is_interesting()) {
1371 + struct strbuf date = STRBUF_INIT;
1372 + format_commit_message(commit, "%ad", &date, &pctx);
1373 + strbuf_addstr(&format, "\n Date: ");
1374 + strbuf_addbuf_percentquote(&format, &date);
1375 + strbuf_release(&date);
1376 + }
1377 if (!committer_ident_sufficiently_given()) {
1378 strbuf_addstr(&format, "\n Committer: ");
1379 strbuf_addbuf_percentquote(&format, &committer_ident);
t/t3508-cherry-pick-many-commits.sh
+6
@@ -65,12 +65,15 @@ test_expect_success 'output to keep user entertained during multi-pick' '
65 cat <<-\EOF >expected &&
66 [master OBJID] second
67 Author: A U Thor <author@example.com>
68 + Date: Thu Apr 7 15:14:13 2005 -0700
69 1 file changed, 1 insertion(+)
70 [master OBJID] third
71 Author: A U Thor <author@example.com>
72 + Date: Thu Apr 7 15:15:13 2005 -0700
73 1 file changed, 1 insertion(+)
74 [master OBJID] fourth
75 Author: A U Thor <author@example.com>
76 + Date: Thu Apr 7 15:16:13 2005 -0700
77 1 file changed, 1 insertion(+)
78 EOF
79
@@ -98,14 +101,17 @@ test_expect_success 'output during multi-pick indicates merge strategy' '
101 Trying simple merge.
102 [master OBJID] second
103 Author: A U Thor <author@example.com>
104 + Date: Thu Apr 7 15:14:13 2005 -0700
105 1 file changed, 1 insertion(+)
106 Trying simple merge.
107 [master OBJID] third
108 Author: A U Thor <author@example.com>
109 + Date: Thu Apr 7 15:15:13 2005 -0700
110 1 file changed, 1 insertion(+)
111 Trying simple merge.
112 [master OBJID] fourth
113 Author: A U Thor <author@example.com>
114 + Date: Thu Apr 7 15:16:13 2005 -0700
115 1 file changed, 1 insertion(+)
116 EOF
117
t/t7501-commit.sh
+5
@@ -346,6 +346,11 @@ test_expect_success 'amend commit to fix date' '
346
347 '
348
349 +test_expect_success 'commit mentions forced date in output' '
350 + git commit --amend --date=2010-01-02T03:04:05 >output &&
351 + grep "Date: *Sat Jan 2 03:04:05 2010" output
352 +'
353 +
354 test_expect_success 'commit complains about bogus date' '
355 test_must_fail git commit --amend --date=10.11.2010
356 '
t/t7502-commit.sh
+7
@@ -344,6 +344,13 @@ test_expect_success 'message shows author when it is not equal to committer' '
344 .git/COMMIT_EDITMSG
345 '
346
347 +test_expect_success 'message shows date when it is explicitly set' '
348 + git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
349 + test_i18ngrep \
350 + "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
351 + .git/COMMIT_EDITMSG
352 +'
353 +
354 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
355
356 echo >>negative &&