read_packed_refs(): do more of the work of reading packed refs

Teach `read_packed_refs()` to also * Allocate and initialize the new `packed_ref_cache` * Open and close the `packed-refs` file * Update the `validity` field of the new object This decreases the coupling between `packed_refs_cache` and `files_ref_store` by a little bit. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed May 22, 2017 at 16:17 UTC 099a912a279415dd27716ee5de07ff347bfc49da
2 files changed +26 -17
refs/files-backend.c
+24 -16
@@ -209,7 +209,9 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
209 }
210
211 /*
212 - * Read f, which is a packed-refs file, into dir.
212 + * Read from `packed_refs_file` into a newly-allocated
213 + * `packed_ref_cache` and return it. The return value will already
214 + * have its reference count incremented.
215 *
216 * A comment line of the form "# pack-refs with: " may contain zero or
217 * more traits. We interpret the traits as follows:
@@ -235,12 +237,26 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
237 * compatibility with older clients, but we do not require it
238 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
239 */
238 -static void read_packed_refs(FILE *f, struct ref_dir *dir)
240 +static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
241 {
242 + FILE *f;
243 + struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
244 struct ref_entry *last = NULL;
245 struct strbuf line = STRBUF_INIT;
246 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
247 + struct ref_dir *dir;
248
249 + acquire_packed_ref_cache(packed_refs);
250 + packed_refs->cache = create_ref_cache(NULL, NULL);
251 + packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
252 +
253 + f = fopen(packed_refs_file, "r");
254 + if (!f)
255 + return packed_refs;
256 +
257 + stat_validity_update(&packed_refs->validity, fileno(f));
258 +
259 + dir = get_ref_dir(packed_refs->cache->root);
260 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
261 struct object_id oid;
262 const char *refname;
@@ -287,7 +303,10 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
303 }
304 }
305
306 + fclose(f);
307 strbuf_release(&line);
308 +
309 + return packed_refs;
310 }
311
312 static const char *files_packed_refs_path(struct files_ref_store *refs)
@@ -357,20 +376,9 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
376 !stat_validity_check(&refs->packed->validity, packed_refs_file))
377 clear_packed_ref_cache(refs);
378
360 - if (!refs->packed) {
361 - FILE *f;
362 -
363 - refs->packed = xcalloc(1, sizeof(*refs->packed));
364 - acquire_packed_ref_cache(refs->packed);
365 - refs->packed->cache = create_ref_cache(&refs->base, NULL);
366 - refs->packed->cache->root->flag &= ~REF_INCOMPLETE;
367 - f = fopen(packed_refs_file, "r");
368 - if (f) {
369 - stat_validity_update(&refs->packed->validity, fileno(f));
370 - read_packed_refs(f, get_ref_dir(refs->packed->cache->root));
371 - fclose(f);
372 - }
373 - }
379 + if (!refs->packed)
380 + refs->packed = read_packed_refs(packed_refs_file);
381 +
382 return refs->packed;
383 }
384
refs/ref-cache.h
+2 -1
@@ -194,7 +194,8 @@ struct ref_entry *create_ref_entry(const char *refname,
194 * function called to fill in incomplete directories in the
195 * `ref_cache` when they are accessed. If it is NULL, then the whole
196 * `ref_cache` must be filled (including clearing its directories'
197 - * `REF_INCOMPLETE` bits) before it is used.
197 + * `REF_INCOMPLETE` bits) before it is used, and `refs` can be NULL,
198 + * too.
199 */
200 struct ref_cache *create_ref_cache(struct ref_store *refs,
201 fill_ref_dir_fn *fill_ref_dir);