Skip to content

Commit 6d058c8

Browse files
Martin Ågrengitster
authored andcommitted
apply: move lockfile into apply_state
We have two users of `struct apply_state` and the related functionality in apply.c. Each user sets up its `apply_state` by handing over a pointer to its static `lock_file`. (Before 076aa2c (tempfile: auto-allocate tempfiles on heap, 2017-09-05), we could never free lockfiles, so making them static was a reasonable approach.) Other than that, they never directly access their `lock_file`s, which are instead handled by the functionality in apply.c. To make life easier for the caller and to make it less tempting for a future caller to mess with the lock, make apply.c fully responsible for setting up the `lock_file`. As mentioned above, it is now safe to free a `lock_file`, so we can make the `struct apply_state` contain an actual `struct lock_file` instead of a pointer to one. The user in builtin/apply.c is rather simple. For builtin/am.c, we might worry that the lock state is actually meant to be inherited across calls. But the lock is only taken as `apply_all_patches()` executes, and code inspection shows that it will always be released. Alternatively, we can observe that the lock itself is never queried directly. When we decide whether we should lock, we check a related variable `newfd`. That variable is not inherited, so from the point of view of apply.c, the state machine really is reset with each call to `init_apply_state()`. (It would be a bug if `newfd` and the lock status were not in sync. The duplication of information in `newfd` and the lock will be addressed in the next patch.) Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2954e5e commit 6d058c8

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

apply.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ static int parse_ignorewhitespace_option(struct apply_state *state,
7575
}
7676

7777
int init_apply_state(struct apply_state *state,
78-
const char *prefix,
79-
struct lock_file *lock_file)
78+
const char *prefix)
8079
{
8180
memset(state, 0, sizeof(*state));
8281
state->prefix = prefix;
83-
state->lock_file = lock_file;
8482
state->newfd = -1;
8583
state->apply = 1;
8684
state->line_termination = '\n';
@@ -146,8 +144,6 @@ int check_apply_state(struct apply_state *state, int force_apply)
146144
}
147145
if (state->check_index)
148146
state->unsafe_paths = 0;
149-
if (!state->lock_file)
150-
return error("BUG: state->lock_file should not be NULL");
151147

152148
if (state->apply_verbosity <= verbosity_silent) {
153149
state->saved_error_routine = get_error_routine();
@@ -4711,11 +4707,11 @@ static int apply_patch(struct apply_state *state,
47114707
state->update_index = state->check_index && state->apply;
47124708
if (state->update_index && state->newfd < 0) {
47134709
if (state->index_file)
4714-
state->newfd = hold_lock_file_for_update(state->lock_file,
4710+
state->newfd = hold_lock_file_for_update(&state->lock_file,
47154711
state->index_file,
47164712
LOCK_DIE_ON_ERROR);
47174713
else
4718-
state->newfd = hold_locked_index(state->lock_file, LOCK_DIE_ON_ERROR);
4714+
state->newfd = hold_locked_index(&state->lock_file, LOCK_DIE_ON_ERROR);
47194715
}
47204716

47214717
if (state->check_index && read_apply_cache(state) < 0) {
@@ -4911,7 +4907,7 @@ int apply_all_patches(struct apply_state *state,
49114907
}
49124908

49134909
if (state->update_index) {
4914-
res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4910+
res = write_locked_index(&the_index, &state->lock_file, COMMIT_LOCK);
49154911
if (res) {
49164912
error(_("Unable to write new index file"));
49174913
res = -128;
@@ -4924,7 +4920,7 @@ int apply_all_patches(struct apply_state *state,
49244920

49254921
end:
49264922
if (state->newfd >= 0) {
4927-
rollback_lock_file(state->lock_file);
4923+
rollback_lock_file(&state->lock_file);
49284924
state->newfd = -1;
49294925
}
49304926

apply.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct apply_state {
3737
const char *prefix;
3838

3939
/* These are lock_file related */
40-
struct lock_file *lock_file;
40+
struct lock_file lock_file;
4141
int newfd;
4242

4343
/* These control what gets looked at and modified */
@@ -116,8 +116,7 @@ extern int apply_parse_options(int argc, const char **argv,
116116
int *force_apply, int *options,
117117
const char * const *apply_usage);
118118
extern int init_apply_state(struct apply_state *state,
119-
const char *prefix,
120-
struct lock_file *lock_file);
119+
const char *prefix);
121120
extern void clear_apply_state(struct apply_state *state);
122121
extern int check_apply_state(struct apply_state *state, int force_apply);
123122

builtin/am.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,11 +1488,10 @@ static int run_apply(const struct am_state *state, const char *index_file)
14881488
struct argv_array apply_opts = ARGV_ARRAY_INIT;
14891489
struct apply_state apply_state;
14901490
int res, opts_left;
1491-
static struct lock_file lock_file;
14921491
int force_apply = 0;
14931492
int options = 0;
14941493

1495-
if (init_apply_state(&apply_state, NULL, &lock_file))
1494+
if (init_apply_state(&apply_state, NULL))
14961495
die("BUG: init_apply_state() failed");
14971496

14981497
argv_array_push(&apply_opts, "apply");

builtin/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ static const char * const apply_usage[] = {
99
NULL
1010
};
1111

12-
static struct lock_file lock_file;
13-
1412
int cmd_apply(int argc, const char **argv, const char *prefix)
1513
{
1614
int force_apply = 0;
1715
int options = 0;
1816
int ret;
1917
struct apply_state state;
2018

21-
if (init_apply_state(&state, prefix, &lock_file))
19+
if (init_apply_state(&state, prefix))
2220
exit(128);
2321

2422
argc = apply_parse_options(argc, argv,

0 commit comments

Comments
 (0)