apply: make init_apply_state() return -1 instead of exit()ing

To libify `git apply` functionality we have to signal errors to the caller instead of exit()ing. To do that in a compatible manner with the rest of the error handling in "builtin/apply.c", init_apply_state() should return -1 instead of calling exit(). 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 2f5a6d1218de4dfa326ff289b784d3e293b8141f
3 files changed +11 -9
apply.c
+6 -5
@@ -55,9 +55,9 @@ int parse_ignorewhitespace_option(struct apply_state *state,
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)
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;
@@ -79,9 +79,10 @@ void init_apply_state(struct apply_state *state,
79
80 git_apply_config();
81 if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
82 - exit(1);
82 + return -1;
83 if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
84 - exit(1);
84 + return -1;
85 + return 0;
86 }
87
88 void clear_apply_state(struct apply_state *state)
apply.h
+3 -3
@@ -102,9 +102,9 @@ extern int parse_whitespace_option(struct apply_state *state,
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);
105 +extern int 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
+2 -1
@@ -4741,7 +4741,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4741 OPT_END()
4742 };
4743
4744 - init_apply_state(&state, prefix, &lock_file);
4744 + if (init_apply_state(&state, prefix, &lock_file))
4745 + exit(128);
4746
4747 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4748 apply_usage, 0);