Skip to content

Commit 2f18b46

Browse files
committed
Merge branch 'mh/ref-api'
* mh/ref-api: clear_ref_cache(): inline function write_ref_sha1(): only invalidate the loose ref cache clear_ref_cache(): extract two new functions clear_ref_cache(): rename parameter invalidate_ref_cache(): expose this function in the refs API invalidate_ref_cache(): take the submodule as parameter invalidate_ref_cache(): rename function from invalidate_cached_refs()
2 parents 470bbbc + c5f29ab commit 2f18b46

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

refs.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ static struct ref_entry *search_ref_array(struct ref_array *array, const char *n
134134
* Future: need to be in "struct repository"
135135
* when doing a full libification.
136136
*/
137-
static struct cached_refs {
138-
struct cached_refs *next;
137+
static struct ref_cache {
138+
struct ref_cache *next;
139139
char did_loose;
140140
char did_packed;
141141
struct ref_array loose;
142142
struct ref_array packed;
143143
/* The submodule name, or "" for the main repo. */
144144
char name[FLEX_ARRAY];
145-
} *cached_refs;
145+
} *ref_cache;
146146

147147
static struct ref_entry *current_ref;
148148

@@ -158,36 +158,41 @@ static void free_ref_array(struct ref_array *array)
158158
array->refs = NULL;
159159
}
160160

161-
static void clear_cached_refs(struct cached_refs *ca)
161+
static void clear_packed_ref_cache(struct ref_cache *refs)
162162
{
163-
if (ca->did_loose)
164-
free_ref_array(&ca->loose);
165-
if (ca->did_packed)
166-
free_ref_array(&ca->packed);
167-
ca->did_loose = ca->did_packed = 0;
163+
if (refs->did_packed)
164+
free_ref_array(&refs->packed);
165+
refs->did_packed = 0;
168166
}
169167

170-
static struct cached_refs *create_cached_refs(const char *submodule)
168+
static void clear_loose_ref_cache(struct ref_cache *refs)
169+
{
170+
if (refs->did_loose)
171+
free_ref_array(&refs->loose);
172+
refs->did_loose = 0;
173+
}
174+
175+
static struct ref_cache *create_ref_cache(const char *submodule)
171176
{
172177
int len;
173-
struct cached_refs *refs;
178+
struct ref_cache *refs;
174179
if (!submodule)
175180
submodule = "";
176181
len = strlen(submodule) + 1;
177-
refs = xcalloc(1, sizeof(struct cached_refs) + len);
182+
refs = xcalloc(1, sizeof(struct ref_cache) + len);
178183
memcpy(refs->name, submodule, len);
179184
return refs;
180185
}
181186

182187
/*
183-
* Return a pointer to a cached_refs for the specified submodule. For
188+
* Return a pointer to a ref_cache for the specified submodule. For
184189
* the main repository, use submodule==NULL. The returned structure
185190
* will be allocated and initialized but not necessarily populated; it
186191
* should not be freed.
187192
*/
188-
static struct cached_refs *get_cached_refs(const char *submodule)
193+
static struct ref_cache *get_ref_cache(const char *submodule)
189194
{
190-
struct cached_refs *refs = cached_refs;
195+
struct ref_cache *refs = ref_cache;
191196
if (!submodule)
192197
submodule = "";
193198
while (refs) {
@@ -196,19 +201,17 @@ static struct cached_refs *get_cached_refs(const char *submodule)
196201
refs = refs->next;
197202
}
198203

199-
refs = create_cached_refs(submodule);
200-
refs->next = cached_refs;
201-
cached_refs = refs;
204+
refs = create_ref_cache(submodule);
205+
refs->next = ref_cache;
206+
ref_cache = refs;
202207
return refs;
203208
}
204209

205-
static void invalidate_cached_refs(void)
210+
void invalidate_ref_cache(const char *submodule)
206211
{
207-
struct cached_refs *refs = cached_refs;
208-
while (refs) {
209-
clear_cached_refs(refs);
210-
refs = refs->next;
211-
}
212+
struct ref_cache *refs = get_ref_cache(submodule);
213+
clear_packed_ref_cache(refs);
214+
clear_loose_ref_cache(refs);
212215
}
213216

214217
static void read_packed_refs(FILE *f, struct ref_array *array)
@@ -257,7 +260,7 @@ void clear_extra_refs(void)
257260

258261
static struct ref_array *get_packed_refs(const char *submodule)
259262
{
260-
struct cached_refs *refs = get_cached_refs(submodule);
263+
struct ref_cache *refs = get_ref_cache(submodule);
261264

262265
if (!refs->did_packed) {
263266
const char *packed_refs_file;
@@ -379,7 +382,7 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
379382

380383
static struct ref_array *get_loose_refs(const char *submodule)
381384
{
382-
struct cached_refs *refs = get_cached_refs(submodule);
385+
struct ref_cache *refs = get_ref_cache(submodule);
383386

384387
if (!refs->did_loose) {
385388
get_ref_dir(submodule, "refs", &refs->loose);
@@ -1238,7 +1241,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
12381241
ret |= repack_without_ref(refname);
12391242

12401243
unlink_or_warn(git_path("logs/%s", lock->ref_name));
1241-
invalidate_cached_refs();
1244+
invalidate_ref_cache(NULL);
12421245
unlock_ref(lock);
12431246
return ret;
12441247
}
@@ -1529,7 +1532,7 @@ int write_ref_sha1(struct ref_lock *lock,
15291532
unlock_ref(lock);
15301533
return -1;
15311534
}
1532-
invalidate_cached_refs();
1535+
clear_loose_ref_cache(get_ref_cache(NULL));
15331536
if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
15341537
(strcmp(lock->ref_name, lock->orig_ref_name) &&
15351538
log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {

refs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ extern void unlock_ref(struct ref_lock *lock);
8080
/** Writes sha1 into the ref specified by the lock. **/
8181
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
8282

83+
/*
84+
* Invalidate the reference cache for the specified submodule. Use
85+
* submodule=NULL to invalidate the cache for the main module. This
86+
* function must be called if references are changed via a mechanism
87+
* other than the refs API.
88+
*/
89+
extern void invalidate_ref_cache(const char *submodule);
90+
8391
/** Setup reflog before using. **/
8492
int log_ref_setup(const char *ref_name, char *logfile, int bufsize);
8593

0 commit comments

Comments
 (0)