]> git.ipfire.org Git - thirdparty/git.git/blobdiff - sequencer.c
sequencer: lib'ify read_and_refresh_cache()
[thirdparty/git.git] / sequencer.c
index 2e9c7d0f03734c20f1fb9d5c6214a7960d80b112..631b75dabe7cf11a0e4823a5b8f7a018ecb8e30a 100644 (file)
@@ -180,17 +180,20 @@ static void print_advice(int show_hint, struct replay_opts *opts)
        }
 }
 
-static void write_message(struct strbuf *msgbuf, const char *filename)
+static int write_message(struct strbuf *msgbuf, const char *filename)
 {
        static struct lock_file msg_file;
 
-       int msg_fd = hold_lock_file_for_update(&msg_file, filename,
-                                              LOCK_DIE_ON_ERROR);
+       int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
+       if (msg_fd < 0)
+               return error_errno(_("Could not lock '%s'"), filename);
        if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
-               die_errno(_("Could not write to %s"), filename);
+               return error_errno(_("Could not write to %s"), filename);
        strbuf_release(msgbuf);
        if (commit_lock_file(&msg_file) < 0)
-               die(_("Error wrapping up %s."), filename);
+               return error(_("Error wrapping up %s."), filename);
+
+       return 0;
 }
 
 static struct tree *empty_tree(void)
@@ -300,7 +303,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
        if (active_cache_changed &&
            write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
                /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
-               die(_("%s: Unable to write new index file"), action_name(opts));
+               return error(_("%s: Unable to write new index file"),
+                       action_name(opts));
        rollback_lock_file(&index_lock);
 
        if (opts->signoff)
@@ -460,7 +464,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                 * to work on.
                 */
                if (write_cache_as_tree(head, 0, NULL))
-                       die (_("Your index file is unmerged."));
+                       return error(_("Your index file is unmerged."));
        } else {
                unborn = get_sha1("HEAD", head);
                if (unborn)
@@ -564,16 +568,16 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                                         head, &msgbuf, opts);
                if (res < 0)
                        return res;
-               write_message(&msgbuf, git_path_merge_msg());
+               res |= write_message(&msgbuf, git_path_merge_msg());
        } else {
                struct commit_list *common = NULL;
                struct commit_list *remotes = NULL;
 
-               write_message(&msgbuf, git_path_merge_msg());
+               res = write_message(&msgbuf, git_path_merge_msg());
 
                commit_list_insert(base, &common);
                commit_list_insert(next, &remotes);
-               res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
+               res |= try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
                                        common, sha1_to_hex(head), remotes);
                free_commit_list(common);
                free_commit_list(remotes);
@@ -585,12 +589,14 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
         * However, if the merge did not even start, then we don't want to
         * write it at all.
         */
