builtin/apply: move init_apply_state() to apply.c

To libify `git apply` functionality we must make init_apply_state() usable outside "builtin/apply.c". Let's do that by moving it into a new "apply.c". Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Aug 8, 2016 at 23:03 UTC bb493a5c147a4b60f0f412a71bf9236ede4a560c
4 files changed +105 -91
Makefile
+1
@@ -683,6 +683,7 @@ LIB_OBJS += abspath.o
683 LIB_OBJS += advice.o
684 LIB_OBJS += alias.o
685 LIB_OBJS += alloc.o
686 +LIB_OBJS += apply.o
687 LIB_OBJS += archive.o
688 LIB_OBJS += archive-tar.o
689 LIB_OBJS += archive-zip.o
apply.c new
+94
@@ -0,0 +1,94 @@
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 +void 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 + exit(1);
83 + if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
84 + exit(1);
85 +}
86 +
87 +void clear_apply_state(struct apply_state *state)
88 +{
89 + string_list_clear(&state->limit_by_name, 0);
90 + string_list_clear(&state->symlink_changes, 0);
91 + strbuf_release(&state->root);
92 +
93 + /* &state->fn_table is cleared at the end of apply_patch() */
94 +}
apply.h
+10
@@ -97,4 +97,14 @@ struct apply_state {
97 int applied_after_fixing_ws;
98 };
99
100 +extern int parse_whitespace_option(struct apply_state *state,
101 + const char *option);
102 +extern int parse_ignorewhitespace_option(struct apply_state *state,
103 + const char *option);
104 +
105 +extern void init_apply_state(struct apply_state *state,
106 + const char *prefix,
107 + struct lock_file *lock_file);
108 +extern void clear_apply_state(struct apply_state *state);
109 +
110 #endif
builtin/apply.c
-91
@@ -27,52 +27,6 @@ static const char * const apply_usage[] = {
27 NULL
28 };
29
30 -static int parse_whitespace_option(struct apply_state *state, const char *option)
31 -{
32 - if (!option) {
33 - state->ws_error_action = warn_on_ws_error;
34 - return 0;
35 - }
36 - if (!strcmp(option, "warn")) {
37 - state->ws_error_action = warn_on_ws_error;
38 - return 0;
39 - }
40 - if (!strcmp(option, "nowarn")) {
41 - state->ws_error_action = nowarn_ws_error;
42 - return 0;
43 - }
44 - if (!strcmp(option, "error")) {
45 - state->ws_error_action = die_on_ws_error;
46 - return 0;
47 - }
48 - if (!strcmp(option, "error-all")) {
49 - state->ws_error_action = die_on_ws_error;
50 - state->squelch_whitespace_errors = 0;
51 - return 0;
52 - }
53 - if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
54 - state->ws_error_action = correct_ws_error;
55 - return 0;
56 - }
57 - return error(_("unrecognized whitespace option '%s'"), option);
58 -}
59 -
60 -static int parse_ignorewhitespace_option(struct apply_state *state,
61 - const char *option)
62 -{
63 - if (!option || !strcmp(option, "no") ||
64 - !strcmp(option, "false") || !strcmp(option, "never") ||
65 - !strcmp(option, "none")) {
66 - state->ws_ignore_action = ignore_ws_none;
67 - return 0;
68 - }
69 - if (!strcmp(option, "change")) {
70 - state->ws_ignore_action = ignore_ws_change;
71 - return 0;
72 - }
73 - return error(_("unrecognized whitespace ignore option '%s'"), option);
74 -}
75 -
30 static void set_default_whitespace_mode(struct apply_state *state)
31 {
32 if (!state->whitespace_option && !apply_default_whitespace)
@@ -4539,13 +4493,6 @@ end:
4493 return res;
4494 }
4495
4542 -static void git_apply_config(void)
4543 -{
4544 - git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
4545 - git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
4546 - git_config(git_default_config, NULL);
4547 -}
4548 -
4496 static int option_parse_exclude(const struct option *opt,
4497 const char *arg, int unset)
4498 {
@@ -4604,44 +4551,6 @@ static int option_parse_directory(const struct option *opt,
4551 return 0;
4552 }
4553
4607 -static void init_apply_state(struct apply_state *state,
4608 - const char *prefix,
4609 - struct lock_file *lock_file)
4610 -{
4611 - memset(state, 0, sizeof(*state));
4612 - state->prefix = prefix;
4613 - state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4614 - state->lock_file = lock_file;
4615 - state->newfd = -1;
4616 - state->apply = 1;
4617 - state->line_termination = '\n';
4618 - state->p_value = 1;
4619 - state->p_context = UINT_MAX;
4620 - state->squelch_whitespace_errors = 5;
4621 - state->ws_error_action = warn_on_ws_error;
4622 - state->ws_ignore_action = ignore_ws_none;
4623 - state->linenr = 1;
4624 - string_list_init(&state->fn_table, 0);
4625 - string_list_init(&state->limit_by_name, 0);
4626 - string_list_init(&state->symlink_changes, 0);
4627 - strbuf_init(&state->root, 0);
4628 -
4629 - git_apply_config();
4630 - if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
4631 - exit(1);
4632 - if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
4633 - exit(1);
4634 -}
4635 -
4636 -static void clear_apply_state(struct apply_state *state)
4637 -{
4638 - string_list_clear(&state->limit_by_name, 0);
4639 - string_list_clear(&state->symlink_changes, 0);
4640 - strbuf_release(&state->root);
4641 -
4642 - /* &state->fn_table is cleared at the end of apply_patch() */
4643 -}
4644 -
4554 static void check_apply_state(struct apply_state *state, int force_apply)
4555 {
4556 int is_not_gitdir = !startup_info->have_repository;