Skip to content

Commit e00d1a4

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

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

refs/files-backend.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static void add_packed_ref(struct files_ref_store *refs,
432432
*/
433433
void read_loose_refs(const char *dirname, struct ref_dir *dir)
434434
{
435-
struct files_ref_store *refs = dir->ref_store;
435+
struct files_ref_store *refs = dir->cache->ref_store;
436436
DIR *d;
437437
struct dirent *de;
438438
int dirnamelen = strlen(dirname);
@@ -468,7 +468,7 @@ void read_loose_refs(const char *dirname, struct ref_dir *dir)
468468
} else if (S_ISDIR(st.st_mode)) {
469469
strbuf_addch(&refname, '/');
470470
add_entry_to_dir(dir,
471-
create_dir_entry(refs, refname.buf,
471+
create_dir_entry(dir->cache, refname.buf,
472472
refname.len, 1));
473473
} else {
474474
if (!refs_resolve_ref_unsafe(&refs->base,
@@ -525,7 +525,7 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
525525
* lazily):
526526
*/
527527
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));
529529
}
530530
return get_ref_dir(refs->loose->root);
531531
}

refs/ref-cache.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry)
3636
int pos = search_ref_dir(dir, "refs/bisect/", 12);
3737
if (pos < 0) {
3838
struct ref_entry *child_entry;
39-
child_entry = create_dir_entry(dir->ref_store,
39+
child_entry = create_dir_entry(dir->cache,
4040
"refs/bisect/",
4141
12, 1);
4242
add_entry_to_dir(dir, child_entry);
@@ -67,7 +67,8 @@ struct ref_cache *create_ref_cache(struct files_ref_store *refs)
6767
{
6868
struct ref_cache *ret = xcalloc(1, sizeof(*ret));
6969

70-
ret->root = create_dir_entry(refs, "", 0, 1);
70+
ret->ref_store = refs;
71+
ret->root = create_dir_entry(ret, "", 0, 1);
7172
return ret;
7273
}
7374

@@ -104,13 +105,14 @@ static void clear_ref_dir(struct ref_dir *dir)
104105
dir->entries = NULL;
105106
}
106107

107-
struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
108+
struct ref_entry *create_dir_entry(struct ref_cache *cache,
108109
const char *dirname, size_t len,
109110
int incomplete)
110111
{
111112
struct ref_entry *direntry;
113+
112114
FLEX_ALLOC_MEM(direntry, name, dirname, len);
113-
direntry->u.subdir.ref_store = ref_store;
115+
direntry->u.subdir.cache = cache;
114116
direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
115117
return direntry;
116118
}
@@ -181,7 +183,7 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
181183
* therefore, create an empty record for it but mark
182184
* the record complete.
183185
*/
184-
entry = create_dir_entry(dir->ref_store, subdirname, len, 0);
186+
entry = create_dir_entry(dir->cache, subdirname, len, 0);
185187
add_entry_to_dir(dir, entry);
186188
} else {
187189
entry = dir->entries[entry_index];

refs/ref-cache.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
struct ref_cache {
55
struct ref_entry *root;
6+
7+
/* A pointer to the files_ref_store whose cache this is: */
8+
struct files_ref_store *ref_store;
69
};
710

811
/*
@@ -66,8 +69,8 @@ struct ref_dir {
6669
*/
6770
int sorted;
6871

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;
7174

7275
struct ref_entry **entries;
7376
};
@@ -161,7 +164,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry);
161164
* dirname is the name of the directory with a trailing slash (e.g.,
162165
* "refs/heads/") or "" for the top-level directory.
163166
*/
164-
struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
167+
struct ref_entry *create_dir_entry(struct ref_cache *cache,
165168
const char *dirname, size_t len,
166169
int incomplete);
167170

0 commit comments

Comments
 (0)