Skip to content

Commit 099a912

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28ed983 commit 099a912

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

refs/files-backend.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
209209
}
210210

211211
/*
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.
213215
*
214216
* A comment line of the form "# pack-refs with: " may contain zero or
215217
* 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)
235237
* compatibility with older clients, but we do not require it
236238
* (i.e., "peeled" is a no-op if "fully-peeled" is set).
237239
*/
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)
239241
{
242+
FILE *f;
243+
struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
240244
struct ref_entry *last = NULL;
241245
struct strbuf line = STRBUF_INIT;
242246
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
247+
struct ref_dir *dir;
243248

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);
244260
while (strbuf_getwholeline(&line, f, '\n') != EOF) {
245261
struct object_id oid;
246262
const char *refname;
@@ -287,7 +303,10 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
287303
}
288304
}
289305

306+
fclose(f);
290307
strbuf_release(&line);
308+
309+
return packed_refs;
291310
}
292311

293312
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
357376
!stat_validity_check(&refs->packed->validity, packed_refs_file))
358377
clear_packed_ref_cache(refs);
359378

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+
374382
return refs->packed;
375383
}
376384

refs/ref-cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ struct ref_entry *create_ref_entry(const char *refname,
194194
* function called to fill in incomplete directories in the
195195
* `ref_cache` when they are accessed. If it is NULL, then the whole
196196
* `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.
198199
*/
199200
struct ref_cache *create_ref_cache(struct ref_store *refs,
200201
fill_ref_dir_fn *fill_ref_dir);

0 commit comments

Comments
 (0)