packed_ref_store: new struct

Start extracting the packed-refs-related data structures into a new class, `packed_ref_store`. It doesn't yet implement `ref_store`, but it will. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 23, 2017 at 09:01 UTC bdf55fa6b23b14ac8726fc29182739762b8f6011
1 file changed +33 -9
refs/files-backend.c
+33 -9
@@ -47,6 +47,28 @@ struct packed_ref_cache {
47 struct stat_validity validity;
48 };
49
50 +/*
51 + * A container for `packed-refs`-related data. It is not (yet) a
52 + * `ref_store`.
53 + */
54 +struct packed_ref_store {
55 + unsigned int store_flags;
56 +
57 + /*
58 + * A cache of the values read from the `packed-refs` file, if
59 + * it might still be current; otherwise, NULL.
60 + */
61 + struct packed_ref_cache *cache;
62 +};
63 +
64 +static struct packed_ref_store *packed_ref_store_create(unsigned int store_flags)
65 +{
66 + struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
67 +
68 + refs->store_flags = store_flags;
69 + return refs;
70 +}
71 +
72 /*
73 * Future: need to be in "struct repository"
74 * when doing a full libification.
@@ -60,13 +82,14 @@ struct files_ref_store {
82 char *packed_refs_path;
83
84 struct ref_cache *loose;
63 - struct packed_ref_cache *packed;
85
86 /*
87 * Lock used for the "packed-refs" file. Note that this (and
88 * thus the enclosing `files_ref_store`) must not be freed.
89 */
90 struct lock_file packed_refs_lock;
91 +
92 + struct packed_ref_store *packed_ref_store;
93 };
94
95 /*
@@ -95,12 +118,12 @@ static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
118
119 static void clear_packed_ref_cache(struct files_ref_store *refs)
120 {
98 - if (refs->packed) {
99 - struct packed_ref_cache *packed_refs = refs->packed;
121 + if (refs->packed_ref_store->cache) {
122 + struct packed_ref_cache *packed_refs = refs->packed_ref_store->cache;
123
124 if (is_lock_file_locked(&refs->packed_refs_lock))
125 die("BUG: packed-ref cache cleared while locked");
103 - refs->packed = NULL;
126 + refs->packed_ref_store->cache = NULL;
127 release_packed_ref_cache(packed_refs);
128 }
129 }
@@ -132,6 +155,7 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
155 refs->gitcommondir = strbuf_detach(&sb, NULL);
156 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
157 refs->packed_refs_path = strbuf_detach(&sb, NULL);
158 + refs->packed_ref_store = packed_ref_store_create(flags);
159
160 return ref_store;
161 }
@@ -375,8 +399,8 @@ static void files_ref_path(struct files_ref_store *refs,
399 */
400 static void validate_packed_ref_cache(struct files_ref_store *refs)
401 {
378 - if (refs->packed &&
379 - !stat_validity_check(&refs->packed->validity,
402 + if (refs->packed_ref_store->cache &&
403 + !stat_validity_check(&refs->packed_ref_store->cache->validity,
404 files_packed_refs_path(refs)))
405 clear_packed_ref_cache(refs);
406 }
@@ -396,10 +420,10 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
420 if (!is_lock_file_locked(&refs->packed_refs_lock))
421 validate_packed_ref_cache(refs);
422
399 - if (!refs->packed)
400 - refs->packed = read_packed_refs(packed_refs_file);
423 + if (!refs->packed_ref_store->cache)
424 + refs->packed_ref_store->cache = read_packed_refs(packed_refs_file);
425
402 - return refs->packed;
426 + return refs->packed_ref_store->cache;
427 }
428
429 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)