-       if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1))
-               update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
-                          REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
-       if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1))
-               update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
-                          REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
+       if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1) &&
+           update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
+                      REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
+               res = -1;
+       if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
+           update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
+                      REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
+               res = -1;
 
        if (res) {
                error(opts->action == REPLAY_REVERT
@@ -617,7 +623,7 @@ leave:
        return res;
 }
 
-static void prepare_revs(struct replay_opts *opts)
+static int prepare_revs(struct replay_opts *opts)
 {
        /*
         * picking (but not reverting) ranges (but not individual revisions)
@@ -627,24 +633,28 @@ static void prepare_revs(struct replay_opts *opts)
                opts->revs->reverse ^= 1;
 
        if (prepare_revision_walk(opts->revs))
-               die(_("revision walk setup failed"));
+               return error(_("revision walk setup failed"));
 
        if (!opts->revs->commits)
-               die(_("empty commit set passed"));
+               return error(_("empty commit set passed"));
+       return 0;
 }
 
-static void read_and_refresh_cache(struct replay_opts *opts)
+static int read_and_refresh_cache(struct replay_opts *opts)
 {
        static struct lock_file index_lock;
        int index_fd = hold_locked_index(&index_lock, 0);
        if (read_index_preload(&the_index, NULL) < 0)
-               die(_("git %s: failed to read the index"), action_name(opts));
+               return error(_("git %s: failed to read the index"),
+                       action_name(opts));
        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
        if (the_index.cache_changed && index_fd >= 0) {
                if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
-                       die(_("git %s: failed to refresh the index"), action_name(opts));
+                       return error(_("git %s: failed to refresh the index"),
+                               action_name(opts));
        }
        rollback_lock_file(&index_lock);
+       return 0;
 }
 
 static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
@@ -702,7 +712,7 @@ static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *
        if (action != opts->action) {
                if (action == REPLAY_REVERT)
                      error((opts->action == REPLAY_REVERT)
-                           ? _("Cannot revert during a another revert.")
+                           ? _("Cannot revert during another revert.")
                            : _("Cannot revert during a cherry-pick."));
                else
                      error((opts->action == REPLAY_REVERT)
@@ -803,17 +813,19 @@ static void read_populate_opts(struct replay_opts **opts_ptr)
                die(_("Malformed options sheet: %s"), git_path_opts_file());
 }
 
-static void walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct commit_list **todo_list,
                                struct replay_opts *opts)
 {
        struct commit *commit;
        struct commit_list **next;
 
-       prepare_revs(opts);
+       if (prepare_revs(opts))
+               return -1;
 
        next = todo_list;
        while ((commit = get_revision(opts->revs)))
                next = commit_list_append(commit, next);
+       return 0;
 }
 
 static int create_seq_dir(void)
@@ -972,7 +984,8 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
        if (opts->allow_ff)
                assert(!(opts->signoff || opts->no_commit ||
                                opts->record_origin || opts->edit));
-       read_and_refresh_cache(opts);
+       if (read_and_refresh_cache(opts))
+               return -1;
 
        for (cur = todo_list; cur; cur = cur->next) {
                save_todo(cur, opts);
@@ -1036,7 +1049,8 @@ int sequencer_pick_revisions(struct replay_opts *opts)
        if (opts->subcommand == REPLAY_NONE)
                assert(opts->revs);
 
-       read_and_refresh_cache(opts);
+       if (read_and_refresh_cache(opts))
+               return -1;
 
        /*
         * Decide what to do depending on the arguments; a fresh
@@ -1063,10 +1077,11 @@ int sequencer_pick_revisions(struct replay_opts *opts)
                if (!get_sha1(name, sha1)) {
                        if (!lookup_commit_reference_gently(sha1, 1)) {
                                enum object_type type = sha1_object_info(sha1, NULL);
-                               die(_("%s: can't cherry-pick a %s"), name, typename(type));
+                               return error(_("%s: can't cherry-pick a %s"),
+                                       name, typename(type));
                        }
                } else
-                       die(_("%s: bad revision"), name);
+                       return error(_("%s: bad revision"), name);
        }
 
        /*
@@ -1082,10 +1097,10 @@ int sequencer_pick_revisions(struct replay_opts *opts)
            !opts->revs->cmdline.rev->flags) {
                struct commit *cmit;
                if (prepare_revision_walk(opts->revs))
-                       die(_("revision walk setup failed"));
+                       return error(_("revision walk setup failed"));
                cmit = get_revision(opts->revs);
                if (!cmit || get_revision(opts->revs))
-                       die("BUG: expected exactly one commit from walk");
+                       return error("BUG: expected exactly one commit from walk");
                return single_pick(cmit, opts);
        }
 
@@ -1095,8 +1110,8 @@ int sequencer_pick_revisions(struct replay_opts *opts)
         * progress
         */
 
-       walk_revs_populate_todo(&todo_list, opts);
-       if (create_seq_dir() < 0)
+       if (walk_revs_populate_todo(&todo_list, opts) ||
+                       create_seq_dir() < 0)
                return -1;
        if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT))
                return error(_("Can't revert as initial commit"));