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);
clean = merge_recursive(o, head_commit, next_commit, ca,
result);
if (clean < 0)
if (clean < 0) {
rollback_lock_file(&lock);
return clean;
}
if (active_cache_changed &&
write_locked_index(&the_index, &lock, COMMIT_LOCK))
return err(o, _("Unable to write index."));
rollback_lock_file(&lock);
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");
trees[nr_trees] = parse_tree_indirect(head);
if (!trees[nr_trees++])
if (!trees[nr_trees++]) {
rollback_lock_file(&lock_file);
return -1;
}
trees[nr_trees] = parse_tree_indirect(remote);
if (!trees[nr_trees++])
if (!trees[nr_trees++]) {
rollback_lock_file(&lock_file);
return -1;
}
for (i = 0; i < nr_trees; i++) {
parse_tree(trees[i]);
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;
}
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
return error(_("unable to write new index file"));
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,
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);
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);
return error_errno(_("could not write eol to '%s'"), filename);
}
if (commit_lock_file(&msg_file) < 0) {
rollback_lock_file(&msg_file);
return error(_("failed to finalize '%s'."), filename);
}
if (commit_lock_file(&msg_file) < 0)
return error(_("failed to finalize '%s'"), filename);
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;
int clean;
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)
return -1;
@ -514,8 +512,10 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
fputs(o.obuf.buf, stdout);
strbuf_release(&o.obuf);
diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0);
if (clean < 0)
if (clean < 0) {
rollback_lock_file(&index_lock);
return clean;
}
if (active_cache_changed &&
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 struct lock_file index_lock;
struct lock_file index_lock = LOCK_INIT;
int index_fd = hold_locked_index(&index_lock, 0);
if (read_index_preload(&the_index, NULL) < 0) {
rollback_lock_file(&index_lock);
@ -2108,16 +2108,14 @@ static int create_seq_dir(void)
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;
int fd;
ssize_t written;
fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
if (fd < 0) {
rollback_lock_file(&head_lock);
if (fd < 0)
return error_errno(_("could not lock HEAD"));
}
strbuf_addf(&buf, "%s\n", head);
written = write_in_full(fd, buf.buf, buf.len);
strbuf_release(&buf);
@ -2126,10 +2124,8 @@ static int save_head(const char *head)
return error_errno(_("could not write to '%s'"),
git_path_head_file());
}
if (commit_lock_file(&head_lock) < 0) {
rollback_lock_file(&head_lock);
return error(_("failed to finalize '%s'."), git_path_head_file());
}
if (commit_lock_file(&head_lock) < 0)
return error(_("failed to finalize '%s'"), git_path_head_file());
return 0;
}
@ -2233,7 +2229,7 @@ int sequencer_rollback(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);
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)
return error_errno(_("could not write to '%s'"), todo_path);
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)) {
const char *done_path = rebase_path_done();