builtin/apply: make build_fake_ancestor() return -1 on error
[git] / apply.c
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "apply.h"
4
5 static void git_apply_config(void)
6 {
7         git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
8         git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
9         git_config(git_default_config, NULL);
10 }
11
12 int parse_whitespace_option(struct apply_state *state, const char *option)
13 {
14         if (!option) {
15                 state->ws_error_action = warn_on_ws_error;
16                 return 0;
17         }
18         if (!strcmp(option, "warn")) {
19                 state->ws_error_action = warn_on_ws_error;
20                 return 0;
21         }
22         if (!strcmp(option, "nowarn")) {
23                 state->ws_error_action = nowarn_ws_error;
24                 return 0;
25         }
26         if (!strcmp(option, "error")) {
27                 state->ws_error_action = die_on_ws_error;
28                 return 0;
29         }
30         if (!strcmp(option, "error-all")) {
31                 state->ws_error_action = die_on_ws_error;
32                 state->squelch_whitespace_errors = 0;
33                 return 0;
34         }
35         if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
36                 state->ws_error_action = correct_ws_error;
37                 return 0;
38         }
39         return error(_("unrecognized whitespace option '%s'"), option);
40 }
41
42 int parse_ignorewhitespace_option(struct apply_state *state,
43                                   const char *option)
44 {
45         if (!option || !strcmp(option, "no") ||
46             !strcmp(option, "false") || !strcmp(option, "never") ||
47             !strcmp(option, "none")) {
48                 state->ws_ignore_action = ignore_ws_none;
49                 return 0;
50         }
51         if (!strcmp(option, "change")) {
52                 state->ws_ignore_action = ignore_ws_change;
53                 return 0;
54         }
55         return error(_("unrecognized whitespace ignore option '%s'"), option);
56 }
57
58 int init_apply_state(struct apply_state *state,
59                      const char *prefix,
60                      struct lock_file *lock_file)
61 {
62         memset(state, 0, sizeof(*state));
63         state->prefix = prefix;
64         state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
65         state->lock_file = lock_file;
66         state->newfd = -1;
67         state->apply = 1;
68         state->line_termination = '\n';
69         state->p_value = 1;
70         state->p_context = UINT_MAX;
71         state->squelch_whitespace_errors = 5;
72         state->ws_error_action = warn_on_ws_error;
73         state->ws_ignore_action = ignore_ws_none;
74         state->linenr = 1;
75         string_list_init(&state->fn_table, 0);
76         string_list_init(&state->limit_by_name, 0);
77         string_list_init(&state->symlink_changes, 0);
78         strbuf_init(&state->root, 0);
79
80         git_apply_config();
81         if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
82                 return -1;
83         if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
84                 return -1;
85         return 0;
86 }
87
88 void clear_apply_state(struct apply_state *state)
89 {
90         string_list_clear(&state->limit_by_name, 0);
91         string_list_clear(&state->symlink_changes, 0);
92         strbuf_release(&state->root);
93
94         /* &state->fn_table is cleared at the end of apply_patch() */
95 }
96
97 int check_apply_state(struct apply_state *state, int force_apply)
98 {
99         int is_not_gitdir = !startup_info->have_repository;
100
101         if (state->apply_with_reject && state->threeway)
102                 return error("--reject and --3way cannot be used together.");
103         if (state->cached && state->threeway)
104                 return error("--cached and --3way cannot be used together.");
105         if (state->threeway) {
106                 if (is_not_gitdir)
107                         return error(_("--3way outside a repository"));
108                 state->check_index = 1;
109         }
110         if (state->apply_with_reject)
111                 state->apply = state->apply_verbosely = 1;
112         if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
113                 state->apply = 0;
114         if (state->check_index && is_not_gitdir)
115                 return error(_("--index outside a repository"));
116         if (state->cached) {
117                 if (is_not_gitdir)
118                         return error(_("--cached outside a repository"));
119                 state->check_index = 1;
120         }
121         if (state->check_index)
122                 state->unsafe_paths = 0;
123         if (!state->lock_file)
124                 return error("BUG: state->lock_file should not be NULL");
125
126         return 0;
127 }