commit: print "Date" line when the user has set date
authorJeff King <peff@peff.net>
Fri, 2 May 2014 01:10:01 +0000 (21:10 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 May 2014 21:14:21 +0000 (14:14 -0700)
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>
builtin/commit.c
t/t3508-cherry-pick-many-commits.sh
t/t7501-commit.sh
t/t7502-commit.sh
index 728cc9bbfc9294312674c2318c7084017eba33c7..a25661f34349f714966c62cd087e357badaefa7c 100644 (file)
@@ -592,6 +592,11 @@ static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
                die(_("Malformed ident string: '%s'"), buf->buf);
 }
 
+static int author_date_is_interesting(void)
+{
+       return author_message || force_date;
+}
+
 static int prepare_to_commit(const char *index_file, const char *prefix,
                             struct commit *current_head,
                             struct wt_status *s,
@@ -805,6 +810,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                                (int)(ai.name_end - ai.name_begin), ai.name_begin,
                                (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
 
+               if (author_date_is_interesting())
+                       status_printf_ln(s, GIT_COLOR_NORMAL,
+                               _("%s"
+                               "Date:      %s"),
+                               ident_shown++ ? "" : "\n",
+                               show_ident_date(&ai, DATE_NORMAL));
+
                if (!committer_ident_sufficiently_given())
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                                _("%s"
@@ -1355,6 +1367,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
                strbuf_addstr(&format, "\n Author: ");
                strbuf_addbuf_percentquote(&format, &author_ident);
        }
+       if (author_date_is_interesting()) {
+               struct strbuf date = STRBUF_INIT;
+               format_commit_message(commit, "%ad", &date, &pctx);
+               strbuf_addstr(&format, "\n Date: ");
+               strbuf_addbuf_percentquote(&format, &date);
+               strbuf_release(&date);
+       }
        if (!committer_ident_sufficiently_given()) {
                strbuf_addstr(&format, "\n Committer: ");
                strbuf_addbuf_percentquote(&format, &committer_ident);
index 19c99d7ef1bc02e67a7b995206b9f66c04c1b31c..b457333e1865de794e47fa6b601648fd67a02a7c 100755 (executable)
@@ -65,12 +65,15 @@ test_expect_success 'output to keep user entertained during multi-pick' '
        cat <<-\EOF >expected &&
        [master OBJID] second
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:14:13 2005 -0700
         1 file changed, 1 insertion(+)
        [master OBJID] third
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:15:13 2005 -0700
         1 file changed, 1 insertion(+)
        [master OBJID] fourth
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:16:13 2005 -0700
         1 file changed, 1 insertion(+)
        EOF
 
@@ -98,14 +101,17 @@ test_expect_success 'output during multi-pick indicates merge strategy' '
        Trying simple merge.
        [master OBJID] second
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:14:13 2005 -0700
         1 file changed, 1 insertion(+)
        Trying simple merge.
        [master OBJID] third
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:15:13 2005 -0700
         1 file changed, 1 insertion(+)
        Trying simple merge.
        [master OBJID] fourth
         Author: A U Thor <author@example.com>
+        Date: Thu Apr 7 15:16:13 2005 -0700
         1 file changed, 1 insertion(+)
        EOF
 
index d58b097ff3b3d0e6e68d15491d38f1f32460f6e7..5a76823d4c943368d9ca07506bacb578e6273caf 100755 (executable)
@@ -346,6 +346,11 @@ test_expect_success 'amend commit to fix date' '
 
 '
 
+test_expect_success 'commit mentions forced date in output' '
+       git commit --amend --date=2010-01-02T03:04:05 >output &&
+       grep "Date: *Sat Jan 2 03:04:05 2010" output
+'
+
 test_expect_success 'commit complains about bogus date' '
        test_must_fail git commit --amend --date=10.11.2010
 '
index 9a3f3a1b4151ebf16cfd773a0f9efcb1b61d40d7..6465cd59afb6dcd4c2e554947da4755a1beb883e 100755 (executable)
@@ -344,6 +344,13 @@ test_expect_success 'message shows author when it is not equal to committer' '
          .git/COMMIT_EDITMSG
 '
 
+test_expect_success 'message shows date when it is explicitly set' '
+       git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
+       test_i18ngrep \
+         "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
+         .git/COMMIT_EDITMSG
+'
+
 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
 
        echo >>negative &&