1
0
mirror of https://github.com/git/git.git synced 2024-11-18 02:04:08 +01:00

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>
This commit is contained in:
Christian Couder 2016-08-08 23:03:16 +02:00 committed by Junio C Hamano
parent 119ab159e6
commit fe41b80225

@ -3900,11 +3900,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
} }
/* Build an index that contains the just the files needed for a 3way merge */ /* Build an index that contains the just the files needed for a 3way merge */
static void build_fake_ancestor(struct patch *list, const char *filename) static int build_fake_ancestor(struct patch *list, const char *filename)
{ {
struct patch *patch; struct patch *patch;
struct index_state result = { NULL }; struct index_state result = { NULL };
static struct lock_file lock; static struct lock_file lock;
int res;
/* Once we start supporting the reverse patch, it may be /* Once we start supporting the reverse patch, it may be
* worth showing the new sha1 prefix, but until then... * worth showing the new sha1 prefix, but until then...
@ -3922,31 +3923,38 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
if (!preimage_sha1_in_gitlink_patch(patch, sha1)) if (!preimage_sha1_in_gitlink_patch(patch, sha1))
; /* ok, the textual part looks sane */ ; /* ok, the textual part looks sane */
else else
die("sha1 information is lacking or useless for submodule %s", return error("sha1 information is lacking or "
name); "useless for submodule %s", name);
} else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) { } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
; /* ok */ ; /* ok */
} else if (!patch->lines_added && !patch->lines_deleted) { } else if (!patch->lines_added && !patch->lines_deleted) {
/* mode-only change: update the current */ /* mode-only change: update the current */
if (get_current_sha1(patch->old_name, sha1)) if (get_current_sha1(patch->old_name, sha1))
die("mode change for %s, which is not " return error("mode change for %s, which is not "
"in current HEAD", name); "in current HEAD", name);
} else } else
die("sha1 information is lacking or useless " return error("sha1 information is lacking or useless "
"(%s).", name); "(%s).", name);
ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0); ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
if (!ce) if (!ce)
die(_("make_cache_entry failed for path '%s'"), name); return error(_("make_cache_entry failed for path '%s'"),
if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) name);
die ("Could not add %s to temporary index", name); if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
free(ce);
return error("Could not add %s to temporary index",
name);
}
} }
hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR); hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
if (write_locked_index(&result, &lock, COMMIT_LOCK)) res = write_locked_index(&result, &lock, COMMIT_LOCK);
die ("Could not write temporary index to %s", filename);
discard_index(&result); discard_index(&result);
if (res)
return error("Could not write temporary index to %s", filename);
return 0;
} }
static void stat_patch_list(struct apply_state *state, struct patch *patch) static void stat_patch_list(struct apply_state *state, struct patch *patch)
@ -4495,8 +4503,11 @@ static int apply_patch(struct apply_state *state,
goto end; goto end;
} }
if (state->fake_ancestor) if (state->fake_ancestor &&
build_fake_ancestor(list, state->fake_ancestor); build_fake_ancestor(list, state->fake_ancestor)) {
res = -128;
goto end;
}
if (state->diffstat) if (state->diffstat)
stat_patch_list(state, list); stat_patch_list(state, list);