sequencer: simplify root commit creation

Adapt try_to_commit() to create a new root commit rather than special casing this in run_git_commit(). This 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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Aug 19, 2019 at 02:18 UTC b0a3186140dbc7bd64cbc6ef733386a0f1eb6a4d
1 file changed +4 -71
sequencer.c
+4 -71
@@ -869,34 +869,6 @@ static char *get_author(const char *message)
869 return NULL;
870 }
871
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 -
872 static const char staged_changes_advice[] =
873 N_("you have staged changes in your working tree\n"
874 "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,
926 {
927 struct child_process cmd = CHILD_PROCESS_INIT;
928
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 -
929 cmd.git_cmd = 1;
930
931 if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
@@ -1376,7 +1309,7 @@ static int try_to_commit(struct repository *r,
1309 struct object_id *oid)
1310 {
1311 struct object_id tree;
1379 - struct commit *current_head;
1312 + struct commit *current_head = NULL;
1313 struct commit_list *parents = NULL;
1314 struct commit_extra_header *extra = NULL;
1315 struct strbuf err = STRBUF_INIT;
@@ -1411,7 +1344,8 @@ static int try_to_commit(struct repository *r,
1344 }
1345 parents = copy_commit_list(current_head->parents);
1346 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))) {
1349 commit_list_insert(current_head, &parents);
1350 }
1351
@@ -1488,8 +1422,7 @@ static int do_commit(struct repository *r,
1422 {
1423 int res = 1;
1424
1491 - if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG) &&
1492 - !(flags & CREATE_ROOT_COMMIT)) {
1425 + if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) {
1426 struct object_id oid;
1427 struct strbuf sb = STRBUF_INIT;
1428