apply: make init_apply_state() return -1 instead of exit()ing
[gitweb.git] / builtin / apply.c
index dd7afee09f61e8e33376a10b804e3626d442804c..61fd3163638f892f238c9e7f8140cf1f207db3bd 100644 (file)
@@ -27,52 +27,6 @@ static const char * const apply_usage[] = {
        NULL
 };
 
-static void parse_whitespace_option(struct apply_state *state, const char *option)
-{
-       if (!option) {
-               state->ws_error_action = warn_on_ws_error;
-               return;
-       }
-       if (!strcmp(option, "warn")) {
-               state->ws_error_action = warn_on_ws_error;
-               return;
-       }
-       if (!strcmp(option, "nowarn")) {
-               state->ws_error_action = nowarn_ws_error;
-               return;
-       }
-       if (!strcmp(option, "error")) {
-               state->ws_error_action = die_on_ws_error;
-               return;
-       }
-       if (!strcmp(option, "error-all")) {
-               state->ws_error_action = die_on_ws_error;
-               state->squelch_whitespace_errors = 0;
-               return;
-       }
-       if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
-               state->ws_error_action = correct_ws_error;
-               return;
-       }
-       die(_("unrecognized whitespace option '%s'"), option);
-}
-
-static void parse_ignorewhitespace_option(struct apply_state *state,
-                                         const char *option)
-{
-       if (!option || !strcmp(option, "no") ||
-           !strcmp(option, "false") || !strcmp(option, "never") ||
-           !strcmp(option, "none")) {
-               state->ws_ignore_action = ignore_ws_none;
-               return;
-       }
-       if (!strcmp(option, "change")) {
-               state->ws_ignore_action = ignore_ws_change;
-               return;
-       }
-       die(_("unrecognized whitespace ignore option '%s'"), option);
-}
-
 static void set_default_whitespace_mode(struct apply_state *state)
 {
        if (!state->whitespace_option && !apply_default_whitespace)
@@ -1419,6 +1373,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 +1414,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 +1431,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;
@@ -1658,6 +1625,10 @@ static int parse_fragment(struct apply_state *state,
  *
  * The (fragment->patch, fragment->size) pair points into the memory given
  * by the caller, not a copy, when we return.
+ *
+ * Returns:
+ *   -1 in case of error,
+ *   the number of bytes in the patch otherwise.
  */
 static int parse_single_patch(struct apply_state *state,
                              const char *line,
@@ -1675,8 +1646,10 @@ static int parse_single_patch(struct apply_state *state,
                fragment = xcalloc(1, sizeof(*fragment));
                fragment->linenr = state->linenr;
                len = parse_fragment(state, line, size, patch, fragment);
-               if (len <= 0)
-                       die(_("corrupt patch at line %d"), state->linenr);
+               if (len <= 0) {
+                       free(fragment);
+                       return error(_("corrupt patch at line %d"), state->linenr);
+               }
                fragment->patch = line;
                fragment->size = len;
                oldlines += fragment->oldlines;
@@ -1712,9 +1685,9 @@ static int parse_single_patch(struct apply_state *state,
                patch->is_delete = 0;
 
        if (0 < patch->is_new && oldlines)
-               die(_("new file %s depends on old contents"), patch->new_name);
+               return error(_("new file %s depends on old contents"), patch->new_name);
        if (0 < patch->is_delete && newlines)
-               die(_("deleted file %s still has contents"), patch->old_name);
+               return error(_("deleted file %s still has contents"), patch->old_name);
        if (!patch->is_delete && !newlines && context)
                fprintf_ln(stderr,
                           _("** warning: "
@@ -1983,13 +1956,16 @@ static int use_patch(struct apply_state *state, struct patch *p)
        return !state->has_include;
 }
 
-
 /*
  * Read the patch text in "buffer" that extends for "size" bytes; stop
  * reading after seeing a single patch (i.e. changes to a single file).
  * Create fragments (i.e. patch hunks) and hang them to the given patch.
- * Return the number of bytes consumed, so that the caller can call us
- * again for the next patch.
+ *
+ * Returns:
+ *   -1 if no header was found or parse_binary() failed,
+ *   -128 on another error,
+ *   the number of bytes consumed otherwise,
+ *     so that the caller can call us again for the next patch.
  */
 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
 {
@@ -2013,6 +1989,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
                                       size - offset - hdrsize,
                                       patch);
 
+       if (patchsize < 0)
+               return -128;
+
        if (!patchsize) {
                static const char git_binary[] = "GIT binary patch\n";
                int hd = hdrsize + offset;
@@ -2055,8 +2034,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
                 * empty to us here.
                 */
                if ((state->apply || state->check) &&
-                   (!patch->is_binary && !metadata_changes(patch)))
-                       die(_("patch with only garbage at line %d"), state->linenr);
+                   (!patch->is_binary && !metadata_changes(patch))) {
+                       error(_("patch with only garbage at line %d"), state->linenr);
+                       return -128;
+               }
        }
 
        return offset + hdrsize + patchsize;
@@ -4439,6 +4420,10 @@ static int apply_patch(struct apply_state *state,
                nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
                if (nr < 0) {
                        free_patch(patch);
+                       if (nr == -128) {
+                               res = -128;
+                               goto end;
+                       }
                        break;
                }
                if (state->apply_in_reverse)
@@ -4508,13 +4493,6 @@ static int apply_patch(struct apply_state *state,
        return res;
 }
 
-static void git_apply_config(void)
-{
-       git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
-       git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
-       git_config(git_default_config, NULL);
-}
-
 static int option_parse_exclude(const struct option *opt,
                                const char *arg, int unset)
 {
@@ -4558,7 +4536,8 @@ static int option_parse_whitespace(const struct option *opt,
 {
        struct apply_state *state = opt->value;
        state->whitespace_option = arg;
-       parse_whitespace_option(state, arg);
+       if (parse_whitespace_option(state, arg))
+               exit(1);
        return 0;
 }
 
@@ -4572,44 +4551,6 @@ static int option_parse_directory(const struct option *opt,
        return 0;
 }
 
-static void init_apply_state(struct apply_state *state,
-                            const char *prefix,
-                            struct lock_file *lock_file)
-{
-       memset(state, 0, sizeof(*state));
-       state->prefix = prefix;
-       state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
-       state->lock_file = lock_file;
-       state->newfd = -1;
-       state->apply = 1;
-       state->line_termination = '\n';
-       state->p_value = 1;
-       state->p_context = UINT_MAX;
-       state->squelch_whitespace_errors = 5;
-       state->ws_error_action = warn_on_ws_error;
-       state->ws_ignore_action = ignore_ws_none;
-       state->linenr = 1;
-       string_list_init(&state->fn_table, 0);
-       string_list_init(&state->limit_by_name, 0);
-       string_list_init(&state->symlink_changes, 0);
-       strbuf_init(&state->root, 0);
-
-       git_apply_config();
-       if (apply_default_whitespace)
-               parse_whitespace_option(state, apply_default_whitespace);
-       if (apply_default_ignorewhitespace)
-               parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
-}
-
-static void clear_apply_state(struct apply_state *state)
-{
-       string_list_clear(&state->limit_by_name, 0);
-       string_list_clear(&state->symlink_changes, 0);
-       strbuf_release(&state->root);
-
-       /* &state->fn_table is cleared at the end of apply_patch() */
-}
-
 static void check_apply_state(struct apply_state *state, int force_apply)
 {
        int is_not_gitdir = !startup_info->have_repository;
@@ -4800,7 +4741,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
-       init_apply_state(&state, prefix, &lock_file);
+       if (init_apply_state(&state, prefix, &lock_file))
+               exit(128);
 
        argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
                        apply_usage, 0);