]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sequencer: lib'ify save_head()
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 9 Sep 2016 14:37:47 +0000 (16:37 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 9 Sep 2016 18:24:52 +0000 (11:24 -0700)
Instead of dying there, let the caller high up in the callchain notice
the error and handle it (by dying, still).

The only caller of save_head(), sequencer_pick_revisions() can already
return errors, so its caller must be already prepared to handle error
returns, and with this step, we make it notice an error return from
this function.

So this is a safe conversion to make save_head() callable from new
callers that want it not to die, without changing the external
behaviour of anything existing.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sequencer.c

index eb9c47332890ff2d34183561f2d9c9f3c4f80f96..7a1561e6a0664bb75d6d1eafc964330db8d3162f 100644 (file)
@@ -852,18 +852,28 @@ static int create_seq_dir(void)
        return 0;
 }
 
-static void save_head(const char *head)
+static int save_head(const char *head)
 {
        static struct lock_file head_lock;
        struct strbuf buf = STRBUF_INIT;
        int fd;
 
-       fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), LOCK_DIE_ON_ERROR);
+       fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
+       if (fd < 0) {
+               rollback_lock_file(&head_lock);
+               return error_errno(_("Could not lock HEAD"));
+       }
        strbuf_addf(&buf, "%s\n", head);
-       if (write_in_full(fd, buf.buf, buf.len) < 0)
-               die_errno(_("Could not write to %s"), git_path_head_file());
-       if (commit_lock_file(&head_lock) < 0)
-               die(_("Error wrapping up %s."), git_path_head_file());
+       if (write_in_full(fd, buf.buf, buf.len) < 0) {
+               rollback_lock_file(&head_lock);
+               return error_errno(_("Could not write to %s"),
+                                  git_path_head_file());
+       }
+       if (commit_lock_file(&head_lock) < 0) {
+               rollback_lock_file(&head_lock);
+               return error(_("Error wrapping up %s."), git_path_head_file());
+       }
+       return 0;
 }
 
 static int reset_for_rollback(const unsigned char *sha1)
@@ -1127,7 +1137,8 @@ int sequencer_pick_revisions(struct replay_opts *opts)
                return -1;
        if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT))
                return error(_("Can't revert as initial commit"));
-       save_head(sha1_to_hex(sha1));
+       if (save_head(sha1_to_hex(sha1)))
+               return -1;
        save_opts(opts);
        return pick_commits(todo_list, opts);
 }