]>
Commit | Line | Data |
---|---|---|
958f9646 MH |
1 | #include "../cache.h" |
2 | #include "../refs.h" | |
3 | #include "refs-internal.h" | |
4 | #include "ref-cache.h" | |
5 | #include "../iterator.h" | |
6 | ||
958f9646 MH |
7 | void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry) |
8 | { | |
9 | ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc); | |
10 | dir->entries[dir->nr++] = entry; | |
11 | /* optimize for the case that entries are added in order */ | |
12 | if (dir->nr == 1 || | |
13 | (dir->nr == dir->sorted + 1 && | |
14 | strcmp(dir->entries[dir->nr - 2]->name, | |
15 | dir->entries[dir->nr - 1]->name) < 0)) | |
16 | dir->sorted = dir->nr; | |
17 | } | |
18 | ||
19 | struct ref_dir *get_ref_dir(struct ref_entry *entry) | |
20 | { | |
21 | struct ref_dir *dir; | |
22 | assert(entry->flag & REF_DIR); | |
23 | dir = &entry->u.subdir; | |
24 | if (entry->flag & REF_INCOMPLETE) { | |
df308759 | 25 | if (!dir->cache->fill_ref_dir) |
033abf97 | 26 | BUG("incomplete ref_store without fill_ref_dir function"); |
df308759 MH |
27 | |
28 | dir->cache->fill_ref_dir(dir->cache->ref_store, dir, entry->name); | |
958f9646 MH |
29 | entry->flag &= ~REF_INCOMPLETE; |
30 | } | |
31 | return dir; | |
32 | } | |
33 | ||
34 | struct ref_entry *create_ref_entry(const char *refname, | |
c1da06c6 | 35 | const struct object_id *oid, int flag) |
958f9646 MH |
36 | { |
37 | struct ref_entry *ref; | |
38 | ||
958f9646 | 39 | FLEX_ALLOC_STR(ref, name, refname); |
4417df8c | 40 | oidcpy(&ref->u.value.oid, oid); |
958f9646 MH |
41 | ref->flag = flag; |
42 | return ref; | |
43 | } | |
44 | ||
df308759 MH |
45 | struct ref_cache *create_ref_cache(struct ref_store *refs, |
46 | fill_ref_dir_fn *fill_ref_dir) | |
7c22bc8a MH |
47 | { |
48 | struct ref_cache *ret = xcalloc(1, sizeof(*ret)); | |
49 | ||
e00d1a4f | 50 | ret->ref_store = refs; |
df308759 | 51 | ret->fill_ref_dir = fill_ref_dir; |
e00d1a4f | 52 | ret->root = create_dir_entry(ret, "", 0, 1); |
7c22bc8a MH |
53 | return ret; |
54 | } | |
55 | ||
958f9646 MH |
56 | static void clear_ref_dir(struct ref_dir *dir); |
57 | ||
7c22bc8a | 58 | static void free_ref_entry(struct ref_entry *entry) |
958f9646 MH |
59 | { |
60 | if (entry->flag & REF_DIR) { | |
61 | /* | |
62 | * Do not use get_ref_dir() here, as that might | |
63 | * trigger the reading of loose refs. | |
64 | */ | |
65 | clear_ref_dir(&entry->u.subdir); | |
66 | } | |
67 | free(entry); | |
68 | } | |
69 | ||
7c22bc8a MH |
70 | void free_ref_cache(struct ref_cache *cache) |
71 | { | |
72 | free_ref_entry(cache->root); | |
73 | free(cache); | |
74 | } | |
75 | ||
958f9646 MH |
76 | /* |
77 | * Clear and free all entries in dir, recursively. | |
78 | */ | |
79 | static void clear_ref_dir(struct ref_dir *dir) | |
80 | { | |
81 | int i; | |
82 | for (i = 0; i < dir->nr; i++) | |
83 | free_ref_entry(dir->entries[i]); | |
88ce3ef6 | 84 | FREE_AND_NULL(dir->entries); |
958f9646 | 85 | dir->sorted = dir->nr = dir->alloc = 0; |
958f9646 MH |
86 | } |
87 | ||
e00d1a4f | 88 | struct ref_entry *create_dir_entry(struct ref_cache *cache, |
958f9646 MH |
89 | const char *dirname, size_t len, |
90 | int incomplete) | |
91 | { | |
92 | struct ref_entry *direntry; | |
e00d1a4f | 93 | |
958f9646 | 94 | FLEX_ALLOC_MEM(direntry, name, dirname, len); |
e00d1a4f | 95 | direntry->u.subdir.cache = cache; |
958f9646 MH |
96 | direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0); |
97 | return direntry; | |
98 | } | |
99 | ||
100 | static int ref_entry_cmp(const void *a, const void *b) | |
101 | { | |
102 | struct ref_entry *one = *(struct ref_entry **)a; | |
103 | struct ref_entry *two = *(struct ref_entry **)b; | |
104 | return strcmp(one->name, two->name); | |
105 | } | |
106 | ||
107 | static void sort_ref_dir(struct ref_dir *dir); | |
108 | ||
109 | struct string_slice { | |
110 | size_t len; | |
111 | const char *str; | |
112 | }; | |
113 | ||
114 | static int ref_entry_cmp_sslice(const void *key_, const void *ent_) | |
115 | { | |
116 | const struct string_slice *key = key_; | |
117 | const struct ref_entry *ent = *(const struct ref_entry * const *)ent_; | |
118 | int cmp = strncmp(key->str, ent->name, key->len); | |
119 | if (cmp) | |
120 | return cmp; | |
121 | return '\0' - (unsigned char)ent->name[key->len]; | |
122 | } | |
123 | ||
124 | int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len) | |
125 | { | |
126 | struct ref_entry **r; | |
127 | struct string_slice key; | |
128 | ||
129 | if (refname == NULL || !dir->nr) | |
130 | return -1; | |
131 | ||
132 | sort_ref_dir(dir); | |
133 | key.len = len; | |
134 | key.str = refname; | |
135 | r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries), | |
136 | ref_entry_cmp_sslice); | |
137 | ||
138 | if (r == NULL) | |
139 | return -1; | |
140 | ||
141 | return r - dir->entries; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Search for a directory entry directly within dir (without | |
146 | * recursing). Sort dir if necessary. subdirname must be a directory | |
147 | * name (i.e., end in '/'). If mkdir is set, then create the | |
148 | * directory if it is missing; otherwise, return NULL if the desired | |
149 | * directory cannot be found. dir must already be complete. | |
150 | */ | |
151 | static struct ref_dir *search_for_subdir(struct ref_dir *dir, | |
152 | const char *subdirname, size_t len, | |
153 | int mkdir) | |
154 | { | |
155 | int entry_index = search_ref_dir(dir, subdirname, len); | |
156 | struct ref_entry *entry; | |
157 | if (entry_index == -1) { | |
158 | if (!mkdir) | |
159 | return NULL; | |
160 | /* | |
161 | * Since dir is complete, the absence of a subdir | |
162 | * means that the subdir really doesn't exist; | |
163 | * therefore, create an empty record for it but mark | |
164 | * the record complete. | |
165 | */ | |
e00d1a4f | 166 | entry = create_dir_entry(dir->cache, subdirname, len, 0); |
958f9646 MH |
167 | add_entry_to_dir(dir, entry); |
168 | } else { | |
169 | entry = dir->entries[entry_index]; | |
170 | } | |
171 | return get_ref_dir(entry); | |
172 | } | |
173 | ||
059ae35a MH |
174 | /* |
175 | * If refname is a reference name, find the ref_dir within the dir | |
176 | * tree that should hold refname. If refname is a directory name | |
177 | * (i.e., it ends in '/'), then return that ref_dir itself. dir must | |
178 | * represent the top-level directory and must already be complete. | |
179 | * Sort ref_dirs and recurse into subdirectories as necessary. If | |
180 | * mkdir is set, then create any missing directories; otherwise, | |
181 | * return NULL if the desired directory cannot be found. | |
182 | */ | |
183 | static struct ref_dir *find_containing_dir(struct ref_dir *dir, | |
184 | const char *refname, int mkdir) | |
958f9646 MH |
185 | { |
186 | const char *slash; | |
187 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { | |
188 | size_t dirnamelen = slash - refname + 1; | |
189 | struct ref_dir *subdir; | |
190 | subdir = search_for_subdir(dir, refname, dirnamelen, mkdir); | |
191 | if (!subdir) { | |
192 | dir = NULL; | |
193 | break; | |
194 | } | |
195 | dir = subdir; | |
196 | } | |
197 | ||
198 | return dir; | |
199 | } | |
200 | ||
201 | struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname) | |
202 | { | |
203 | int entry_index; | |
204 | struct ref_entry *entry; | |
205 | dir = find_containing_dir(dir, refname, 0); | |
206 | if (!dir) | |
207 | return NULL; | |
208 | entry_index = search_ref_dir(dir, refname, strlen(refname)); | |
209 | if (entry_index == -1) | |
210 | return NULL; | |
211 | entry = dir->entries[entry_index]; | |
212 | return (entry->flag & REF_DIR) ? NULL : entry; | |
213 | } | |
214 | ||
958f9646 MH |
215 | /* |
216 | * Emit a warning and return true iff ref1 and ref2 have the same name | |
78fb4579 MH |
217 | * and the same oid. Die if they have the same name but different |
218 | * oids. | |
958f9646 MH |
219 | */ |
220 | static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2) | |
221 | { | |
222 | if (strcmp(ref1->name, ref2->name)) | |
223 | return 0; | |
224 | ||
225 | /* Duplicate name; make sure that they don't conflict: */ | |
226 | ||
227 | if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR)) | |
228 | /* This is impossible by construction */ | |
229 | die("Reference directory conflict: %s", ref1->name); | |
230 | ||
9001dc2a | 231 | if (!oideq(&ref1->u.value.oid, &ref2->u.value.oid)) |
958f9646 MH |
232 | die("Duplicated ref, and SHA1s don't match: %s", ref1->name); |
233 | ||
234 | warning("Duplicated ref: %s", ref1->name); | |
235 | return 1; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Sort the entries in dir non-recursively (if they are not already | |
240 | * sorted) and remove any duplicate entries. | |
241 | */ | |
242 | static void sort_ref_dir(struct ref_dir *dir) | |
243 | { | |
244 | int i, j; | |
245 | struct ref_entry *last = NULL; | |
246 | ||
247 | /* | |
248 | * This check also prevents passing a zero-length array to qsort(), | |
249 | * which is a problem on some platforms. | |
250 | */ | |
251 | if (dir->sorted == dir->nr) | |
252 | return; | |
253 | ||
254 | QSORT(dir->entries, dir->nr, ref_entry_cmp); | |
255 | ||
256 | /* Remove any duplicates: */ | |
257 | for (i = 0, j = 0; j < dir->nr; j++) { | |
258 | struct ref_entry *entry = dir->entries[j]; | |
259 | if (last && is_dup_ref(last, entry)) | |
260 | free_ref_entry(entry); | |
261 | else | |
262 | last = dir->entries[i++] = entry; | |
263 | } | |
264 | dir->sorted = dir->nr = i; | |
265 | } | |
266 | ||
f23092f1 MH |
267 | enum prefix_state { |
268 | /* All refs within the directory would match prefix: */ | |
269 | PREFIX_CONTAINS_DIR, | |
270 | ||
271 | /* Some, but not all, refs within the directory might match prefix: */ | |
272 | PREFIX_WITHIN_DIR, | |
273 | ||
274 | /* No refs within the directory could possibly match prefix: */ | |
275 | PREFIX_EXCLUDES_DIR | |
276 | }; | |
277 | ||
059ae35a | 278 | /* |
f23092f1 MH |
279 | * Return a `prefix_state` constant describing the relationship |
280 | * between the directory with the specified `dirname` and `prefix`. | |
059ae35a | 281 | */ |
f23092f1 MH |
282 | static enum prefix_state overlaps_prefix(const char *dirname, |
283 | const char *prefix) | |
284 | { | |
285 | while (*prefix && *dirname == *prefix) { | |
286 | dirname++; | |
287 | prefix++; | |
288 | } | |
289 | if (!*prefix) | |
290 | return PREFIX_CONTAINS_DIR; | |
291 | else if (!*dirname) | |
292 | return PREFIX_WITHIN_DIR; | |
293 | else | |
294 | return PREFIX_EXCLUDES_DIR; | |
295 | } | |
296 | ||
297 | /* | |
298 | * Load all of the refs from `dir` (recursively) that could possibly | |
299 | * contain references matching `prefix` into our in-memory cache. If | |
300 | * `prefix` is NULL, prime unconditionally. | |
301 | */ | |
302 | static void prime_ref_dir(struct ref_dir *dir, const char *prefix) | |
958f9646 MH |
303 | { |
304 | /* | |
305 | * The hard work of loading loose refs is done by get_ref_dir(), so we | |
306 | * just need to recurse through all of the sub-directories. We do not | |
307 | * even need to care about sorting, as traversal order does not matter | |
308 | * to us. | |
309 | */ | |
310 | int i; | |
311 | for (i = 0; i < dir->nr; i++) { | |
312 | struct ref_entry *entry = dir->entries[i]; | |
f23092f1 MH |
313 | if (!(entry->flag & REF_DIR)) { |
314 | /* Not a directory; no need to recurse. */ | |
315 | } else if (!prefix) { | |
316 | /* Recurse in any case: */ | |
317 | prime_ref_dir(get_ref_dir(entry), NULL); | |
318 | } else { | |
319 | switch (overlaps_prefix(entry->name, prefix)) { | |
320 | case PREFIX_CONTAINS_DIR: | |
321 | /* | |
322 | * Recurse, and from here down we | |
323 | * don't have to check the prefix | |
324 | * anymore: | |
325 | */ | |
326 | prime_ref_dir(get_ref_dir(entry), NULL); | |
327 | break; | |
328 | case PREFIX_WITHIN_DIR: | |
329 | prime_ref_dir(get_ref_dir(entry), prefix); | |
330 | break; | |
331 | case PREFIX_EXCLUDES_DIR: | |
332 | /* No need to prime this directory. */ | |
333 | break; | |
334 | } | |
335 | } | |
958f9646 MH |
336 | } |
337 | } | |
338 | ||
339 | /* | |
340 | * A level in the reference hierarchy that is currently being iterated | |
341 | * through. | |
342 | */ | |
343 | struct cache_ref_iterator_level { | |
344 | /* | |
345 | * The ref_dir being iterated over at this level. The ref_dir | |
346 | * is sorted before being stored here. | |
347 | */ | |
348 | struct ref_dir *dir; | |
349 | ||
f23092f1 MH |
350 | enum prefix_state prefix_state; |
351 | ||
958f9646 MH |
352 | /* |
353 | * The index of the current entry within dir (which might | |
354 | * itself be a directory). If index == -1, then the iteration | |
355 | * hasn't yet begun. If index == dir->nr, then the iteration | |
356 | * through this level is over. | |
357 | */ | |
358 | int index; | |
359 | }; | |
360 | ||
361 | /* | |
362 | * Represent an iteration through a ref_dir in the memory cache. The | |
363 | * iteration recurses through subdirectories. | |
364 | */ | |
365 | struct cache_ref_iterator { | |
366 | struct ref_iterator base; | |
367 | ||
368 | /* | |
369 | * The number of levels currently on the stack. This is always | |
370 | * at least 1, because when it becomes zero the iteration is | |
371 | * ended and this struct is freed. | |
372 | */ | |
373 | size_t levels_nr; | |
374 | ||
375 | /* The number of levels that have been allocated on the stack */ | |
376 | size_t levels_alloc; | |
377 | ||
f23092f1 MH |
378 | /* |
379 | * Only include references with this prefix in the iteration. | |
380 | * The prefix is matched textually, without regard for path | |
381 | * component boundaries. | |
382 | */ | |
383 | const char *prefix; | |
384 | ||
958f9646 MH |
385 | /* |
386 | * A stack of levels. levels[0] is the uppermost level that is | |
387 | * being iterated over in this iteration. (This is not | |
388 | * necessary the top level in the references hierarchy. If we | |
389 | * are iterating through a subtree, then levels[0] will hold | |
390 | * the ref_dir for that subtree, and subsequent levels will go | |
391 | * on from there.) | |
392 | */ | |
393 | struct cache_ref_iterator_level *levels; | |
394 | }; | |
395 | ||
396 | static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator) | |
397 | { | |
398 | struct cache_ref_iterator *iter = | |
399 | (struct cache_ref_iterator *)ref_iterator; | |
400 | ||
401 | while (1) { | |
402 | struct cache_ref_iterator_level *level = | |
403 | &iter->levels[iter->levels_nr - 1]; | |
404 | struct ref_dir *dir = level->dir; | |
405 | struct ref_entry *entry; | |
f23092f1 | 406 | enum prefix_state entry_prefix_state; |
958f9646 MH |
407 | |
408 | if (level->index == -1) | |
409 | sort_ref_dir(dir); | |
410 | ||
411 | if (++level->index == level->dir->nr) { | |
412 | /* This level is exhausted; pop up a level */ | |
413 | if (--iter->levels_nr == 0) | |
414 | return ref_iterator_abort(ref_iterator); | |
415 | ||
416 | continue; | |
417 | } | |
418 | ||
419 | entry = dir->entries[level->index]; | |
420 | ||
f23092f1 MH |
421 | if (level->prefix_state == PREFIX_WITHIN_DIR) { |
422 | entry_prefix_state = overlaps_prefix(entry->name, iter->prefix); | |
423 | if (entry_prefix_state == PREFIX_EXCLUDES_DIR) | |
424 | continue; | |
425 | } else { | |
426 | entry_prefix_state = level->prefix_state; | |
427 | } | |
428 | ||
958f9646 MH |
429 | if (entry->flag & REF_DIR) { |
430 | /* push down a level */ | |
431 | ALLOC_GROW(iter->levels, iter->levels_nr + 1, | |
432 | iter->levels_alloc); | |
433 | ||
434 | level = &iter->levels[iter->levels_nr++]; | |
435 | level->dir = get_ref_dir(entry); | |
f23092f1 | 436 | level->prefix_state = entry_prefix_state; |
958f9646 MH |
437 | level->index = -1; |
438 | } else { | |
439 | iter->base.refname = entry->name; | |
440 | iter->base.oid = &entry->u.value.oid; | |
441 | iter->base.flags = entry->flag; | |
442 | return ITER_OK; | |
443 | } | |
444 | } | |
445 | } | |
446 | ||
958f9646 MH |
447 | static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator, |
448 | struct object_id *peeled) | |
449 | { | |
617480d7 | 450 | return peel_object(ref_iterator->oid, peeled) ? -1 : 0; |
958f9646 MH |
451 | } |
452 | ||
453 | static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
454 | { | |
455 | struct cache_ref_iterator *iter = | |
456 | (struct cache_ref_iterator *)ref_iterator; | |
457 | ||
f23092f1 | 458 | free((char *)iter->prefix); |
958f9646 MH |
459 | free(iter->levels); |
460 | base_ref_iterator_free(ref_iterator); | |
461 | return ITER_DONE; | |
462 | } | |
463 | ||
464 | static struct ref_iterator_vtable cache_ref_iterator_vtable = { | |
465 | cache_ref_iterator_advance, | |
466 | cache_ref_iterator_peel, | |
467 | cache_ref_iterator_abort | |
468 | }; | |
469 | ||
059ae35a MH |
470 | struct ref_iterator *cache_ref_iterator_begin(struct ref_cache *cache, |
471 | const char *prefix, | |
472 | int prime_dir) | |
958f9646 | 473 | { |
059ae35a | 474 | struct ref_dir *dir; |
958f9646 MH |
475 | struct cache_ref_iterator *iter; |
476 | struct ref_iterator *ref_iterator; | |
477 | struct cache_ref_iterator_level *level; | |
478 | ||
059ae35a MH |
479 | dir = get_ref_dir(cache->root); |
480 | if (prefix && *prefix) | |
481 | dir = find_containing_dir(dir, prefix, 0); | |
482 | if (!dir) | |
483 | /* There's nothing to iterate over. */ | |
f23092f1 | 484 | return empty_ref_iterator_begin(); |
059ae35a MH |
485 | |
486 | if (prime_dir) | |
f23092f1 | 487 | prime_ref_dir(dir, prefix); |
059ae35a | 488 | |
ca56dadb | 489 | CALLOC_ARRAY(iter, 1); |
958f9646 | 490 | ref_iterator = &iter->base; |
8738a8a4 | 491 | base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable, 1); |
958f9646 MH |
492 | ALLOC_GROW(iter->levels, 10, iter->levels_alloc); |
493 | ||
494 | iter->levels_nr = 1; | |
495 | level = &iter->levels[0]; | |
496 | level->index = -1; | |
497 | level->dir = dir; | |
498 | ||
f23092f1 MH |
499 | if (prefix && *prefix) { |
500 | iter->prefix = xstrdup(prefix); | |
501 | level->prefix_state = PREFIX_WITHIN_DIR; | |
502 | } else { | |
503 | level->prefix_state = PREFIX_CONTAINS_DIR; | |
504 | } | |
059ae35a | 505 | |
958f9646 MH |
506 | return ref_iterator; |
507 | } |