refs: record the ref_store in ref_cache, not ref_dir

Instead of keeping a pointer to the `ref_store` in every `ref_dir` entry, store it once in `struct ref_cache`, and change `struct ref_dir` to include a pointer to its containing `ref_cache` instead. This makes it easier to add to the information that is accessible from a `ref_dir` without increasing the size of every `ref_dir` instance. Note that previously, every `ref_dir` pointed at the containing `files_ref_store` regardless of whether it was a part of the loose or packed reference cache. Now we have to be sure to initialize the instances to point at the correct containing `ref_cache`. So change `create_dir_entry()` to take a `ref_cache` parameter, and change its callers to pass the correct `ref_cache` depending on the purpose of the new `dir_entry`. 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 e00d1a4ff7d79b403bf97a0746138d584c9ebf9a
3 files changed +16 -11
refs/files-backend.c
+3 -3
@@ -432,7 +432,7 @@ static void add_packed_ref(struct files_ref_store *refs,
432 */
433 void read_loose_refs(const char *dirname, struct ref_dir *dir)
434 {
435 - struct files_ref_store *refs = dir->ref_store;
435 + struct files_ref_store *refs = dir->cache->ref_store;
436 DIR *d;
437 struct dirent *de;
438 int dirnamelen = strlen(dirname);
@@ -468,7 +468,7 @@ void read_loose_refs(const char *dirname, struct ref_dir *dir)
468 } else if (S_ISDIR(st.st_mode)) {
469 strbuf_addch(&refname, '/');
470 add_entry_to_dir(dir,
471 - create_dir_entry(refs, refname.buf,
471 + create_dir_entry(dir->cache, refname.buf,
472 refname.len, 1));
473 } else {
474 if (!refs_resolve_ref_unsafe(&refs->base,
@@ -525,7 +525,7 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
525 * lazily):
526 */
527 add_entry_to_dir(get_ref_dir(refs->loose->root),
528 - create_dir_entry(refs, "refs/", 5, 1));
528 + create_dir_entry(refs->loose, "refs/", 5, 1));
529 }
530 return get_ref_dir(refs->loose->root);
531 }
refs/ref-cache.c
+7 -5
@@ -36,7 +36,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry)
36 int pos = search_ref_dir(dir, "refs/bisect/", 12);
37 if (pos < 0) {
38 struct ref_entry *child_entry;
39 - child_entry = create_dir_entry(dir->ref_store,
39 + child_entry = create_dir_entry(dir->cache,
40 "refs/bisect/",
41 12, 1);
42 add_entry_to_dir(dir, child_entry);
@@ -67,7 +67,8 @@ 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);
70 + ret->ref_store = refs;
71 + ret->root = create_dir_entry(ret, "", 0, 1);
72 return ret;
73 }
74
@@ -104,13 +105,14 @@ static void clear_ref_dir(struct ref_dir *dir)
105 dir->entries = NULL;
106 }
107
107 -struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
108 +struct ref_entry *create_dir_entry(struct ref_cache *cache,
109 const char *dirname, size_t len,
110 int incomplete)
111 {
112 struct ref_entry *direntry;
113 +
114 FLEX_ALLOC_MEM(direntry, name, dirname, len);
113 - direntry->u.subdir.ref_store = ref_store;
115 + direntry->u.subdir.cache = cache;
116 direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
117 return direntry;
118 }
@@ -181,7 +183,7 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
183 * therefore, create an empty record for it but mark
184 * the record complete.
185 */
184 - entry = create_dir_entry(dir->ref_store, subdirname, len, 0);
186 + entry = create_dir_entry(dir->cache, subdirname, len, 0);
187 add_entry_to_dir(dir, entry);
188 } else {
189 entry = dir->entries[entry_index];
refs/ref-cache.h
+6 -3
@@ -3,6 +3,9 @@
3
4 struct ref_cache {
5 struct ref_entry *root;
6 +
7 + /* A pointer to the files_ref_store whose cache this is: */
8 + struct files_ref_store *ref_store;
9 };
10
11 /*
@@ -66,8 +69,8 @@ struct ref_dir {
69 */
70 int sorted;
71
69 - /* A pointer to the files_ref_store that contains this ref_dir. */
70 - struct files_ref_store *ref_store;
72 + /* The ref_cache containing this entry: */
73 + struct ref_cache *cache;
74
75 struct ref_entry **entries;
76 };
@@ -161,7 +164,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry);
164 * dirname is the name of the directory with a trailing slash (e.g.,
165 * "refs/heads/") or "" for the top-level directory.
166 */
164 -struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
167 +struct ref_entry *create_dir_entry(struct ref_cache *cache,
168 const char *dirname, size_t len,
169 int incomplete);
170