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
This commit is contained in:
Junio C Hamano 2018-03-14 12:01:03 -07:00
commit 787aa97f21
3 changed files with 27 additions and 22 deletions

View File

@ -2218,12 +2218,15 @@ int merge_recursive_generic(struct merge_options *o,
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
clean = merge_recursive(o, head_commit, next_commit, ca, clean = merge_recursive(o, head_commit, next_commit, ca,
result); result);
if (clean < 0) if (clean < 0) {
rollback_lock_file(&lock);
return clean; return clean;
}
if (active_cache_changed && if (active_cache_changed &&
write_locked_index(&the_index, &lock, COMMIT_LOCK)) write_locked_index(&the_index, &lock, COMMIT_LOCK))
return err(o, _("Unable to write index.")); return err(o, _("Unable to write index."));
rollback_lock_file(&lock);
return clean ? 0 : 1; return clean ? 0 : 1;
} }

12
merge.c
View File

@ -113,17 +113,23 @@ int checkout_fast_forward(const struct object_id *head,
setup_unpack_trees_porcelain(&opts, "merge"); setup_unpack_trees_porcelain(&opts, "merge");
trees[nr_trees] = parse_tree_indirect(head); trees[nr_trees] = parse_tree_indirect(head);
if (!trees[nr_trees++]) if (!trees[nr_trees++]) {
rollback_lock_file(&lock_file);
return -1; return -1;
}
trees[nr_trees] = parse_tree_indirect(remote); trees[nr_trees] = parse_tree_indirect(remote);
if (!trees[nr_trees++]) if (!trees[nr_trees++]) {
rollback_lock_file(&lock_file);
return -1; return -1;
}
for (i = 0; i < nr_trees; i++) { for (i = 0; i < nr_trees; i++) {
parse_tree(trees[i]); parse_tree(trees[i]);
init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
} }
if (unpack_trees(nr_trees, t, &opts)) if (unpack_trees(nr_trees, t, &opts)) {
rollback_lock_file(&lock_file);
return -1; return -1;
}
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
return error(_("unable to write new index file")); return error(_("unable to write new index file"));
return 0; return 0;

View File

@ -339,7 +339,7 @@ static void print_advice(int show_hint, struct replay_opts *opts)
static int write_message(const void *buf, size_t len, const char *filename, static int write_message(const void *buf, size_t len, const char *filename,
int append_eol) int append_eol)
{ {
static struct lock_file msg_file; struct lock_file msg_file = LOCK_INIT;
int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
if (msg_fd < 0) if (msg_fd < 0)
@ -352,10 +352,8 @@ static int write_message(const void *buf, size_t len, const char *filename,
rollback_lock_file(&msg_file); rollback_lock_file(&msg_file);
return error_errno(_("could not write eol to '%s'"), filename); return error_errno(_("could not write eol to '%s'"), filename);
} }
if (commit_lock_file(&msg_file) < 0) { if (commit_lock_file(&msg_file) < 0)
rollback_lock_file(&msg_file); return error(_("failed to finalize '%s'"), filename);
return error(_("failed to finalize '%s'."), filename);
}
return 0; return 0;
} }
@ -485,7 +483,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
struct tree *result, *next_tree, *base_tree, *head_tree; struct tree *result, *next_tree, *base_tree, *head_tree;
int clean; int clean;
char **xopt; char **xopt;
static struct lock_file index_lock; struct lock_file index_lock = LOCK_INIT;
if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0) if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
@ -514,8 +512,10 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
fputs(o.obuf.buf, stdout); fputs(o.obuf.buf, stdout);
strbuf_release(&o.obuf); strbuf_release(&o.obuf);
diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0); diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0);
if (clean < 0) if (clean < 0) {
rollback_lock_file(&index_lock);
return clean; return clean;
}
if (active_cache_changed && if (active_cache_changed &&
write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
@ -1705,7 +1705,7 @@ static int prepare_revs(struct replay_opts *opts)
static int read_and_refresh_cache(struct replay_opts *opts) static int read_and_refresh_cache(struct replay_opts *opts)
{ {
static struct lock_file index_lock; struct lock_file index_lock = LOCK_INIT;
int index_fd = hold_locked_index(&index_lock, 0); int index_fd = hold_locked_index(&index_lock, 0);
if (read_index_preload(&the_index, NULL) < 0) { if (read_index_preload(&the_index, NULL) < 0) {
rollback_lock_file(&index_lock); rollback_lock_file(&index_lock);
@ -2108,16 +2108,14 @@ static int create_seq_dir(void)
static int save_head(const char *head) static int save_head(const char *head)
{ {
static struct lock_file head_lock; struct lock_file head_lock = LOCK_INIT;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int fd; int fd;
ssize_t written; ssize_t written;
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
if (fd < 0) { if (fd < 0)
rollback_lock_file(&head_lock);
return error_errno(_("could not lock HEAD")); return error_errno(_("could not lock HEAD"));
}
strbuf_addf(&buf, "%s\n", head); strbuf_addf(&buf, "%s\n", head);
written = write_in_full(fd, buf.buf, buf.len); written = write_in_full(fd, buf.buf, buf.len);
strbuf_release(&buf); strbuf_release(&buf);
@ -2126,10 +2124,8 @@ static int save_head(const char *head)
return error_errno(_("could not write to '%s'"), return error_errno(_("could not write to '%s'"),
git_path_head_file()); git_path_head_file());
} }
if (commit_lock_file(&head_lock) < 0) { if (commit_lock_file(&head_lock) < 0)
rollback_lock_file(&head_lock); return error(_("failed to finalize '%s'"), git_path_head_file());
return error(_("failed to finalize '%s'."), git_path_head_file());
}
return 0; return 0;
} }
@ -2233,7 +2229,7 @@ fail:
static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
{ {
static struct lock_file todo_lock; struct lock_file todo_lock = LOCK_INIT;
const char *todo_path = get_todo_path(opts); const char *todo_path = get_todo_path(opts);
int next = todo_list->current, offset, fd; int next = todo_list->current, offset, fd;
@ -2253,7 +2249,7 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
todo_list->buf.len - offset) < 0) todo_list->buf.len - offset) < 0)
return error_errno(_("could not write to '%s'"), todo_path); return error_errno(_("could not write to '%s'"), todo_path);
if (commit_lock_file(&todo_lock) < 0) if (commit_lock_file(&todo_lock) < 0)
return error(_("failed to finalize '%s'."), todo_path); return error(_("failed to finalize '%s'"), todo_path);
if (is_rebase_i(opts)) { if (is_rebase_i(opts)) {
const char *done_path = rebase_path_done(); const char *done_path = rebase_path_done();