3 #include "refs-internal.h"
5 #include "../iterator.h"
7 /* FIXME: This declaration shouldn't be here */
8 void read_loose_refs(const char *dirname, struct ref_dir *dir);
10 void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
12 ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc);
13 dir->entries[dir->nr++] = entry;
14 /* optimize for the case that entries are added in order */
16 (dir->nr == dir->sorted + 1 &&
17 strcmp(dir->entries[dir->nr - 2]->name,
18 dir->entries[dir->nr - 1]->name) < 0))
19 dir->sorted = dir->nr;
22 struct ref_dir *get_ref_dir(struct ref_entry *entry)
25 assert(entry->flag & REF_DIR);
26 dir = &entry->u.subdir;
27 if (entry->flag & REF_INCOMPLETE) {
28 read_loose_refs(entry->name, dir);
31 * Manually add refs/bisect, which, being
32 * per-worktree, might not appear in the directory
33 * listing for refs/ in the main repo.
35 if (!strcmp(entry->name, "refs/")) {
36 int pos = search_ref_dir(dir, "refs/bisect/", 12);
38 struct ref_entry *child_entry;
39 child_entry = create_dir_entry(dir->cache,
42 add_entry_to_dir(dir, child_entry);
45 entry->flag &= ~REF_INCOMPLETE;
50 struct ref_entry *create_ref_entry(const char *refname,
51 const unsigned char *sha1, int flag,
54 struct ref_entry *ref;
57 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
58 die("Reference has invalid format: '%s'", refname);
59 FLEX_ALLOC_STR(ref, name, refname);
60 hashcpy(ref->u.value.oid.hash, sha1);
61 oidclr(&ref->u.value.peeled);
66 struct ref_cache *create_ref_cache(struct files_ref_store *refs)
68 struct ref_cache *ret = xcalloc(1, sizeof(*ret));
70 ret->ref_store = refs;
71 ret->root = create_dir_entry(ret, "", 0, 1);
75 static void clear_ref_dir(struct ref_dir *dir);
77 static void free_ref_entry(struct ref_entry *entry)
79 if (entry->flag & REF_DIR) {
81 * Do not use get_ref_dir() here, as that might
82 * trigger the reading of loose refs.
84 clear_ref_dir(&entry->u.subdir);
89 void free_ref_cache(struct ref_cache *cache)
91 free_ref_entry(cache->root);
96 * Clear and free all entries in dir, recursively.
98 static void clear_ref_dir(struct ref_dir *dir)
101 for (i = 0; i < dir->nr; i++)
102 free_ref_entry(dir->entries[i]);
104 dir->sorted = dir->nr = dir->alloc = 0;
108 struct ref_entry *create_dir_entry(struct ref_cache *cache,
109 const char *dirname, size_t len,
112 struct ref_entry *direntry;
114 FLEX_ALLOC_MEM(direntry, name, dirname, len);
115 direntry->u.subdir.cache = cache;
116 direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
120 static int ref_entry_cmp(const void *a, const void *b)
122 struct ref_entry *one = *(struct ref_entry **)a;
123 struct ref_entry *two = *(struct ref_entry **)b;
124 return strcmp(one->name, two->name);
127 static void sort_ref_dir(struct ref_dir *dir);
129 struct string_slice {
134 static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
136 const struct string_slice *key = key_;
137 const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
138 int cmp = strncmp(key->str, ent->name, key->len);
141 return '\0' - (unsigned char)ent->name[key->len];
144 int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
146 struct ref_entry **r;
147 struct string_slice key;
149 if (refname == NULL || !dir->nr)
155 r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries),
156 ref_entry_cmp_sslice);
161 return r - dir->entries;
165 * Search for a directory entry directly within dir (without
166 * recursing). Sort dir if necessary. subdirname must be a directory
167 * name (i.e., end in '/'). If mkdir is set, then create the
168 * directory if it is missing; otherwise, return NULL if the desired
169 * directory cannot be found. dir must already be complete.
171 static struct ref_dir *search_for_subdir(struct ref_dir *dir,
172 const char *subdirname, size_t len,
175 int entry_index = search_ref_dir(dir, subdirname, len);
176 struct ref_entry *entry;
177 if (entry_index == -1) {
181 * Since dir is complete, the absence of a subdir
182 * means that the subdir really doesn't exist;
183 * therefore, create an empty record for it but mark
184 * the record complete.
186 entry = create_dir_entry(dir->cache, subdirname, len, 0);
187 add_entry_to_dir(dir, entry);
189 entry = dir->entries[entry_index];
191 return get_ref_dir(entry);
194 struct ref_dir *find_containing_dir(struct ref_dir *dir,
195 const char *refname, int mkdir)
198 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
199 size_t dirnamelen = slash - refname + 1;
200 struct ref_dir *subdir;
201 subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
212 struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname)
215 struct ref_entry *entry;
216 dir = find_containing_dir(dir, refname, 0);
219 entry_index = search_ref_dir(dir, refname, strlen(refname));
220 if (entry_index == -1)
222 entry = dir->entries[entry_index];
223 return (entry->flag & REF_DIR) ? NULL : entry;
226 int remove_entry_from_dir(struct ref_dir *dir, const char *refname)
228 int refname_len = strlen(refname);
230 struct ref_entry *entry;
231 int is_dir = refname[refname_len - 1] == '/';
234 * refname represents a reference directory. Remove
235 * the trailing slash; otherwise we will get the
236 * directory *representing* refname rather than the
237 * one *containing* it.
239 char *dirname = xmemdupz(refname, refname_len - 1);
240 dir = find_containing_dir(dir, dirname, 0);
243 dir = find_containing_dir(dir, refname, 0);
247 entry_index = search_ref_dir(dir, refname, refname_len);
248 if (entry_index == -1)
250 entry = dir->entries[entry_index];
252 memmove(&dir->entries[entry_index],
253 &dir->entries[entry_index + 1],
254 (dir->nr - entry_index - 1) * sizeof(*dir->entries)
257 if (dir->sorted > entry_index)
259 free_ref_entry(entry);
263 int add_ref_entry(struct ref_dir *dir, struct ref_entry *ref)
265 dir = find_containing_dir(dir, ref->name, 1);
268 add_entry_to_dir(dir, ref);
273 * Emit a warning and return true iff ref1 and ref2 have the same name
274 * and the same sha1. Die if they have the same name but different
277 static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
279 if (strcmp(ref1->name, ref2->name))
282 /* Duplicate name; make sure that they don't conflict: */
284 if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
285 /* This is impossible by construction */
286 die("Reference directory conflict: %s", ref1->name);
288 if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
289 die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
291 warning("Duplicated ref: %s", ref1->name);
296 * Sort the entries in dir non-recursively (if they are not already
297 * sorted) and remove any duplicate entries.
299 static void sort_ref_dir(struct ref_dir *dir)
302 struct ref_entry *last = NULL;
305 * This check also prevents passing a zero-length array to qsort(),
306 * which is a problem on some platforms.
308 if (dir->sorted == dir->nr)
311 QSORT(dir->entries, dir->nr, ref_entry_cmp);
313 /* Remove any duplicates: */
314 for (i = 0, j = 0; j < dir->nr; j++) {
315 struct ref_entry *entry = dir->entries[j];
316 if (last && is_dup_ref(last, entry))
317 free_ref_entry(entry);
319 last = dir->entries[i++] = entry;
321 dir->sorted = dir->nr = i;
324 int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
325 each_ref_entry_fn fn, void *cb_data)
328 assert(dir->sorted == dir->nr);
329 for (i = offset; i < dir->nr; i++) {
330 struct ref_entry *entry = dir->entries[i];
332 if (entry->flag & REF_DIR) {
333 struct ref_dir *subdir = get_ref_dir(entry);
334 sort_ref_dir(subdir);
335 retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data);
337 retval = fn(entry, cb_data);
345 void prime_ref_dir(struct ref_dir *dir)
348 * The hard work of loading loose refs is done by get_ref_dir(), so we
349 * just need to recurse through all of the sub-directories. We do not
350 * even need to care about sorting, as traversal order does not matter
354 for (i = 0; i < dir->nr; i++) {
355 struct ref_entry *entry = dir->entries[i];
356 if (entry->flag & REF_DIR)
357 prime_ref_dir(get_ref_dir(entry));
362 * A level in the reference hierarchy that is currently being iterated
365 struct cache_ref_iterator_level {
367 * The ref_dir being iterated over at this level. The ref_dir
368 * is sorted before being stored here.
373 * The index of the current entry within dir (which might
374 * itself be a directory). If index == -1, then the iteration
375 * hasn't yet begun. If index == dir->nr, then the iteration
376 * through this level is over.
382 * Represent an iteration through a ref_dir in the memory cache. The
383 * iteration recurses through subdirectories.
385 struct cache_ref_iterator {
386 struct ref_iterator base;
389 * The number of levels currently on the stack. This is always
390 * at least 1, because when it becomes zero the iteration is
391 * ended and this struct is freed.
395 /* The number of levels that have been allocated on the stack */
399 * A stack of levels. levels[0] is the uppermost level that is
400 * being iterated over in this iteration. (This is not
401 * necessary the top level in the references hierarchy. If we
402 * are iterating through a subtree, then levels[0] will hold
403 * the ref_dir for that subtree, and subsequent levels will go
406 struct cache_ref_iterator_level *levels;
409 static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
411 struct cache_ref_iterator *iter =
412 (struct cache_ref_iterator *)ref_iterator;
415 struct cache_ref_iterator_level *level =
416 &iter->levels[iter->levels_nr - 1];
417 struct ref_dir *dir = level->dir;
418 struct ref_entry *entry;
420 if (level->index == -1)
423 if (++level->index == level->dir->nr) {
424 /* This level is exhausted; pop up a level */
425 if (--iter->levels_nr == 0)
426 return ref_iterator_abort(ref_iterator);
431 entry = dir->entries[level->index];
433 if (entry->flag & REF_DIR) {
434 /* push down a level */
435 ALLOC_GROW(iter->levels, iter->levels_nr + 1,
438 level = &iter->levels[iter->levels_nr++];
439 level->dir = get_ref_dir(entry);
442 iter->base.refname = entry->name;
443 iter->base.oid = &entry->u.value.oid;
444 iter->base.flags = entry->flag;
450 enum peel_status peel_entry(struct ref_entry *entry, int repeel)
452 enum peel_status status;
454 if (entry->flag & REF_KNOWS_PEELED) {
456 entry->flag &= ~REF_KNOWS_PEELED;
457 oidclr(&entry->u.value.peeled);
459 return is_null_oid(&entry->u.value.peeled) ?
460 PEEL_NON_TAG : PEEL_PEELED;
463 if (entry->flag & REF_ISBROKEN)
465 if (entry->flag & REF_ISSYMREF)
466 return PEEL_IS_SYMREF;
468 status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
469 if (status == PEEL_PEELED || status == PEEL_NON_TAG)
470 entry->flag |= REF_KNOWS_PEELED;
474 static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator,
475 struct object_id *peeled)
477 struct cache_ref_iterator *iter =
478 (struct cache_ref_iterator *)ref_iterator;
479 struct cache_ref_iterator_level *level;
480 struct ref_entry *entry;
482 level = &iter->levels[iter->levels_nr - 1];
484 if (level->index == -1)
485 die("BUG: peel called before advance for cache iterator");
487 entry = level->dir->entries[level->index];
489 if (peel_entry(entry, 0))
491 oidcpy(peeled, &entry->u.value.peeled);
495 static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator)
497 struct cache_ref_iterator *iter =
498 (struct cache_ref_iterator *)ref_iterator;
501 base_ref_iterator_free(ref_iterator);
505 static struct ref_iterator_vtable cache_ref_iterator_vtable = {
506 cache_ref_iterator_advance,
507 cache_ref_iterator_peel,
508 cache_ref_iterator_abort
511 struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir)
513 struct cache_ref_iterator *iter;
514 struct ref_iterator *ref_iterator;
515 struct cache_ref_iterator_level *level;
517 iter = xcalloc(1, sizeof(*iter));
518 ref_iterator = &iter->base;
519 base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable);
520 ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
523 level = &iter->levels[0];