path: root/apply.c
diff options
authorMartin Ågren <>2017-10-05 20:32:09 (GMT)
committerJunio C Hamano <>2017-10-06 01:07:18 (GMT)
commit6d058c882643dc45c8d708be98e86c38f25511a9 (patch)
tree7eca9465676f0f7289ea492f92f69f2a028cc908 /apply.c
parent2954e5ec4361c6932593663859aa437583edb500 (diff)
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 076aa2cbd (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 <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'apply.c')
1 files changed, 5 insertions, 9 deletions
diff --git a/apply.c b/apply.c
index c022af5..5a6ca10 100644
--- a/apply.c
+++ b/apply.c
@@ -75,12 +75,10 @@ static int parse_ignorewhitespace_option(struct apply_state *state,
int init_apply_state(struct apply_state *state,
- const char *prefix,
- struct lock_file *lock_file)
+ const char *prefix)
memset(state, 0, sizeof(*state));
state->prefix = prefix;
- state->lock_file = lock_file;
state->newfd = -1;
state->apply = 1;
state->line_termination = '\n';
@@ -146,8 +144,6 @@ int check_apply_state(struct apply_state *state, int force_apply)
if (state->check_index)
state->unsafe_paths = 0;
- if (!state->lock_file)
- return error("BUG: state->lock_file should not be NULL");
if (state->apply_verbosity <= verbosity_silent) {
state->saved_error_routine = get_error_routine();
@@ -4711,11 +4707,11 @@ static int apply_patch(struct apply_state *state,
state->update_index = state->check_index && state->apply;
if (state->update_index && state->newfd < 0) {
if (state->index_file)
- state->newfd = hold_lock_file_for_update(state->lock_file,
+ state->newfd = hold_lock_file_for_update(&state->lock_file,
- state->newfd = hold_locked_index(state->lock_file, LOCK_DIE_ON_ERROR);
+ state->newfd = hold_locked_index(&state->lock_file, LOCK_DIE_ON_ERROR);
if (state->check_index && read_apply_cache(state) < 0) {
@@ -4911,7 +4907,7 @@ int apply_all_patches(struct apply_state *state,
if (state->update_index) {
- res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
+ res = write_locked_index(&the_index, &state->lock_file, COMMIT_LOCK);
if (res) {
error(_("Unable to write new index file"));
res = -128;
@@ -4924,7 +4920,7 @@ int apply_all_patches(struct apply_state *state,
if (state->newfd >= 0) {
- rollback_lock_file(state->lock_file);
+ rollback_lock_file(&state->lock_file);
state->newfd = -1;