Skip to content

Commit 7c22bc8

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

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

refs/files-backend.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static int entry_resolves_to_object(struct ref_entry *entry)
4444
}
4545

4646
struct packed_ref_cache {
47-
struct ref_entry *root;
47+
struct ref_cache *cache;
4848

4949
/*
5050
* Count of references to the data structure in this instance,
@@ -79,7 +79,7 @@ struct files_ref_store {
7979
char *gitcommondir;
8080
char *packed_refs_path;
8181

82-
struct ref_entry *loose;
82+
struct ref_cache *loose;
8383
struct packed_ref_cache *packed;
8484
};
8585

@@ -101,7 +101,7 @@ static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
101101
static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
102102
{
103103
if (!--packed_refs->referrers) {
104-
free_ref_entry(packed_refs->root);
104+
free_ref_cache(packed_refs->cache);
105105
stat_validity_clear(&packed_refs->validity);
106106
free(packed_refs);
107107
return 1;
@@ -125,7 +125,7 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)
125125
static void clear_loose_ref_cache(struct files_ref_store *refs)
126126
{
127127
if (refs->loose) {
128-
free_ref_entry(refs->loose);
128+
free_ref_cache(refs->loose);
129129
refs->loose = NULL;
130130
}
131131
}
@@ -386,11 +386,12 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
386386

387387
refs->packed = xcalloc(1, sizeof(*refs->packed));
388388
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;
390391
f = fopen(packed_refs_file, "r");
391392
if (f) {
392393
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));
394395
fclose(f);
395396
}
396397
}
@@ -399,7 +400,7 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
399400

400401
static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
401402
{
402-
return get_ref_dir(packed_ref_cache->root);
403+
return get_ref_dir(packed_ref_cache->cache->root);
403404
}
404405

405406
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)
514515
* are about to read the only subdirectory that can
515516
* hold references:
516517
*/
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+
518523
/*
519-
* Create an incomplete entry for "refs/":
524+
* Add an incomplete entry for "refs/" (to be filled
525+
* lazily):
520526
*/
521-
add_entry_to_dir(get_ref_dir(refs->loose),
527+
add_entry_to_dir(get_ref_dir(refs->loose->root),
522528
create_dir_entry(refs, "refs/", 5, 1));
523529
}
524-
return get_ref_dir(refs->loose);
530+
return get_ref_dir(refs->loose->root);
525531
}
526532

527533
/*

refs/ref-cache.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ struct ref_entry *create_ref_entry(const char *refname,
6363
return ref;
6464
}
6565

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+
6674
static void clear_ref_dir(struct ref_dir *dir);
6775

68-
void free_ref_entry(struct ref_entry *entry)
76+
static void free_ref_entry(struct ref_entry *entry)
6977
{
7078
if (entry->flag & REF_DIR) {
7179
/*
@@ -77,6 +85,12 @@ void free_ref_entry(struct ref_entry *entry)
7785
free(entry);
7886
}
7987

88+
void free_ref_cache(struct ref_cache *cache)
89+
{
90+
free_ref_entry(cache->root);
91+
free(cache);
92+
}
93+
8094
/*
8195
* Clear and free all entries in dir, recursively.
8296
*/

refs/ref-cache.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef REFS_REF_CACHE_H
22
#define REFS_REF_CACHE_H
33

4+
struct ref_cache {
5+
struct ref_entry *root;
6+
};
7+
48
/*
59
* Information used (along with the information in ref_entry) to
610
* describe a single cached reference. This data structure only
@@ -165,7 +169,16 @@ struct ref_entry *create_ref_entry(const char *refname,
165169
const unsigned char *sha1, int flag,
166170
int check_name);
167171

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);
169182

170183
/*
171184
* Add a ref_entry to the end of dir (unsorted). Entry is always

0 commit comments

Comments
 (0)