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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Phillip Wood committed
Aug 17, 2020 at 18:40 UTC
e8cbe2118a8e3c1aa71ed8d93f96001decebde1d
6 files changed
+52
-28
builtin/am.c
+24
-6
@@ -98,6 +98,8 @@ struct am_state {
98
char *author_name;
99
char *author_email;
100
char *author_date;
101
+ char *committer_name;
102
+ char *committer_email;
103
char *msg;
104
size_t msg_len;
105
@@ -130,6 +132,8 @@ struct am_state {
132
*/
133
static void am_state_init(struct am_state *state)
134
{
135
+ const char *committer;
136
+ struct ident_split id;
137
int gpgsign;
138
139
memset(state, 0, sizeof(*state));
@@ -150,6 +154,14 @@ static void am_state_init(struct am_state *state)
154
155
if (!git_config_get_bool("commit.gpgsign", &gpgsign))
156
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);
165
}
166
167
/**
@@ -161,6 +173,8 @@ static void am_state_release(struct am_state *state)
173
free(state->author_name);
174
free(state->author_email);
175
free(state->author_date);
176
+ free(state->committer_name);
177
+ free(state->committer_email);
178
free(state->msg);
179
argv_array_clear(&state->git_apply_opts);
180
}
@@ -1556,7 +1570,7 @@ static void do_commit(const struct am_state *state)
1570
struct object_id tree, parent, commit;
1571
const struct object_id *old_oid;
1572
struct commit_list *parents = NULL;
1559
- const char *reflog_msg, *author;
1573
+ const char *reflog_msg, *author, *committer = NULL;
1574
struct strbuf sb = STRBUF_INIT;
1575
1576
if (run_hook_le(NULL, "pre-applypatch", NULL))
@@ -1580,11 +1594,15 @@ static void do_commit(const struct am_state *state)
1594
IDENT_STRICT);
1595
1596
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))
1606
die(_("failed to write commit object"));
1607
1608
reflog_msg = getenv("GIT_REFLOG_ACTION");
builtin/commit.c
+2
-2
@@ -1675,8 +1675,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1675
}
1676
1677
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)) {
1680
rollback_index_files();
1681
die(_("failed to write commit object"));
1682
}
commit.c
+7
-4
@@ -1324,8 +1324,8 @@ int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1324
int result;
1325
1326
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);
1329
free_commit_extra_headers(extra);
1330
return result;
1331
}
@@ -1448,7 +1448,8 @@ N_("Warning: commit message did not conform to UTF-8.\n"
1448
int commit_tree_extended(const char *msg, size_t msg_len,
1449
const struct object_id *tree,
1450
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,
1453
struct commit_extra_header *extra)
1454
{
1455
int result;
@@ -1481,7 +1482,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
1482
if (!author)
1483
author = git_author_info(IDENT_STRICT);
1484
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);
1488
if (!encoding_is_utf8)
1489
strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1490
commit.h
+3
-4
@@ -316,10 +316,9 @@ int commit_tree(const char *msg, size_t msg_len,
316
317
int commit_tree_extended(const char *msg, size_t msg_len,
318
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 *);
322
323
struct commit_extra_header *read_commit_extra_headers(struct commit *, const char **);
324
ident.c
+14
-10
@@ -361,11 +361,15 @@ N_("\n"
361
const char *fmt_ident(const char *name, const char *email,
362
enum want_ident whose_ident, const char *date_str, int flag)
363
{
364
- static struct strbuf ident = STRBUF_INIT;
364
+ static int index;
365
+ static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
366
int strict = (flag & IDENT_STRICT);
367
int want_date = !(flag & IDENT_NO_DATE);
368
int want_name = !(flag & IDENT_NO_NAME);
369
370
+ struct strbuf *ident = &ident_pool[index];
371
+ index = (index + 1) % ARRAY_SIZE(ident_pool);
372
+
373
if (!email) {
374
if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
375
email = git_author_email.buf;
@@ -421,25 +425,25 @@ const char *fmt_ident(const char *name, const char *email,
425
die(_("name consists only of disallowed characters: %s"), name);
426
}
427
424
- strbuf_reset(&ident);
428
+ strbuf_reset(ident);
429
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, " <");
432
}
429
- strbuf_addstr_without_crud(&ident, email);
433
+ strbuf_addstr_without_crud(ident, email);
434
if (want_name)
431
- strbuf_addch(&ident, '>');
435
+ strbuf_addch(ident, '>');
436
if (want_date) {
433
- strbuf_addch(&ident, ' ');
437
+ strbuf_addch(ident, ' ');
438
if (date_str && date_str[0]) {
435
- if (parse_date(date_str, &ident) < 0)
439
+ if (parse_date(date_str, ident) < 0)
440
die(_("invalid date format: %s"), date_str);
441
}
442
else
439
- strbuf_addstr(&ident, ident_default_date());
443
+ strbuf_addstr(ident, ident_default_date());
444
}
445
442
- return ident.buf;
446
+ return ident->buf;
447
}
448
449
const char *fmt_name(enum want_ident whose_ident)
sequencer.c
+2
-2
@@ -1408,8 +1408,8 @@ static int try_to_commit(struct repository *r,
1408
1409
reset_ident_date();
1410
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)) {
1413
res = error(_("failed to write commit object"));
1414
goto out;
1415
}