Skip to content

Commit 69e1609

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a902edc commit 69e1609

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

builtin/apply.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,19 +4099,19 @@ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir
40994099
return 0;
41004100
}
41014101

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)
41074107
{
41084108
struct stat st;
41094109
struct cache_entry *ce;
41104110
int namelen = strlen(path);
41114111
unsigned ce_size = cache_entry_size(namelen);
41124112

41134113
if (!state->update_index)
4114-
return;
4114+
return 0;
41154115

41164116
ce = xcalloc(1, ce_size);
41174117
memcpy(ce->name, path, namelen);
@@ -4122,20 +4122,32 @@ static void add_index_file(struct apply_state *state,
41224122
const char *s;
41234123

41244124
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+
}
41274129
} else {
41284130
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+
}
41324137
fill_stat_cache_info(ce, &st);
41334138
}
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+
}
41364144
}
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;
41394151
}
41404152

41414153
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)
42714283
if (patch->conflicted_threeway) {
42724284
if (add_conflicted_stages_file(state, patch))
42734285
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+
}
42764290
}
42774291

42784292
/* phase zero is to remove, phase one is to create */

0 commit comments

Comments
 (0)