diff options
author | Christian Couder <christian.couder@gmail.com> | 2016-06-03 18:58:51 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-06-03 10:30:16 -0700 |
commit | 8f31fac365c312aa9109a7a1fc1014e56ed473d2 (patch) | |
tree | ad51b6cbf8585d4960fe8e576f62a8a22fa25097 /builtin/apply.c | |
parent | builtin/apply: move applying patches into apply_all_patches() (diff) | |
download | tgif-8f31fac365c312aa9109a7a1fc1014e56ed473d2.tar.xz |
builtin/apply: add 'lock_file' pointer into 'struct apply_state'
We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c
keeps a linked list of all created lock_file structures.
Also 'struct apply_state' users might later want the same 'struct lock_file'
instance to be reused by different series of calls to the apply api.
So let's add a 'struct lock_file *lock_file' pointer into 'struct apply_state'
and have the user of 'struct apply_state' allocate memory for the actual
'struct lock_file' instance.
Let's also add an argument to init_apply_state(), so that the caller can
easily supply a pointer to the allocated instance.
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/apply.c')
-rw-r--r-- | builtin/apply.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/builtin/apply.c b/builtin/apply.c index 5027f1b504..cc635ebd1a 100644 --- a/builtin/apply.c +++ b/builtin/apply.c @@ -52,6 +52,12 @@ struct apply_state { const char *prefix; int prefix_length; + /* + * Since lockfile.c keeps a linked list of all created + * lock_file structures, it isn't safe to free(lock_file). + */ + struct lock_file *lock_file; + /* These control what gets looked at and modified */ int apply; /* this is not a dry-run */ int cached; /* apply to the index only */ @@ -4547,7 +4553,7 @@ static int apply_patch(struct apply_state *state, state->update_index = state->check_index && state->apply; if (state->update_index && newfd < 0) - newfd = hold_locked_index(&lock_file, 1); + newfd = hold_locked_index(state->lock_file, 1); if (state->check_index) { if (read_cache() < 0) @@ -4648,11 +4654,14 @@ static int option_parse_directory(const struct option *opt, return 0; } -static void init_apply_state(struct apply_state *state, const char *prefix) +static void init_apply_state(struct apply_state *state, + const char *prefix, + struct lock_file *lock_file) { memset(state, 0, sizeof(*state)); state->prefix = prefix; state->prefix_length = state->prefix ? strlen(state->prefix) : 0; + state->lock_file = lock_file; state->apply = 1; state->line_termination = '\n'; state->p_value = 1; @@ -4705,6 +4714,8 @@ static void check_apply_state(struct apply_state *state, int force_apply) } if (state->check_index) state->unsafe_paths = 0; + if (!state->lock_file) + die("BUG: state->lock_file should not be NULL"); } static int apply_all_patches(struct apply_state *state, @@ -4769,7 +4780,7 @@ static int apply_all_patches(struct apply_state *state, } if (state->update_index) { - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) + if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK)) die(_("Unable to write new index file")); } @@ -4852,7 +4863,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix) OPT_END() }; - init_apply_state(&state, prefix); + init_apply_state(&state, prefix, &lock_file); argc = parse_options(argc, argv, state.prefix, builtin_apply_options, apply_usage, 0); |