refs: minor restructuring of cached refs data.
Once we read packed and loose refs, for_each_ref() and friends kept using them even after write_ref_sha1() and delete_ref() changed the refs. This adds invalidate_cached_refs() as a way to flush the cached information. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 30, 2006 at 12:37 UTC
5e290ff75a0e6996f297dc438aceb8e1f29a20a5
1 file changed
+43
-13
refs.c
+43
-13
@@ -66,12 +66,42 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
66
return list;
67
}
68
69
-static struct ref_list *get_packed_refs(void)
69
+/*
70
+ * Future: need to be in "struct repository"
71
+ * when doing a full libification.
72
+ */
73
+struct cached_refs {
74
+ char did_loose;
75
+ char did_packed;
76
+ struct ref_list *loose;
77
+ struct ref_list *packed;
78
+} cached_refs;
79
+
80
+static void free_ref_list(struct ref_list *list)
81
+{
82
+ struct ref_list *next;
83
+ for ( ; list; list = next) {
84
+ next = list->next;
85
+ free(list);
86
+ }
87
+}
88
+
89
+static void invalidate_cached_refs(void)
90
{
71
- static int did_refs = 0;
72
- static struct ref_list *refs = NULL;
91
+ struct cached_refs *ca = &cached_refs;
92
+
93
+ if (ca->did_loose && ca->loose)
94
+ free_ref_list(ca->loose);
95
+ if (ca->did_packed && ca->packed)
96
+ free_ref_list(ca->packed);
97
+ ca->loose = ca->packed = NULL;
98
+ ca->did_loose = ca->did_packed = 0;
99
+}
100
74
- if (!did_refs) {
101
+static struct ref_list *get_packed_refs(void)
102
+{
103
+ if (!cached_refs.did_packed) {
104
+ struct ref_list *refs = NULL;
105
FILE *f = fopen(git_path("packed-refs"), "r");
106
if (f) {
107
struct ref_list *list = NULL;
@@ -86,9 +116,10 @@ static struct ref_list *get_packed_refs(void)
116
fclose(f);
117
refs = list;
118
}
89
- did_refs = 1;
119
+ cached_refs.packed = refs;
120
+ cached_refs.did_packed = 1;
121
}
91
- return refs;
122
+ return cached_refs.packed;
123
}
124
125
static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
@@ -138,14 +169,11 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
169
170
static struct ref_list *get_loose_refs(void)
171
{
141
- static int did_refs = 0;
142
- static struct ref_list *refs = NULL;
143
-
144
- if (!did_refs) {
145
- refs = get_ref_dir("refs", NULL);
146
- did_refs = 1;
172
+ if (!cached_refs.did_loose) {
173
+ cached_refs.loose = get_ref_dir("refs", NULL);
174
+ cached_refs.did_loose = 1;
175
}
148
- return refs;
176
+ return cached_refs.loose;
177
}
178
179
/* We allow "recursive" symbolic refs. Only within reason, though */
@@ -401,6 +429,7 @@ int delete_ref(const char *refname, unsigned char *sha1)
429
fprintf(stderr, "warning: unlink(%s) failed: %s",
430
lock->log_file, strerror(errno));
431
432
+ invalidate_cached_refs();
433
return ret;
434
}
435
@@ -665,6 +694,7 @@ int write_ref_sha1(struct ref_lock *lock,
694
unlock_ref(lock);
695
return -1;
696
}
697
+ invalidate_cached_refs();
698
if (log_ref_write(lock, sha1, logmsg) < 0) {
699
unlock_ref(lock);
700
return -1;