1 #include "../git-compat-util.h"
5 #include "../repository.h"
6 #include "refs-internal.h"
8 #include "../iterator.h"
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 if (!dir
->cache
->fill_ref_dir
)
29 BUG("incomplete ref_store without fill_ref_dir function");
31 dir
->cache
->fill_ref_dir(dir
->cache
->ref_store
, dir
, entry
->name
);
32 entry
->flag
&= ~REF_INCOMPLETE
;
37 struct ref_entry
*create_ref_entry(const char *refname
,
38 const struct object_id
*oid
, int flag
)
40 struct ref_entry
*ref
;
42 FLEX_ALLOC_STR(ref
, name
, refname
);
43 oidcpy(&ref
->u
.value
.oid
, oid
);
48 struct ref_cache
*create_ref_cache(struct ref_store
*refs
,
49 fill_ref_dir_fn
*fill_ref_dir
)
51 struct ref_cache
*ret
= xcalloc(1, sizeof(*ret
));
53 ret
->ref_store
= refs
;
54 ret
->fill_ref_dir
= fill_ref_dir
;
55 ret
->root
= create_dir_entry(ret
, "", 0);
59 static void clear_ref_dir(struct ref_dir
*dir
);
61 static void free_ref_entry(struct ref_entry
*entry
)
63 if (entry
->flag
& REF_DIR
) {
65 * Do not use get_ref_dir() here, as that might
66 * trigger the reading of loose refs.
68 clear_ref_dir(&entry
->u
.subdir
);
73 void free_ref_cache(struct ref_cache
*cache
)
75 free_ref_entry(cache
->root
);
80 * Clear and free all entries in dir, recursively.
82 static void clear_ref_dir(struct ref_dir
*dir
)
85 for (i
= 0; i
< dir
->nr
; i
++)
86 free_ref_entry(dir
->entries
[i
]);
87 FREE_AND_NULL(dir
->entries
);
88 dir
->sorted
= dir
->nr
= dir
->alloc
= 0;
91 struct ref_entry
*create_dir_entry(struct ref_cache
*cache
,
92 const char *dirname
, size_t len
)
94 struct ref_entry
*direntry
;
96 FLEX_ALLOC_MEM(direntry
, name
, dirname
, len
);
97 direntry
->u
.subdir
.cache
= cache
;
98 direntry
->flag
= REF_DIR
| REF_INCOMPLETE
;
102 static int ref_entry_cmp(const void *a
, const void *b
)
104 struct ref_entry
*one
= *(struct ref_entry
**)a
;
105 struct ref_entry
*two
= *(struct ref_entry
**)b
;
106 return strcmp(one
->name
, two
->name
);
109 static void sort_ref_dir(struct ref_dir
*dir
);
111 struct string_slice
{
116 static int ref_entry_cmp_sslice(const void *key_
, const void *ent_
)
118 const struct string_slice
*key
= key_
;
119 const struct ref_entry
*ent
= *(const struct ref_entry
* const *)ent_
;
120 int cmp
= strncmp(key
->str
, ent
->name
, key
->len
);
123 return '\0' - (unsigned char)ent
->name
[key
->len
];
126 int search_ref_dir(struct ref_dir
*dir
, const char *refname
, size_t len
)
128 struct ref_entry
**r
;
129 struct string_slice key
;
131 if (refname
== NULL
|| !dir
->nr
)
137 r
= bsearch(&key
, dir
->entries
, dir
->nr
, sizeof(*dir
->entries
),
138 ref_entry_cmp_sslice
);
143 return r
- dir
->entries
;
147 * Search for a directory entry directly within dir (without
148 * recursing). Sort dir if necessary. subdirname must be a directory
149 * name (i.e., end in '/'). Returns NULL if the desired
150 * directory cannot be found. dir must already be complete.
152 static struct ref_dir
*search_for_subdir(struct ref_dir
*dir
,
153 const char *subdirname
, size_t len
)
155 int entry_index
= search_ref_dir(dir
, subdirname
, len
);
156 struct ref_entry
*entry
;
158 if (entry_index
== -1)
161 entry
= dir
->entries
[entry_index
];
162 return get_ref_dir(entry
);
166 * If refname is a reference name, find the ref_dir within the dir
167 * tree that should hold refname. If refname is a directory name
168 * (i.e., it ends in '/'), then return that ref_dir itself. dir must
169 * represent the top-level directory and must already be complete.
170 * Sort ref_dirs and recurse into subdirectories as necessary. Will
171 * return NULL if the desired directory cannot be found.
173 static struct ref_dir
*find_containing_dir(struct ref_dir
*dir
,
177 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
178 size_t dirnamelen
= slash
- refname
+ 1;
179 struct ref_dir
*subdir
;
180 subdir
= search_for_subdir(dir
, refname
, dirnamelen
);
191 struct ref_entry
*find_ref_entry(struct ref_dir
*dir
, const char *refname
)
194 struct ref_entry
*entry
;
195 dir
= find_containing_dir(dir
, refname
);
198 entry_index
= search_ref_dir(dir
, refname
, strlen(refname
));
199 if (entry_index
== -1)
201 entry
= dir
->entries
[entry_index
];
202 return (entry
->flag
& REF_DIR
) ? NULL
: entry
;
206 * Emit a warning and return true iff ref1 and ref2 have the same name
207 * and the same oid. Die if they have the same name but different
210 static int is_dup_ref(const struct ref_entry
*ref1
, const struct ref_entry
*ref2
)
212 if (strcmp(ref1
->name
, ref2
->name
))
215 /* Duplicate name; make sure that they don't conflict: */
217 if ((ref1
->flag
& REF_DIR
) || (ref2
->flag
& REF_DIR
))
218 /* This is impossible by construction */
219 die("Reference directory conflict: %s", ref1
->name
);
221 if (!oideq(&ref1
->u
.value
.oid
, &ref2
->u
.value
.oid
))
222 die("Duplicated ref, and SHA1s don't match: %s", ref1
->name
);
224 warning("Duplicated ref: %s", ref1
->name
);
229 * Sort the entries in dir non-recursively (if they are not already
230 * sorted) and remove any duplicate entries.
232 static void sort_ref_dir(struct ref_dir
*dir
)
235 struct ref_entry
*last
= NULL
;
238 * This check also prevents passing a zero-length array to qsort(),
239 * which is a problem on some platforms.
241 if (dir
->sorted
== dir
->nr
)
244 QSORT(dir
->entries
, dir
->nr
, ref_entry_cmp
);
246 /* Remove any duplicates: */
247 for (i
= 0, j
= 0; j
< dir
->nr
; j
++) {
248 struct ref_entry
*entry
= dir
->entries
[j
];
249 if (last
&& is_dup_ref(last
, entry
))
250 free_ref_entry(entry
);
252 last
= dir
->entries
[i
++] = entry
;
254 dir
->sorted
= dir
->nr
= i
;
258 /* All refs within the directory would match prefix: */
261 /* Some, but not all, refs within the directory might match prefix: */
264 /* No refs within the directory could possibly match prefix: */
269 * Return a `prefix_state` constant describing the relationship
270 * between the directory with the specified `dirname` and `prefix`.
272 static enum prefix_state
overlaps_prefix(const char *dirname
,
275 while (*prefix
&& *dirname
== *prefix
) {
280 return PREFIX_CONTAINS_DIR
;
282 return PREFIX_WITHIN_DIR
;
284 return PREFIX_EXCLUDES_DIR
;
288 * Load all of the refs from `dir` (recursively) that could possibly
289 * contain references matching `prefix` into our in-memory cache. If
290 * `prefix` is NULL, prime unconditionally.
292 static void prime_ref_dir(struct ref_dir
*dir
, const char *prefix
)
295 * The hard work of loading loose refs is done by get_ref_dir(), so we
296 * just need to recurse through all of the sub-directories. We do not
297 * even need to care about sorting, as traversal order does not matter
301 for (i
= 0; i
< dir
->nr
; i
++) {
302 struct ref_entry
*entry
= dir
->entries
[i
];
303 if (!(entry
->flag
& REF_DIR
)) {
304 /* Not a directory; no need to recurse. */
305 } else if (!prefix
) {
306 /* Recurse in any case: */
307 prime_ref_dir(get_ref_dir(entry
), NULL
);
309 switch (overlaps_prefix(entry
->name
, prefix
)) {
310 case PREFIX_CONTAINS_DIR
:
312 * Recurse, and from here down we
313 * don't have to check the prefix
316 prime_ref_dir(get_ref_dir(entry
), NULL
);
318 case PREFIX_WITHIN_DIR
:
319 prime_ref_dir(get_ref_dir(entry
), prefix
);
321 case PREFIX_EXCLUDES_DIR
:
322 /* No need to prime this directory. */
330 * A level in the reference hierarchy that is currently being iterated
333 struct cache_ref_iterator_level
{
335 * The ref_dir being iterated over at this level. The ref_dir
336 * is sorted before being stored here.
340 enum prefix_state prefix_state
;
343 * The index of the current entry within dir (which might
344 * itself be a directory). If index == -1, then the iteration
345 * hasn't yet begun. If index == dir->nr, then the iteration
346 * through this level is over.
352 * Represent an iteration through a ref_dir in the memory cache. The
353 * iteration recurses through subdirectories.
355 struct cache_ref_iterator
{
356 struct ref_iterator base
;
359 * The number of levels currently on the stack. This is always
360 * at least 1, because when it becomes zero the iteration is
361 * ended and this struct is freed.
365 /* The number of levels that have been allocated on the stack */
369 * Only include references with this prefix in the iteration.
370 * The prefix is matched textually, without regard for path
371 * component boundaries.
376 * A stack of levels. levels[0] is the uppermost level that is
377 * being iterated over in this iteration. (This is not
378 * necessary the top level in the references hierarchy. If we
379 * are iterating through a subtree, then levels[0] will hold
380 * the ref_dir for that subtree, and subsequent levels will go
383 struct cache_ref_iterator_level
*levels
;
385 struct repository
*repo
;
388 static int cache_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
390 struct cache_ref_iterator
*iter
=
391 (struct cache_ref_iterator
*)ref_iterator
;
394 struct cache_ref_iterator_level
*level
=
395 &iter
->levels
[iter
->levels_nr
- 1];
396 struct ref_dir
*dir
= level
->dir
;
397 struct ref_entry
*entry
;
398 enum prefix_state entry_prefix_state
;
400 if (level
->index
== -1)
403 if (++level
->index
== level
->dir
->nr
) {
404 /* This level is exhausted; pop up a level */
405 if (--iter
->levels_nr
== 0)
406 return ref_iterator_abort(ref_iterator
);
411 entry
= dir
->entries
[level
->index
];
413 if (level
->prefix_state
== PREFIX_WITHIN_DIR
) {
414 entry_prefix_state
= overlaps_prefix(entry
->name
, iter
->prefix
);
415 if (entry_prefix_state
== PREFIX_EXCLUDES_DIR
)
418 entry_prefix_state
= level
->prefix_state
;
421 if (entry
->flag
& REF_DIR
) {
422 /* push down a level */
423 ALLOC_GROW(iter
->levels
, iter
->levels_nr
+ 1,
426 level
= &iter
->levels
[iter
->levels_nr
++];
427 level
->dir
= get_ref_dir(entry
);
428 level
->prefix_state
= entry_prefix_state
;
431 iter
->base
.refname
= entry
->name
;
432 iter
->base
.oid
= &entry
->u
.value
.oid
;
433 iter
->base
.flags
= entry
->flag
;
439 static int cache_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
440 struct object_id
*peeled
)
442 struct cache_ref_iterator
*iter
=
443 (struct cache_ref_iterator
*)ref_iterator
;
445 if (iter
->repo
!= the_repository
)
446 BUG("peeling for non-the_repository is not supported");
447 return peel_object(ref_iterator
->oid
, peeled
) ? -1 : 0;
450 static int cache_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
452 struct cache_ref_iterator
*iter
=
453 (struct cache_ref_iterator
*)ref_iterator
;
455 free((char *)iter
->prefix
);
457 base_ref_iterator_free(ref_iterator
);
461 static struct ref_iterator_vtable cache_ref_iterator_vtable
= {
462 .advance
= cache_ref_iterator_advance
,
463 .peel
= cache_ref_iterator_peel
,
464 .abort
= cache_ref_iterator_abort
467 struct ref_iterator
*cache_ref_iterator_begin(struct ref_cache
*cache
,
469 struct repository
*repo
,
473 struct cache_ref_iterator
*iter
;
474 struct ref_iterator
*ref_iterator
;
475 struct cache_ref_iterator_level
*level
;
477 dir
= get_ref_dir(cache
->root
);
478 if (prefix
&& *prefix
)
479 dir
= find_containing_dir(dir
, prefix
);
481 /* There's nothing to iterate over. */
482 return empty_ref_iterator_begin();
485 prime_ref_dir(dir
, prefix
);
487 CALLOC_ARRAY(iter
, 1);
488 ref_iterator
= &iter
->base
;
489 base_ref_iterator_init(ref_iterator
, &cache_ref_iterator_vtable
, 1);
490 ALLOC_GROW(iter
->levels
, 10, iter
->levels_alloc
);
493 level
= &iter
->levels
[0];
497 if (prefix
&& *prefix
) {
498 iter
->prefix
= xstrdup(prefix
);
499 level
->prefix_state
= PREFIX_WITHIN_DIR
;
501 level
->prefix_state
= PREFIX_CONTAINS_DIR
;