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 <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 0997384..005ba78 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4088,11 +4088,11 @@ static int remove_file(struct apply_state *state, 
struct patch *patch, int rmdir
        return 0;
 }
 
-static void add_index_file(struct apply_state *state,
-                          const char *path,
-                          unsigned mode,
-                          void *buf,
-                          unsigned long size)
+static int add_index_file(struct apply_state *state,
+                         const char *path,
+                         unsigned mode,
+                         void *buf,
+                         unsigned long size)
 {
        struct stat st;
        struct cache_entry *ce;
@@ -4100,7 +4100,7 @@ static void add_index_file(struct apply_state *state,
        unsigned ce_size = cache_entry_size(namelen);
 
        if (!state->update_index)
-               return;
+               return 0;
 
        ce = xcalloc(1, ce_size);
        memcpy(ce->name, path, namelen);
@@ -4111,20 +4111,32 @@ static void add_index_file(struct apply_state *state,
                const char *s;
 
                if (!skip_prefix(buf, "Subproject commit ", &s) ||
-                   get_sha1_hex(s, ce->sha1))
-                       die(_("corrupt patch for submodule %s"), path);
+                   get_sha1_hex(s, ce->sha1)) {
+                       free(ce);
+                       return error(_("corrupt patch for submodule %s"), path);
+               }
        } else {
                if (!state->cached) {
-                       if (lstat(path, &st) < 0)
-                               die_errno(_("unable to stat newly created file 
'%s'"),
-                                         path);
+                       if (lstat(path, &st) < 0) {
+                               free(ce);
+                               return error(_("unable to stat newly "
+                                              "created file '%s': %s"),
+                                            path, strerror(errno));
+                       }
                        fill_stat_cache_info(ce, &st);
                }
-               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-                       die(_("unable to create backing store for newly created 
file %s"), path);
+               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
+                       free(ce);
+                       return error(_("unable to create backing store "
+                                      "for newly created file %s"), path);
+               }
        }
-       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-               die(_("unable to add cache entry for %s"), path);
+       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+               free(ce);
+               return error(_("unable to add cache entry for %s"), path);
+       }
+
+       return 0;
 }
 
 static int try_create_file(const char *path, unsigned int mode, const char 
*buf, unsigned long size)
@@ -4260,8 +4272,10 @@ static void create_file(struct apply_state *state, 
struct patch *patch)
        if (patch->conflicted_threeway) {
                if (add_conflicted_stages_file(state, patch))
                        exit(1);
-       } else
-               add_index_file(state, path, mode, buf, size);
+       } else {
+               if (add_index_file(state, path, mode, buf, size))
+                       exit(1);
+       }
 }
 
 /* phase zero is to remove, phase one is to create */
-- 
2.9.0.rc2.362.g3cd93d0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to