treewide: prefer lockfiles on the stack

There is no longer any need to allocate and leak a `struct lock_file`.
The previous patch addressed an instance where we needed a minor tweak
alongside the trivial changes.

Deal with the remaining instances where we allocate and leak a struct
within a single function. Change them to have the `struct lock_file` on
the stack instead.

These instances were identified by running `git grep "^\s*struct
lock_file\s*\*"`.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Martin Ågren 2017-10-05 22:32:04 +02:00 коммит произвёл Junio C Hamano
Родитель f132a127ee
Коммит 837e34eba4
8 изменённых файлов: 42 добавлений и 49 удалений

Просмотреть файл

@ -1134,11 +1134,11 @@ static const char *msgnum(const struct am_state *state)
*/ */
static void refresh_and_write_cache(void) static void refresh_and_write_cache(void)
{ {
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); struct lock_file lock_file = LOCK_INIT;
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("unable to write index file")); die(_("unable to write index file"));
} }
@ -1946,15 +1946,14 @@ next:
*/ */
static int fast_forward_to(struct tree *head, struct tree *remote, int reset) static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
{ {
struct lock_file *lock_file; struct lock_file lock_file = LOCK_INIT;
struct unpack_trees_options opts; struct unpack_trees_options opts;
struct tree_desc t[2]; struct tree_desc t[2];
if (parse_tree(head) || parse_tree(remote)) if (parse_tree(head) || parse_tree(remote))
return -1; return -1;
lock_file = xcalloc(1, sizeof(struct lock_file)); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
@ -1970,11 +1969,11 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
init_tree_desc(&t[1], remote->buffer, remote->size); init_tree_desc(&t[1], remote->buffer, remote->size);
if (unpack_trees(2, t, &opts)) { if (unpack_trees(2, t, &opts)) {
rollback_lock_file(lock_file); 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))
die(_("unable to write new index file")); die(_("unable to write new index file"));
return 0; return 0;
@ -1986,15 +1985,14 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
*/ */
static int merge_tree(struct tree *tree) static int merge_tree(struct tree *tree)
{ {
struct lock_file *lock_file; struct lock_file lock_file = LOCK_INIT;
struct unpack_trees_options opts; struct unpack_trees_options opts;
struct tree_desc t[1]; struct tree_desc t[1];
if (parse_tree(tree)) if (parse_tree(tree))
return -1; return -1;
lock_file = xcalloc(1, sizeof(struct lock_file)); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.head_idx = 1; opts.head_idx = 1;
@ -2005,11 +2003,11 @@ static int merge_tree(struct tree *tree)
init_tree_desc(&t[0], tree->buffer, tree->size); init_tree_desc(&t[0], tree->buffer, tree->size);
if (unpack_trees(1, t, &opts)) { if (unpack_trees(1, t, &opts)) {
rollback_lock_file(lock_file); 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))
die(_("unable to write new index file")); die(_("unable to write new index file"));
return 0; return 0;

Просмотреть файл

@ -247,7 +247,7 @@ static int checkout_paths(const struct checkout_opts *opts,
struct object_id rev; struct object_id rev;
struct commit *head; struct commit *head;
int errs = 0; int errs = 0;
struct lock_file *lock_file; struct lock_file lock_file = LOCK_INIT;
if (opts->track != BRANCH_TRACK_UNSPECIFIED) if (opts->track != BRANCH_TRACK_UNSPECIFIED)
die(_("'%s' cannot be used with updating paths"), "--track"); die(_("'%s' cannot be used with updating paths"), "--track");
@ -275,9 +275,7 @@ static int checkout_paths(const struct checkout_opts *opts,
return run_add_interactive(revision, "--patch=checkout", return run_add_interactive(revision, "--patch=checkout",
&opts->pathspec); &opts->pathspec);
lock_file = xcalloc(1, sizeof(struct lock_file)); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
if (read_cache_preload(&opts->pathspec) < 0) if (read_cache_preload(&opts->pathspec) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
@ -376,7 +374,7 @@ static int checkout_paths(const struct checkout_opts *opts,
} }
errs |= finish_delayed_checkout(&state); errs |= finish_delayed_checkout(&state);
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("unable to write new index file")); die(_("unable to write new index file"));
read_ref_full("HEAD", 0, rev.hash, NULL); read_ref_full("HEAD", 0, rev.hash, NULL);
@ -472,9 +470,9 @@ static int merge_working_tree(const struct checkout_opts *opts,
int *writeout_error) int *writeout_error)
{ {
int ret; int ret;
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); struct lock_file lock_file = LOCK_INIT;
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
if (read_cache_preload(NULL) < 0) if (read_cache_preload(NULL) < 0)
return error(_("index file corrupt")); return error(_("index file corrupt"));
@ -591,7 +589,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
if (!cache_tree_fully_valid(active_cache_tree)) if (!cache_tree_fully_valid(active_cache_tree))
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("unable to write new index file")); die(_("unable to write new index file"));
if (!opts->force && !opts->quiet) if (!opts->force && !opts->quiet)

Просмотреть файл

@ -706,7 +706,7 @@ static int checkout(int submodule_progress)
{ {
struct object_id oid; struct object_id oid;
char *head; char *head;
struct lock_file *lock_file; struct lock_file lock_file = LOCK_INIT;
struct unpack_trees_options opts; struct unpack_trees_options opts;
struct tree *tree; struct tree *tree;
struct tree_desc t; struct tree_desc t;
@ -733,8 +733,7 @@ static int checkout(int submodule_progress)
/* We need to be in the new work tree for the checkout */ /* We need to be in the new work tree for the checkout */
setup_work_tree(); setup_work_tree();
lock_file = xcalloc(1, sizeof(struct lock_file)); hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
memset(&opts, 0, sizeof opts); memset(&opts, 0, sizeof opts);
opts.update = 1; opts.update = 1;
@ -750,7 +749,7 @@ static int checkout(int submodule_progress)
if (unpack_trees(1, &t, &opts) < 0) if (unpack_trees(1, &t, &opts) < 0)
die(_("unable to checkout working tree")); die(_("unable to checkout working tree"));
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("unable to write new index file")); die(_("unable to write new index file"));
err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1), err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),

Просмотреть файл

@ -203,17 +203,16 @@ static int builtin_diff_combined(struct rev_info *revs,
static void refresh_index_quietly(void) static void refresh_index_quietly(void)
{ {
struct lock_file *lock_file; struct lock_file lock_file = LOCK_INIT;
int fd; int fd;
lock_file = xcalloc(1, sizeof(struct lock_file)); fd = hold_locked_index(&lock_file, 0);
fd = hold_locked_index(lock_file, 0);
if (fd < 0) if (fd < 0)
return; return;
discard_cache(); discard_cache();
read_cache(); read_cache();
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED); refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
update_index_if_able(&the_index, lock_file); update_index_if_able(&the_index, &lock_file);
} }
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv) static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)

Просмотреть файл

@ -2748,7 +2748,7 @@ int git_config_rename_section_in_file(const char *config_filename,
{ {
int ret = 0, remove = 0; int ret = 0, remove = 0;
char *filename_buf = NULL; char *filename_buf = NULL;
struct lock_file *lock; struct lock_file lock = LOCK_INIT;
int out_fd; int out_fd;
char buf[1024]; char buf[1024];
FILE *config_file = NULL; FILE *config_file = NULL;
@ -2762,8 +2762,7 @@ int git_config_rename_section_in_file(const char *config_filename,
if (!config_filename) if (!config_filename)
config_filename = filename_buf = git_pathdup("config"); config_filename = filename_buf = git_pathdup("config");
lock = xcalloc(1, sizeof(struct lock_file)); out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
if (out_fd < 0) { if (out_fd < 0) {
ret = error("could not lock config file %s", config_filename); ret = error("could not lock config file %s", config_filename);
goto out; goto out;
@ -2782,9 +2781,9 @@ int git_config_rename_section_in_file(const char *config_filename,
goto out; goto out;
} }
if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
ret = error_errno("chmod on %s failed", ret = error_errno("chmod on %s failed",
get_lock_file_path(lock)); get_lock_file_path(&lock));
goto out; goto out;
} }
@ -2805,7 +2804,7 @@ int git_config_rename_section_in_file(const char *config_filename,
} }
store.baselen = strlen(new_name); store.baselen = strlen(new_name);
if (write_section(out_fd, new_name) < 0) { if (write_section(out_fd, new_name) < 0) {
ret = write_error(get_lock_file_path(lock)); ret = write_error(get_lock_file_path(&lock));
goto out; goto out;
} }
/* /*
@ -2831,20 +2830,20 @@ int git_config_rename_section_in_file(const char *config_filename,
continue; continue;
length = strlen(output); length = strlen(output);
if (write_in_full(out_fd, output, length) < 0) { if (write_in_full(out_fd, output, length) < 0) {
ret = write_error(get_lock_file_path(lock)); ret = write_error(get_lock_file_path(&lock));
goto out; goto out;
} }
} }
fclose(config_file); fclose(config_file);
config_file = NULL; config_file = NULL;
commit_and_out: commit_and_out:
if (commit_lock_file(lock) < 0) if (commit_lock_file(&lock) < 0)
ret = error_errno("could not write config file %s", ret = error_errno("could not write config file %s",
config_filename); config_filename);
out: out:
if (config_file) if (config_file)
fclose(config_file); fclose(config_file);
rollback_lock_file(lock); rollback_lock_file(&lock);
out_no_rollback: out_no_rollback:
free(filename_buf); free(filename_buf);
return ret; return ret;

Просмотреть файл

@ -2162,7 +2162,7 @@ int merge_recursive_generic(struct merge_options *o,
struct commit **result) struct commit **result)
{ {
int clean; int clean;
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); struct lock_file lock = LOCK_INIT;
struct commit *head_commit = get_ref(head, o->branch1); struct commit *head_commit = get_ref(head, o->branch1);
struct commit *next_commit = get_ref(merge, o->branch2); struct commit *next_commit = get_ref(merge, o->branch2);
struct commit_list *ca = NULL; struct commit_list *ca = NULL;
@ -2178,14 +2178,14 @@ 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)
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."));
return clean ? 0 : 1; return clean ? 0 : 1;

Просмотреть файл

@ -53,11 +53,11 @@ int checkout_fast_forward(const struct object_id *head,
struct tree_desc t[MAX_UNPACK_TREES]; struct tree_desc t[MAX_UNPACK_TREES];
int i, nr_trees = 0; int i, nr_trees = 0;
struct dir_struct dir; struct dir_struct dir;
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); struct lock_file lock_file = LOCK_INIT;
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (hold_locked_index(lock_file, LOCK_REPORT_ON_ERROR) < 0) if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
memset(&trees, 0, sizeof(trees)); memset(&trees, 0, sizeof(trees));
@ -91,8 +91,8 @@ int checkout_fast_forward(const struct object_id *head,
} }
if (unpack_trees(nr_trees, t, &opts)) if (unpack_trees(nr_trees, t, &opts))
return -1; return -1;
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) { if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) {
rollback_lock_file(lock_file); rollback_lock_file(&lock_file);
return error(_("unable to write new index file")); return error(_("unable to write new index file"));
} }
return 0; return 0;

Просмотреть файл

@ -2299,14 +2299,14 @@ int has_uncommitted_changes(int ignore_submodules)
*/ */
int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently) int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
{ {
struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file)); struct lock_file lock_file = LOCK_INIT;
int err = 0, fd; int err = 0, fd;
fd = hold_locked_index(lock_file, 0); fd = hold_locked_index(&lock_file, 0);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (0 <= fd) if (0 <= fd)
update_index_if_able(&the_index, lock_file); update_index_if_able(&the_index, &lock_file);
rollback_lock_file(lock_file); rollback_lock_file(&lock_file);
if (has_unstaged_changes(ignore_submodules)) { if (has_unstaged_changes(ignore_submodules)) {
/* TRANSLATORS: the action is e.g. "pull with rebase" */ /* TRANSLATORS: the action is e.g. "pull with rebase" */