ref-cache: introduce a new type, ref_cache

For now, it just wraps a `ref_entry *` that points at the root of the tree. Soon it will hold more information. Add two new functions, `create_ref_cache()` and `free_ref_cache()`. Make `free_ref_entry()` private. Change files-backend to use this type to hold its caches. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 16, 2017 at 08:41 UTC 7c22bc8a18744cbd79436a8ce598a0dd5d49869b
3 files changed +46 -13
refs/files-backend.c
+17 -11
@@ -44,7 +44,7 @@ static int entry_resolves_to_object(struct ref_entry *entry)
44 }
45
46 struct packed_ref_cache {
47 - struct ref_entry *root;
47 + struct ref_cache *cache;
48
49 /*
50 * Count of references to the data structure in this instance,
@@ -79,7 +79,7 @@ struct files_ref_store {
79 char *gitcommondir;
80 char *packed_refs_path;
81
82 - struct ref_entry *loose;
82 + struct ref_cache *loose;
83 struct packed_ref_cache *packed;
84 };
85
@@ -101,7 +101,7 @@ static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
101 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
102 {
103 if (!--packed_refs->referrers) {
104 - free_ref_entry(packed_refs->root);
104 + free_ref_cache(packed_refs->cache);
105 stat_validity_clear(&packed_refs->validity);
106 free(packed_refs);
107 return 1;
@@ -125,7 +125,7 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)
125 static void clear_loose_ref_cache(struct files_ref_store *refs)
126 {
127 if (refs->loose) {
128 - free_ref_entry(refs->loose);
128 + free_ref_cache(refs->loose);
129 refs->loose = NULL;
130 }
131 }
@@ -386,11 +386,12 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
386
387 refs->packed = xcalloc(1, sizeof(*refs->packed));
388 acquire_packed_ref_cache(refs->packed);
389 - refs->packed->root = create_dir_entry(refs, "", 0, 0);
389 + refs->packed->cache = create_ref_cache(refs);
390 + refs->packed->cache->root->flag &= ~REF_INCOMPLETE;
391 f = fopen(packed_refs_file, "r");
392 if (f) {
393 stat_validity_update(&refs->packed->validity, fileno(f));
393 - read_packed_refs(f, get_ref_dir(refs->packed->root));
394 + read_packed_refs(f, get_ref_dir(refs->packed->cache->root));
395 fclose(f);
396 }
397 }
@@ -399,7 +400,7 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
400
401 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
402 {
402 - return get_ref_dir(packed_ref_cache->root);
403 + return get_ref_dir(packed_ref_cache->cache->root);
404 }
405
406 static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
@@ -514,14 +515,19 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
515 * are about to read the only subdirectory that can
516 * hold references:
517 */
517 - refs->loose = create_dir_entry(refs, "", 0, 0);
518 + refs->loose = create_ref_cache(refs);
519 +
520 + /* We're going to fill the top level ourselves: */
521 + refs->loose->root->flag &= ~REF_INCOMPLETE;
522 +
523 /*
519 - * Create an incomplete entry for "refs/":
524 + * Add an incomplete entry for "refs/" (to be filled
525 + * lazily):
526 */
521 - add_entry_to_dir(get_ref_dir(refs->loose),
527 + add_entry_to_dir(get_ref_dir(refs->loose->root),
528 create_dir_entry(refs, "refs/", 5, 1));
529 }
524 - return get_ref_dir(refs->loose);
530 + return get_ref_dir(refs->loose->root);
531 }
532
533 /*
refs/ref-cache.c
+15 -1
@@ -63,9 +63,17 @@ struct ref_entry *create_ref_entry(const char *refname,
63 return ref;
64 }
65
66 +struct ref_cache *create_ref_cache(struct files_ref_store *refs)
67 +{
68 + struct ref_cache *ret = xcalloc(1, sizeof(*ret));
69 +
70 + ret->root = create_dir_entry(refs, "", 0, 1);
71 + return ret;
72 +}
73 +
74 static void clear_ref_dir(struct ref_dir *dir);
75
68 -void free_ref_entry(struct ref_entry *entry)
76 +static void free_ref_entry(struct ref_entry *entry)
77 {
78 if (entry->flag & REF_DIR) {
79 /*
@@ -77,6 +85,12 @@ void free_ref_entry(struct ref_entry *entry)
85 free(entry);
86 }
87
88 +void free_ref_cache(struct ref_cache *cache)
89 +{
90 + free_ref_entry(cache->root);
91 + free(cache);
92 +}
93 +
94 /*
95 * Clear and free all entries in dir, recursively.
96 */
refs/ref-cache.h
+14 -1
@@ -1,6 +1,10 @@
1 #ifndef REFS_REF_CACHE_H
2 #define REFS_REF_CACHE_H
3
4 +struct ref_cache {
5 + struct ref_entry *root;
6 +};
7 +
8 /*
9 * Information used (along with the information in ref_entry) to
10 * describe a single cached reference. This data structure only
@@ -165,7 +169,16 @@ struct ref_entry *create_ref_entry(const char *refname,
169 const unsigned char *sha1, int flag,
170 int check_name);
171
168 -void free_ref_entry(struct ref_entry *entry);
172 +/*
173 + * Return a pointer to a new `ref_cache`. Its top-level starts out
174 + * marked incomplete.
175 + */
176 +struct ref_cache *create_ref_cache(struct files_ref_store *refs);
177 +
178 +/*
179 + * Free the `ref_cache` and all of its associated data.
180 + */
181 +void free_ref_cache(struct ref_cache *cache);
182
183 /*
184 * Add a ref_entry to the end of dir (unsorted). Entry is always