builtin/apply: make add_index_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_index_file() should return -1 instead of calling die(). 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 69e1609f812d5accc1b54f737faba354f5bd85db
1 file changed +31 -17
builtin/apply.c
+31 -17
@@ -4099,11 +4099,11 @@ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir
4099 return 0;
4100 }
4101
4102 -static void add_index_file(struct apply_state *state,
4103 - const char *path,
4104 - unsigned mode,
4105 - void *buf,
4106 - unsigned long size)
4102 +static int add_index_file(struct apply_state *state,
4103 + const char *path,
4104 + unsigned mode,
4105 + void *buf,
4106 + unsigned long size)
4107 {
4108 struct stat st;
4109 struct cache_entry *ce;
@@ -4111,7 +4111,7 @@ static void add_index_file(struct apply_state *state,
4111 unsigned ce_size = cache_entry_size(namelen);
4112
4113 if (!state->update_index)
4114 - return;
4114 + return 0;
4115
4116 ce = xcalloc(1, ce_size);
4117 memcpy(ce->name, path, namelen);
@@ -4122,20 +4122,32 @@ static void add_index_file(struct apply_state *state,
4122 const char *s;
4123
4124 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4125 - get_sha1_hex(s, ce->sha1))
4126 - die(_("corrupt patch for submodule %s"), path);
4125 + get_sha1_hex(s, ce->sha1)) {
4126 + free(ce);
4127 + return error(_("corrupt patch for submodule %s"), path);
4128 + }
4129 } else {
4130 if (!state->cached) {
4129 - if (lstat(path, &st) < 0)
4130 - die_errno(_("unable to stat newly created file '%s'"),
4131 - path);
4131 + if (lstat(path, &st) < 0) {
4132 + free(ce);
4133 + return error(_("unable to stat newly "
4134 + "created file '%s': %s"),
4135 + path, strerror(errno));
4136 + }
4137 fill_stat_cache_info(ce, &st);
4138 }
4134 - if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
4135 - die(_("unable to create backing store for newly created file %s"), path);
4139 + if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
4140 + free(ce);
4141 + return error(_("unable to create backing store "
4142 + "for newly created file %s"), path);
4143 + }
4144 }
4137 - if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4138 - die(_("unable to add cache entry for %s"), path);
4145 + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4146 + free(ce);
4147 + return error(_("unable to add cache entry for %s"), path);
4148 + }
4149 +
4150 + return 0;
4151 }
4152
4153 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
@@ -4271,8 +4283,10 @@ static void create_file(struct apply_state *state, struct patch *patch)
4283 if (patch->conflicted_threeway) {
4284 if (add_conflicted_stages_file(state, patch))
4285 exit(128);
4274 - } else
4275 - add_index_file(state, path, mode, buf, size);
4286 + } else {
4287 + if (add_index_file(state, path, mode, buf, size))
4288 + exit(128);
4289 + }
4290 }
4291
4292 /* phase zero is to remove, phase one is to create */