]> git.ipfire.org Git - thirdparty/git.git/blobdiff - sequencer.c
Merge branch 'ag/sequencer-reduce-rewriting-todo' into pw/rebase-i-internal
[thirdparty/git.git] / sequencer.c
index 2a0fcb1cce19494723b9327e6f3b96b98cafe635..281a8ade19c91b7e0356ec357ac1cd538cac2272 100644 (file)
@@ -35,7 +35,7 @@
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
-const char sign_off_header[] = "Signed-off-by: ";
+static const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
 GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
@@ -149,6 +149,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
 static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
 static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
+static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
 static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff")
 static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
 static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
@@ -156,7 +157,7 @@ static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
 static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
 static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
-static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
+static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec")
 
 static int git_sequencer_config(const char *k, const char *v, void *cb)
 {
@@ -445,9 +446,9 @@ static struct tree *empty_tree(struct repository *r)
        return lookup_tree(r, the_hash_algo->empty_tree);
 }
 
-static int error_dirty_index(struct index_state *istate, struct replay_opts *opts)
+static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
 {
-       if (read_index_unmerged(istate))
+       if (repo_read_index_unmerged(repo))
                return error_resolve_conflict(_(action_name(opts)));
 
        error(_("your local changes would be overwritten by %s."),
@@ -482,7 +483,7 @@ static int fast_forward_to(struct repository *r,
        struct strbuf sb = STRBUF_INIT;
        struct strbuf err = STRBUF_INIT;
 
-       read_index(r->index);
+       repo_read_index(r);
        if (checkout_fast_forward(r, from, to, 1))
                return -1; /* the callee should have complained already */
 
@@ -539,12 +540,12 @@ static int do_recursive_merge(struct repository *r,
        char **xopt;
        struct lock_file index_lock = LOCK_INIT;
 
-       if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0)
+       if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
 
-       read_index(r->index);
+       repo_read_index(r);
 
-       init_merge_options(&o);
+       init_merge_options(&o, r);
        o.ancestor = base ? base_label : "(empty tree)";
        o.branch1 = "HEAD";
        o.branch2 = next ? next_label : "(empty tree)";
@@ -835,7 +836,7 @@ static const char *read_author_ident(struct strbuf *buf)
        }
 
        strbuf_reset(&out);
-       strbuf_addstr(&out, fmt_ident(name, email, date, 0));
+       strbuf_addstr(&out, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, 0));
        strbuf_swap(buf, &out);
        strbuf_release(&out);
        free(name);
@@ -1101,6 +1102,7 @@ static int run_rewrite_hook(const struct object_id *oldoid,
        proc.argv = argv;
        proc.in = -1;
        proc.stdout_to_stderr = 1;
+       proc.trace2_hook_name = "post-rewrite";
 
        code = start_command(&proc);
        if (code)
@@ -1114,7 +1116,8 @@ static int run_rewrite_hook(const struct object_id *oldoid,
        return finish_command(&proc);
 }
 
-void commit_post_rewrite(const struct commit *old_head,
+void commit_post_rewrite(struct repository *r,
+                        const struct commit *old_head,
                         const struct object_id *new_head)
 {
        struct notes_rewrite_cfg *cfg;
@@ -1123,7 +1126,7 @@ void commit_post_rewrite(const struct commit *old_head,
        if (cfg) {
                /* we are amending, so old_head is not NULL */
                copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
-               finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
+               finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'");
        }
        run_rewrite_hook(&old_head->object.oid, new_head);
 }
@@ -1404,7 +1407,7 @@ static int try_to_commit(struct repository *r,
        }
 
        if (flags & AMEND_MSG)
-               commit_post_rewrite(current_head, oid);
+               commit_post_rewrite(r, current_head, oid);
 
 out:
        free_commit_extra_headers(extra);
@@ -1662,7 +1665,8 @@ static int update_squash_messages(struct repository *r,
        return res;
 }
 
-static void flush_rewritten_pending(void) {
+static void flush_rewritten_pending(void)
+{
        struct strbuf buf = STRBUF_INIT;
        struct object_id newoid;
        FILE *out;
@@ -1687,7 +1691,8 @@ static void flush_rewritten_pending(void) {
 }
 
 static void record_in_rewritten(struct object_id *oid,
-               enum todo_command next_command) {
+               enum todo_command next_command)
+{
        FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a");
 
        if (!out)
@@ -1738,7 +1743,7 @@ static int do_pick_commit(struct repository *r,
                        oidcpy(&head, the_hash_algo->empty_tree);
                if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
                                       NULL, 0))
-                       return error_dirty_index(r->index, opts);
+                       return error_dirty_index(r, opts);
        }
        discard_index(r->index);
 
@@ -1761,9 +1766,13 @@ static int do_pick_commit(struct repository *r,
                        return error(_("commit %s does not have parent %d"),
                                oid_to_hex(&commit->object.oid), opts->mainline);
                parent = p->item;
-       } else if (0 < opts->mainline)
-               return error(_("mainline was specified but commit %s is not a merge."),
-                       oid_to_hex(&commit->object.oid));
+       } else if (1 < opts->mainline)
+               /*
+                *  Non-first parent explicitly specified as mainline for
+                *  non-merge commit
+                */
+               return error(_("commit %s does not have parent %d"),
+                            oid_to_hex(&commit->object.oid), opts->mainline);
        else
                parent = commit->parents->item;
 
@@ -1964,8 +1973,8 @@ static int read_and_refresh_cache(struct repository *r,
                                  struct replay_opts *opts)
 {
        struct lock_file index_lock = LOCK_INIT;
-       int index_fd = hold_locked_index(&index_lock, 0);
-       if (read_index(r->index) < 0) {
+       int index_fd = repo_hold_locked_index(r, &index_lock, 0);
+       if (repo_read_index(r) < 0) {
                rollback_lock_file(&index_lock);
                return error(_("git %s: failed to read the index"),
                        _(action_name(opts)));
@@ -2349,11 +2358,17 @@ static int read_populate_opts(struct replay_opts *opts)
                if (file_exists(rebase_path_verbose()))
                        opts->verbose = 1;
 
+               if (file_exists(rebase_path_quiet()))
+                       opts->quiet = 1;
+
                if (file_exists(rebase_path_signoff())) {
                        opts->allow_ff = 0;
                        opts->signoff = 1;
                }
 
+               if (file_exists(rebase_path_reschedule_failed_exec()))
+                       opts->reschedule_failed_exec = 1;
+
                read_strategy_opts(opts, &buf);
                strbuf_release(&buf);
 
@@ -2416,9 +2431,6 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
 
        if (quiet)
                write_file(rebase_path_quiet(), "%s\n", quiet);
-       else
-               write_file(rebase_path_quiet(), "\n");
-
        if (opts->verbose)
                write_file(rebase_path_verbose(), "%s", "");
        if (opts->strategy)
@@ -2435,6 +2447,8 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
                write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign);
        if (opts->signoff)
                write_file(rebase_path_signoff(), "--signoff\n");
+       if (opts->reschedule_failed_exec)
+               write_file(rebase_path_reschedule_failed_exec(), "%s", "");
 
        return 0;
 }
@@ -2819,7 +2833,7 @@ static int do_exec(struct repository *r, const char *command_line)
                                          child_env.argv);
 
        /* force re-reading of the cache */
-       if (discard_index(r->index) < 0 || read_index(r->index) < 0)
+       if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
                return error(_("could not read index"));
 
        dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@@ -2943,7 +2957,7 @@ static int do_reset(struct repository *r,
        struct unpack_trees_options unpack_tree_opts;
        int ret = 0;
 
-       if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
+       if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
 
        if (len == 10 && !strncmp("[new root]", name, len)) {
@@ -2988,7 +3002,7 @@ static int do_reset(struct repository *r,
        unpack_tree_opts.merge = 1;
        unpack_tree_opts.update = 1;
 
-       if (read_index_unmerged(r->index)) {
+       if (repo_read_index_unmerged(r)) {
                rollback_lock_file(&lock);
                strbuf_release(&ref_name);
                return error_resolve_conflict(_(action_name(opts)));
@@ -3061,7 +3075,7 @@ static int do_merge(struct repository *r,
        static struct lock_file lock;
        const char *p;
 
-       if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
+       if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
                ret = -1;
                goto leave_merge;
        }
@@ -3242,7 +3256,7 @@ static int do_merge(struct repository *r,
 
                /* force re-reading of the cache */
                if (!ret && (discard_index(r->index) < 0 ||
-                            read_index(r->index) < 0))
+                            repo_read_index(r) < 0))
                        ret = error(_("could not read index"));
                goto leave_merge;
        }
@@ -3264,8 +3278,8 @@ static int do_merge(struct repository *r,
                commit_list_insert(j->item, &reversed);
        free_commit_list(bases);
 
-       read_index(r->index);
-       init_merge_options(&o);
+       repo_read_index(r);
+       init_merge_options(&o, r);
        o.branch1 = "HEAD";
        o.branch2 = ref_name.buf;
        o.buffer_output = 2;
@@ -3511,10 +3525,11 @@ static int pick_commits(struct repository *r,
                                        fprintf(f, "%d\n", todo_list->done_nr);
                                        fclose(f);
                                }
-                               fprintf(stderr, "Rebasing (%d/%d)%s",
-                                       todo_list->done_nr,
-                                       todo_list->total_nr,
-                                       opts->verbose ? "\n" : "\r");
+                               if (!opts->quiet)
+                                       fprintf(stderr, "Rebasing (%d/%d)%s",
+                                               todo_list->done_nr,
+                                               todo_list->total_nr,
+                                               opts->verbose ? "\n" : "\r");
                        }
                        unlink(rebase_path_message());
                        unlink(rebase_path_author_script());
@@ -3593,9 +3608,10 @@ static int pick_commits(struct repository *r,
                        *end_of_arg = saved;
 
                        /* Reread the todo file if it has changed. */
-                       if (res)
-                               ; /* fall through */
-                       else if (stat(get_todo_path(opts), &st))
+                       if (res) {
+                               if (opts->reschedule_failed_exec)
+                                       reschedule = 1;
+                       } else if (stat(get_todo_path(opts), &st))
                                res = error_errno(_("could not stat '%s'"),
                                                  get_todo_path(opts));
                        else if (match_stat_data(&todo_list->stat, &st)) {
@@ -3736,6 +3752,7 @@ cleanup_head_ref:
                                hook.in = open(rebase_path_rewritten_list(),
                                        O_RDONLY);
                                hook.stdout_to_stderr = 1;
+                               hook.trace2_hook_name = "post-rewrite";
                                argv_array_push(&hook.args, post_rewrite_hook);
                                argv_array_push(&hook.args, "rebase");
                                /* we don't care if this hook failed */
@@ -3744,8 +3761,10 @@ cleanup_head_ref:
                }
                apply_autostash(opts);
 
-               fprintf(stderr, "Successfully rebased and updated %s.\n",
-                       head_ref.buf);
+               if (!opts->quiet)
+                       fprintf(stderr,
+                               "Successfully rebased and updated %s.\n",
+                               head_ref.buf);
 
                strbuf_release(&buf);
                strbuf_release(&head_ref);
@@ -3936,7 +3955,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
                                goto release_todo_list;
                }
                if (index_differs_from(r, "HEAD", NULL, 0)) {
-                       res = error_dirty_index(r->index, opts);
+                       res = error_dirty_index(r, opts);
                        goto release_todo_list;
                }
                todo_list.current++;
@@ -4046,8 +4065,7 @@ void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
        int has_footer;
 
        strbuf_addstr(&sob, sign_off_header);
-       strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
-                               getenv("GIT_COMMITTER_EMAIL")));
+       strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT));
        strbuf_addch(&sob, '\n');
 
        if (!ignore_footer)