repository.c: free the "path cache" in repo_clear()

The "struct path_cache" added in 102de880d24 (path.c: migrate global git_path_* to take a repository argument, 2018-05-17) is only used directly by code in repository.[ch] (but populated in path.[ch]). Let's move this code to repository.[ch], and stop leaking this memory when we run repo_clear(). To avoid the cast change it from a "const char *" to a "char *". This also removes the "PATH_CACHE_INIT" macro, which has never been used for anything. For the "struct repository" we already make a hard assumption that it (and "the_repository") can be identically initialized by making it a "static" variable, so making use of a "PATH_CACHE_INIT" somewhere would have been confusing. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 4, 2022 at 19:32 UTC 759f34073807119ffb935a84aa86e6a8fa7a9bc4
3 files changed +29 -15
path.h
-14
@@ -169,20 +169,6 @@ void report_linked_checkout_garbage(void);
169 return r->cached_paths.var; \
170 }
171
172 -struct path_cache {
173 - const char *squash_msg;
174 - const char *merge_msg;
175 - const char *merge_rr;
176 - const char *merge_mode;
177 - const char *merge_head;
178 - const char *merge_autostash;
179 - const char *auto_merge;
180 - const char *fetch_head;
181 - const char *shallow;
182 -};
183 -
184 -#define PATH_CACHE_INIT { 0 }
185 -
172 const char *git_path_squash_msg(struct repository *r);
173 const char *git_path_merge_msg(struct repository *r);
174 const char *git_path_merge_rr(struct repository *r);
repository.c
+16
@@ -240,6 +240,20 @@ out:
240 return ret;
241 }
242
243 +static void repo_clear_path_cache(struct repo_path_cache *cache)
244 +{
245 + FREE_AND_NULL(cache->squash_msg);
246 + FREE_AND_NULL(cache->squash_msg);
247 + FREE_AND_NULL(cache->merge_msg);
248 + FREE_AND_NULL(cache->merge_rr);
249 + FREE_AND_NULL(cache->merge_mode);
250 + FREE_AND_NULL(cache->merge_head);
251 + FREE_AND_NULL(cache->merge_autostash);
252 + FREE_AND_NULL(cache->auto_merge);
253 + FREE_AND_NULL(cache->fetch_head);
254 + FREE_AND_NULL(cache->shallow);
255 +}
256 +
257 void repo_clear(struct repository *repo)
258 {
259 FREE_AND_NULL(repo->gitdir);
@@ -280,6 +294,8 @@ void repo_clear(struct repository *repo)
294 remote_state_clear(repo->remote_state);
295 FREE_AND_NULL(repo->remote_state);
296 }
297 +
298 + repo_clear_path_cache(&repo->cached_paths);
299 }
300
301 int repo_read_index(struct repository *repo)
repository.h
+13 -1
@@ -44,6 +44,18 @@ struct repo_settings {
44 int core_multi_pack_index;
45 };
46
47 +struct repo_path_cache {
48 + char *squash_msg;
49 + char *merge_msg;
50 + char *merge_rr;
51 + char *merge_mode;
52 + char *merge_head;
53 + char *merge_autostash;
54 + char *auto_merge;
55 + char *fetch_head;
56 + char *shallow;
57 +};
58 +
59 struct repository {
60 /* Environment */
61 /*
@@ -82,7 +94,7 @@ struct repository {
94 /*
95 * Contains path to often used file names.
96 */
85 - struct path_cache cached_paths;
97 + struct repo_path_cache cached_paths;
98
99 /*
100 * Path to the repository's graft file.