builtin/apply: make check_apply_state() return -1 instead of die()ing
[git/git.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 }