Skip to content

Commit 787aa97

Browse files
committed
Merge branch 'ma/roll-back-lockfiles'
Some codepaths used to take a lockfile and did not roll it back; they are automatically rolled back at program exit, so there is no real "breakage", but it still is a good practice to roll back when you are done with a lockfile. * ma/roll-back-lockfiles: sequencer: do not roll back lockfile unnecessarily merge: always roll back lock in `checkout_fast_forward()` merge-recursive: always roll back lock in `merge_recursive_generic()` sequencer: always roll back lock in `do_recursive_merge()` sequencer: make lockfiles non-static
2 parents 868f7d2 + 350292a commit 787aa97

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

merge-recursive.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,12 +2218,15 @@ int merge_recursive_generic(struct merge_options *o,
22182218
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
22192219
clean = merge_recursive(o, head_commit, next_commit, ca,
22202220
result);
2221-
if (clean < 0)
2221+
if (clean < 0) {
2222+
rollback_lock_file(&lock);
22222223
return clean;
2224+
}
22232225

22242226
if (active_cache_changed &&
22252227
write_locked_index(&the_index, &lock, COMMIT_LOCK))
22262228
return err(o, _("Unable to write index."));
2229+
rollback_lock_file(&lock);
22272230

22282231
return clean ? 0 : 1;
22292232
}

merge.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,23 @@ int checkout_fast_forward(const struct object_id *head,
113113
setup_unpack_trees_porcelain(&opts, "merge");
114114

115115
trees[nr_trees] = parse_tree_indirect(head);
116-
if (!trees[nr_trees++])
116+
if (!trees[nr_trees++]) {
117+
rollback_lock_file(&lock_file);
117118
return -1;
119+
}
118120
trees[nr_trees] = parse_tree_indirect(remote);
119-
if (!trees[nr_trees++])
121+
if (!trees[nr_trees++]) {
122+
rollback_lock_file(&lock_file);
120123
return -1;
124+
}
121125
for (i = 0; i < nr_trees; i++) {
122126
parse_tree(trees[i]);
123127
init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
124128
}
125-
if (unpack_trees(nr_trees, t, &opts))
129+
if (unpack_trees(nr_trees, t, &opts)) {
130+
rollback_lock_file(&lock_file);
126131
return -1;
132+
}
127133
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
128134
return error(_("unable to write new index file"));
129135
return 0;

sequencer.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static void print_advice(int show_hint, struct replay_opts *opts)
339339
static int write_message(const void *buf, size_t len, const char *filename,
340340
int append_eol)
341341
{
342-
static struct lock_file msg_file;
342+
struct lock_file msg_file = LOCK_INIT;
343343

344344
int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
345345
if (msg_fd < 0)
@@ -352,10 +352,8 @@ static int write_message(const void *buf, size_t len, const char *filename,
352352
rollback_lock_file(&msg_file);
353353
return error_errno(_("could not write eol to '%s'"), filename);
354354
}
355-
if (commit_lock_file(&msg_file) < 0) {
356-
rollback_lock_file(&msg_file);
357-
return error(_("failed to finalize '%s'."), filename);
358-
}
355+
if (commit_lock_file(&msg_file) < 0)
356+
return error(_("failed to finalize '%s'"), filename);
359357

360358
return 0;
361359
}
@@ -485,7 +483,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
485483
struct tree *result, *next_tree, *base_tree, *head_tree;
486484
int clean;
487485
char **xopt;
488-
static struct lock_file index_lock;
486+
struct lock_file index_lock = LOCK_INIT;
489487

490488
if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0)
491489
return -1;
@@ -514,8 +512,10 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
514512
fputs(o.obuf.buf, stdout);
515513
strbuf_release(&o.obuf);
516514
diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0);
517-
if (clean < 0)
515+
if (clean < 0) {
516+
rollback_lock_file(&index_lock);
518517
return clean;
518+
}
519519

520520
if (active_cache_changed &&
521521
write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
@@ -1705,7 +1705,7 @@ static int prepare_revs(struct replay_opts *opts)
17051705

17061706
static int read_and_refresh_cache(struct replay_opts *opts)
17071707
{
1708-
static struct lock_file index_lock;
1708+
struct lock_file index_lock = LOCK_INIT;
17091709
int index_fd = hold_locked_index(&index_lock, 0);
17101710
if (read_index_preload(&the_index, NULL) < 0) {
17111711
rollback_lock_file(&index_lock);
@@ -2108,16 +2108,14 @@ static int create_seq_dir(void)
21082108

21092109
static int save_head(const char *head)
21102110
{
2111-
static struct lock_file head_lock;
2111+
struct lock_file head_lock = LOCK_INIT;
21122112
struct strbuf buf = STRBUF_INIT;
21132113
int fd;
21142114
ssize_t written;
21152115

21162116
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
2117-
if (fd < 0) {
2118-
rollback_lock_file(&head_lock);
2117+
if (fd < 0)
21192118
return error_errno(_("could not lock HEAD"));
2120-
}
21212119
strbuf_addf(&buf, "%s\n", head);
21222120
written = write_in_full(fd, buf.buf, buf.len);
21232121
strbuf_release(&buf);
@@ -2126,10 +2124,8 @@ static int save_head(const char *head)
21262124
return error_errno(_("could not write to '%s'"),
21272125
git_path_head_file());
21282126
}
2129-
if (commit_lock_file(&head_lock) < 0) {
2130-
rollback_lock_file(&head_lock);
2131-
return error(_("failed to finalize '%s'."), git_path_head_file());
2132-
}
2127+
if (commit_lock_file(&head_lock) < 0)
2128+
return error(_("failed to finalize '%s'"), git_path_head_file());
21332129
return 0;
21342130
}
21352131

@@ -2233,7 +2229,7 @@ int sequencer_rollback(struct replay_opts *opts)
22332229

22342230
static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
22352231
{
2236-
static struct lock_file todo_lock;
2232+
struct lock_file todo_lock = LOCK_INIT;
22372233
const char *todo_path = get_todo_path(opts);
22382234
int next = todo_list->current, offset, fd;
22392235

@@ -2253,7 +2249,7 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
22532249
todo_list->buf.len - offset) < 0)
22542250
return error_errno(_("could not write to '%s'"), todo_path);
22552251
if (commit_lock_file(&todo_lock) < 0)
2256-
return error(_("failed to finalize '%s'."), todo_path);
2252+
return error(_("failed to finalize '%s'"), todo_path);
22572253

22582254
if (is_rebase_i(opts)) {
22592255
const char *done_path = rebase_path_done();

0 commit comments

Comments
 (0)