Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit b7242b8

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

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

builtin/commit.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,11 @@ static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
592592
die(_("Malformed ident string: '%s'"), buf->buf);
593593
}
594594

595+
static int author_date_is_interesting(void)
596+
{
597+
return author_message || force_date;
598+
}
599+
595600
static int prepare_to_commit(const char *index_file, const char *prefix,
596601
struct commit *current_head,
597602
struct wt_status *s,
@@ -805,6 +810,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
805810
(int)(ai.name_end - ai.name_begin), ai.name_begin,
806811
(int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
807812

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+
808820
if (!committer_ident_sufficiently_given())
809821
status_printf_ln(s, GIT_COLOR_NORMAL,
810822
_("%s"
@@ -1355,6 +1367,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13551367
strbuf_addstr(&format, "\n Author: ");
13561368
strbuf_addbuf_percentquote(&format, &author_ident);
13571369
}
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+
}
13581377
if (!committer_ident_sufficiently_given()) {
13591378
strbuf_addstr(&format, "\n Committer: ");
13601379
strbuf_addbuf_percentquote(&format, &committer_ident);

t/t3508-cherry-pick-many-commits.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ test_expect_success 'output to keep user entertained during multi-pick' '
6565
cat <<-\EOF >expected &&
6666
[master OBJID] second
6767
Author: A U Thor <[email protected]>
68+
Date: Thu Apr 7 15:14:13 2005 -0700
6869
1 file changed, 1 insertion(+)
6970
[master OBJID] third
7071
Author: A U Thor <[email protected]>
72+
Date: Thu Apr 7 15:15:13 2005 -0700
7173
1 file changed, 1 insertion(+)
7274
[master OBJID] fourth
7375
Author: A U Thor <[email protected]>
76+
Date: Thu Apr 7 15:16:13 2005 -0700
7477
1 file changed, 1 insertion(+)
7578
EOF
7679
@@ -98,14 +101,17 @@ test_expect_success 'output during multi-pick indicates merge strategy' '
98101
Trying simple merge.
99102
[master OBJID] second
100103
Author: A U Thor <[email protected]>
104+
Date: Thu Apr 7 15:14:13 2005 -0700
101105
1 file changed, 1 insertion(+)
102106
Trying simple merge.
103107
[master OBJID] third
104108
Author: A U Thor <[email protected]>
109+
Date: Thu Apr 7 15:15:13 2005 -0700
105110
1 file changed, 1 insertion(+)
106111
Trying simple merge.
107112
[master OBJID] fourth
108113
Author: A U Thor <[email protected]>
114+
Date: Thu Apr 7 15:16:13 2005 -0700
109115
1 file changed, 1 insertion(+)
110116
EOF
111117

t/t7501-commit.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ test_expect_success 'amend commit to fix date' '
346346
347347
'
348348

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+
349354
test_expect_success 'commit complains about bogus date' '
350355
test_must_fail git commit --amend --date=10.11.2010
351356
'

t/t7502-commit.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ test_expect_success 'message shows author when it is not equal to committer' '
344344
.git/COMMIT_EDITMSG
345345
'
346346

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+
347354
test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
348355
349356
echo >>negative &&

0 commit comments

Comments
 (0)