split_ident_line(): make best effort when parsing author/committer line

Commits made by ancient version of Git allowed committer without human readable name, like this (00213b17c in the kernel history): tree 6947dba41f8b0e7fe7bccd41a4840d6de6a27079 parent 352dd1df32e672be4cff71132eb9c06a257872fe author Petr Baudis <pasky@ucw.cz> 1135223044 +0100 committer <sam@mars.ravnborg.org> 1136151043 +0100 kconfig: Remove support for lxdialog --checklist ... Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Sam Ravnborg <sam@ravnborg.org> When fed such a commit, --format='%ci' fails to parse it, and gives back an empty string. Update the split_ident_line() to be a bit more lenient when parsing, but make sure the caller that wants to pick up sane value from its return value does its own validation. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 31, 2012 at 14:54 UTC e27ddb64568412b75035a05366c9f091153155ed
2 files changed +20 -3
builtin/commit.c
+16 -1
@@ -478,6 +478,20 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
478 strbuf_release(&buf);
479 }
480
481 +static int sane_ident_split(struct ident_split *person)
482 +{
483 + if (!person->name_begin || !person->name_end ||
484 + person->name_begin == person->name_end)
485 + return 0; /* no human readable name */
486 + if (!person->mail_begin || !person->mail_end ||
487 + person->mail_begin == person->mail_end)
488 + return 0; /* no usable mail */
489 + if (!person->date_begin || !person->date_end ||
490 + !person->tz_begin || !person->tz_end)
491 + return 0;
492 + return 1;
493 +}
494 +
495 static void determine_author_info(struct strbuf *author_ident)
496 {
497 char *name, *email, *date;
@@ -530,7 +544,8 @@ static void determine_author_info(struct strbuf *author_ident)
544 if (force_date)
545 date = force_date;
546 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
533 - if (!split_ident_line(&author, author_ident->buf, author_ident->len)) {
547 + if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
548 + sane_ident_split(&author)) {
549 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
550 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
551 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
ident.c
+4 -2
@@ -210,8 +210,10 @@ int split_ident_line(struct ident_split *split, const char *line, int len)
210 split->name_end = cp + 1;
211 break;
212 }
213 - if (!split->name_end)
214 - return status;
213 + if (!split->name_end) {
214 + /* no human readable name */
215 + split->name_end = split->name_begin;
216 + }
217
218 for (cp = split->mail_begin; cp < line + len; cp++)
219 if (*cp == '>') {