builtin/apply: make find_header() return -128 instead of die()ing
authorChristian Couder <christian.couder@gmail.com>
Mon, 8 Aug 2016 21:03:02 +0000 (23:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Aug 2016 19:41:46 +0000 (12:41 -0700)
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, let's make find_header() return -128 instead of
calling die().

We could make it return -1, unfortunately find_header() already
returns -1 when no header is found.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/apply.c
t/t4254-am-corrupt.sh
index dd7afee09f61e8e33376a10b804e3626d442804c..434ba0c5429d53a196dbf9b8dd707181c6f13bac 100644 (file)
@@ -1419,6 +1419,14 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
        return offset;
 }
 
+/*
+ * Find file diff header
+ *
+ * Returns:
+ *  -1 if no header was found
+ *  -128 in case of error
+ *   the size of the header in bytes (called "offset") otherwise
+ */
 static int find_header(struct apply_state *state,
                       const char *line,
                       unsigned long size,
@@ -1452,8 +1460,9 @@ static int find_header(struct apply_state *state,
                        struct fragment dummy;
                        if (parse_fragment_header(line, len, &dummy) < 0)
                                continue;
-                       die(_("patch fragment without header at line %d: %.*s"),
-                           state->linenr, (int)len-1, line);
+                       error(_("patch fragment without header at line %d: %.*s"),
+                                    state->linenr, (int)len-1, line);
+                       return -128;
                }
 
                if (size < len + 6)
@@ -1468,19 +1477,23 @@ static int find_header(struct apply_state *state,
                        if (git_hdr_len <= len)
                                continue;
                        if (!patch->old_name && !patch->new_name) {
-                               if (!patch->def_name)
-                                       die(Q_("git diff header lacks filename information when removing "
-                                              "%d leading pathname component (line %d)",
-                                              "git diff header lacks filename information when removing "
-                                              "%d leading pathname components (line %d)",
-                                              state->p_value),
-                                           state->p_value, state->linenr);
+                               if (!patch->def_name) {
+                                       error(Q_("git diff header lacks filename information when removing "
+                                                       "%d leading pathname component (line %d)",
+                                                       "git diff header lacks filename information when removing "
+                                                       "%d leading pathname components (line %d)",
+                                                       state->p_value),
+                                                    state->p_value, state->linenr);
+                                       return -128;
+                               }
                                patch->old_name = xstrdup(patch->def_name);
                                patch->new_name = xstrdup(patch->def_name);
                        }
-                       if (!patch->is_delete && !patch->new_name)
-                               die("git diff header lacks filename information "
-                                   "(line %d)", state->linenr);
+                       if (!patch->is_delete && !patch->new_name) {
+                               error("git diff header lacks filename information "
+                                            "(line %d)", state->linenr);
+                               return -128;
+                       }
                        patch->is_toplevel_relative = 1;
                        *hdrsize = git_hdr_len;
                        return offset;
@@ -1996,6 +2009,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
        int hdrsize, patchsize;
        int offset = find_header(state, buffer, size, &hdrsize, patch);
 
+       if (offset == -128)
+               exit(128);
+
        if (offset < 0)
                return offset;
 
index 85716dd6ec566f34f7c4e0b30477bb1531c6030e..9bd7dd2ba15cfd1096d3e89b012f5979b1a01eee 100755 (executable)
@@ -29,7 +29,7 @@ test_expect_success 'try to apply corrupted patch' '
 '
 
 test_expect_success 'compare diagnostic; ensure file is still here' '
-       echo "fatal: git diff header lacks filename information (line 4)" >expected &&
+       echo "error: git diff header lacks filename information (line 4)" >expected &&
        test_path_is_file f &&
        test_cmp expected actual
 '