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
This commit is contained in:
Junio C Hamano 2014-06-03 12:06:46 -07:00
Родитель 6753d8a85d 14ac2864dc
Коммит e1857af923
6 изменённых файлов: 97 добавлений и 23 удалений

Просмотреть файл

@ -526,10 +526,29 @@ static int sane_ident_split(struct ident_split *person)
return 1; return 1;
} }
static int parse_force_date(const char *in, char *out, int len)
{
if (len < 1)
return -1;
*out++ = '@';
len--;
if (parse_date(in, out, len) < 0) {
int errors = 0;
unsigned long t = approxidate_careful(in, &errors);
if (errors)
return -1;
snprintf(out, len, "%lu", t);
}
return 0;
}
static void determine_author_info(struct strbuf *author_ident) static void determine_author_info(struct strbuf *author_ident)
{ {
char *name, *email, *date; char *name, *email, *date;
struct ident_split author; struct ident_split author;
char date_buf[64];
name = getenv("GIT_AUTHOR_NAME"); name = getenv("GIT_AUTHOR_NAME");
email = getenv("GIT_AUTHOR_EMAIL"); email = getenv("GIT_AUTHOR_EMAIL");
@ -574,8 +593,12 @@ static void determine_author_info(struct strbuf *author_ident)
email = xstrndup(lb + 2, rb - (lb + 2)); email = xstrndup(lb + 2, rb - (lb + 2));
} }
if (force_date) if (force_date) {
date = force_date; if (parse_force_date(force_date, date_buf, sizeof(date_buf)))
die(_("invalid date format: %s"), force_date);
date = date_buf;
}
strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT)); strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
if (!split_ident_line(&author, author_ident->buf, author_ident->len) && if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
sane_ident_split(&author)) { sane_ident_split(&author)) {
@ -585,13 +608,16 @@ static void determine_author_info(struct strbuf *author_ident)
} }
} }
static char *cut_ident_timestamp_part(char *string) static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
{ {
char *ket = strrchr(string, '>'); if (split_ident_line(id, buf->buf, buf->len) ||
if (!ket || ket[1] != ' ') !sane_ident_split(id))
die(_("Malformed ident string: '%s'"), string); die(_("Malformed ident string: '%s'"), buf->buf);
*++ket = '\0'; }
return ket;
static int author_date_is_interesting(void)
{
return author_message || force_date;
} }
static int prepare_to_commit(const char *index_file, const char *prefix, 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,
if (use_editor && include_status) { if (use_editor && include_status) {
int ident_shown = 0; int ident_shown = 0;
int saved_color_setting; int saved_color_setting;
char *ai_tmp, *ci_tmp; struct ident_split ci, ai;
if (whence != FROM_COMMIT) { if (whence != FROM_COMMIT) {
if (cleanup_mode == CLEANUP_SCISSORS) if (cleanup_mode == CLEANUP_SCISSORS)
wt_status_add_cut_line(s->fp); wt_status_add_cut_line(s->fp);
@ -795,21 +822,31 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
status_printf_ln(s, GIT_COLOR_NORMAL, status_printf_ln(s, GIT_COLOR_NORMAL,
"%s", only_include_assumed); "%s", only_include_assumed);
ai_tmp = cut_ident_timestamp_part(author_ident->buf); split_ident_or_die(&ai, author_ident);
ci_tmp = cut_ident_timestamp_part(committer_ident.buf); split_ident_or_die(&ci, &committer_ident);
if (strcmp(author_ident->buf, committer_ident.buf))
if (ident_cmp(&ai, &ci))
status_printf_ln(s, GIT_COLOR_NORMAL, status_printf_ln(s, GIT_COLOR_NORMAL,
_("%s" _("%s"
"Author: %s"), "Author: %.*s <%.*s>"),
ident_shown++ ? "" : "\n", ident_shown++ ? "" : "\n",
author_ident->buf); (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()) if (!committer_ident_sufficiently_given())
status_printf_ln(s, GIT_COLOR_NORMAL, status_printf_ln(s, GIT_COLOR_NORMAL,
_("%s" _("%s"
"Committer: %s"), "Committer: %.*s <%.*s>"),
ident_shown++ ? "" : "\n", ident_shown++ ? "" : "\n",
committer_ident.buf); (int)(ci.name_end - ci.name_begin), ci.name_begin,
(int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
if (ident_shown) if (ident_shown)
status_printf_ln(s, GIT_COLOR_NORMAL, ""); status_printf_ln(s, GIT_COLOR_NORMAL, "");
@ -818,9 +855,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
s->use_color = 0; s->use_color = 0;
commitable = run_status(s->fp, index_file, prefix, 1, s); commitable = run_status(s->fp, index_file, prefix, 1, s);
s->use_color = saved_color_setting; s->use_color = saved_color_setting;
*ai_tmp = ' ';
*ci_tmp = ' ';
} else { } else {
unsigned char sha1[20]; unsigned char sha1[20];
const char *parent = "HEAD"; const char *parent = "HEAD";
@ -1356,6 +1390,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
strbuf_addstr(&format, "\n Author: "); strbuf_addstr(&format, "\n Author: ");
strbuf_addbuf_percentquote(&format, &author_ident); 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()) { if (!committer_ident_sufficiently_given()) {
strbuf_addstr(&format, "\n Committer: "); strbuf_addstr(&format, "\n Committer: ");
strbuf_addbuf_percentquote(&format, &committer_ident); strbuf_addbuf_percentquote(&format, &committer_ident);

Просмотреть файл

@ -1061,6 +1061,13 @@ struct ident_split {
*/ */
extern int split_ident_line(struct ident_split *, const char *, int); extern int split_ident_line(struct ident_split *, const char *, int);
/*
* Like show_date, but pull the timestamp and tz parameters from
* the ident_split. It will also sanity-check the values and produce
* a well-known sentinel date if they appear bogus.
*/
const char *show_ident_date(const struct ident_split *id, enum date_mode mode);
/* /*
* Compare split idents for equality or strict ordering. Note that we * Compare split idents for equality or strict ordering. Note that we
* compare only the ident part of the line, ignoring any timestamp. * compare only the ident part of the line, ignoring any timestamp.

Просмотреть файл

@ -393,8 +393,8 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
strbuf_addstr(sb, "?="); strbuf_addstr(sb, "?=");
} }
static const char *show_ident_date(const struct ident_split *ident, const char *show_ident_date(const struct ident_split *ident,
enum date_mode mode) enum date_mode mode)
{ {
unsigned long date = 0; unsigned long date = 0;
long tz = 0; long tz = 0;

Просмотреть файл

@ -65,12 +65,15 @@ test_expect_success 'output to keep user entertained during multi-pick' '
cat <<-\EOF >expected && cat <<-\EOF >expected &&
[master OBJID] second [master OBJID] second
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:14:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
[master OBJID] third [master OBJID] third
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:15:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
[master OBJID] fourth [master OBJID] fourth
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:16:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
EOF EOF
@ -98,14 +101,17 @@ test_expect_success 'output during multi-pick indicates merge strategy' '
Trying simple merge. Trying simple merge.
[master OBJID] second [master OBJID] second
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:14:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
Trying simple merge. Trying simple merge.
[master OBJID] third [master OBJID] third
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:15:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
Trying simple merge. Trying simple merge.
[master OBJID] fourth [master OBJID] fourth
Author: A U Thor <author@example.com> Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:16:13 2005 -0700
1 file changed, 1 insertion(+) 1 file changed, 1 insertion(+)
EOF EOF

Просмотреть файл

@ -346,8 +346,21 @@ test_expect_success 'amend commit to fix date' '
' '
test_expect_success 'commit complains about bogus date' ' test_expect_success 'commit mentions forced date in output' '
test_must_fail git commit --amend --date=10.11.2010 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 completely bogus dates' '
test_must_fail git commit --amend --date=seventeen
'
test_expect_success 'commit --date allows approxidate' '
git commit --amend \
--date="midnight the 12th of october, anno domini 1979" &&
echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
git log -1 --format=%ad >actual &&
test_cmp expect actual
' '
test_expect_success 'sign off (1)' ' test_expect_success 'sign off (1)' '

Просмотреть файл

@ -344,6 +344,13 @@ test_expect_success 'message shows author when it is not equal to committer' '
.git/COMMIT_EDITMSG .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' ' test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
echo >>negative && echo >>negative &&