refs: split `ref_cache` code into separate files

The `ref_cache` code is currently too tightly coupled to `files-backend`, making the code harder to understand and making it awkward for new code to use `ref_cache` (as we indeed have planned). Start loosening that coupling by splitting `ref_cache` into a separate module. This commit moves code, adds declarations, and changes the visibility of some functions, but doesn't change any code. The modules are still too tightly coupled, but the situation will be improved in subsequent commits. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 16, 2017 at 08:41 UTC 958f964691a250d9a7173f1c0a4c03fd9e71e2aa
4 files changed +767 -733
Makefile
+1
@@ -807,6 +807,7 @@ LIB_OBJS += reflog-walk.o
807 LIB_OBJS += refs.o
808 LIB_OBJS += refs/files-backend.o
809 LIB_OBJS += refs/iterator.o
810 +LIB_OBJS += refs/ref-cache.o
811 LIB_OBJS += ref-filter.o
812 LIB_OBJS += remote.o
813 LIB_OBJS += replace_object.o
refs/files-backend.c
+3 -733
@@ -1,6 +1,7 @@
1 #include "../cache.h"
2 #include "../refs.h"
3 #include "refs-internal.h"
4 +#include "ref-cache.h"
5 #include "../iterator.h"
6 #include "../dir-iterator.h"
7 #include "../lockfile.h"
@@ -13,509 +14,6 @@ struct ref_lock {
14 struct object_id old_oid;
15 };
16
16 -struct ref_entry;
17 -
18 -/*
19 - * Information used (along with the information in ref_entry) to
20 - * describe a single cached reference. This data structure only
21 - * occurs embedded in a union in struct ref_entry, and only when
22 - * (ref_entry->flag & REF_DIR) is zero.
23 - */
24 -struct ref_value {
25 - /*
26 - * The name of the object to which this reference resolves
27 - * (which may be a tag object). If REF_ISBROKEN, this is
28 - * null. If REF_ISSYMREF, then this is the name of the object
29 - * referred to by the last reference in the symlink chain.
30 - */
31 - struct object_id oid;
32 -
33 - /*
34 - * If REF_KNOWS_PEELED, then this field holds the peeled value
35 - * of this reference, or null if the reference is known not to
36 - * be peelable. See the documentation for peel_ref() for an
37 - * exact definition of "peelable".
38 - */
39 - struct object_id peeled;
40 -};
41 -
42 -struct files_ref_store;
43 -
44 -/*
45 - * Information used (along with the information in ref_entry) to
46 - * describe a level in the hierarchy of references. This data
47 - * structure only occurs embedded in a union in struct ref_entry, and
48 - * only when (ref_entry.flag & REF_DIR) is set. In that case,
49 - * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
50 - * in the directory have already been read:
51 - *
52 - * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
53 - * or packed references, already read.
54 - *
55 - * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
56 - * references that hasn't been read yet (nor has any of its
57 - * subdirectories).
58 - *
59 - * Entries within a directory are stored within a growable array of
60 - * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
61 - * sorted are sorted by their component name in strcmp() order and the
62 - * remaining entries are unsorted.
63 - *
64 - * Loose references are read lazily, one directory at a time. When a
65 - * directory of loose references is read, then all of the references
66 - * in that directory are stored, and REF_INCOMPLETE stubs are created
67 - * for any subdirectories, but the subdirectories themselves are not
68 - * read. The reading is triggered by get_ref_dir().
69 - */
70 -struct ref_dir {
71 - int nr, alloc;
72 -
73 - /*
74 - * Entries with index 0 <= i < sorted are sorted by name. New
75 - * entries are appended to the list unsorted, and are sorted
76 - * only when required; thus we avoid the need to sort the list
77 - * after the addition of every reference.
78 - */
79 - int sorted;
80 -
81 - /* A pointer to the files_ref_store that contains this ref_dir. */
82 - struct files_ref_store *ref_store;
83 -
84 - struct ref_entry **entries;
85 -};
86 -
87 -/*
88 - * Bit values for ref_entry::flag. REF_ISSYMREF=0x01,
89 - * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are
90 - * public values; see refs.h.
91 - */
92 -
93 -/*
94 - * The field ref_entry->u.value.peeled of this value entry contains
95 - * the correct peeled value for the reference, which might be
96 - * null_sha1 if the reference is not a tag or if it is broken.
97 - */
98 -#define REF_KNOWS_PEELED 0x10
99 -
100 -/* ref_entry represents a directory of references */
101 -#define REF_DIR 0x20
102 -
103 -/*
104 - * Entry has not yet been read from disk (used only for REF_DIR
105 - * entries representing loose references)
106 - */
107 -#define REF_INCOMPLETE 0x40
108 -
109 -/*
110 - * A ref_entry represents either a reference or a "subdirectory" of
111 - * references.
112 - *
113 - * Each directory in the reference namespace is represented by a
114 - * ref_entry with (flags & REF_DIR) set and containing a subdir member
115 - * that holds the entries in that directory that have been read so
116 - * far. If (flags & REF_INCOMPLETE) is set, then the directory and
117 - * its subdirectories haven't been read yet. REF_INCOMPLETE is only
118 - * used for loose reference directories.
119 - *
120 - * References are represented by a ref_entry with (flags & REF_DIR)
121 - * unset and a value member that describes the reference's value. The
122 - * flag member is at the ref_entry level, but it is also needed to
123 - * interpret the contents of the value field (in other words, a
124 - * ref_value object is not very much use without the enclosing
125 - * ref_entry).
126 - *
127 - * Reference names cannot end with slash and directories' names are
128 - * always stored with a trailing slash (except for the top-level
129 - * directory, which is always denoted by ""). This has two nice
130 - * consequences: (1) when the entries in each subdir are sorted
131 - * lexicographically by name (as they usually are), the references in
132 - * a whole tree can be generated in lexicographic order by traversing
133 - * the tree in left-to-right, depth-first order; (2) the names of
134 - * references and subdirectories cannot conflict, and therefore the
135 - * presence of an empty subdirectory does not block the creation of a
136 - * similarly-named reference. (The fact that reference names with the
137 - * same leading components can conflict *with each other* is a
138 - * separate issue that is regulated by verify_refname_available().)
139 - *
140 - * Please note that the name field contains the fully-qualified
141 - * reference (or subdirectory) name. Space could be saved by only
142 - * storing the relative names. But that would require the full names
143 - * to be generated on the fly when iterating in do_for_each_ref(), and
144 - * would break callback functions, who have always been able to assume
145 - * that the name strings that they are passed will not be freed during
146 - * the iteration.
147 - */
148 -struct ref_entry {
149 - unsigned char flag; /* ISSYMREF? ISPACKED? */
150 - union {
151 - struct ref_value value; /* if not (flags&REF_DIR) */
152 - struct ref_dir subdir; /* if (flags&REF_DIR) */
153 - } u;
154 - /*
155 - * The full name of the reference (e.g., "refs/heads/master")
156 - * or the full name of the directory with a trailing slash
157 - * (e.g., "refs/heads/"):
158 - */
159 - char name[FLEX_ARRAY];
160 -};
161 -
162 -static void read_loose_refs(const char *dirname, struct ref_dir *dir);
163 -static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
164 -static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
165 - const char *dirname, size_t len,
166 - int incomplete);
167 -static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
168 -static int files_log_ref_write(struct files_ref_store *refs,
169 - const char *refname, const unsigned char *old_sha1,
170 - const unsigned char *new_sha1, const char *msg,
171 - int flags, struct strbuf *err);
172 -
173 -static struct ref_dir *get_ref_dir(struct ref_entry *entry)
174 -{
175 - struct ref_dir *dir;
176 - assert(entry->flag & REF_DIR);
177 - dir = &entry->u.subdir;
178 - if (entry->flag & REF_INCOMPLETE) {
179 - read_loose_refs(entry->name, dir);
180 -
181 - /*
182 - * Manually add refs/bisect, which, being
183 - * per-worktree, might not appear in the directory
184 - * listing for refs/ in the main repo.
185 - */
186 - if (!strcmp(entry->name, "refs/")) {
187 - int pos = search_ref_dir(dir, "refs/bisect/", 12);
188 - if (pos < 0) {
189 - struct ref_entry *child_entry;
190 - child_entry = create_dir_entry(dir->ref_store,
191 - "refs/bisect/",
192 - 12, 1);
193 - add_entry_to_dir(dir, child_entry);
194 - }
195 - }
196 - entry->flag &= ~REF_INCOMPLETE;
197 - }
198 - return dir;
199 -}
200 -
201 -static struct ref_entry *create_ref_entry(const char *refname,
202 - const unsigned char *sha1, int flag,
203 - int check_name)
204 -{
205 - struct ref_entry *ref;
206 -
207 - if (check_name &&
208 - check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
209 - die("Reference has invalid format: '%s'", refname);
210 - FLEX_ALLOC_STR(ref, name, refname);
211 - hashcpy(ref->u.value.oid.hash, sha1);
212 - oidclr(&ref->u.value.peeled);
213 - ref->flag = flag;
214 - return ref;
215 -}
216 -
217 -static void clear_ref_dir(struct ref_dir *dir);
218 -
219 -static void free_ref_entry(struct ref_entry *entry)
220 -{
221 - if (entry->flag & REF_DIR) {
222 - /*
223 - * Do not use get_ref_dir() here, as that might
224 - * trigger the reading of loose refs.
225 - */
226 - clear_ref_dir(&entry->u.subdir);
227 - }
228 - free(entry);
229 -}
230 -
231 -/*
232 - * Add a ref_entry to the end of dir (unsorted). Entry is always
233 - * stored directly in dir; no recursion into subdirectories is
234 - * done.
235 - */
236 -static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
237 -{
238 - ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc);
239 - dir->entries[dir->nr++] = entry;
240 - /* optimize for the case that entries are added in order */
241 - if (dir->nr == 1 ||
242 - (dir->nr == dir->sorted + 1 &&
243 - strcmp(dir->entries[dir->nr - 2]->name,
244 - dir->entries[dir->nr - 1]->name) < 0))
245 - dir->sorted = dir->nr;
246 -}
247 -
248 -/*
249 - * Clear and free all entries in dir, recursively.
250 - */
251 -static void clear_ref_dir(struct ref_dir *dir)
252 -{
253 - int i;
254 - for (i = 0; i < dir->nr; i++)
255 - free_ref_entry(dir->entries[i]);
256 - free(dir->entries);
257 - dir->sorted = dir->nr = dir->alloc = 0;
258 - dir->entries = NULL;
259 -}
260 -
261 -/*
262 - * Create a struct ref_entry object for the specified dirname.
263 - * dirname is the name of the directory with a trailing slash (e.g.,
264 - * "refs/heads/") or "" for the top-level directory.
265 - */
266 -static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
267 - const char *dirname, size_t len,
268 - int incomplete)
269 -{
270 - struct ref_entry *direntry;
271 - FLEX_ALLOC_MEM(direntry, name, dirname, len);
272 - direntry->u.subdir.ref_store = ref_store;
273 - direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
274 - return direntry;
275 -}
276 -
277 -static int ref_entry_cmp(const void *a, const void *b)
278 -{
279 - struct ref_entry *one = *(struct ref_entry **)a;
280 - struct ref_entry *two = *(struct ref_entry **)b;
281 - return strcmp(one->name, two->name);
282 -}
283 -
284 -static void sort_ref_dir(struct ref_dir *dir);
285 -
286 -struct string_slice {
287 - size_t len;
288 - const char *str;
289 -};
290 -
291 -static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
292 -{
293 - const struct string_slice *key = key_;
294 - const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
295 - int cmp = strncmp(key->str, ent->name, key->len);
296 - if (cmp)
297 - return cmp;
298 - return '\0' - (unsigned char)ent->name[key->len];
299 -}
300 -
301 -/*
302 - * Return the index of the entry with the given refname from the
303 - * ref_dir (non-recursively), sorting dir if necessary. Return -1 if
304 - * no such entry is found. dir must already be complete.
305 - */
306 -static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
307 -{
308 - struct ref_entry **r;
309 - struct string_slice key;
310 -
311 - if (refname == NULL || !dir->nr)
312 - return -1;
313 -
314 - sort_ref_dir(dir);
315 - key.len = len;
316 - key.str = refname;
317 - r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries),
318 - ref_entry_cmp_sslice);
319 -
320 - if (r == NULL)
321 - return -1;
322 -
323 - return r - dir->entries;
324 -}
325 -
326 -/*
327 - * Search for a directory entry directly within dir (without
328 - * recursing). Sort dir if necessary. subdirname must be a directory
329 - * name (i.e., end in '/'). If mkdir is set, then create the
330 - * directory if it is missing; otherwise, return NULL if the desired
331 - * directory cannot be found. dir must already be complete.
332 - */
333 -static struct ref_dir *search_for_subdir(struct ref_dir *dir,
334 - const char *subdirname, size_t len,
335 - int mkdir)
336 -{
337 - int entry_index = search_ref_dir(dir, subdirname, len);
338 - struct ref_entry *entry;
339 - if (entry_index == -1) {
340 - if (!mkdir)
341 - return NULL;
342 - /*
343 - * Since dir is complete, the absence of a subdir
344 - * means that the subdir really doesn't exist;
345 - * therefore, create an empty record for it but mark
346 - * the record complete.
347 - */
348 - entry = create_dir_entry(dir->ref_store, subdirname, len, 0);
349 - add_entry_to_dir(dir, entry);
350 - } else {
351 - entry = dir->entries[entry_index];
352 - }
353 - return get_ref_dir(entry);
354 -}
355 -
356 -/*
357 - * If refname is a reference name, find the ref_dir within the dir
358 - * tree that should hold refname. If refname is a directory name
359 - * (i.e., ends in '/'), then return that ref_dir itself. dir must
360 - * represent the top-level directory and must already be complete.
361 - * Sort ref_dirs and recurse into subdirectories as necessary. If
362 - * mkdir is set, then create any missing directories; otherwise,
363 - * return NULL if the desired directory cannot be found.
364 - */
365 -static struct ref_dir *find_containing_dir(struct ref_dir *dir,
366 - const char *refname, int mkdir)
367 -{
368 - const char *slash;
369 - for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
370 - size_t dirnamelen = slash - refname + 1;
371 - struct ref_dir *subdir;
372 - subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
373 - if (!subdir) {
374 - dir = NULL;
375 - break;
376 - }
377 - dir = subdir;
378 - }
379 -
380 - return dir;
381 -}
382 -
383 -/*
384 - * Find the value entry with the given name in dir, sorting ref_dirs
385 - * and recursing into subdirectories as necessary. If the name is not
386 - * found or it corresponds to a directory entry, return NULL.
387 - */
388 -static struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname)
389 -{
390 - int entry_index;
391 - struct ref_entry *entry;
392 - dir = find_containing_dir(dir, refname, 0);
393 - if (!dir)
394 - return NULL;
395 - entry_index = search_ref_dir(dir, refname, strlen(refname));
396 - if (entry_index == -1)
397 - return NULL;
398 - entry = dir->entries[entry_index];
399 - return (entry->flag & REF_DIR) ? NULL : entry;
400 -}
401 -
402 -/*
403 - * Remove the entry with the given name from dir, recursing into
404 - * subdirectories as necessary. If refname is the name of a directory
405 - * (i.e., ends with '/'), then remove the directory and its contents.
406 - * If the removal was successful, return the number of entries
407 - * remaining in the directory entry that contained the deleted entry.
408 - * If the name was not found, return -1. Please note that this
409 - * function only deletes the entry from the cache; it does not delete
410 - * it from the filesystem or ensure that other cache entries (which
411 - * might be symbolic references to the removed entry) are updated.
412 - * Nor does it remove any containing dir entries that might be made
413 - * empty by the removal. dir must represent the top-level directory
414 - * and must already be complete.
415 - */
416 -static int remove_entry_from_dir(struct ref_dir *dir, const char *refname)
417 -{
418 - int refname_len = strlen(refname);
419 - int entry_index;
420 - struct ref_entry *entry;
421 - int is_dir = refname[refname_len - 1] == '/';
422 - if (is_dir) {
423 - /*
424 - * refname represents a reference directory. Remove
425 - * the trailing slash; otherwise we will get the
426 - * directory *representing* refname rather than the
427 - * one *containing* it.
428 - */
429 - char *dirname = xmemdupz(refname, refname_len - 1);
430 - dir = find_containing_dir(dir, dirname, 0);
431 - free(dirname);
432 - } else {
433 - dir = find_containing_dir(dir, refname, 0);
434 - }
435 - if (!dir)
436 - return -1;
437 - entry_index = search_ref_dir(dir, refname, refname_len);
438 - if (entry_index == -1)
439 - return -1;
440 - entry = dir->entries[entry_index];
441 -
442 - memmove(&dir->entries[entry_index],
443 - &dir->entries[entry_index + 1],
444 - (dir->nr - entry_index - 1) * sizeof(*dir->entries)
445 - );
446 - dir->nr--;
447 - if (dir->sorted > entry_index)
448 - dir->sorted--;
449 - free_ref_entry(entry);
450 - return dir->nr;
451 -}
452 -
453 -/*
454 - * Add a ref_entry to the ref_dir (unsorted), recursing into
455 - * subdirectories as necessary. dir must represent the top-level
456 - * directory. Return 0 on success.
457 - */
458 -static int add_ref_entry(struct ref_dir *dir, struct ref_entry *ref)
459 -{
460 - dir = find_containing_dir(dir, ref->name, 1);
461 - if (!dir)
462 - return -1;
463 - add_entry_to_dir(dir, ref);
464 - return 0;
465 -}
466 -
467 -/*
468 - * Emit a warning and return true iff ref1 and ref2 have the same name
469 - * and the same sha1. Die if they have the same name but different
470 - * sha1s.
471 - */
472 -static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
473 -{
474 - if (strcmp(ref1->name, ref2->name))
475 - return 0;
476 -
477 - /* Duplicate name; make sure that they don't conflict: */
478 -
479 - if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
480 - /* This is impossible by construction */
481 - die("Reference directory conflict: %s", ref1->name);
482 -
483 - if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
484 - die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
485 -
486 - warning("Duplicated ref: %s", ref1->name);
487 - return 1;
488 -}
489 -
490 -/*
491 - * Sort the entries in dir non-recursively (if they are not already
492 - * sorted) and remove any duplicate entries.
493 - */
494 -static void sort_ref_dir(struct ref_dir *dir)
495 -{
496 - int i, j;
497 - struct ref_entry *last = NULL;
498 -
499 - /*
500 - * This check also prevents passing a zero-length array to qsort(),
501 - * which is a problem on some platforms.
502 - */
503 - if (dir->sorted == dir->nr)
504 - return;
505 -
506 - QSORT(dir->entries, dir->nr, ref_entry_cmp);
507 -
508 - /* Remove any duplicates: */
509 - for (i = 0, j = 0; j < dir->nr; j++) {
510 - struct ref_entry *entry = dir->entries[j];
511 - if (last && is_dup_ref(last, entry))
512 - free_ref_entry(entry);
513 - else
514 - last = dir->entries[i++] = entry;
515 - }
516 - dir->sorted = dir->nr = i;
517 -}
518 -
17 /*
18 * Return true if refname, which has the specified oid and flags, can
19 * be resolved to an object in the database. If the referred-to object
@@ -545,199 +43,6 @@ static int entry_resolves_to_object(struct ref_entry *entry)
43 &entry->u.value.oid, entry->flag);
44 }
45
548 -typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
549 -
550 -/*
551 - * Call fn for each reference in dir that has index in the range
552 - * offset <= index < dir->nr. Recurse into subdirectories that are in
553 - * that index range, sorting them before iterating. This function
554 - * does not sort dir itself; it should be sorted beforehand. fn is
555 - * called for all references, including broken ones.
556 - */
557 -static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
558 - each_ref_entry_fn fn, void *cb_data)
559 -{
560 - int i;
561 - assert(dir->sorted == dir->nr);
562 - for (i = offset; i < dir->nr; i++) {
563 - struct ref_entry *entry = dir->entries[i];
564 - int retval;
565 - if (entry->flag & REF_DIR) {
566 - struct ref_dir *subdir = get_ref_dir(entry);
567 - sort_ref_dir(subdir);
568 - retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data);
569 - } else {
570 - retval = fn(entry, cb_data);
571 - }
572 - if (retval)
573 - return retval;
574 - }
575 - return 0;
576 -}
577 -
578 -/*
579 - * Load all of the refs from the dir into our in-memory cache. The hard work
580 - * of loading loose refs is done by get_ref_dir(), so we just need to recurse
581 - * through all of the sub-directories. We do not even need to care about
582 - * sorting, as traversal order does not matter to us.
583 - */
584 -static void prime_ref_dir(struct ref_dir *dir)
585 -{
586 - int i;
587 - for (i = 0; i < dir->nr; i++) {
588 - struct ref_entry *entry = dir->entries[i];
589 - if (entry->flag & REF_DIR)
590 - prime_ref_dir(get_ref_dir(entry));
591 - }
592 -}
593 -
594 -/*
595 - * A level in the reference hierarchy that is currently being iterated
596 - * through.
597 - */
598 -struct cache_ref_iterator_level {
599 - /*
600 - * The ref_dir being iterated over at this level. The ref_dir
601 - * is sorted before being stored here.
602 - */
603 - struct ref_dir *dir;
604 -
605 - /*
606 - * The index of the current entry within dir (which might
607 - * itself be a directory). If index == -1, then the iteration
608 - * hasn't yet begun. If index == dir->nr, then the iteration
609 - * through this level is over.
610 - */
611 - int index;
612 -};
613 -
614 -/*
615 - * Represent an iteration through a ref_dir in the memory cache. The
616 - * iteration recurses through subdirectories.
617 - */
618 -struct cache_ref_iterator {
619 - struct ref_iterator base;
620 -
621 - /*
622 - * The number of levels currently on the stack. This is always
623 - * at least 1, because when it becomes zero the iteration is
624 - * ended and this struct is freed.
625 - */
626 - size_t levels_nr;
627 -
628 - /* The number of levels that have been allocated on the stack */
629 - size_t levels_alloc;
630 -
631 - /*
632 - * A stack of levels. levels[0] is the uppermost level that is
633 - * being iterated over in this iteration. (This is not
634 - * necessary the top level in the references hierarchy. If we
635 - * are iterating through a subtree, then levels[0] will hold
636 - * the ref_dir for that subtree, and subsequent levels will go
637 - * on from there.)
638 - */
639 - struct cache_ref_iterator_level *levels;
640 -};
641 -
642 -static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
643 -{
644 - struct cache_ref_iterator *iter =
645 - (struct cache_ref_iterator *)ref_iterator;
646 -
647 - while (1) {
648 - struct cache_ref_iterator_level *level =
649 - &iter->levels[iter->levels_nr - 1];
650 - struct ref_dir *dir = level->dir;
651 - struct ref_entry *entry;
652 -
653 - if (level->index == -1)
654 - sort_ref_dir(dir);
655 -
656 - if (++level->index == level->dir->nr) {
657 - /* This level is exhausted; pop up a level */
658 - if (--iter->levels_nr == 0)
659 - return ref_iterator_abort(ref_iterator);
660 -
661 - continue;
662 - }
663 -
664 - entry = dir->entries[level->index];
665 -
666 - if (entry->flag & REF_DIR) {
667 - /* push down a level */
668 - ALLOC_GROW(iter->levels, iter->levels_nr + 1,
669 - iter->levels_alloc);
670 -
671 - level = &iter->levels[iter->levels_nr++];
672 - level->dir = get_ref_dir(entry);
673 - level->index = -1;
674 - } else {
675 - iter->base.refname = entry->name;
676 - iter->base.oid = &entry->u.value.oid;
677 - iter->base.flags = entry->flag;
678 - return ITER_OK;
679 - }
680 - }
681 -}
682 -
683 -static enum peel_status peel_entry(struct ref_entry *entry, int repeel);
684 -
685 -static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator,
686 - struct object_id *peeled)
687 -{
688 - struct cache_ref_iterator *iter =
689 - (struct cache_ref_iterator *)ref_iterator;
690 - struct cache_ref_iterator_level *level;
691 - struct ref_entry *entry;
692 -
693 - level = &iter->levels[iter->levels_nr - 1];
694 -
695 - if (level->index == -1)
696 - die("BUG: peel called before advance for cache iterator");
697 -
698 - entry = level->dir->entries[level->index];
699 -
700 - if (peel_entry(entry, 0))
701 - return -1;
702 - oidcpy(peeled, &entry->u.value.peeled);
703 - return 0;
704 -}
705 -
706 -static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator)
707 -{
708 - struct cache_ref_iterator *iter =
709 - (struct cache_ref_iterator *)ref_iterator;
710 -
711 - free(iter->levels);
712 - base_ref_iterator_free(ref_iterator);
713 - return ITER_DONE;
714 -}
715 -
716 -static struct ref_iterator_vtable cache_ref_iterator_vtable = {
717 - cache_ref_iterator_advance,
718 - cache_ref_iterator_peel,
719 - cache_ref_iterator_abort
720 -};
721 -
722 -static struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir)
723 -{
724 - struct cache_ref_iterator *iter;
725 - struct ref_iterator *ref_iterator;
726 - struct cache_ref_iterator_level *level;
727 -
728 - iter = xcalloc(1, sizeof(*iter));
729 - ref_iterator = &iter->base;
730 - base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable);
731 - ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
732 -
733 - iter->levels_nr = 1;
734 - level = &iter->levels[0];
735 - level->index = -1;
736 - level->dir = dir;
737 -
738 - return ref_iterator;
739 -}
740 -
46 struct packed_ref_cache {
47 struct ref_entry *root;
48
@@ -1116,7 +421,7 @@ static void add_packed_ref(struct files_ref_store *refs,
421 if (!packed_ref_cache->lock)
422 die("internal error: packed refs not locked");
423 add_ref_entry(get_packed_ref_dir(packed_ref_cache),
1119 - create_ref_entry(refname, sha1, REF_ISPACKED, 1));
424 + create_ref_entry(refname, sha1, REF_ISPACKED, 1));
425 }
426
427 /*
@@ -1124,7 +429,7 @@ static void add_packed_ref(struct files_ref_store *refs,
429 * (without recursing). dirname must end with '/'. dir must be the
430 * directory entry corresponding to dirname.
431 */
1127 -static void read_loose_refs(const char *dirname, struct ref_dir *dir)
432 +void read_loose_refs(const char *dirname, struct ref_dir *dir)
433 {
434 struct files_ref_store *refs = dir->ref_store;
435 DIR *d;
@@ -1634,41 +939,6 @@ out:
939 return ret;
940 }
941
1637 -/*
1638 - * Peel the entry (if possible) and return its new peel_status. If
1639 - * repeel is true, re-peel the entry even if there is an old peeled
1640 - * value that is already stored in it.
1641 - *
1642 - * It is OK to call this function with a packed reference entry that
1643 - * might be stale and might even refer to an object that has since
1644 - * been garbage-collected. In such a case, if the entry has
1645 - * REF_KNOWS_PEELED then leave the status unchanged and return
1646 - * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
1647 - */
1648 -static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
1649 -{
1650 - enum peel_status status;
1651 -
1652 - if (entry->flag & REF_KNOWS_PEELED) {
1653 - if (repeel) {
1654 - entry->flag &= ~REF_KNOWS_PEELED;
1655 - oidclr(&entry->u.value.peeled);
1656 - } else {
1657 - return is_null_oid(&entry->u.value.peeled) ?
1658 - PEEL_NON_TAG : PEEL_PEELED;
1659 - }
1660 - }
1661 - if (entry->flag & REF_ISBROKEN)
1662 - return PEEL_BROKEN;
1663 - if (entry->flag & REF_ISSYMREF)
1664 - return PEEL_IS_SYMREF;
1665 -
1666 - status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
1667 - if (status == PEEL_PEELED || status == PEEL_NON_TAG)
1668 - entry->flag |= REF_KNOWS_PEELED;
1669 - return status;
1670 -}
1671 -
942 static int files_peel_ref(struct ref_store *ref_store,
943 const char *refname, unsigned char *sha1)
944 {
refs/ref-cache.c new
+512
@@ -0,0 +1,512 @@
1 +#include "../cache.h"
2 +#include "../refs.h"
3 +#include "refs-internal.h"
4 +#include "ref-cache.h"
5 +#include "../iterator.h"
6 +
7 +/* FIXME: This declaration shouldn't be here */
8 +void read_loose_refs(const char *dirname, struct ref_dir *dir);
9 +
10 +void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
11 +{
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 */
15 + if (dir->nr == 1 ||
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;
20 +}
21 +
22 +struct ref_dir *get_ref_dir(struct ref_entry *entry)
23 +{
24 + struct ref_dir *dir;
25 + assert(entry->flag & REF_DIR);
26 + dir = &entry->u.subdir;
27 + if (entry->flag & REF_INCOMPLETE) {
28 + read_loose_refs(entry->name, dir);
29 +
30 + /*
31 + * Manually add refs/bisect, which, being
32 + * per-worktree, might not appear in the directory
33 + * listing for refs/ in the main repo.
34 + */
35 + if (!strcmp(entry->name, "refs/")) {
36 + int pos = search_ref_dir(dir, "refs/bisect/", 12);
37 + if (pos < 0) {
38 + struct ref_entry *child_entry;
39 + child_entry = create_dir_entry(dir->ref_store,
40 + "refs/bisect/",
41 + 12, 1);
42 + add_entry_to_dir(dir, child_entry);
43 + }
44 + }
45 + entry->flag &= ~REF_INCOMPLETE;
46 + }
47 + return dir;
48 +}
49 +
50 +struct ref_entry *create_ref_entry(const char *refname,
51 + const unsigned char *sha1, int flag,
52 + int check_name)
53 +{
54 + struct ref_entry *ref;
55 +
56 + if (check_name &&
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);
62 + ref->flag = flag;
63 + return ref;
64 +}
65 +
66 +static void clear_ref_dir(struct ref_dir *dir);
67 +
68 +void free_ref_entry(struct ref_entry *entry)
69 +{
70 + if (entry->flag & REF_DIR) {
71 + /*
72 + * Do not use get_ref_dir() here, as that might
73 + * trigger the reading of loose refs.
74 + */
75 + clear_ref_dir(&entry->u.subdir);
76 + }
77 + free(entry);
78 +}
79 +
80 +/*
81 + * Clear and free all entries in dir, recursively.
82 + */
83 +static void clear_ref_dir(struct ref_dir *dir)
84 +{
85 + int i;
86 + for (i = 0; i < dir->nr; i++)
87 + free_ref_entry(dir->entries[i]);
88 + free(dir->entries);
89 + dir->sorted = dir->nr = dir->alloc = 0;
90 + dir->entries = NULL;
91 +}
92 +
93 +struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
94 + const char *dirname, size_t len,
95 + int incomplete)
96 +{
97 + struct ref_entry *direntry;
98 + FLEX_ALLOC_MEM(direntry, name, dirname, len);
99 + direntry->u.subdir.ref_store = ref_store;
100 + direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
101 + return direntry;
102 +}
103 +
104 +static int ref_entry_cmp(const void *a, const void *b)
105 +{
106 + struct ref_entry *one = *(struct ref_entry **)a;
107 + struct ref_entry *two = *(struct ref_entry **)b;
108 + return strcmp(one->name, two->name);
109 +}
110 +
111 +static void sort_ref_dir(struct ref_dir *dir);
112 +
113 +struct string_slice {
114 + size_t len;
115 + const char *str;
116 +};
117 +
118 +static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
119 +{
120 + const struct string_slice *key = key_;
121 + const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
122 + int cmp = strncmp(key->str, ent->name, key->len);
123 + if (cmp)
124 + return cmp;
125 + return '\0' - (unsigned char)ent->name[key->len];
126 +}
127 +
128 +int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
129 +{
130 + struct ref_entry **r;
131 + struct string_slice key;
132 +
133 + if (refname == NULL || !dir->nr)
134 + return -1;
135 +
136 + sort_ref_dir(dir);
137 + key.len = len;
138 + key.str = refname;
139 + r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries),
140 + ref_entry_cmp_sslice);
141 +
142 + if (r == NULL)
143 + return -1;
144 +
145 + return r - dir->entries;
146 +}
147 +
148 +/*
149 + * Search for a directory entry directly within dir (without
150 + * recursing). Sort dir if necessary. subdirname must be a directory
151 + * name (i.e., end in '/'). If mkdir is set, then create the
152 + * directory if it is missing; otherwise, return NULL if the desired
153 + * directory cannot be found. dir must already be complete.
154 + */
155 +static struct ref_dir *search_for_subdir(struct ref_dir *dir,
156 + const char *subdirname, size_t len,
157 + int mkdir)
158 +{
159 + int entry_index = search_ref_dir(dir, subdirname, len);
160 + struct ref_entry *entry;
161 + if (entry_index == -1) {
162 + if (!mkdir)
163 + return NULL;
164 + /*
165 + * Since dir is complete, the absence of a subdir
166 + * means that the subdir really doesn't exist;
167 + * therefore, create an empty record for it but mark
168 + * the record complete.
169 + */
170 + entry = create_dir_entry(dir->ref_store, subdirname, len, 0);
171 + add_entry_to_dir(dir, entry);
172 + } else {
173 + entry = dir->entries[entry_index];
174 + }
175 + return get_ref_dir(entry);
176 +}
177 +
178 +struct ref_dir *find_containing_dir(struct ref_dir *dir,
179 + const char *refname, int mkdir)
180 +{
181 + const char *slash;
182 + for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
183 + size_t dirnamelen = slash - refname + 1;
184 + struct ref_dir *subdir;
185 + subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
186 + if (!subdir) {
187 + dir = NULL;
188 + break;
189 + }
190 + dir = subdir;
191 + }
192 +
193 + return dir;
194 +}
195 +
196 +struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname)
197 +{
198 + int entry_index;
199 + struct ref_entry *entry;
200 + dir = find_containing_dir(dir, refname, 0);
201 + if (!dir)
202 + return NULL;
203 + entry_index = search_ref_dir(dir, refname, strlen(refname));
204 + if (entry_index == -1)
205 + return NULL;
206 + entry = dir->entries[entry_index];
207 + return (entry->flag & REF_DIR) ? NULL : entry;
208 +}
209 +
210 +int remove_entry_from_dir(struct ref_dir *dir, const char *refname)
211 +{
212 + int refname_len = strlen(refname);
213 + int entry_index;
214 + struct ref_entry *entry;
215 + int is_dir = refname[refname_len - 1] == '/';
216 + if (is_dir) {
217 + /*
218 + * refname represents a reference directory. Remove
219 + * the trailing slash; otherwise we will get the
220 + * directory *representing* refname rather than the
221 + * one *containing* it.
222 + */
223 + char *dirname = xmemdupz(refname, refname_len - 1);
224 + dir = find_containing_dir(dir, dirname, 0);
225 + free(dirname);
226 + } else {
227 + dir = find_containing_dir(dir, refname, 0);
228 + }
229 + if (!dir)
230 + return -1;
231 + entry_index = search_ref_dir(dir, refname, refname_len);
232 + if (entry_index == -1)
233 + return -1;
234 + entry = dir->entries[entry_index];
235 +
236 + memmove(&dir->entries[entry_index],
237 + &dir->entries[entry_index + 1],
238 + (dir->nr - entry_index - 1) * sizeof(*dir->entries)
239 + );
240 + dir->nr--;
241 + if (dir->sorted > entry_index)
242 + dir->sorted--;
243 + free_ref_entry(entry);
244 + return dir->nr;
245 +}
246 +
247 +int add_ref_entry(struct ref_dir *dir, struct ref_entry *ref)
248 +{
249 + dir = find_containing_dir(dir, ref->name, 1);
250 + if (!dir)
251 + return -1;
252 + add_entry_to_dir(dir, ref);
253 + return 0;
254 +}
255 +
256 +/*
257 + * Emit a warning and return true iff ref1 and ref2 have the same name
258 + * and the same sha1. Die if they have the same name but different
259 + * sha1s.
260 + */
261 +static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
262 +{
263 + if (strcmp(ref1->name, ref2->name))
264 + return 0;
265 +
266 + /* Duplicate name; make sure that they don't conflict: */
267 +
268 + if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
269 + /* This is impossible by construction */
270 + die("Reference directory conflict: %s", ref1->name);
271 +
272 + if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
273 + die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
274 +
275 + warning("Duplicated ref: %s", ref1->name);
276 + return 1;
277 +}
278 +
279 +/*
280 + * Sort the entries in dir non-recursively (if they are not already
281 + * sorted) and remove any duplicate entries.
282 + */
283 +static void sort_ref_dir(struct ref_dir *dir)
284 +{
285 + int i, j;
286 + struct ref_entry *last = NULL;
287 +
288 + /*
289 + * This check also prevents passing a zero-length array to qsort(),
290 + * which is a problem on some platforms.
291 + */
292 + if (dir->sorted == dir->nr)
293 + return;
294 +
295 + QSORT(dir->entries, dir->nr, ref_entry_cmp);
296 +
297 + /* Remove any duplicates: */
298 + for (i = 0, j = 0; j < dir->nr; j++) {
299 + struct ref_entry *entry = dir->entries[j];
300 + if (last && is_dup_ref(last, entry))
301 + free_ref_entry(entry);
302 + else
303 + last = dir->entries[i++] = entry;
304 + }
305 + dir->sorted = dir->nr = i;
306 +}
307 +
308 +int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
309 + each_ref_entry_fn fn, void *cb_data)
310 +{
311 + int i;
312 + assert(dir->sorted == dir->nr);
313 + for (i = offset; i < dir->nr; i++) {
314 + struct ref_entry *entry = dir->entries[i];
315 + int retval;
316 + if (entry->flag & REF_DIR) {
317 + struct ref_dir *subdir = get_ref_dir(entry);
318 + sort_ref_dir(subdir);
319 + retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data);
320 + } else {
321 + retval = fn(entry, cb_data);
322 + }
323 + if (retval)
324 + return retval;
325 + }
326 + return 0;
327 +}
328 +
329 +void prime_ref_dir(struct ref_dir *dir)
330 +{
331 + /*
332 + * The hard work of loading loose refs is done by get_ref_dir(), so we
333 + * just need to recurse through all of the sub-directories. We do not
334 + * even need to care about sorting, as traversal order does not matter
335 + * to us.
336 + */
337 + int i;
338 + for (i = 0; i < dir->nr; i++) {
339 + struct ref_entry *entry = dir->entries[i];
340 + if (entry->flag & REF_DIR)
341 + prime_ref_dir(get_ref_dir(entry));
342 + }
343 +}
344 +
345 +/*
346 + * A level in the reference hierarchy that is currently being iterated
347 + * through.
348 + */
349 +struct cache_ref_iterator_level {
350 + /*
351 + * The ref_dir being iterated over at this level. The ref_dir
352 + * is sorted before being stored here.
353 + */
354 + struct ref_dir *dir;
355 +
356 + /*
357 + * The index of the current entry within dir (which might
358 + * itself be a directory). If index == -1, then the iteration
359 + * hasn't yet begun. If index == dir->nr, then the iteration
360 + * through this level is over.
361 + */
362 + int index;
363 +};
364 +
365 +/*
366 + * Represent an iteration through a ref_dir in the memory cache. The
367 + * iteration recurses through subdirectories.
368 + */
369 +struct cache_ref_iterator {
370 + struct ref_iterator base;
371 +
372 + /*
373 + * The number of levels currently on the stack. This is always
374 + * at least 1, because when it becomes zero the iteration is
375 + * ended and this struct is freed.
376 + */
377 + size_t levels_nr;
378 +
379 + /* The number of levels that have been allocated on the stack */
380 + size_t levels_alloc;
381 +
382 + /*
383 + * A stack of levels. levels[0] is the uppermost level that is
384 + * being iterated over in this iteration. (This is not
385 + * necessary the top level in the references hierarchy. If we
386 + * are iterating through a subtree, then levels[0] will hold
387 + * the ref_dir for that subtree, and subsequent levels will go
388 + * on from there.)
389 + */
390 + struct cache_ref_iterator_level *levels;
391 +};
392 +
393 +static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
394 +{
395 + struct cache_ref_iterator *iter =
396 + (struct cache_ref_iterator *)ref_iterator;
397 +
398 + while (1) {
399 + struct cache_ref_iterator_level *level =
400 + &iter->levels[iter->levels_nr - 1];
401 + struct ref_dir *dir = level->dir;
402 + struct ref_entry *entry;
403 +
404 + if (level->index == -1)
405 + sort_ref_dir(dir);
406 +
407 + if (++level->index == level->dir->nr) {
408 + /* This level is exhausted; pop up a level */
409 + if (--iter->levels_nr == 0)
410 + return ref_iterator_abort(ref_iterator);
411 +
412 + continue;
413 + }
414 +
415 + entry = dir->entries[level->index];
416 +
417 + if (entry->flag & REF_DIR) {
418 + /* push down a level */
419 + ALLOC_GROW(iter->levels, iter->levels_nr + 1,
420 + iter->levels_alloc);
421 +
422 + level = &iter->levels[iter->levels_nr++];
423 + level->dir = get_ref_dir(entry);
424 + level->index = -1;
425 + } else {
426 + iter->base.refname = entry->name;
427 + iter->base.oid = &entry->u.value.oid;
428 + iter->base.flags = entry->flag;
429 + return ITER_OK;
430 + }
431 + }
432 +}
433 +
434 +enum peel_status peel_entry(struct ref_entry *entry, int repeel)
435 +{
436 + enum peel_status status;
437 +
438 + if (entry->flag & REF_KNOWS_PEELED) {
439 + if (repeel) {
440 + entry->flag &= ~REF_KNOWS_PEELED;
441 + oidclr(&entry->u.value.peeled);
442 + } else {
443 + return is_null_oid(&entry->u.value.peeled) ?
444 + PEEL_NON_TAG : PEEL_PEELED;
445 + }
446 + }
447 + if (entry->flag & REF_ISBROKEN)
448 + return PEEL_BROKEN;
449 + if (entry->flag & REF_ISSYMREF)
450 + return PEEL_IS_SYMREF;
451 +
452 + status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
453 + if (status == PEEL_PEELED || status == PEEL_NON_TAG)
454 + entry->flag |= REF_KNOWS_PEELED;
455 + return status;
456 +}
457 +
458 +static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator,
459 + struct object_id *peeled)
460 +{
461 + struct cache_ref_iterator *iter =
462 + (struct cache_ref_iterator *)ref_iterator;
463 + struct cache_ref_iterator_level *level;
464 + struct ref_entry *entry;
465 +
466 + level = &iter->levels[iter->levels_nr - 1];
467 +
468 + if (level->index == -1)
469 + die("BUG: peel called before advance for cache iterator");
470 +
471 + entry = level->dir->entries[level->index];
472 +
473 + if (peel_entry(entry, 0))
474 + return -1;
475 + oidcpy(peeled, &entry->u.value.peeled);
476 + return 0;
477 +}
478 +
479 +static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator)
480 +{
481 + struct cache_ref_iterator *iter =
482 + (struct cache_ref_iterator *)ref_iterator;
483 +
484 + free(iter->levels);
485 + base_ref_iterator_free(ref_iterator);
486 + return ITER_DONE;
487 +}
488 +
489 +static struct ref_iterator_vtable cache_ref_iterator_vtable = {
490 + cache_ref_iterator_advance,
491 + cache_ref_iterator_peel,
492 + cache_ref_iterator_abort
493 +};
494 +
495 +struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir)
496 +{
497 + struct cache_ref_iterator *iter;
498 + struct ref_iterator *ref_iterator;
499 + struct cache_ref_iterator_level *level;
500 +
501 + iter = xcalloc(1, sizeof(*iter));
502 + ref_iterator = &iter->base;
503 + base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable);
504 + ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
505 +
506 + iter->levels_nr = 1;
507 + level = &iter->levels[0];
508 + level->index = -1;
509 + level->dir = dir;
510 +
511 + return ref_iterator;
512 +}
refs/ref-cache.h new
+251
@@ -0,0 +1,251 @@
1 +#ifndef REFS_REF_CACHE_H
2 +#define REFS_REF_CACHE_H
3 +
4 +/*
5 + * Information used (along with the information in ref_entry) to
6 + * describe a single cached reference. This data structure only
7 + * occurs embedded in a union in struct ref_entry, and only when
8 + * (ref_entry->flag & REF_DIR) is zero.
9 + */
10 +struct ref_value {
11 + /*
12 + * The name of the object to which this reference resolves
13 + * (which may be a tag object). If REF_ISBROKEN, this is
14 + * null. If REF_ISSYMREF, then this is the name of the object
15 + * referred to by the last reference in the symlink chain.
16 + */
17 + struct object_id oid;
18 +
19 + /*
20 + * If REF_KNOWS_PEELED, then this field holds the peeled value
21 + * of this reference, or null if the reference is known not to
22 + * be peelable. See the documentation for peel_ref() for an
23 + * exact definition of "peelable".
24 + */
25 + struct object_id peeled;
26 +};
27 +
28 +/*
29 + * Information used (along with the information in ref_entry) to
30 + * describe a level in the hierarchy of references. This data
31 + * structure only occurs embedded in a union in struct ref_entry, and
32 + * only when (ref_entry.flag & REF_DIR) is set. In that case,
33 + * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
34 + * in the directory have already been read:
35 + *
36 + * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
37 + * or packed references, already read.
38 + *
39 + * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
40 + * references that hasn't been read yet (nor has any of its
41 + * subdirectories).
42 + *
43 + * Entries within a directory are stored within a growable array of
44 + * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
45 + * sorted are sorted by their component name in strcmp() order and the
46 + * remaining entries are unsorted.
47 + *
48 + * Loose references are read lazily, one directory at a time. When a
49 + * directory of loose references is read, then all of the references
50 + * in that directory are stored, and REF_INCOMPLETE stubs are created
51 + * for any subdirectories, but the subdirectories themselves are not
52 + * read. The reading is triggered by get_ref_dir().
53 + */
54 +struct ref_dir {
55 + int nr, alloc;
56 +
57 + /*
58 + * Entries with index 0 <= i < sorted are sorted by name. New
59 + * entries are appended to the list unsorted, and are sorted
60 + * only when required; thus we avoid the need to sort the list
61 + * after the addition of every reference.
62 + */
63 + int sorted;
64 +
65 + /* A pointer to the files_ref_store that contains this ref_dir. */
66 + struct files_ref_store *ref_store;
67 +
68 + struct ref_entry **entries;
69 +};
70 +
71 +/*
72 + * Bit values for ref_entry::flag. REF_ISSYMREF=0x01,
73 + * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are
74 + * public values; see refs.h.
75 + */
76 +
77 +/*
78 + * The field ref_entry->u.value.peeled of this value entry contains
79 + * the correct peeled value for the reference, which might be
80 + * null_sha1 if the reference is not a tag or if it is broken.
81 + */
82 +#define REF_KNOWS_PEELED 0x10
83 +
84 +/* ref_entry represents a directory of references */
85 +#define REF_DIR 0x20
86 +
87 +/*
88 + * Entry has not yet been read from disk (used only for REF_DIR
89 + * entries representing loose references)
90 + */
91 +#define REF_INCOMPLETE 0x40
92 +
93 +/*
94 + * A ref_entry represents either a reference or a "subdirectory" of
95 + * references.
96 + *
97 + * Each directory in the reference namespace is represented by a
98 + * ref_entry with (flags & REF_DIR) set and containing a subdir member
99 + * that holds the entries in that directory that have been read so
100 + * far. If (flags & REF_INCOMPLETE) is set, then the directory and
101 + * its subdirectories haven't been read yet. REF_INCOMPLETE is only
102 + * used for loose reference directories.
103 + *
104 + * References are represented by a ref_entry with (flags & REF_DIR)
105 + * unset and a value member that describes the reference's value. The
106 + * flag member is at the ref_entry level, but it is also needed to
107 + * interpret the contents of the value field (in other words, a
108 + * ref_value object is not very much use without the enclosing
109 + * ref_entry).
110 + *
111 + * Reference names cannot end with slash and directories' names are
112 + * always stored with a trailing slash (except for the top-level
113 + * directory, which is always denoted by ""). This has two nice
114 + * consequences: (1) when the entries in each subdir are sorted
115 + * lexicographically by name (as they usually are), the references in
116 + * a whole tree can be generated in lexicographic order by traversing
117 + * the tree in left-to-right, depth-first order; (2) the names of
118 + * references and subdirectories cannot conflict, and therefore the
119 + * presence of an empty subdirectory does not block the creation of a
120 + * similarly-named reference. (The fact that reference names with the
121 + * same leading components can conflict *with each other* is a
122 + * separate issue that is regulated by refs_verify_refname_available().)
123 + *
124 + * Please note that the name field contains the fully-qualified
125 + * reference (or subdirectory) name. Space could be saved by only
126 + * storing the relative names. But that would require the full names
127 + * to be generated on the fly when iterating in do_for_each_ref(), and
128 + * would break callback functions, who have always been able to assume
129 + * that the name strings that they are passed will not be freed during
130 + * the iteration.
131 + */
132 +struct ref_entry {
133 + unsigned char flag; /* ISSYMREF? ISPACKED? */
134 + union {
135 + struct ref_value value; /* if not (flags&REF_DIR) */
136 + struct ref_dir subdir; /* if (flags&REF_DIR) */
137 + } u;
138 + /*
139 + * The full name of the reference (e.g., "refs/heads/master")
140 + * or the full name of the directory with a trailing slash
141 + * (e.g., "refs/heads/"):
142 + */
143 + char name[FLEX_ARRAY];
144 +};
145 +
146 +/*
147 + * Return the index of the entry with the given refname from the
148 + * ref_dir (non-recursively), sorting dir if necessary. Return -1 if
149 + * no such entry is found. dir must already be complete.
150 + */
151 +int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
152 +
153 +struct ref_dir *get_ref_dir(struct ref_entry *entry);
154 +
155 +/*
156 + * Create a struct ref_entry object for the specified dirname.
157 + * dirname is the name of the directory with a trailing slash (e.g.,
158 + * "refs/heads/") or "" for the top-level directory.
159 + */
160 +struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
161 + const char *dirname, size_t len,
162 + int incomplete);
163 +
164 +struct ref_entry *create_ref_entry(const char *refname,
165 + const unsigned char *sha1, int flag,
166 + int check_name);
167 +
168 +void free_ref_entry(struct ref_entry *entry);
169 +
170 +/*
171 + * Add a ref_entry to the end of dir (unsorted). Entry is always
172 + * stored directly in dir; no recursion into subdirectories is
173 + * done.
174 + */
175 +void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
176 +
177 +/*
178 + * Remove the entry with the given name from dir, recursing into
179 + * subdirectories as necessary. If refname is the name of a directory
180 + * (i.e., ends with '/'), then remove the directory and its contents.
181 + * If the removal was successful, return the number of entries
182 + * remaining in the directory entry that contained the deleted entry.
183 + * If the name was not found, return -1. Please note that this
184 + * function only deletes the entry from the cache; it does not delete
185 + * it from the filesystem or ensure that other cache entries (which
186 + * might be symbolic references to the removed entry) are updated.
187 + * Nor does it remove any containing dir entries that might be made
188 + * empty by the removal. dir must represent the top-level directory
189 + * and must already be complete.
190 + */
191 +int remove_entry_from_dir(struct ref_dir *dir, const char *refname);
192 +
193 +/*
194 + * Add a ref_entry to the ref_dir (unsorted), recursing into
195 + * subdirectories as necessary. dir must represent the top-level
196 + * directory. Return 0 on success.
197 + */
198 +int add_ref_entry(struct ref_dir *dir, struct ref_entry *ref);
199 +
200 +/*
201 + * If refname is a reference name, find the ref_dir within the dir
202 + * tree that should hold refname. If refname is a directory name
203 + * (i.e., it ends in '/'), then return that ref_dir itself. dir must
204 + * represent the top-level directory and must already be complete.
205 + * Sort ref_dirs and recurse into subdirectories as necessary. If
206 + * mkdir is set, then create any missing directories; otherwise,
207 + * return NULL if the desired directory cannot be found.
208 + */
209 +struct ref_dir *find_containing_dir(struct ref_dir *dir,
210 + const char *refname, int mkdir);
211 +
212 +/*
213 + * Find the value entry with the given name in dir, sorting ref_dirs
214 + * and recursing into subdirectories as necessary. If the name is not
215 + * found or it corresponds to a directory entry, return NULL.
216 + */
217 +struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname);
218 +
219 +struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir);
220 +
221 +typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
222 +
223 +/*
224 + * Call fn for each reference in dir that has index in the range
225 + * offset <= index < dir->nr. Recurse into subdirectories that are in
226 + * that index range, sorting them before iterating. This function
227 + * does not sort dir itself; it should be sorted beforehand. fn is
228 + * called for all references, including broken ones.
229 + */
230 +int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
231 + each_ref_entry_fn fn, void *cb_data);
232 +
233 +/*
234 + * Peel the entry (if possible) and return its new peel_status. If
235 + * repeel is true, re-peel the entry even if there is an old peeled
236 + * value that is already stored in it.
237 + *
238 + * It is OK to call this function with a packed reference entry that
239 + * might be stale and might even refer to an object that has since
240 + * been garbage-collected. In such a case, if the entry has
241 + * REF_KNOWS_PEELED then leave the status unchanged and return
242 + * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
243 + */
244 +enum peel_status peel_entry(struct ref_entry *entry, int repeel);
245 +
246 +/*
247 + * Load all of the refs from `dir` into our in-memory cache.
248 + */
249 +void prime_ref_dir(struct ref_dir *dir);
250 +
251 +#endif /* REFS_REF_CACHE_H */