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>

Martin Ågren committed Oct 5, 2017 at 22:32 UTC 837e34eba47f209a38fc9ab458bd103fd7515325
8 files changed +42 -49
builtin/am.c
+11 -13
@@ -1134,11 +1134,11 @@ static const char *msgnum(const struct am_state *state)
1134 */
1135 static void refresh_and_write_cache(void)
1136 {
1137 - struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
1137 + struct lock_file lock_file = LOCK_INIT;
1138
1139 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
1139 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
1140 refresh_cache(REFRESH_QUIET);
1141 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1141 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1142 die(_("unable to write index file"));
1143 }
1144
@@ -1946,15 +1946,14 @@ next:
1946 */
1947 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1948 {
1949 - struct lock_file *lock_file;
1949 + struct lock_file lock_file = LOCK_INIT;
1950 struct unpack_trees_options opts;
1951 struct tree_desc t[2];
1952
1953 if (parse_tree(head) || parse_tree(remote))
1954 return -1;
1955
1956 - lock_file = xcalloc(1, sizeof(struct lock_file));
1957 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
1956 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
1957
1958 refresh_cache(REFRESH_QUIET);
1959
@@ -1970,11 +1969,11 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1969 init_tree_desc(&t[1], remote->buffer, remote->size);
1970
1971 if (unpack_trees(2, t, &opts)) {
1973 - rollback_lock_file(lock_file);
1972 + rollback_lock_file(&lock_file);
1973 return -1;
1974 }
1975
1977 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1976 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1977 die(_("unable to write new index file"));
1978
1979 return 0;
@@ -1986,15 +1985,14 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1985 */
1986 static int merge_tree(struct tree *tree)
1987 {
1989 - struct lock_file *lock_file;
1988 + struct lock_file lock_file = LOCK_INIT;
1989 struct unpack_trees_options opts;
1990 struct tree_desc t[1];
1991
1992 if (parse_tree(tree))
1993 return -1;
1994
1996 - lock_file = xcalloc(1, sizeof(struct lock_file));
1997 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
1995 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
1996
1997 memset(&opts, 0, sizeof(opts));
1998 opts.head_idx = 1;
@@ -2005,11 +2003,11 @@ static int merge_tree(struct tree *tree)
2003 init_tree_desc(&t[0], tree->buffer, tree->size);
2004
2005 if (unpack_trees(1, t, &opts)) {
2008 - rollback_lock_file(lock_file);
2006 + rollback_lock_file(&lock_file);
2007 return -1;
2008 }
2009
2012 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
2010 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
2011 die(_("unable to write new index file"));
2012
2013 return 0;
builtin/checkout.c
+6 -8
@@ -247,7 +247,7 @@ static int checkout_paths(const struct checkout_opts *opts,
247 struct object_id rev;
248 struct commit *head;
249 int errs = 0;
250 - struct lock_file *lock_file;
250 + struct lock_file lock_file = LOCK_INIT;
251
252 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
253 die(_("'%s' cannot be used with updating paths"), "--track");
@@ -275,9 +275,7 @@ static int checkout_paths(const struct checkout_opts *opts,
275 return run_add_interactive(revision, "--patch=checkout",
276 &opts->pathspec);
277
278 - lock_file = xcalloc(1, sizeof(struct lock_file));
279 -
280 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
278 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
279 if (read_cache_preload(&opts->pathspec) < 0)
280 return error(_("index file corrupt"));
281
@@ -376,7 +374,7 @@ static int checkout_paths(const struct checkout_opts *opts,
374 }
375 errs |= finish_delayed_checkout(&state);
376
379 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
377 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
378 die(_("unable to write new index file"));
379
380 read_ref_full("HEAD", 0, rev.hash, NULL);
@@ -472,9 +470,9 @@ static int merge_working_tree(const struct checkout_opts *opts,
470 int *writeout_error)
471 {
472 int ret;
475 - struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
473 + struct lock_file lock_file = LOCK_INIT;
474
477 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
475 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
476 if (read_cache_preload(NULL) < 0)
477 return error(_("index file corrupt"));
478
@@ -591,7 +589,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
589 if (!cache_tree_fully_valid(active_cache_tree))
590 cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
591
594 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
592 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
593 die(_("unable to write new index file"));
594
595 if (!opts->force && !opts->quiet)
builtin/clone.c
+3 -4
@@ -706,7 +706,7 @@ static int checkout(int submodule_progress)
706 {
707 struct object_id oid;
708 char *head;
709 - struct lock_file *lock_file;
709 + struct lock_file lock_file = LOCK_INIT;
710 struct unpack_trees_options opts;
711 struct tree *tree;
712 struct tree_desc t;
@@ -733,8 +733,7 @@ static int checkout(int submodule_progress)
733 /* We need to be in the new work tree for the checkout */
734 setup_work_tree();
735
736 - lock_file = xcalloc(1, sizeof(struct lock_file));
737 - hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
736 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
737
738 memset(&opts, 0, sizeof opts);
739 opts.update = 1;
@@ -750,7 +749,7 @@ static int checkout(int submodule_progress)
749 if (unpack_trees(1, &t, &opts) < 0)
750 die(_("unable to checkout working tree"));
751
753 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
752 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
753 die(_("unable to write new index file"));
754
755 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
builtin/diff.c
+3 -4
@@ -203,17 +203,16 @@ static int builtin_diff_combined(struct rev_info *revs,
203
204 static void refresh_index_quietly(void)
205 {
206 - struct lock_file *lock_file;
206 + struct lock_file lock_file = LOCK_INIT;
207 int fd;
208
209 - lock_file = xcalloc(1, sizeof(struct lock_file));
210 - fd = hold_locked_index(lock_file, 0);
209 + fd = hold_locked_index(&lock_file, 0);
210 if (fd < 0)
211 return;
212 discard_cache();
213 read_cache();
214 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
216 - update_index_if_able(&the_index, lock_file);
215 + update_index_if_able(&the_index, &lock_file);
216 }
217
218 static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
config.c
+8 -9
@@ -2748,7 +2748,7 @@ int git_config_rename_section_in_file(const char *config_filename,
2748 {
2749 int ret = 0, remove = 0;
2750 char *filename_buf = NULL;
2751 - struct lock_file *lock;
2751 + struct lock_file lock = LOCK_INIT;
2752 int out_fd;
2753 char buf[1024];
2754 FILE *config_file = NULL;
@@ -2762,8 +2762,7 @@ int git_config_rename_section_in_file(const char *config_filename,
2762 if (!config_filename)
2763 config_filename = filename_buf = git_pathdup("config");
2764
2765 - lock = xcalloc(1, sizeof(struct lock_file));
2766 - out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2765 + out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
2766 if (out_fd < 0) {
2767 ret = error("could not lock config file %s", config_filename);
2768 goto out;
@@ -2782,9 +2781,9 @@ int git_config_rename_section_in_file(const char *config_filename,
2781 goto out;
2782 }
2783
2785 - if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2784 + if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
2785 ret = error_errno("chmod on %s failed",
2787 - get_lock_file_path(lock));
2786 + get_lock_file_path(&lock));
2787 goto out;
2788 }
2789
@@ -2805,7 +2804,7 @@ int git_config_rename_section_in_file(const char *config_filename,
2804 }
2805 store.baselen = strlen(new_name);
2806 if (write_section(out_fd, new_name) < 0) {
2808 - ret = write_error(get_lock_file_path(lock));
2807 + ret = write_error(get_lock_file_path(&lock));
2808 goto out;
2809 }
2810 /*
@@ -2831,20 +2830,20 @@ int git_config_rename_section_in_file(const char *config_filename,
2830 continue;
2831 length = strlen(output);
2832 if (write_in_full(out_fd, output, length) < 0) {
2834 - ret = write_error(get_lock_file_path(lock));
2833 + ret = write_error(get_lock_file_path(&lock));
2834 goto out;
2835 }
2836 }
2837 fclose(config_file);
2838 config_file = NULL;
2839 commit_and_out:
2841 - if (commit_lock_file(lock) < 0)
2840 + if (commit_lock_file(&lock) < 0)
2841 ret = error_errno("could not write config file %s",
2842 config_filename);
2843 out:
2844 if (config_file)
2845 fclose(config_file);
2847 - rollback_lock_file(lock);
2846 + rollback_lock_file(&lock);
2847 out_no_rollback:
2848 free(filename_buf);
2849 return ret;
merge-recursive.c
+3 -3
@@ -2162,7 +2162,7 @@ int merge_recursive_generic(struct merge_options *o,
2162 struct commit **result)
2163 {
2164 int clean;
2165 - struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
2165 + struct lock_file lock = LOCK_INIT;
2166 struct commit *head_commit = get_ref(head, o->branch1);
2167 struct commit *next_commit = get_ref(merge, o->branch2);
2168 struct commit_list *ca = NULL;
@@ -2178,14 +2178,14 @@ int merge_recursive_generic(struct merge_options *o,
2178 }
2179 }
2180
2181 - hold_locked_index(lock, LOCK_DIE_ON_ERROR);
2181 + hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
2182 clean = merge_recursive(o, head_commit, next_commit, ca,
2183 result);
2184 if (clean < 0)
2185 return clean;
2186
2187 if (active_cache_changed &&
2188 - write_locked_index(&the_index, lock, COMMIT_LOCK))
2188 + write_locked_index(&the_index, &lock, COMMIT_LOCK))
2189 return err(o, _("Unable to write index."));
2190
2191 return clean ? 0 : 1;
merge.c
+4 -4
@@ -53,11 +53,11 @@ int checkout_fast_forward(const struct object_id *head,
53 struct tree_desc t[MAX_UNPACK_TREES];
54 int i, nr_trees = 0;
55 struct dir_struct dir;
56 - struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
56 + struct lock_file lock_file = LOCK_INIT;
57
58 refresh_cache(REFRESH_QUIET);
59
60 - if (hold_locked_index(lock_file, LOCK_REPORT_ON_ERROR) < 0)
60 + if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
61 return -1;
62
63 memset(&trees, 0, sizeof(trees));
@@ -91,8 +91,8 @@ int checkout_fast_forward(const struct object_id *head,
91 }
92 if (unpack_trees(nr_trees, t, &opts))
93 return -1;
94 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) {
95 - rollback_lock_file(lock_file);
94 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) {
95 + rollback_lock_file(&lock_file);
96 return error(_("unable to write new index file"));
97 }
98 return 0;
wt-status.c
+4 -4
@@ -2299,14 +2299,14 @@ int has_uncommitted_changes(int ignore_submodules)
2299 */
2300 int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2301 {
2302 - struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
2302 + struct lock_file lock_file = LOCK_INIT;
2303 int err = 0, fd;
2304
2305 - fd = hold_locked_index(lock_file, 0);
2305 + fd = hold_locked_index(&lock_file, 0);
2306 refresh_cache(REFRESH_QUIET);
2307 if (0 <= fd)
2308 - update_index_if_able(&the_index, lock_file);
2309 - rollback_lock_file(lock_file);
2308 + update_index_if_able(&the_index, &lock_file);
2309 + rollback_lock_file(&lock_file);
2310
2311 if (has_unstaged_changes(ignore_submodules)) {
2312 /* TRANSLATORS: the action is e.g. "pull with rebase" */