Skip to content

Commit 311fd39

Browse files
dschogitster
authored andcommitted
sequencer: lib'ify save_head()
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>
1 parent f6e82b0 commit 311fd39

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

sequencer.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -852,18 +852,28 @@ static int create_seq_dir(void)
852852
return 0;
853853
}
854854

855-
static void save_head(const char *head)
855+
static int save_head(const char *head)
856856
{
857857
static struct lock_file head_lock;
858858
struct strbuf buf = STRBUF_INIT;
859859
int fd;
860860

861-
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), LOCK_DIE_ON_ERROR);
861+
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
862+
if (fd < 0) {
863+
rollback_lock_file(&head_lock);
864+
return error_errno(_("Could not lock HEAD"));
865+
}
862866
strbuf_addf(&buf, "%s\n", head);
863-
if (write_in_full(fd, buf.buf, buf.len) < 0)
864-
die_errno(_("Could not write to %s"), git_path_head_file());
865-
if (commit_lock_file(&head_lock) < 0)
866-
die(_("Error wrapping up %s."), git_path_head_file());
867+
if (write_in_full(fd, buf.buf, buf.len) < 0) {
868+
rollback_lock_file(&head_lock);
869+
return error_errno(_("Could not write to %s"),
870+
git_path_head_file());
871+
}
872+
if (commit_lock_file(&head_lock) < 0) {
873+
rollback_lock_file(&head_lock);
874+
return error(_("Error wrapping up %s."), git_path_head_file());
875+
}
876+
return 0;
867877
}
868878

869879
static int reset_for_rollback(const unsigned char *sha1)
@@ -1127,7 +1137,8 @@ int sequencer_pick_revisions(struct replay_opts *opts)
11271137
return -1;
11281138
if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT))
11291139
return error(_("Can't revert as initial commit"));
1130-
save_head(sha1_to_hex(sha1));
1140+
if (save_head(sha1_to_hex(sha1)))
1141+
return -1;
11311142
save_opts(opts);
11321143
return pick_commits(todo_list, opts);
11331144
}

0 commit comments

Comments
 (0)