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>

Johannes Schindelin committed Sep 9, 2016 at 16:37 UTC 311fd397f0e750333e0aaa8e45b9284a5227e353
1 file changed +18 -7
sequencer.c
+18 -7
@@ -852,18 +852,28 @@ static int create_seq_dir(void)
852 return 0;
853 }
854
855 -static void save_head(const char *head)
855 +static int save_head(const char *head)
856 {
857 static struct lock_file head_lock;
858 struct strbuf buf = STRBUF_INIT;
859 int fd;
860
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 + }
866 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;
877 }
878
879 static int reset_for_rollback(const unsigned char *sha1)
@@ -1127,7 +1137,8 @@ int sequencer_pick_revisions(struct replay_opts *opts)
1137 return -1;
1138 if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT))
1139 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;
1142 save_opts(opts);
1143 return pick_commits(todo_list, opts);
1144 }