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
,
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
);
45 ref
->u
.value
.referent
= xstrdup_or_null(referent
);
50 struct ref_cache
*create_ref_cache(struct ref_store
*refs
,
51 fill_ref_dir_fn
*fill_ref_dir
)
53 struct ref_cache
*ret
= xcalloc(1, sizeof(*ret
));
55 ret
->ref_store
= refs
;
56 ret
->fill_ref_dir
= fill_ref_dir
;
57 ret
->root
= create_dir_entry(ret
, "", 0);
61 static void clear_ref_dir(struct ref_dir
*dir
);
63 static void free_ref_entry(struct ref_entry
*entry
)
65 if (entry
->flag
& REF_DIR
) {
67 * Do not use get_ref_dir() here, as that might
68 * trigger the reading of loose refs.
70 clear_ref_dir(&entry
->u
.subdir
);
72 free(entry
->u
.value
.referent
);
77 void free_ref_cache(struct ref_cache
*cache
)
81 free_ref_entry(cache
->root
);
86 * Clear and free all entries in dir, recursively.
88 static void clear_ref_dir(struct ref_dir
*dir
)
91 for (i
= 0; i
< dir
->nr
; i
++)
92 free_ref_entry(dir
->entries
[i
]);
93 FREE_AND_NULL(dir
->entries
);
94 dir
->sorted
= dir
->nr
= dir
->alloc
= 0;
97 struct ref_entry
*create_dir_entry(struct ref_cache
*cache
,
98 const char *dirname
, size_t len
)
100 struct ref_entry
*direntry
;
102 FLEX_ALLOC_MEM(direntry
, name
, dirname
, len
);
103 direntry
->u
.subdir
.cache
= cache
;
104 direntry
->flag
= REF_DIR
| REF_INCOMPLETE
;
108 static int ref_entry_cmp(const void *a
, const void *b
)
110 struct ref_entry
*one
= *(struct ref_entry
**)a
;
111 struct ref_entry
*two
= *(struct ref_entry
**)b
;
112 return strcmp(one
->name
, two
->name
);
115 static void sort_ref_dir(struct ref_dir
*dir
);
117 struct string_slice
{
122 static int ref_entry_cmp_sslice(const void *key_
, const void *ent_
)
124 const struct string_slice
*key
= key_
;
125 const struct ref_entry
*ent
= *(const struct ref_entry
* const *)ent_
;
126 int cmp
= strncmp(key
->str
, ent
->name
, key
->len
);
129 return '\0' - (unsigned char)ent
->name
[key
->len
];
132 int search_ref_dir(struct ref_dir
*dir
, const char *refname
, size_t len
)
134 struct ref_entry
**r
;
135 struct string_slice key
;
137 if (refname
== NULL
|| !dir
->nr
)
143 r
= bsearch(&key
, dir
->entries
, dir
->nr
, sizeof(*dir
->entries
),
144 ref_entry_cmp_sslice
);
149 return r
- dir
->entries
;
153 * Search for a directory entry directly within dir (without
154 * recursing). Sort dir if necessary. subdirname must be a directory
155 * name (i.e., end in '/'). Returns NULL if the desired
156 * directory cannot be found. dir must already be complete.
158 static struct ref_dir
*search_for_subdir(struct ref_dir
*dir
,
159 const char *subdirname
, size_t len
)
161 int entry_index
= search_ref_dir(dir
, subdirname
, len
);
162 struct ref_entry
*entry
;
164 if (entry_index
== -1)
167 entry
= dir
->entries
[entry_index
];
168 return get_ref_dir(entry
);
172 * If refname is a reference name, find the ref_dir within the dir
173 * tree that should hold refname. If refname is a directory name
174 * (i.e., it ends in '/'), then return that ref_dir itself. dir must
175 * represent the top-level directory and must already be complete.
176 * Sort ref_dirs and recurse into subdirectories as necessary. Will
177 * return NULL if the desired directory cannot be found.
179 static struct ref_dir
*find_containing_dir(struct ref_dir
*dir
,
183 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
184 size_t dirnamelen
= slash
- refname
+ 1;
185 struct ref_dir
*subdir
;
186 subdir
= search_for_subdir(dir
, refname
, dirnamelen
);
197 struct ref_entry
*find_ref_entry(struct ref_dir
*dir
, const char *refname
)
200 struct ref_entry
*entry
;
201 dir
= find_containing_dir(dir
, refname
);
204 entry_index
= search_ref_dir(dir
, refname
, strlen(refname
));
205 if (entry_index
== -1)
207 entry
= dir
->entries
[entry_index
];
208 return (entry
->flag
& REF_DIR
) ? NULL
: entry
;
212 * Emit a warning and return true iff ref1 and ref2 have the same name
213 * and the same oid. Die if they have the same name but different
216 static int is_dup_ref(const struct ref_entry
*ref1
, const struct ref_entry
*ref2
)
218 if (strcmp(ref1
->name
, ref2
->name
))
221 /* Duplicate name; make sure that they don't conflict: */
223 if ((ref1
->flag
& REF_DIR
) || (ref2
->flag
& REF_DIR
))
224 /* This is impossible by construction */
225 die("Reference directory conflict: %s", ref1
->name
);
227 if (!oideq(&ref1
->u
.value
.oid
, &ref2
->u
.value
.oid
))
228 die("Duplicated ref, and SHA1s don't match: %s", ref1
->name
);
230 warning("Duplicated ref: %s", ref1
->name
);
235 * Sort the entries in dir non-recursively (if they are not already
236 * sorted) and remove any duplicate entries.
238 static void sort_ref_dir(struct ref_dir
*dir
)
241 struct ref_entry
*last
= NULL
;
244 * This check also prevents passing a zero-length array to qsort(),
245 * which is a problem on some platforms.
247 if (dir
->sorted
== dir
->nr
)
250 QSORT(dir
->entries
, dir
->nr
, ref_entry_cmp
);
252 /* Remove any duplicates: */
253 for (i
= 0, j
= 0; j
< dir
->nr
; j
++) {
254 struct ref_entry
*entry
= dir
->entries
[j
];
255 if (last
&& is_dup_ref(last
, entry
))
256 free_ref_entry(entry
);
258 last
= dir
->entries
[i
++] = entry
;
260 dir
->sorted
= dir
->nr
= i
;
264 /* All refs within the directory would match prefix: */
267 /* Some, but not all, refs within the directory might match prefix: */
270 /* No refs within the directory could possibly match prefix: */
275 * Return a `prefix_state` constant describing the relationship
276 * between the directory with the specified `dirname` and `prefix`.
278 static enum prefix_state
overlaps_prefix(const char *dirname
,
281 while (*prefix
&& *dirname
== *prefix
) {
286 return PREFIX_CONTAINS_DIR
;
288 return PREFIX_WITHIN_DIR
;
290 return PREFIX_EXCLUDES_DIR
;
294 * Load all of the refs from `dir` (recursively) that could possibly
295 * contain references matching `prefix` into our in-memory cache. If
296 * `prefix` is NULL, prime unconditionally.
298 static void prime_ref_dir(struct ref_dir
*dir
, const char *prefix
)
301 * The hard work of loading loose refs is done by get_ref_dir(), so we
302 * just need to recurse through all of the sub-directories. We do not
303 * even need to care about sorting, as traversal order does not matter
307 for (i
= 0; i
< dir
->nr
; i
++) {
308 struct ref_entry
*entry
= dir
->entries
[i
];
309 if (!(entry
->flag
& REF_DIR
)) {
310 /* Not a directory; no need to recurse. */
311 } else if (!prefix
) {
312 /* Recurse in any case: */
313 prime_ref_dir(get_ref_dir(entry
), NULL
);
315 switch (overlaps_prefix(entry
->name
, prefix
)) {
316 case PREFIX_CONTAINS_DIR
:
318 * Recurse, and from here down we
319 * don't have to check the prefix
322 prime_ref_dir(get_ref_dir(entry
), NULL
);
324 case PREFIX_WITHIN_DIR
:
325 prime_ref_dir(get_ref_dir(entry
), prefix
);
327 case PREFIX_EXCLUDES_DIR
:
328 /* No need to prime this directory. */
336 * A level in the reference hierarchy that is currently being iterated
339 struct cache_ref_iterator_level
{
341 * The ref_dir being iterated over at this level. The ref_dir
342 * is sorted before being stored here.
346 enum prefix_state prefix_state
;
349 * The index of the current entry within dir (which might
350 * itself be a directory). If index == -1, then the iteration
351 * hasn't yet begun. If index == dir->nr, then the iteration
352 * through this level is over.
358 * Represent an iteration through a ref_dir in the memory cache. The
359 * iteration recurses through subdirectories.
361 struct cache_ref_iterator
{
362 struct ref_iterator base
;
365 * The number of levels currently on the stack.
369 /* The number of levels that have been allocated on the stack */
373 * Only include references with this prefix in the iteration.
374 * The prefix is matched textually, without regard for path
375 * component boundaries.
380 * A stack of levels. levels[0] is the uppermost level that is
381 * being iterated over in this iteration. (This is not
382 * necessary the top level in the references hierarchy. If we
383 * are iterating through a subtree, then levels[0] will hold
384 * the ref_dir for that subtree, and subsequent levels will go
387 struct cache_ref_iterator_level
*levels
;
389 struct repository
*repo
;
390 struct ref_cache
*cache
;
395 static int cache_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
397 struct cache_ref_iterator
*iter
=
398 (struct cache_ref_iterator
*)ref_iterator
;
400 if (!iter
->levels_nr
)
404 struct cache_ref_iterator_level
*level
=
405 &iter
->levels
[iter
->levels_nr
- 1];
406 struct ref_dir
*dir
= level
->dir
;
407 struct ref_entry
*entry
;
408 enum prefix_state entry_prefix_state
;
410 if (level
->index
== -1)
413 if (++level
->index
== level
->dir
->nr
) {
414 /* This level is exhausted; pop up a level */
415 if (--iter
->levels_nr
== 0)
421 entry
= dir
->entries
[level
->index
];
423 if (level
->prefix_state
== PREFIX_WITHIN_DIR
) {
424 entry_prefix_state
= overlaps_prefix(entry
->name
, iter
->prefix
);
425 if (entry_prefix_state
== PREFIX_EXCLUDES_DIR
||
426 (entry_prefix_state
== PREFIX_WITHIN_DIR
&& !(entry
->flag
& REF_DIR
)))
429 entry_prefix_state
= level
->prefix_state
;
432 if (entry
->flag
& REF_DIR
) {
433 /* push down a level */
434 ALLOC_GROW(iter
->levels
, iter
->levels_nr
+ 1,
437 level
= &iter
->levels
[iter
->levels_nr
++];
438 level
->dir
= get_ref_dir(entry
);
439 level
->prefix_state
= entry_prefix_state
;
442 iter
->base
.refname
= entry
->name
;
443 iter
->base
.referent
= entry
->u
.value
.referent
;
444 iter
->base
.oid
= &entry
->u
.value
.oid
;
445 iter
->base
.flags
= entry
->flag
;
451 static int cache_ref_iterator_seek(struct ref_iterator
*ref_iterator
,
454 struct cache_ref_iterator
*iter
=
455 (struct cache_ref_iterator
*)ref_iterator
;
456 struct cache_ref_iterator_level
*level
;
459 dir
= get_ref_dir(iter
->cache
->root
);
460 if (prefix
&& *prefix
)
461 dir
= find_containing_dir(dir
, prefix
);
468 prime_ref_dir(dir
, prefix
);
470 level
= &iter
->levels
[0];
474 if (prefix
&& *prefix
) {
476 iter
->prefix
= xstrdup(prefix
);
477 level
->prefix_state
= PREFIX_WITHIN_DIR
;
479 FREE_AND_NULL(iter
->prefix
);
480 level
->prefix_state
= PREFIX_CONTAINS_DIR
;
486 static int cache_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
487 struct object_id
*peeled
)
489 struct cache_ref_iterator
*iter
=
490 (struct cache_ref_iterator
*)ref_iterator
;
491 return peel_object(iter
->repo
, ref_iterator
->oid
, peeled
) ? -1 : 0;
494 static void cache_ref_iterator_release(struct ref_iterator
*ref_iterator
)
496 struct cache_ref_iterator
*iter
=
497 (struct cache_ref_iterator
*)ref_iterator
;
502 static struct ref_iterator_vtable cache_ref_iterator_vtable
= {
503 .advance
= cache_ref_iterator_advance
,
504 .seek
= cache_ref_iterator_seek
,
505 .peel
= cache_ref_iterator_peel
,
506 .release
= cache_ref_iterator_release
,
509 struct ref_iterator
*cache_ref_iterator_begin(struct ref_cache
*cache
,
511 struct repository
*repo
,
514 struct cache_ref_iterator
*iter
;
515 struct ref_iterator
*ref_iterator
;
517 CALLOC_ARRAY(iter
, 1);
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
);
524 iter
->prime_dir
= prime_dir
;
526 if (cache_ref_iterator_seek(&iter
->base
, prefix
) < 0) {
527 ref_iterator_free(&iter
->base
);