builtin/apply: make add_conflicted_stages_file() 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", add_conflicted_stages_file() 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 a902edceebd0a25a307163f050326bda8f494204
1 file changed +13 -7
builtin/apply.c
+13 -7
@@ -4224,7 +4224,7 @@ static void create_one_file(struct apply_state *state,
4224 die_errno(_("unable to write file '%s' mode %o"), path, mode);
4225 }
4226
4227 -static void add_conflicted_stages_file(struct apply_state *state,
4227 +static int add_conflicted_stages_file(struct apply_state *state,
4228 struct patch *patch)
4229 {
4230 int stage, namelen;
@@ -4232,7 +4232,7 @@ static void add_conflicted_stages_file(struct apply_state *state,
4232 struct cache_entry *ce;
4233
4234 if (!state->update_index)
4235 - return;
4235 + return 0;
4236 namelen = strlen(patch->new_name);
4237 ce_size = cache_entry_size(namelen);
4238 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
@@ -4247,9 +4247,14 @@ static void add_conflicted_stages_file(struct apply_state *state,
4247 ce->ce_flags = create_ce_flags(stage);
4248 ce->ce_namelen = namelen;
4249 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4250 - if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4251 - die(_("unable to add cache entry for %s"), patch->new_name);
4250 + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4251 + free(ce);
4252 + return error(_("unable to add cache entry for %s"),
4253 + patch->new_name);
4254 + }
4255 }
4256 +
4257 + return 0;
4258 }
4259
4260 static void create_file(struct apply_state *state, struct patch *patch)
@@ -4263,9 +4268,10 @@ static void create_file(struct apply_state *state, struct patch *patch)
4268 mode = S_IFREG | 0644;
4269 create_one_file(state, path, mode, buf, size);
4270
4266 - if (patch->conflicted_threeway)
4267 - add_conflicted_stages_file(state, patch);
4268 - else
4271 + if (patch->conflicted_threeway) {
4272 + if (add_conflicted_stages_file(state, patch))
4273 + exit(128);
4274 + } else
4275 add_index_file(state, path, mode, buf, size);
4276 }
4277