builtin/apply: move 'state' init into init_apply_state()

When the apply functionality will be libified, the 'struct apply_state' will be used by different pieces of code. To properly initialize a 'struct apply_state', let's provide a nice and easy to use init_apply_state() function. Let's also provide clear_apply_state() to release memory used by 'struct apply_state' members, so that a 'struct apply_state' instance can be easily reused without leaking memory. Note that clear_apply_state() does nothing for now, but it will later. While at it, let's rename 'prefix_' parameter to 'prefix'. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed May 24, 2016 at 10:10 UTC 6f27b941f2664c1653d0c4edcec5686119f0c023
1 file changed +22 -10
builtin/apply.c
+22 -10
@@ -4522,7 +4522,25 @@ static int option_parse_directory(const struct option *opt,
4522 return 0;
4523 }
4524
4525 -int cmd_apply(int argc, const char **argv, const char *prefix_)
4525 +static void init_apply_state(struct apply_state *state, const char *prefix)
4526 +{
4527 + memset(state, 0, sizeof(*state));
4528 + state->prefix = prefix;
4529 + state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4530 +
4531 + git_apply_config();
4532 + if (apply_default_whitespace)
4533 + parse_whitespace_option(apply_default_whitespace);
4534 + if (apply_default_ignorewhitespace)
4535 + parse_ignorewhitespace_option(apply_default_ignorewhitespace);
4536 +}
4537 +
4538 +static void clear_apply_state(struct apply_state *state)
4539 +{
4540 + /* empty for now */
4541 +}
4542 +
4543 +int cmd_apply(int argc, const char **argv, const char *prefix)
4544 {
4545 int i;
4546 int errs = 0;
@@ -4603,15 +4621,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
4621 OPT_END()
4622 };
4623
4606 - memset(&state, 0, sizeof(state));
4607 - state.prefix = prefix_;
4608 - state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
4609 -
4610 - git_apply_config();
4611 - if (apply_default_whitespace)
4612 - parse_whitespace_option(apply_default_whitespace);
4613 - if (apply_default_ignorewhitespace)
4614 - parse_ignorewhitespace_option(apply_default_ignorewhitespace);
4624 + init_apply_state(&state, prefix);
4625
4626 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4627 apply_usage, 0);
@@ -4695,5 +4705,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
4705 die(_("Unable to write new index file"));
4706 }
4707
4708 + clear_apply_state(&state);
4709 +
4710 return !!errs;
4711 }