Skip to content

Commit c83a509

Browse files
peffgitster
authored andcommitted
commit: always populate GIT_AUTHOR_* variables
To figure out the author ident for a commit, we call determine_author_info(). This function collects information from the environment, other commits (in the case of "--amend" or "-c/-C"), and the "--author" option. It then uses fmt_ident to generate the final ident string that goes into the commit object. fmt_ident is therefore responsible for any quality or validation checks on what is allowed to go into a commit. Before returning, though, we call split_ident_line on the result, and feed the individual components to hooks via the GIT_AUTHOR_* variables. Furthermore, we do extra validation by feeding the split to sane_ident_split(), which is pickier than fmt_ident (in particular, it will complain about an empty email field). If this parsing or validation fails, we skip updating the environment variables. This is bad, because it means that hooks may silently see a different ident than what we are putting into the commit. We should drop the extra sane_ident_split checks entirely, and take whatever fmt_ident has fed us (and what will go into the commit object). If parsing fails, we should actually abort here rather than continuing (and feeding the hooks bogus data). However, split_ident_line should never fail here. The ident was just generated by fmt_ident, so we know that it's sane. We can use assert_split_ident to double-check this. Note that we also teach that assertion to check that we found a date (it always should, but until now, no caller cared whether we found a date or not). Checking the return value of sane_ident_split is enough to ensure we have the name/email pointers set, and checking date_begin is enough to know that all of the date/tz variables are set. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fac9083 commit c83a509

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

builtin/commit.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static int is_a_merge(const struct commit *current_head)
504504

505505
static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
506506
{
507-
if (split_ident_line(id, buf->buf, buf->len))
507+
if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
508508
die("BUG: unable to parse our own ident: %s", buf->buf);
509509
}
510510

@@ -518,20 +518,6 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
518518
strbuf_release(&buf);
519519
}
520520

521-
static int sane_ident_split(struct ident_split *person)
522-
{
523-
if (!person->name_begin || !person->name_end ||
524-
person->name_begin == person->name_end)
525-
return 0; /* no human readable name */
526-
if (!person->mail_begin || !person->mail_end ||
527-
person->mail_begin == person->mail_end)
528-
return 0; /* no usable mail */
529-
if (!person->date_begin || !person->date_end ||
530-
!person->tz_begin || !person->tz_end)
531-
return 0;
532-
return 1;
533-
}
534-
535521
static int parse_force_date(const char *in, char *out, int len)
536522
{
537523
if (len < 1)
@@ -606,12 +592,10 @@ static void determine_author_info(struct strbuf *author_ident)
606592
}
607593

608594
strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
609-
if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
610-
sane_ident_split(&author)) {
611-
export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
612-
export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
613-
export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
614-
}
595+
assert_split_ident(&author, author_ident);
596+
export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
597+
export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
598+
export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
615599
}
616600

617601
static int author_date_is_interesting(void)

0 commit comments

Comments
 (0)