Skip to content

Commit 8f31fac

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 91b769c commit 8f31fac

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

builtin/apply.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ struct apply_state {
5252
const char *prefix;
5353
int prefix_length;
5454

55+
/*
56+
* Since lockfile.c keeps a linked list of all created
57+
* lock_file structures, it isn't safe to free(lock_file).
58+
*/
59+
struct lock_file *lock_file;
60+
5561
/* These control what gets looked at and modified */
5662
int apply; /* this is not a dry-run */
5763
int cached; /* apply to the index only */
@@ -4547,7 +4553,7 @@ static int apply_patch(struct apply_state *state,
45474553

45484554
state->update_index = state->check_index && state->apply;
45494555
if (state->update_index && newfd < 0)
4550-
newfd = hold_locked_index(&lock_file, 1);
4556+
newfd = hold_locked_index(state->lock_file, 1);
45514557

45524558
if (state->check_index) {
45534559
if (read_cache() < 0)
@@ -4648,11 +4654,14 @@ static int option_parse_directory(const struct option *opt,
46484654
return 0;
46494655
}
46504656

4651-
static void init_apply_state(struct apply_state *state, const char *prefix)
4657+
static void init_apply_state(struct apply_state *state,
4658+
const char *prefix,
4659+
struct lock_file *lock_file)
46524660
{
46534661
memset(state, 0, sizeof(*state));
46544662
state->prefix = prefix;
46554663
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4664+
state->lock_file = lock_file;
46564665
state->apply = 1;
46574666
state->line_termination = '\n';
46584667
state->p_value = 1;
@@ -4705,6 +4714,8 @@ static void check_apply_state(struct apply_state *state, int force_apply)
47054714
}
47064715
if (state->check_index)
47074716
state->unsafe_paths = 0;
4717+
if (!state->lock_file)
4718+
die("BUG: state->lock_file should not be NULL");
47084719
}
47094720

47104721
static int apply_all_patches(struct apply_state *state,
@@ -4769,7 +4780,7 @@ static int apply_all_patches(struct apply_state *state,
47694780
}
47704781

47714782
if (state->update_index) {
4772-
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4783+
if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
47734784
die(_("Unable to write new index file"));
47744785
}
47754786

@@ -4852,7 +4863,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
48524863
OPT_END()
48534864
};
48544865

4855-
init_apply_state(&state, prefix);
4866+
init_apply_state(&state, prefix, &lock_file);
48564867

48574868
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
48584869
apply_usage, 0);

0 commit comments

Comments
 (0)