Skip to content

Commit 3c3b459

Browse files
committed
sequencer: simplify root commit creation
Adapt try_to_commit() to create a new root commit rather than special casing this in run_git_commit(). The significantly reduces the amount of special case code for creating the root commit and reduces the number of commit code paths we have to worry about. Signed-off-by: Phillip Wood <[email protected]>
1 parent 7839a84 commit 3c3b459

File tree

1 file changed

+4
-71
lines changed

1 file changed

+4
-71
lines changed

sequencer.c

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -869,34 +869,6 @@ static char *get_author(const char *message)
869869
return NULL;
870870
}
871871

872-
/* Read author-script and return an ident line (author <email> timestamp) */
873-
static const char *read_author_ident(struct strbuf *buf)
874-
{
875-
struct strbuf out = STRBUF_INIT;
876-
char *name, *email, *date;
877-
878-
if (read_author_script(rebase_path_author_script(),
879-
&name, &email, &date, 0))
880-
return NULL;
881-
882-
/* validate date since fmt_ident() will die() on bad value */
883-
if (parse_date(date, &out)){
884-
warning(_("invalid date format '%s' in '%s'"),
885-
date, rebase_path_author_script());
886-
strbuf_release(&out);
887-
return NULL;
888-
}
889-
890-
strbuf_reset(&out);
891-
strbuf_addstr(&out, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, 0));
892-
strbuf_swap(buf, &out);
893-
strbuf_release(&out);
894-
free(name);
895-
free(email);
896-
free(date);
897-
return buf->buf;
898-
}
899-
900872
static const char staged_changes_advice[] =
901873
N_("you have staged changes in your working tree\n"
902874
"If these changes are meant to be squashed into the previous commit, run:\n"
@@ -954,45 +926,6 @@ static int run_git_commit(struct repository *r,
954926
{
955927
struct child_process cmd = CHILD_PROCESS_INIT;
956928

957-
if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
958-
struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
959-
const char *author = NULL;
960-
struct object_id root_commit, *cache_tree_oid;
961-
int res = 0;
962-
963-
if (is_rebase_i(opts)) {
964-
author = read_author_ident(&script);
965-
if (!author) {
966-
strbuf_release(&script);
967-
return -1;
968-
}
969-
}
970-
971-
if (!defmsg)
972-
BUG("root commit without message");
973-
974-
if (!(cache_tree_oid = get_cache_tree_oid(r->index)))
975-
res = -1;
976-
977-
if (!res)
978-
res = strbuf_read_file(&msg, defmsg, 0);
979-
980-
if (res <= 0)
981-
res = error_errno(_("could not read '%s'"), defmsg);
982-
else
983-
res = commit_tree(msg.buf, msg.len, cache_tree_oid,
984-
NULL, &root_commit, author,
985-
opts->gpg_sign);
986-
987-
strbuf_release(&msg);
988-
strbuf_release(&script);
989-
if (!res)
990-
res = update_ref(NULL, "HEAD", &root_commit, NULL, 0,
991-
UPDATE_REFS_MSG_ON_ERR);
992-
993-
return res < 0 ? error(_("writing root commit")) : 0;
994-
}
995-
996929
cmd.git_cmd = 1;
997930

998931
if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
@@ -1376,7 +1309,7 @@ static int try_to_commit(struct repository *r,
13761309
struct object_id *oid)
13771310
{
13781311
struct object_id tree;
1379-
struct commit *current_head;
1312+
struct commit *current_head = NULL;
13801313
struct commit_list *parents = NULL;
13811314
struct commit_extra_header *extra = NULL;
13821315
struct strbuf err = STRBUF_INIT;
@@ -1411,7 +1344,8 @@ static int try_to_commit(struct repository *r,
14111344
}
14121345
parents = copy_commit_list(current_head->parents);
14131346
extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1414-
} else if (current_head) {
1347+
} else if (current_head &&
1348+
(!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) {
14151349
commit_list_insert(current_head, &parents);
14161350
}
14171351

@@ -1488,8 +1422,7 @@ static int do_commit(struct repository *r,
14881422
{
14891423
int res = 1;
14901424

1491-
if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG) &&
1492-
!(flags & CREATE_ROOT_COMMIT)) {
1425+
if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) {
14931426
struct object_id oid;
14941427
struct strbuf sb = STRBUF_INIT;
14951428

0 commit comments

Comments
 (0)