Access reference caches only through new function get_cached_refs()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 13, 2011 at 00:36 UTC 4349a66805129ba11121b8a5eaf3f81cfd30bdd5
1 file changed +32 -22
refs.c
+32 -22
@@ -181,9 +181,26 @@ static void clear_cached_refs(struct cached_refs *ca)
181 ca->did_loose = ca->did_packed = 0;
182 }
183
184 +/*
185 + * Return a pointer to a cached_refs for the specified submodule. For
186 + * the main repository, use submodule==NULL. The returned structure
187 + * will be allocated and initialized but not necessarily populated; it
188 + * should not be freed.
189 + */
190 +static struct cached_refs *get_cached_refs(const char *submodule)
191 +{
192 + if (!submodule)
193 + return &cached_refs;
194 + else {
195 + /* For now, don't reuse the refs cache for submodules. */
196 + clear_cached_refs(&submodule_refs);
197 + return &submodule_refs;
198 + }
199 +}
200 +
201 static void invalidate_cached_refs(void)
202 {
186 - clear_cached_refs(&cached_refs);
203 + clear_cached_refs(get_cached_refs(NULL));
204 }
205
206 static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
@@ -234,20 +251,17 @@ void clear_extra_refs(void)
251
252 static struct ref_list *get_packed_refs(const char *submodule)
253 {
237 - const char *packed_refs_file;
238 - struct cached_refs *refs;
254 + struct cached_refs *refs = get_cached_refs(submodule);
255
240 - if (submodule) {
241 - packed_refs_file = git_path_submodule(submodule, "packed-refs");
242 - refs = &submodule_refs;
243 - free_ref_list(refs->packed);
244 - } else {
245 - packed_refs_file = git_path("packed-refs");
246 - refs = &cached_refs;
247 - }
256 + if (!refs->did_packed) {
257 + const char *packed_refs_file;
258 + FILE *f;
259
249 - if (!refs->did_packed || submodule) {
250 - FILE *f = fopen(packed_refs_file, "r");
260 + if (submodule)
261 + packed_refs_file = git_path_submodule(submodule, "packed-refs");
262 + else
263 + packed_refs_file = git_path("packed-refs");
264 + f = fopen(packed_refs_file, "r");
265 refs->packed = NULL;
266 if (f) {
267 read_packed_refs(f, refs);
@@ -361,17 +375,13 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
375
376 static struct ref_list *get_loose_refs(const char *submodule)
377 {
364 - if (submodule) {
365 - free_ref_list(submodule_refs.loose);
366 - submodule_refs.loose = get_ref_dir(submodule, "refs", NULL);
367 - return submodule_refs.loose;
368 - }
378 + struct cached_refs *refs = get_cached_refs(submodule);
379
370 - if (!cached_refs.did_loose) {
371 - cached_refs.loose = get_ref_dir(NULL, "refs", NULL);
372 - cached_refs.did_loose = 1;
380 + if (!refs->did_loose) {
381 + refs->loose = get_ref_dir(submodule, "refs", NULL);
382 + refs->did_loose = 1;
383 }
374 - return cached_refs.loose;
384 + return refs->loose;
385 }
386
387 /* We allow "recursive" symbolic refs. Only within reason, though */