Skip to content

Commit e1857af

Browse files
committed
Merge branch 'jk/commit-date-approxidate'
* jk/commit-date-approxidate: commit: accept more date formats for "--date" commit: print "Date" line when the user has set date pretty: make show_ident_date public commit: use split_ident_line to compare author/committer
2 parents 6753d8a + 14ac286 commit e1857af

File tree

6 files changed

+97
-23
lines changed

6 files changed

+97
-23
lines changed

builtin/commit.c

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,29 @@ static int sane_ident_split(struct ident_split *person)
526526
return 1;
527527
}
528528

529+
static int parse_force_date(const char *in, char *out, int len)
530+
{
531+
if (len < 1)
532+
return -1;
533+
*out++ = '@';
534+
len--;
535+
536+
if (parse_date(in, out, len) < 0) {
537+
int errors = 0;
538+
unsigned long t = approxidate_careful(in, &errors);
539+
if (errors)
540+
return -1;
541+
snprintf(out, len, "%lu", t);
542+
}
543+
544+
return 0;
545+
}
546+
529547
static void determine_author_info(struct strbuf *author_ident)
530548
{
531549
char *name, *email, *date;
532550
struct ident_split author;
551+
char date_buf[64];
533552

534553
name = getenv("GIT_AUTHOR_NAME");
535554
email = getenv("GIT_AUTHOR_EMAIL");
@@ -574,8 +593,12 @@ static void determine_author_info(struct strbuf *author_ident)
574593
email = xstrndup(lb + 2, rb - (lb + 2));
575594
}
576595

577-
if (force_date)
578-
date = force_date;
596+
if (force_date) {
597+
if (parse_force_date(force_date, date_buf, sizeof(date_buf)))
598+
die(_("invalid date format: %s"), force_date);
599+
date = date_buf;
600+
}
601+
579602
strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
580603
if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
581604
sane_ident_split(&author)) {
@@ -585,13 +608,16 @@ static void determine_author_info(struct strbuf *author_ident)
585608
}
586609
}
587610

588-
static char *cut_ident_timestamp_part(char *string)
611+
static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
589612
{
590-
char *ket = strrchr(string, '>');
591-
if (!ket || ket[1] != ' ')
592-
die(_("Malformed ident string: '%s'"), string);
593-
*++ket = '\0';
594-
return ket;
613+
if (split_ident_line(id, buf->buf, buf->len) ||
614+
!sane_ident_split(id))
615+
die(_("Malformed ident string: '%s'"), buf->buf);
616+
}
617+
618+
static int author_date_is_interesting(void)
619+
{
620+
return author_message || force_date;
595621
}
596622

597623
static int prepare_to_commit(const char *index_file, const char *prefix,
@@ -755,7 +781,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
755781
if (use_editor && include_status) {
756782
int ident_shown = 0;
757783
int saved_color_setting;
758-
char *ai_tmp, *ci_tmp;
784+
struct ident_split ci, ai;
785+
759786
if (whence != FROM_COMMIT) {
760787
if (cleanup_mode == CLEANUP_SCISSORS)
761788
wt_status_add_cut_line(s->fp);
@@ -795,21 +822,31 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
795822
status_printf_ln(s, GIT_COLOR_NORMAL,
796823
"%s", only_include_assumed);
797824

798-
ai_tmp = cut_ident_timestamp_part(author_ident->buf);
799-
ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
800-
if (strcmp(author_ident->buf, committer_ident.buf))
825+
split_ident_or_die(&ai, author_ident);
826+
split_ident_or_die(&ci, &committer_ident);
827+
828+
if (ident_cmp(&ai, &ci))
801829
status_printf_ln(s, GIT_COLOR_NORMAL,
802830
_("%s"
803-
"Author: %s"),
831+
"Author: %.*s <%.*s>"),
804832
ident_shown++ ? "" : "\n",
805-
author_ident->buf);
833+
(int)(ai.name_end - ai.name_begin), ai.name_begin,
834+
(int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
835+
836+
if (author_date_is_interesting())
837+
status_printf_ln(s, GIT_COLOR_NORMAL,
838+
_("%s"
839+
"Date: %s"),
840+
ident_shown++ ? "" : "\n",
841+
show_ident_date(&ai, DATE_NORMAL));
806842

807843
if (!committer_ident_sufficiently_given())
808844
status_printf_ln(s, GIT_COLOR_NORMAL,
809845
_("%s"
810-
"Committer: %s"),
846+
"Committer: %.*s <%.*s>"),
811847
ident_shown++ ? "" : "\n",
812-
committer_ident.buf);
848+
(int)(ci.name_end - ci.name_begin), ci.name_begin,
849+
(int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
813850

814851
if (ident_shown)
815852
status_printf_ln(s, GIT_COLOR_NORMAL, "");
@@ -818,9 +855,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
818855
s->use_color = 0;
819856
commitable = run_status(s->fp, index_file, prefix, 1, s);
820857
s->use_color = saved_color_setting;
821-
822-
*ai_tmp = ' ';
823-
*ci_tmp = ' ';
824858
} else {
825859
unsigned char sha1[20];
826860
const char *parent = "HEAD";
@@ -1356,6 +1390,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13561390
strbuf_addstr(&format, "\n Author: ");
13571391
strbuf_addbuf_percentquote(&format, &author_ident);
13581392
}
1393+
if (author_date_is_interesting()) {
1394+
struct strbuf date = STRBUF_INIT;
1395+
format_commit_message(commit, "%ad", &date, &pctx);
1396+
strbuf_addstr(&format, "\n Date: ");
1397+
strbuf_addbuf_percentquote(&format, &date);
1398+
strbuf_release(&date);
1399+
}
13591400
if (!committer_ident_sufficiently_given()) {
13601401
strbuf_addstr(&format, "\n Committer: ");
13611402
strbuf_addbuf_percentquote(&format, &committer_ident);

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,13 @@ struct ident_split {
10611061
*/
10621062
extern int split_ident_line(struct ident_split *, const char *, int);
10631063

1064+
/*
1065+
* Like show_date, but pull the timestamp and tz parameters from
1066+
* the ident_split. It will also sanity-check the values and produce
1067+
* a well-known sentinel date if they appear bogus.
1068+
*/
1069+
const char *show_ident_date(const struct ident_split *id, enum date_mode mode);
1070+
10641071
/*
10651072
* Compare split idents for equality or strict ordering. Note that we
10661073
* compare only the ident part of the line, ignoring any timestamp.

pretty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
393393
strbuf_addstr(sb, "?=");
394394
}
395395

396-
static const char *show_ident_date(const struct ident_split *ident,
397-
enum date_mode mode)
396+
const char *show_ident_date(const struct ident_split *ident,
397+
enum date_mode mode)
398398
{
399399
unsigned long date = 0;
400400
long tz = 0;

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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,21 @@ test_expect_success 'amend commit to fix date' '
346346
347347
'
348348

349-
test_expect_success 'commit complains about bogus date' '
350-
test_must_fail git commit --amend --date=10.11.2010
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 completely bogus dates' '
355+
test_must_fail git commit --amend --date=seventeen
356+
'
357+
358+
test_expect_success 'commit --date allows approxidate' '
359+
git commit --amend \
360+
--date="midnight the 12th of october, anno domini 1979" &&
361+
echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
362+
git log -1 --format=%ad >actual &&
363+
test_cmp expect actual
351364
'
352365

353366
test_expect_success 'sign off (1)' '

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)