1 #include "../git-compat-util.h"
4 #include "../repository.h"
5 #include "refs-internal.h"
7 #include "../iterator.h"
9 void add_entry_to_dir(struct ref_dir
*dir
, struct ref_entry
*entry
)
11 ALLOC_GROW(dir
->entries
, dir
->nr
+ 1, dir
->alloc
);
12 dir
->entries
[dir
->nr
++] = entry
;
13 /* optimize for the case that entries are added in order */
15 (dir
->nr
== dir
->sorted
+ 1 &&
16 strcmp(dir
->entries
[dir
->nr
- 2]->name
,
17 dir
->entries
[dir
->nr
- 1]->name
) < 0))
18 dir
->sorted
= dir
->nr
;
21 struct ref_dir
*get_ref_dir(struct ref_entry
*entry
)
24 assert(entry
->flag
& REF_DIR
);
25 dir
= &entry
->u
.subdir
;
26 if (entry
->flag
& REF_INCOMPLETE
) {
27 if (!dir
->cache
->fill_ref_dir
)
28 BUG("incomplete ref_store without fill_ref_dir function");
30 dir
->cache
->fill_ref_dir(dir
->cache
->ref_store
, dir
, entry
->name
);
31 entry
->flag
&= ~REF_INCOMPLETE
;
36 struct ref_entry
*create_ref_entry(const char *refname
,
37 const struct object_id
*oid
, int flag
)
39 struct ref_entry
*ref
;
41 FLEX_ALLOC_STR(ref
, name
, refname
);
42 oidcpy(&ref
->u
.value
.oid
, oid
);
47 struct ref_cache
*create_ref_cache(struct ref_store
*refs
,
48 fill_ref_dir_fn
*fill_ref_dir
)
50 struct ref_cache
*ret
= xcalloc(1, sizeof(*ret
));
52 ret
->ref_store
= refs
;
53 ret
->fill_ref_dir
= fill_ref_dir
;
54 ret
->root
= create_dir_entry(ret
, "", 0);
58 static void clear_ref_dir(struct ref_dir
*dir
);
60 static void free_ref_entry(struct ref_entry
*entry
)
62 if (entry
->flag
& REF_DIR
) {
64 * Do not use get_ref_dir() here, as that might
65 * trigger the reading of loose refs.
67 clear_ref_dir(&entry
->u
.subdir
);
72 void free_ref_cache(struct ref_cache
*cache
)
74 free_ref_entry(cache
->root
);
79 * Clear and free all entries in dir, recursively.
81 static void clear_ref_dir(struct ref_dir
*dir
)
84 for (i
= 0; i
< dir
->nr
; i
++)
85 free_ref_entry(dir
->entries
[i
]);
86 FREE_AND_NULL(dir
->entries
);
87 dir
->sorted
= dir
->nr
= dir
->alloc
= 0;
90 struct ref_entry
*create_dir_entry(struct ref_cache
*cache
,
91 const char *dirname
, size_t len
)
93 struct ref_entry
*direntry
;
95 FLEX_ALLOC_MEM(direntry
, name
, dirname
, len
);
96 direntry
->u
.subdir
.cache
= cache
;
97 direntry
->flag
= REF_DIR
| REF_INCOMPLETE
;
101 static int ref_entry_cmp(const void *a
, const void *b
)
103 struct ref_entry
*one
= *(struct ref_entry
**)a
;
104 struct ref_entry
*two
= *(struct ref_entry
**)b
;
105 return strcmp(one
->name
, two
->name
);
108 static void sort_ref_dir(struct ref_dir
*dir
);
110 struct string_slice
{
115 static int ref_entry_cmp_sslice(const void *key_
, const void *ent_
)
117 const struct string_slice
*key
= key_
;
118 const struct ref_entry
*ent
= *(const struct ref_entry
* const *)ent_
;
119 int cmp
= strncmp(key
->str
, ent
->name
, key
->len
);
122 return '\0' - (unsigned char)ent
->name
[key
->len
];
125 int search_ref_dir(struct ref_dir
*dir
, const char *refname
, size_t len
)
127 struct ref_entry
**r
;
128 struct string_slice key
;
130 if (refname
== NULL
|| !dir
->nr
)
136 r
= bsearch(&key
, dir
->entries
, dir
->nr
, sizeof(*dir
->entries
),
137 ref_entry_cmp_sslice
);
142 return r
- dir
->entries
;
146 * Search for a directory entry directly within dir (without
147 * recursing). Sort dir if necessary. subdirname must be a directory
148 * name (i.e., end in '/'). Returns NULL if the desired
149 * directory cannot be found. dir must already be complete.
151 static struct ref_dir
*search_for_subdir(struct ref_dir
*dir
,
152 const char *subdirname
, size_t len
)
154 int entry_index
= search_ref_dir(dir
, subdirname
, len
);
155 struct ref_entry
*entry
;
157 if (entry_index
== -1)
160 entry
= dir
->entries
[entry_index
];
161 return get_ref_dir(entry
);
165 * If refname is a reference name, find the ref_dir within the dir
166 * tree that should hold refname. If refname is a directory name
167 * (i.e., it ends in '/'), then return that ref_dir itself. dir must
168 * represent the top-level directory and must already be complete.
169 * Sort ref_dirs and recurse into subdirectories as necessary. Will
170 * return NULL if the desired directory cannot be found.
172 static struct ref_dir
*find_containing_dir(struct ref_dir
*dir
,
176 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
177 size_t dirnamelen
= slash
- refname
+ 1;
178 struct ref_dir
*subdir
;
179 subdir
= search_for_subdir(dir
, refname
, dirnamelen
);
190 struct ref_entry
*find_ref_entry(struct ref_dir
*dir
, const char *refname
)
193 struct ref_entry
*entry
;
194 dir
= find_containing_dir(dir
, refname
);
197 entry_index
= search_ref_dir(dir
, refname
, strlen(refname
));
198 if (entry_index
== -1)
200 entry
= dir
->entries
[entry_index
];
201 return (entry
->flag
& REF_DIR
) ? NULL
: entry
;
205 * Emit a warning and return true iff ref1 and ref2 have the same name
206 * and the same oid. Die if they have the same name but different
209 static int is_dup_ref(const struct ref_entry
*ref1
, const struct ref_entry
*ref2
)
211 if (strcmp(ref1
->name
, ref2
->name
))
214 /* Duplicate name; make sure that they don't conflict: */
216 if ((ref1
->flag
& REF_DIR
) || (ref2
->flag
& REF_DIR
))
217 /* This is impossible by construction */
218 die("Reference directory conflict: %s", ref1
->name
);
220 if (!oideq(&ref1
->u
.value
.oid
, &ref2
->u
.value
.oid
))
221 die("Duplicated ref, and SHA1s don't match: %s", ref1
->name
);
223 warning("Duplicated ref: %s", ref1
->name
);
228 * Sort the entries in dir non-recursively (if they are not already
229 * sorted) and remove any duplicate entries.
231 static void sort_ref_dir(struct ref_dir
*dir
)
234 struct ref_entry
*last
= NULL
;
237 * This check also prevents passing a zero-length array to qsort(),
238 * which is a problem on some platforms.
240 if (dir
->sorted
== dir
->nr
)
243 QSORT(dir
->entries
, dir
->nr
, ref_entry_cmp
);
245 /* Remove any duplicates: */
246 for (i
= 0, j
= 0; j
< dir
->nr
; j
++) {
247 struct ref_entry
*entry
= dir
->entries
[j
];
248 if (last
&& is_dup_ref(last
, entry
))
249 free_ref_entry(entry
);
251 last
= dir
->entries
[i
++] = entry
;
253 dir
->sorted
= dir
->nr
= i
;
257 /* All refs within the directory would match prefix: */
260 /* Some, but not all, refs within the directory might match prefix: */
263 /* No refs within the directory could possibly match prefix: */
268 * Return a `prefix_state` constant describing the relationship
269 * between the directory with the specified `dirname` and `prefix`.
271 static enum prefix_state
overlaps_prefix(const char *dirname
,
274 while (*prefix
&& *dirname
== *prefix
) {
279 return PREFIX_CONTAINS_DIR
;
281 return PREFIX_WITHIN_DIR
;
283 return PREFIX_EXCLUDES_DIR
;
287 * Load all of the refs from `dir` (recursively) that could possibly
288 * contain references matching `prefix` into our in-memory cache. If
289 * `prefix` is NULL, prime unconditionally.
291 static void prime_ref_dir(struct ref_dir
*dir
, const char *prefix
)
294 * The hard work of loading loose refs is done by get_ref_dir(), so we
295 * just need to recurse through all of the sub-directories. We do not
296 * even need to care about sorting, as traversal order does not matter
300 for (i
= 0; i
< dir
->nr
; i
++) {
301 struct ref_entry
*entry
= dir
->entries
[i
];
302 if (!(entry
->flag
& REF_DIR
)) {
303 /* Not a directory; no need to recurse. */
304 } else if (!prefix
) {
305 /* Recurse in any case: */
306 prime_ref_dir(get_ref_dir(entry
), NULL
);
308 switch (overlaps_prefix(entry
->name
, prefix
)) {
309 case PREFIX_CONTAINS_DIR
:
311 * Recurse, and from here down we
312 * don't have to check the prefix
315 prime_ref_dir(get_ref_dir(entry
), NULL
);
317 case PREFIX_WITHIN_DIR
:
318 prime_ref_dir(get_ref_dir(entry
), prefix
);
320 case PREFIX_EXCLUDES_DIR
:
321 /* No need to prime this directory. */
329 * A level in the reference hierarchy that is currently being iterated
332 struct cache_ref_iterator_level
{
334 * The ref_dir being iterated over at this level. The ref_dir
335 * is sorted before being stored here.
339 enum prefix_state prefix_state
;
342 * The index of the current entry within dir (which might
343 * itself be a directory). If index == -1, then the iteration
344 * hasn't yet begun. If index == dir->nr, then the iteration
345 * through this level is over.
351 * Represent an iteration through a ref_dir in the memory cache. The
352 * iteration recurses through subdirectories.
354 struct cache_ref_iterator
{
355 struct ref_iterator base
;
358 * The number of levels currently on the stack. This is always
359 * at least 1, because when it becomes zero the iteration is
360 * ended and this struct is freed.
364 /* The number of levels that have been allocated on the stack */
368 * Only include references with this prefix in the iteration.
369 * The prefix is matched textually, without regard for path
370 * component boundaries.
375 * A stack of levels. levels[0] is the uppermost level that is
376 * being iterated over in this iteration. (This is not
377 * necessary the top level in the references hierarchy. If we
378 * are iterating through a subtree, then levels[0] will hold
379 * the ref_dir for that subtree, and subsequent levels will go
382 struct cache_ref_iterator_level
*levels
;
384 struct repository
*repo
;
387 static int cache_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
389 struct cache_ref_iterator
*iter
=
390 (struct cache_ref_iterator
*)ref_iterator
;
393 struct cache_ref_iterator_level
*level
=
394 &iter
->levels
[iter
->levels_nr
- 1];
395 struct ref_dir
*dir
= level
->dir
;
396 struct ref_entry
*entry
;
397 enum prefix_state entry_prefix_state
;
399 if (level
->index
== -1)
402 if (++level
->index
== level
->dir
->nr
) {
403 /* This level is exhausted; pop up a level */
404 if (--iter
->levels_nr
== 0)
405 return ref_iterator_abort(ref_iterator
);
410 entry
= dir
->entries
[level
->index
];
412 if (level
->prefix_state
== PREFIX_WITHIN_DIR
) {
413 entry_prefix_state
= overlaps_prefix(entry
->name
, iter
->prefix
);
414 if (entry_prefix_state
== PREFIX_EXCLUDES_DIR
||
415 (entry_prefix_state
== PREFIX_WITHIN_DIR
&& !(entry
->flag
& REF_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
;