Skip to content

Commit fe41b80

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 119ab15 commit fe41b80

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

builtin/apply.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3900,11 +3900,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
39003900
}
39013901

39023902
/* 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)
39043904
{
39053905
struct patch *patch;
39063906
struct index_state result = { NULL };
39073907
static struct lock_file lock;
3908+
int res;
39083909

39093910
/* Once we start supporting the reverse patch, it may be
39103911
* worth showing the new sha1 prefix, but until then...
@@ -3922,31 +3923,38 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
39223923
if (!preimage_sha1_in_gitlink_patch(patch, sha1))
39233924
; /* ok, the textual part looks sane */
39243925
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);
39273928
} else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
39283929
; /* ok */
39293930
} else if (!patch->lines_added && !patch->lines_deleted) {
39303931
/* mode-only change: update the current */
39313932
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);
39343935
} else
3935-
die("sha1 information is lacking or useless "
3936-
"(%s).", name);
3936+
return error("sha1 information is lacking or useless "
3937+
"(%s).", name);
39373938

39383939
ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
39393940
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+
}
39433948
}
39443949

39453950
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);
39493952
discard_index(&result);
3953+
3954+
if (res)
3955+
return error("Could not write temporary index to %s", filename);
3956+
3957+
return 0;
39503958
}
39513959

39523960
static void stat_patch_list(struct apply_state *state, struct patch *patch)
@@ -4495,8 +4503,11 @@ static int apply_patch(struct apply_state *state,
44954503
goto end;
44964504
}
44974505

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+
}
45004511

45014512
if (state->diffstat)
45024513
stat_patch_list(state, list);

0 commit comments

Comments
 (0)