Skip to content

Commit e8cbe21

Browse files
phillipwoodgitster
authored andcommitted
am: stop exporting GIT_COMMITTER_DATE
The implementation of --committer-date-is-author-date exports GIT_COMMITTER_DATE to override the default committer date but does not reset GIT_COMMITTER_DATE in the environment after creating the commit so it is set in the environment of any hooks that get run. We're about to add the same functionality to the sequencer and do not want to have GIT_COMMITTER_DATE set when running hooks or exec commands so lets update commit_tree_extended() to take an explicit committer so we override the default date without setting GIT_COMMITTER_DATE in the environment. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef484ad commit e8cbe21

File tree

6 files changed

+52
-28
lines changed

6 files changed

+52
-28
lines changed

builtin/am.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ struct am_state {
9898
char *author_name;
9999
char *author_email;
100100
char *author_date;
101+
char *committer_name;
102+
char *committer_email;
101103
char *msg;
102104
size_t msg_len;
103105

@@ -130,6 +132,8 @@ struct am_state {
130132
*/
131133
static void am_state_init(struct am_state *state)
132134
{
135+
const char *committer;
136+
struct ident_split id;
133137
int gpgsign;
134138

135139
memset(state, 0, sizeof(*state));
@@ -150,6 +154,14 @@ static void am_state_init(struct am_state *state)
150154

151155
if (!git_config_get_bool("commit.gpgsign", &gpgsign))
152156
state->sign_commit = gpgsign ? "" : NULL;
157+
158+
committer = git_committer_info(IDENT_STRICT);
159+
if (split_ident_line(&id, committer, strlen(committer)) < 0)
160+
die(_("invalid committer: %s"), committer);
161+
state->committer_name =
162+
xmemdupz(id.name_begin, id.name_end - id.name_begin);
163+
state->committer_email =
164+
xmemdupz(id.mail_begin, id.mail_end - id.mail_end);
153165
}
154166

155167
/**
@@ -161,6 +173,8 @@ static void am_state_release(struct am_state *state)
161173
free(state->author_name);
162174
free(state->author_email);
163175
free(state->author_date);
176+
free(state->committer_name);
177+
free(state->committer_email);
164178
free(state->msg);
165179
argv_array_clear(&state->git_apply_opts);
166180
}
@@ -1556,7 +1570,7 @@ static void do_commit(const struct am_state *state)
15561570
struct object_id tree, parent, commit;
15571571
const struct object_id *old_oid;
15581572
struct commit_list *parents = NULL;
1559-
const char *reflog_msg, *author;
1573+
const char *reflog_msg, *author, *committer = NULL;
15601574
struct strbuf sb = STRBUF_INIT;
15611575

15621576
if (run_hook_le(NULL, "pre-applypatch", NULL))
@@ -1580,11 +1594,15 @@ static void do_commit(const struct am_state *state)
15801594
IDENT_STRICT);
15811595

15821596
if (state->committer_date_is_author_date)
1583-
setenv("GIT_COMMITTER_DATE",
1584-
state->ignore_date ? "" : state->author_date, 1);
1585-
1586-
if (commit_tree(state->msg, state->msg_len, &tree, parents, &commit,
1587-
author, state->sign_commit))
1597+
committer = fmt_ident(state->committer_name,
1598+
state->author_email, WANT_COMMITTER_IDENT,
1599+
state->ignore_date ? NULL
1600+
: state->author_date,
1601+
IDENT_STRICT);
1602+
1603+
if (commit_tree_extended(state->msg, state->msg_len, &tree, parents,
1604+
&commit, author, committer, state->sign_commit,
1605+
NULL))
15881606
die(_("failed to write commit object"));
15891607

15901608
reflog_msg = getenv("GIT_REFLOG_ACTION");

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,8 +1675,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16751675
}
16761676

16771677
if (commit_tree_extended(sb.buf, sb.len, &active_cache_tree->oid,
1678-
parents, &oid, author_ident.buf, sign_commit,
1679-
extra)) {
1678+
parents, &oid, author_ident.buf, NULL,
1679+
sign_commit, extra)) {
16801680
rollback_index_files();
16811681
die(_("failed to write commit object"));
16821682
}

commit.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
13241324
int result;
13251325

13261326
append_merge_tag_headers(parents, &tail);
1327-
result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1328-
author, sign_commit, extra);
1327+
result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1328+
NULL, sign_commit, extra);
13291329
free_commit_extra_headers(extra);
13301330
return result;
13311331
}
@@ -1448,7 +1448,8 @@ N_("Warning: commit message did not conform to UTF-8.\n"
14481448
int commit_tree_extended(const char *msg, size_t msg_len,
14491449
const struct object_id *tree,
14501450
struct commit_list *parents, struct object_id *ret,
1451-
const char *author, const char *sign_commit,
1451+
const char *author, const char *committer,
1452+
const char *sign_commit,
14521453
struct commit_extra_header *extra)
14531454
{
14541455
int result;
@@ -1481,7 +1482,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
14811482
if (!author)
14821483
author = git_author_info(IDENT_STRICT);
14831484
strbuf_addf(&buffer, "author %s\n", author);
1484-
strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1485+
if (!committer)
1486+
committer = git_committer_info(IDENT_STRICT);
1487+
strbuf_addf(&buffer, "committer %s\n", committer);
14851488
if (!encoding_is_utf8)
14861489
strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
14871490

commit.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,9 @@ int commit_tree(const char *msg, size_t msg_len,
316316

317317
int commit_tree_extended(const char *msg, size_t msg_len,
318318
const struct object_id *tree,
319-
struct commit_list *parents,
320-
struct object_id *ret, const char *author,
321-
const char *sign_commit,
322-
struct commit_extra_header *);
319+
struct commit_list *parents, struct object_id *ret,
320+
const char *author, const char *committer,
321+
const char *sign_commit, struct commit_extra_header *);
323322

324323
struct commit_extra_header *read_commit_extra_headers(struct commit *, const char **);
325324

ident.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,15 @@ N_("\n"
361361
const char *fmt_ident(const char *name, const char *email,
362362
enum want_ident whose_ident, const char *date_str, int flag)
363363
{
364-
static struct strbuf ident = STRBUF_INIT;
364+
static int index;
365+
static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
365366
int strict = (flag & IDENT_STRICT);
366367
int want_date = !(flag & IDENT_NO_DATE);
367368
int want_name = !(flag & IDENT_NO_NAME);
368369

370+
struct strbuf *ident = &ident_pool[index];
371+
index = (index + 1) % ARRAY_SIZE(ident_pool);
372+
369373
if (!email) {
370374
if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
371375
email = git_author_email.buf;
@@ -421,25 +425,25 @@ const char *fmt_ident(const char *name, const char *email,
421425
die(_("name consists only of disallowed characters: %s"), name);
422426
}
423427

424-
strbuf_reset(&ident);
428+
strbuf_reset(ident);
425429
if (want_name) {
426-
strbuf_addstr_without_crud(&ident, name);
427-
strbuf_addstr(&ident, " <");
430+
strbuf_addstr_without_crud(ident, name);
431+
strbuf_addstr(ident, " <");
428432
}
429-
strbuf_addstr_without_crud(&ident, email);
433+
strbuf_addstr_without_crud(ident, email);
430434
if (want_name)
431-
strbuf_addch(&ident, '>');
435+
strbuf_addch(ident, '>');
432436
if (want_date) {
433-
strbuf_addch(&ident, ' ');
437+
strbuf_addch(ident, ' ');
434438
if (date_str && date_str[0]) {
435-
if (parse_date(date_str, &ident) < 0)
439+
if (parse_date(date_str, ident) < 0)
436440
die(_("invalid date format: %s"), date_str);
437441
}
438442
else
439-
strbuf_addstr(&ident, ident_default_date());
443+
strbuf_addstr(ident, ident_default_date());
440444
}
441445

442-
return ident.buf;
446+
return ident->buf;
443447
}
444448

445449
const char *fmt_name(enum want_ident whose_ident)

sequencer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,8 +1408,8 @@ static int try_to_commit(struct repository *r,
14081408

14091409
reset_ident_date();
14101410

1411-
if (commit_tree_extended(msg->buf, msg->len, &tree, parents,
1412-
oid, author, opts->gpg_sign, extra)) {
1411+
if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid,
1412+
author, NULL, opts->gpg_sign, extra)) {
14131413
res = error(_("failed to write commit object"));
14141414
goto out;
14151415
}

0 commit comments

Comments
 (0)