builtin/apply: make build_fake_ancestor() return -1 on error

To libify `git apply` functionality we have to signal errors to the caller instead of die()ing. To do that in a compatible manner with the rest of the error handling in "builtin/apply.c", build_fake_ancestor() should return -1 instead of calling die(). Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Aug 8, 2016 at 23:03 UTC fe41b8022560e24c1617cc8b3bd11b72bd1ff4bd
1 file changed +26 -15
builtin/apply.c
+26 -15
@@ -3900,11 +3900,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
3900 }
3901
3902 /* Build an index that contains the just the files needed for a 3way merge */
3903 -static void build_fake_ancestor(struct patch *list, const char *filename)
3903 +static int build_fake_ancestor(struct patch *list, const char *filename)
3904 {
3905 struct patch *patch;
3906 struct index_state result = { NULL };
3907 static struct lock_file lock;
3908 + int res;
3909
3910 /* Once we start supporting the reverse patch, it may be
3911 * worth showing the new sha1 prefix, but until then...
@@ -3922,31 +3923,38 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
3923 if (!preimage_sha1_in_gitlink_patch(patch, sha1))
3924 ; /* ok, the textual part looks sane */
3925 else
3925 - die("sha1 information is lacking or useless for submodule %s",
3926 - name);
3926 + return error("sha1 information is lacking or "
3927 + "useless for submodule %s", name);
3928 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
3929 ; /* ok */
3930 } else if (!patch->lines_added && !patch->lines_deleted) {
3931 /* mode-only change: update the current */
3932 if (get_current_sha1(patch->old_name, sha1))
3932 - die("mode change for %s, which is not "
3933 - "in current HEAD", name);
3933 + return error("mode change for %s, which is not "
3934 + "in current HEAD", name);
3935 } else
3935 - die("sha1 information is lacking or useless "
3936 - "(%s).", name);
3936 + return error("sha1 information is lacking or useless "
3937 + "(%s).", name);
3938
3939 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
3940 if (!ce)
3940 - die(_("make_cache_entry failed for path '%s'"), name);
3941 - if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
3942 - die ("Could not add %s to temporary index", name);
3941 + return error(_("make_cache_entry failed for path '%s'"),
3942 + name);
3943 + if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
3944 + free(ce);
3945 + return error("Could not add %s to temporary index",
3946 + name);
3947 + }
3948 }
3949
3950 hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
3946 - if (write_locked_index(&result, &lock, COMMIT_LOCK))
3947 - die ("Could not write temporary index to %s", filename);
3948 -
3951 + res = write_locked_index(&result, &lock, COMMIT_LOCK);
3952 discard_index(&result);
3953 +
3954 + if (res)
3955 + return error("Could not write temporary index to %s", filename);
3956 +
3957 + return 0;
3958 }
3959
3960 static void stat_patch_list(struct apply_state *state, struct patch *patch)
@@ -4495,8 +4503,11 @@ static int apply_patch(struct apply_state *state,
4503 goto end;
4504 }
4505
4498 - if (state->fake_ancestor)
4499 - build_fake_ancestor(list, state->fake_ancestor);
4506 + if (state->fake_ancestor &&
4507 + build_fake_ancestor(list, state->fake_ancestor)) {
4508 + res = -128;
4509 + goto end;
4510 + }
4511
4512 if (state->diffstat)
4513 stat_patch_list(state, list);