]> git.proxmox.com Git - libgit2.git/blob - src/iterator.c
Merge remote-tracking branch 'origin/development' into ssh_transport
[libgit2.git] / src / iterator.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "iterator.h"
9 #include "tree.h"
10 #include "ignore.h"
11 #include "buffer.h"
12 #include "git2/submodule.h"
13 #include <ctype.h>
14
15 #define ITERATOR_SET_CB(P,NAME_LC) do { \
16 (P)->cb.current = NAME_LC ## _iterator__current; \
17 (P)->cb.advance = NAME_LC ## _iterator__advance; \
18 (P)->cb.advance_into = NAME_LC ## _iterator__advance_into; \
19 (P)->cb.seek = NAME_LC ## _iterator__seek; \
20 (P)->cb.reset = NAME_LC ## _iterator__reset; \
21 (P)->cb.at_end = NAME_LC ## _iterator__at_end; \
22 (P)->cb.free = NAME_LC ## _iterator__free; \
23 } while (0)
24
25 #define ITERATOR_CASE_FLAGS \
26 (GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_IGNORE_CASE)
27
28 #define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC,REPO) do { \
29 (P)->base.type = GIT_ITERATOR_TYPE_ ## NAME_UC; \
30 (P)->base.cb = &(P)->cb; \
31 ITERATOR_SET_CB(P,NAME_LC); \
32 (P)->base.repo = (REPO); \
33 (P)->base.start = start ? git__strdup(start) : NULL; \
34 (P)->base.end = end ? git__strdup(end) : NULL; \
35 if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
36 git__free(P); return -1; } \
37 (P)->base.prefixcomp = git__prefixcmp; \
38 (P)->base.flags = flags & ~ITERATOR_CASE_FLAGS; \
39 if ((P)->base.flags & GIT_ITERATOR_DONT_AUTOEXPAND) \
40 (P)->base.flags |= GIT_ITERATOR_INCLUDE_TREES; \
41 } while (0)
42
43 #define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
44 #define iterator__ignore_case(I) iterator__flag(I,IGNORE_CASE)
45 #define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES)
46 #define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
47 #define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND)
48
49 #define iterator__end(I) ((git_iterator *)(I))->end
50 #define iterator__past_end(I,PATH) \
51 (iterator__end(I) && ((git_iterator *)(I))->prefixcomp((PATH),iterator__end(I)) > 0)
52
53
54 static int iterator__reset_range(
55 git_iterator *iter, const char *start, const char *end)
56 {
57 if (start) {
58 if (iter->start)
59 git__free(iter->start);
60 iter->start = git__strdup(start);
61 GITERR_CHECK_ALLOC(iter->start);
62 }
63
64 if (end) {
65 if (iter->end)
66 git__free(iter->end);
67 iter->end = git__strdup(end);
68 GITERR_CHECK_ALLOC(iter->end);
69 }
70
71 return 0;
72 }
73
74 static int iterator__update_ignore_case(
75 git_iterator *iter,
76 git_iterator_flag_t flags)
77 {
78 int error = 0, ignore_case = -1;
79
80 if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
81 ignore_case = true;
82 else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0)
83 ignore_case = false;
84 else {
85 git_index *index;
86
87 if (!(error = git_repository_index__weakptr(&index, iter->repo)))
88 ignore_case = (index->ignore_case != false);
89 }
90
91 if (ignore_case > 0)
92 iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
93 else if (ignore_case == 0)
94 iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
95
96 iter->prefixcomp = iterator__ignore_case(iter) ?
97 git__prefixcmp_icase : git__prefixcmp;
98
99 return error;
100 }
101
102 GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
103 {
104 if (entry) *entry = NULL;
105 }
106
107
108 static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
109 {
110 GIT_UNUSED(i);
111 iterator__clear_entry(e);
112 return 0;
113 }
114
115 static int empty_iterator__seek(git_iterator *i, const char *p)
116 {
117 GIT_UNUSED(i); GIT_UNUSED(p);
118 return -1;
119 }
120
121 static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
122 {
123 GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
124 return 0;
125 }
126
127 static int empty_iterator__at_end(git_iterator *i)
128 {
129 GIT_UNUSED(i);
130 return 1;
131 }
132
133 static void empty_iterator__free(git_iterator *i)
134 {
135 GIT_UNUSED(i);
136 }
137
138 typedef struct {
139 git_iterator base;
140 git_iterator_callbacks cb;
141 } empty_iterator;
142
143 int git_iterator_for_nothing(
144 git_iterator **iter,
145 git_iterator_flag_t flags,
146 const char *start,
147 const char *end)
148 {
149 empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
150 GITERR_CHECK_ALLOC(i);
151
152 #define empty_iterator__current empty_iterator__noop
153 #define empty_iterator__advance empty_iterator__noop
154 #define empty_iterator__advance_into empty_iterator__noop
155
156 ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
157
158 if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
159 i->base.flags |= GIT_ITERATOR_IGNORE_CASE;
160
161 *iter = (git_iterator *)i;
162 return 0;
163 }
164
165
166 typedef struct tree_iterator_entry tree_iterator_entry;
167 struct tree_iterator_entry {
168 tree_iterator_entry *parent;
169 const git_tree_entry *te;
170 git_tree *tree;
171 };
172
173 typedef struct tree_iterator_frame tree_iterator_frame;
174 struct tree_iterator_frame {
175 tree_iterator_frame *up, *down;
176
177 size_t n_entries; /* items in this frame */
178 size_t current; /* start of currently active range in frame */
179 size_t next; /* start of next range in frame */
180
181 const char *start;
182 size_t startlen;
183
184 tree_iterator_entry *entries[GIT_FLEX_ARRAY];
185 };
186
187 typedef struct {
188 git_iterator base;
189 git_iterator_callbacks cb;
190 tree_iterator_frame *head, *root;
191 git_pool pool;
192 git_index_entry entry;
193 git_buf path;
194 int path_ambiguities;
195 bool path_has_filename;
196 int (*strncomp)(const char *a, const char *b, size_t sz);
197 } tree_iterator;
198
199 static char *tree_iterator__current_filename(
200 tree_iterator *ti, const git_tree_entry *te)
201 {
202 if (!ti->path_has_filename) {
203 if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
204 return NULL;
205
206 if (git_tree_entry__is_tree(te) && git_buf_putc(&ti->path, '/') < 0)
207 return NULL;
208
209 ti->path_has_filename = true;
210 }
211
212 return ti->path.ptr;
213 }
214
215 static void tree_iterator__rewrite_filename(tree_iterator *ti)
216 {
217 tree_iterator_entry *scan = ti->head->entries[ti->head->current];
218 ssize_t strpos = ti->path.size;
219 const git_tree_entry *te;
220
221 if (strpos && ti->path.ptr[strpos - 1] == '/')
222 strpos--;
223
224 for (; scan && (te = scan->te); scan = scan->parent) {
225 strpos -= te->filename_len;
226 memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
227 strpos -= 1; /* separator */
228 }
229 }
230
231 static int tree_iterator__te_cmp(
232 const git_tree_entry *a,
233 const git_tree_entry *b,
234 int (*compare)(const char *, const char *, size_t))
235 {
236 return git_path_cmp(
237 a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
238 b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
239 compare);
240 }
241
242 static int tree_iterator__ci_cmp(const void *a, const void *b, void *p)
243 {
244 const tree_iterator_entry *ae = a, *be = b;
245 int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp);
246
247 if (!cmp) {
248 /* stabilize sort order among equivalent names */
249 if (!ae->parent->te || !be->parent->te)
250 cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncmp);
251 else
252 cmp = tree_iterator__ci_cmp(ae->parent, be->parent, p);
253 }
254
255 return cmp;
256 }
257
258 static int tree_iterator__search_cmp(const void *key, const void *val, void *p)
259 {
260 const tree_iterator_frame *tf = key;
261 const git_tree_entry *te = ((tree_iterator_entry *)val)->te;
262
263 return git_path_cmp(
264 tf->start, tf->startlen, false,
265 te->filename, te->filename_len, te->attr == GIT_FILEMODE_TREE,
266 ((tree_iterator *)p)->strncomp);
267 }
268
269 static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
270 {
271 int error;
272 const git_tree_entry *te, *last = NULL;
273
274 tf->next = tf->current;
275
276 for (; tf->next < tf->n_entries; tf->next++, last = te) {
277 te = tf->entries[tf->next]->te;
278
279 if (last && tree_iterator__te_cmp(last, te, ti->strncomp))
280 break;
281
282 /* load trees for items in [current,next) range */
283 if (git_tree_entry__is_tree(te) &&
284 (error = git_tree_lookup(
285 &tf->entries[tf->next]->tree, ti->base.repo, &te->oid)) < 0)
286 return error;
287 }
288
289 if (tf->next > tf->current + 1)
290 ti->path_ambiguities++;
291
292 if (last && !tree_iterator__current_filename(ti, last))
293 return -1;
294
295 return 0;
296 }
297
298 GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
299 {
300 return (ti->head->current < ti->head->n_entries &&
301 ti->head->entries[ti->head->current]->tree != NULL);
302 }
303
304 static int tree_iterator__push_frame(tree_iterator *ti)
305 {
306 int error = 0;
307 tree_iterator_frame *head = ti->head, *tf = NULL;
308 size_t i, n_entries = 0;
309
310 if (head->current >= head->n_entries || !head->entries[head->current]->tree)
311 return 0;
312
313 for (i = head->current; i < head->next; ++i)
314 n_entries += git_tree_entrycount(head->entries[i]->tree);
315
316 tf = git__calloc(sizeof(tree_iterator_frame) +
317 n_entries * sizeof(tree_iterator_entry *), 1);
318 GITERR_CHECK_ALLOC(tf);
319
320 tf->n_entries = n_entries;
321
322 tf->up = head;
323 head->down = tf;
324 ti->head = tf;
325
326 for (i = head->current, n_entries = 0; i < head->next; ++i) {
327 git_tree *tree = head->entries[i]->tree;
328 size_t j, max_j = git_tree_entrycount(tree);
329
330 for (j = 0; j < max_j; ++j) {
331 tree_iterator_entry *entry = git_pool_malloc(&ti->pool, 1);
332 GITERR_CHECK_ALLOC(entry);
333
334 entry->parent = head->entries[i];
335 entry->te = git_tree_entry_byindex(tree, j);
336 entry->tree = NULL;
337
338 tf->entries[n_entries++] = entry;
339 }
340 }
341
342 /* if ignore_case, sort entries case insensitively */
343 if (iterator__ignore_case(ti))
344 git__tsort_r(
345 (void **)tf->entries, tf->n_entries, tree_iterator__ci_cmp, tf);
346
347 /* pick tf->current based on "start" (or start at zero) */
348 if (head->startlen > 0) {
349 git__bsearch_r((void **)tf->entries, tf->n_entries, head,
350 tree_iterator__search_cmp, ti, &tf->current);
351
352 while (tf->current &&
353 !tree_iterator__search_cmp(head, tf->entries[tf->current-1], ti))
354 tf->current--;
355
356 if ((tf->start = strchr(head->start, '/')) != NULL) {
357 tf->start++;
358 tf->startlen = strlen(tf->start);
359 }
360 }
361
362 ti->path_has_filename = false;
363
364 if ((error = tree_iterator__set_next(ti, tf)) < 0)
365 return error;
366
367 /* autoexpand as needed */
368 if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
369 return tree_iterator__push_frame(ti);
370
371 return 0;
372 }
373
374 static bool tree_iterator__move_to_next(
375 tree_iterator *ti, tree_iterator_frame *tf)
376 {
377 if (tf->next > tf->current + 1)
378 ti->path_ambiguities--;
379
380 if (!tf->up) { /* at root */
381 tf->current = tf->next;
382 return false;
383 }
384
385 for (; tf->current < tf->next; tf->current++) {
386 git_tree_free(tf->entries[tf->current]->tree);
387 tf->entries[tf->current]->tree = NULL;
388 }
389
390 return (tf->current < tf->n_entries);
391 }
392
393 static bool tree_iterator__pop_frame(tree_iterator *ti, bool final)
394 {
395 tree_iterator_frame *tf = ti->head;
396
397 if (!tf->up)
398 return false;
399
400 ti->head = tf->up;
401 ti->head->down = NULL;
402
403 tree_iterator__move_to_next(ti, tf);
404
405 if (!final) { /* if final, don't bother to clean up */
406 git_pool_free_array(&ti->pool, tf->n_entries, (void **)tf->entries);
407 git_buf_rtruncate_at_char(&ti->path, '/');
408 }
409
410 git__free(tf);
411
412 return true;
413 }
414
415 static int tree_iterator__pop_all(tree_iterator *ti, bool to_end, bool final)
416 {
417 while (tree_iterator__pop_frame(ti, final)) /* pop to root */;
418
419 if (!final) {
420 ti->head->current = to_end ? ti->head->n_entries : 0;
421 ti->path_ambiguities = 0;
422 git_buf_clear(&ti->path);
423 }
424
425 return 0;
426 }
427
428 static int tree_iterator__current(
429 const git_index_entry **entry, git_iterator *self)
430 {
431 tree_iterator *ti = (tree_iterator *)self;
432 tree_iterator_frame *tf = ti->head;
433 const git_tree_entry *te;
434
435 iterator__clear_entry(entry);
436
437 if (tf->current >= tf->n_entries)
438 return 0;
439 te = tf->entries[tf->current]->te;
440
441 ti->entry.mode = te->attr;
442 git_oid_cpy(&ti->entry.oid, &te->oid);
443
444 ti->entry.path = tree_iterator__current_filename(ti, te);
445 GITERR_CHECK_ALLOC(ti->entry.path);
446
447 if (ti->path_ambiguities > 0)
448 tree_iterator__rewrite_filename(ti);
449
450 if (iterator__past_end(ti, ti->entry.path))
451 return tree_iterator__pop_all(ti, true, false);
452
453 if (entry)
454 *entry = &ti->entry;
455
456 return 0;
457 }
458
459 static int tree_iterator__advance_into(
460 const git_index_entry **entry, git_iterator *self)
461 {
462 int error = 0;
463 tree_iterator *ti = (tree_iterator *)self;
464
465 iterator__clear_entry(entry);
466
467 if (tree_iterator__at_tree(ti) &&
468 !(error = tree_iterator__push_frame(ti)))
469 error = tree_iterator__current(entry, self);
470
471 return error;
472 }
473
474 static int tree_iterator__advance(
475 const git_index_entry **entry, git_iterator *self)
476 {
477 int error;
478 tree_iterator *ti = (tree_iterator *)self;
479 tree_iterator_frame *tf = ti->head;
480
481 iterator__clear_entry(entry);
482
483 if (tf->current > tf->n_entries)
484 return 0;
485
486 if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
487 tree_iterator__at_tree(ti))
488 return tree_iterator__advance_into(entry, self);
489
490 if (ti->path_has_filename) {
491 git_buf_rtruncate_at_char(&ti->path, '/');
492 ti->path_has_filename = false;
493 }
494
495 /* scan forward and up, advancing in frame or popping frame when done */
496 while (!tree_iterator__move_to_next(ti, tf) &&
497 tree_iterator__pop_frame(ti, false))
498 tf = ti->head;
499
500 /* find next and load trees */
501 if ((error = tree_iterator__set_next(ti, tf)) < 0)
502 return error;
503
504 /* deal with include_trees / auto_expand as needed */
505 if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
506 return tree_iterator__advance_into(entry, self);
507
508 return tree_iterator__current(entry, self);
509 }
510
511 static int tree_iterator__seek(git_iterator *self, const char *prefix)
512 {
513 GIT_UNUSED(self); GIT_UNUSED(prefix);
514 return -1;
515 }
516
517 static int tree_iterator__reset(
518 git_iterator *self, const char *start, const char *end)
519 {
520 tree_iterator *ti = (tree_iterator *)self;
521
522 tree_iterator__pop_all(ti, false, false);
523
524 if (iterator__reset_range(self, start, end) < 0)
525 return -1;
526
527 return tree_iterator__push_frame(ti); /* re-expand root tree */
528 }
529
530 static int tree_iterator__at_end(git_iterator *self)
531 {
532 tree_iterator *ti = (tree_iterator *)self;
533 return (ti->head->current >= ti->head->n_entries);
534 }
535
536 static void tree_iterator__free(git_iterator *self)
537 {
538 tree_iterator *ti = (tree_iterator *)self;
539
540 tree_iterator__pop_all(ti, true, false);
541
542 git_tree_free(ti->head->entries[0]->tree);
543 git__free(ti->head);
544 git_pool_clear(&ti->pool);
545 git_buf_free(&ti->path);
546 }
547
548 static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree)
549 {
550 size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
551 tree_iterator_frame *root = git__calloc(sz, sizeof(char));
552 GITERR_CHECK_ALLOC(root);
553
554 root->n_entries = 1;
555 root->next = 1;
556 root->start = ti->base.start;
557 root->startlen = root->start ? strlen(root->start) : 0;
558 root->entries[0] = git_pool_mallocz(&ti->pool, 1);
559 GITERR_CHECK_ALLOC(root->entries[0]);
560 root->entries[0]->tree = tree;
561
562 ti->head = ti->root = root;
563
564 return 0;
565 }
566
567 int git_iterator_for_tree(
568 git_iterator **iter,
569 git_tree *tree,
570 git_iterator_flag_t flags,
571 const char *start,
572 const char *end)
573 {
574 int error;
575 tree_iterator *ti;
576
577 if (tree == NULL)
578 return git_iterator_for_nothing(iter, flags, start, end);
579
580 if ((error = git_object_dup((git_object **)&tree, (git_object *)tree)) < 0)
581 return error;
582
583 ti = git__calloc(1, sizeof(tree_iterator));
584 GITERR_CHECK_ALLOC(ti);
585
586 ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
587
588 if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
589 goto fail;
590 ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
591
592 if ((error = git_pool_init(&ti->pool, sizeof(tree_iterator_entry),0)) < 0 ||
593 (error = tree_iterator__create_root_frame(ti, tree)) < 0 ||
594 (error = tree_iterator__push_frame(ti)) < 0) /* expand root now */
595 goto fail;
596
597 *iter = (git_iterator *)ti;
598 return 0;
599
600 fail:
601 git_iterator_free((git_iterator *)ti);
602 return error;
603 }
604
605
606 typedef struct {
607 git_iterator base;
608 git_iterator_callbacks cb;
609 git_index *index;
610 size_t current;
611 /* when not in autoexpand mode, use these to represent "tree" state */
612 git_buf partial;
613 size_t partial_pos;
614 char restore_terminator;
615 git_index_entry tree_entry;
616 } index_iterator;
617
618 static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
619 {
620 const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
621
622 if (ie != NULL && iterator__past_end(ii, ie->path)) {
623 ii->current = git_index_entrycount(ii->index);
624 ie = NULL;
625 }
626
627 return ie;
628 }
629
630 static const git_index_entry *index_iterator__skip_conflicts(index_iterator *ii)
631 {
632 const git_index_entry *ie;
633
634 while ((ie = index_iterator__index_entry(ii)) != NULL &&
635 git_index_entry_stage(ie) != 0)
636 ii->current++;
637
638 return ie;
639 }
640
641 static void index_iterator__next_prefix_tree(index_iterator *ii)
642 {
643 const char *slash;
644
645 if (!iterator__include_trees(ii))
646 return;
647
648 slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');
649
650 if (slash != NULL) {
651 ii->partial_pos = (slash - ii->partial.ptr) + 1;
652 ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
653 ii->partial.ptr[ii->partial_pos] = '\0';
654 } else {
655 ii->partial_pos = ii->partial.size;
656 }
657
658 if (index_iterator__index_entry(ii) == NULL)
659 ii->partial_pos = ii->partial.size;
660 }
661
662 static int index_iterator__first_prefix_tree(index_iterator *ii)
663 {
664 const git_index_entry *ie = index_iterator__skip_conflicts(ii);
665 const char *scan, *prior, *slash;
666
667 if (!ie || !iterator__include_trees(ii))
668 return 0;
669
670 /* find longest common prefix with prior index entry */
671 for (scan = slash = ie->path, prior = ii->partial.ptr;
672 *scan && *scan == *prior; ++scan, ++prior)
673 if (*scan == '/')
674 slash = scan;
675
676 if (git_buf_sets(&ii->partial, ie->path) < 0)
677 return -1;
678
679 ii->partial_pos = (slash - ie->path) + 1;
680 index_iterator__next_prefix_tree(ii);
681
682 return 0;
683 }
684
685 #define index_iterator__at_tree(I) \
686 (iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)
687
688 static int index_iterator__current(
689 const git_index_entry **entry, git_iterator *self)
690 {
691 index_iterator *ii = (index_iterator *)self;
692 const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
693
694 if (ie != NULL && index_iterator__at_tree(ii)) {
695 ii->tree_entry.path = ii->partial.ptr;
696 ie = &ii->tree_entry;
697 }
698
699 if (entry)
700 *entry = ie;
701
702 return 0;
703 }
704
705 static int index_iterator__at_end(git_iterator *self)
706 {
707 index_iterator *ii = (index_iterator *)self;
708 return (ii->current >= git_index_entrycount(ii->index));
709 }
710
711 static int index_iterator__advance(
712 const git_index_entry **entry, git_iterator *self)
713 {
714 index_iterator *ii = (index_iterator *)self;
715 size_t entrycount = git_index_entrycount(ii->index);
716 const git_index_entry *ie;
717
718 if (index_iterator__at_tree(ii)) {
719 if (iterator__do_autoexpand(ii)) {
720 ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
721 index_iterator__next_prefix_tree(ii);
722 } else {
723 /* advance to sibling tree (i.e. find entry with new prefix) */
724 while (ii->current < entrycount) {
725 ii->current++;
726
727 if (!(ie = git_index_get_byindex(ii->index, ii->current)) ||
728 ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
729 break;
730 }
731
732 if (index_iterator__first_prefix_tree(ii) < 0)
733 return -1;
734 }
735 } else {
736 if (ii->current < entrycount)
737 ii->current++;
738
739 if (index_iterator__first_prefix_tree(ii) < 0)
740 return -1;
741 }
742
743 return index_iterator__current(entry, self);
744 }
745
746 static int index_iterator__advance_into(
747 const git_index_entry **entry, git_iterator *self)
748 {
749 index_iterator *ii = (index_iterator *)self;
750 const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
751
752 if (ie != NULL && index_iterator__at_tree(ii)) {
753 if (ii->restore_terminator)
754 ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
755 index_iterator__next_prefix_tree(ii);
756 }
757
758 return index_iterator__current(entry, self);
759 }
760
761 static int index_iterator__seek(git_iterator *self, const char *prefix)
762 {
763 GIT_UNUSED(self); GIT_UNUSED(prefix);
764 return -1;
765 }
766
767 static int index_iterator__reset(
768 git_iterator *self, const char *start, const char *end)
769 {
770 index_iterator *ii = (index_iterator *)self;
771 const git_index_entry *ie;
772
773 if (iterator__reset_range(self, start, end) < 0)
774 return -1;
775
776 ii->current = ii->base.start ?
777 git_index__prefix_position(ii->index, ii->base.start) : 0;
778
779 if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
780 return 0;
781
782 if (git_buf_sets(&ii->partial, ie->path) < 0)
783 return -1;
784
785 ii->partial_pos = 0;
786
787 if (ii->base.start) {
788 size_t startlen = strlen(ii->base.start);
789
790 ii->partial_pos = (startlen > ii->partial.size) ?
791 ii->partial.size : startlen;
792 }
793
794 index_iterator__next_prefix_tree(ii);
795
796 return 0;
797 }
798
799 static void index_iterator__free(git_iterator *self)
800 {
801 index_iterator *ii = (index_iterator *)self;
802 git_index_free(ii->index);
803 ii->index = NULL;
804
805 git_buf_free(&ii->partial);
806 }
807
808 int git_iterator_for_index(
809 git_iterator **iter,
810 git_index *index,
811 git_iterator_flag_t flags,
812 const char *start,
813 const char *end)
814 {
815 index_iterator *ii = git__calloc(1, sizeof(index_iterator));
816 GITERR_CHECK_ALLOC(ii);
817
818 ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
819
820 if (index->ignore_case) {
821 ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
822 ii->base.prefixcomp = git__prefixcmp_icase;
823 }
824
825 ii->index = index;
826 GIT_REFCOUNT_INC(index);
827
828 git_buf_init(&ii->partial, 0);
829 ii->tree_entry.mode = GIT_FILEMODE_TREE;
830
831 index_iterator__reset((git_iterator *)ii, NULL, NULL);
832
833 *iter = (git_iterator *)ii;
834
835 return 0;
836 }
837
838
839 typedef struct fs_iterator_frame fs_iterator_frame;
840 struct fs_iterator_frame {
841 fs_iterator_frame *next;
842 git_vector entries;
843 size_t index;
844 };
845
846 typedef struct fs_iterator fs_iterator;
847 struct fs_iterator {
848 git_iterator base;
849 git_iterator_callbacks cb;
850 fs_iterator_frame *stack;
851 git_index_entry entry;
852 git_buf path;
853 size_t root_len;
854 int depth;
855
856 int (*enter_dir_cb)(fs_iterator *self);
857 int (*leave_dir_cb)(fs_iterator *self);
858 int (*update_entry_cb)(fs_iterator *self);
859 };
860
861 #define FS_MAX_DEPTH 100
862
863 static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
864 {
865 fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame));
866 git_vector_cmp entry_compare = CASESELECT(
867 iterator__ignore_case(fi),
868 git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
869
870 if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) {
871 git__free(ff);
872 ff = NULL;
873 }
874
875 return ff;
876 }
877
878 static void fs_iterator__free_frame(fs_iterator_frame *ff)
879 {
880 size_t i;
881 git_path_with_stat *path;
882
883 git_vector_foreach(&ff->entries, i, path)
884 git__free(path);
885 git_vector_free(&ff->entries);
886 git__free(ff);
887 }
888
889 static void fs_iterator__pop_frame(
890 fs_iterator *fi, fs_iterator_frame *ff, bool pop_last)
891 {
892 if (fi && fi->stack == ff) {
893 if (!ff->next && !pop_last) {
894 memset(&fi->entry, 0, sizeof(fi->entry));
895 return;
896 }
897
898 if (fi->leave_dir_cb)
899 (void)fi->leave_dir_cb(fi);
900
901 fi->stack = ff->next;
902 fi->depth--;
903 }
904
905 fs_iterator__free_frame(ff);
906 }
907
908 static int fs_iterator__update_entry(fs_iterator *fi);
909
910 static int fs_iterator__entry_cmp(const void *i, const void *item)
911 {
912 const fs_iterator *fi = (const fs_iterator *)i;
913 const git_path_with_stat *ps = item;
914 return fi->base.prefixcomp(fi->base.start, ps->path);
915 }
916
917 static void fs_iterator__seek_frame_start(
918 fs_iterator *fi, fs_iterator_frame *ff)
919 {
920 if (!ff)
921 return;
922
923 if (fi->base.start)
924 git_vector_bsearch2(
925 &ff->index, &ff->entries, fs_iterator__entry_cmp, fi);
926 else
927 ff->index = 0;
928 }
929
930 static int fs_iterator__expand_dir(fs_iterator *fi)
931 {
932 int error;
933 fs_iterator_frame *ff;
934
935 if (fi->depth > FS_MAX_DEPTH) {
936 giterr_set(GITERR_REPOSITORY,
937 "Directory nesting is too deep (%d)", fi->depth);
938 return -1;
939 }
940
941 ff = fs_iterator__alloc_frame(fi);
942 GITERR_CHECK_ALLOC(ff);
943
944 error = git_path_dirload_with_stat(
945 fi->path.ptr, fi->root_len, iterator__ignore_case(fi),
946 fi->base.start, fi->base.end, &ff->entries);
947
948 if (error < 0 || ff->entries.length == 0) {
949 fs_iterator__free_frame(ff);
950 return GIT_ENOTFOUND;
951 }
952
953 fs_iterator__seek_frame_start(fi, ff);
954
955 ff->next = fi->stack;
956 fi->stack = ff;
957 fi->depth++;
958
959 if (fi->enter_dir_cb && (error = fi->enter_dir_cb(fi)) < 0)
960 return error;
961
962 return fs_iterator__update_entry(fi);
963 }
964
965 static int fs_iterator__current(
966 const git_index_entry **entry, git_iterator *self)
967 {
968 fs_iterator *fi = (fs_iterator *)self;
969 if (entry)
970 *entry = (fi->entry.path == NULL) ? NULL : &fi->entry;
971 return 0;
972 }
973
974 static int fs_iterator__at_end(git_iterator *self)
975 {
976 return (((fs_iterator *)self)->entry.path == NULL);
977 }
978
979 static int fs_iterator__advance_into(
980 const git_index_entry **entry, git_iterator *iter)
981 {
982 int error = 0;
983 fs_iterator *fi = (fs_iterator *)iter;
984
985 iterator__clear_entry(entry);
986
987 /* Allow you to explicitly advance into a commit/submodule (as well as a
988 * tree) to avoid cases where an entry is mislabeled as a submodule in
989 * the working directory. The fs iterator will never have COMMMIT
990 * entries on it's own, but a wrapper might add them.
991 */
992 if (fi->entry.path != NULL &&
993 (fi->entry.mode == GIT_FILEMODE_TREE ||
994 fi->entry.mode == GIT_FILEMODE_COMMIT))
995 /* returns GIT_ENOTFOUND if the directory is empty */
996 error = fs_iterator__expand_dir(fi);
997
998 if (!error && entry)
999 error = fs_iterator__current(entry, iter);
1000
1001 return error;
1002 }
1003
1004 static int fs_iterator__advance_over(
1005 const git_index_entry **entry, git_iterator *self)
1006 {
1007 int error = 0;
1008 fs_iterator *fi = (fs_iterator *)self;
1009 fs_iterator_frame *ff;
1010 git_path_with_stat *next;
1011
1012 if (entry != NULL)
1013 *entry = NULL;
1014
1015 while (fi->entry.path != NULL) {
1016 ff = fi->stack;
1017 next = git_vector_get(&ff->entries, ++ff->index);
1018
1019 if (next != NULL)
1020 break;
1021
1022 fs_iterator__pop_frame(fi, ff, false);
1023 }
1024
1025 error = fs_iterator__update_entry(fi);
1026
1027 if (!error && entry != NULL)
1028 error = fs_iterator__current(entry, self);
1029
1030 return error;
1031 }
1032
1033 static int fs_iterator__advance(
1034 const git_index_entry **entry, git_iterator *self)
1035 {
1036 fs_iterator *fi = (fs_iterator *)self;
1037
1038 /* given include_trees & autoexpand, we might have to go into a tree */
1039 if (iterator__do_autoexpand(fi) &&
1040 fi->entry.path != NULL &&
1041 fi->entry.mode == GIT_FILEMODE_TREE)
1042 {
1043 int error = fs_iterator__advance_into(entry, self);
1044 if (error != GIT_ENOTFOUND)
1045 return error;
1046 /* continue silently past empty directories if autoexpanding */
1047 giterr_clear();
1048 }
1049
1050 return fs_iterator__advance_over(entry, self);
1051 }
1052
1053 static int fs_iterator__seek(git_iterator *self, const char *prefix)
1054 {
1055 GIT_UNUSED(self);
1056 GIT_UNUSED(prefix);
1057 /* pop stack until matching prefix */
1058 /* find prefix item in current frame */
1059 /* push subdirectories as deep as possible while matching */
1060 return 0;
1061 }
1062
1063 static int fs_iterator__reset(
1064 git_iterator *self, const char *start, const char *end)
1065 {
1066 fs_iterator *fi = (fs_iterator *)self;
1067
1068 while (fi->stack != NULL && fi->stack->next != NULL)
1069 fs_iterator__pop_frame(fi, fi->stack, false);
1070 fi->depth = 0;
1071
1072 if (iterator__reset_range(self, start, end) < 0)
1073 return -1;
1074
1075 fs_iterator__seek_frame_start(fi, fi->stack);
1076
1077 return fs_iterator__update_entry(fi);
1078 }
1079
1080 static void fs_iterator__free(git_iterator *self)
1081 {
1082 fs_iterator *fi = (fs_iterator *)self;
1083
1084 while (fi->stack != NULL)
1085 fs_iterator__pop_frame(fi, fi->stack, true);
1086
1087 git_buf_free(&fi->path);
1088 }
1089
1090 static int fs_iterator__update_entry(fs_iterator *fi)
1091 {
1092 git_path_with_stat *ps =
1093 git_vector_get(&fi->stack->entries, fi->stack->index);
1094
1095 git_buf_truncate(&fi->path, fi->root_len);
1096 memset(&fi->entry, 0, sizeof(fi->entry));
1097
1098 if (!ps)
1099 return 0;
1100 if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0)
1101 return -1;
1102 if (iterator__past_end(fi, fi->path.ptr + fi->root_len))
1103 return 0;
1104
1105 fi->entry.path = ps->path;
1106 git_index_entry__init_from_stat(&fi->entry, &ps->st);
1107
1108 /* need different mode here to keep directories during iteration */
1109 fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
1110
1111 /* allow wrapper to check/update the entry (can force skip) */
1112 if (fi->update_entry_cb &&
1113 fi->update_entry_cb(fi) == GIT_ENOTFOUND)
1114 return fs_iterator__advance_over(NULL, (git_iterator *)fi);
1115
1116 /* if this is a tree and trees aren't included, then skip */
1117 if (fi->entry.mode == GIT_FILEMODE_TREE && !iterator__include_trees(fi))
1118 return git_iterator_advance(NULL, (git_iterator *)fi);
1119
1120 return 0;
1121 }
1122
1123 static int fs_iterator__initialize(
1124 git_iterator **out, fs_iterator *fi, const char *root)
1125 {
1126 int error;
1127
1128 if (git_buf_sets(&fi->path, root) < 0 || git_path_to_dir(&fi->path) < 0) {
1129 git__free(fi);
1130 return -1;
1131 }
1132 fi->root_len = fi->path.size;
1133
1134 if ((error = fs_iterator__expand_dir(fi)) == GIT_ENOTFOUND) {
1135 giterr_clear();
1136 error = 0;
1137 }
1138 if (error) {
1139 git_iterator_free((git_iterator *)fi);
1140 fi = NULL;
1141 }
1142
1143 *out = (git_iterator *)fi;
1144 return error;
1145 }
1146
1147 int git_iterator_for_filesystem(
1148 git_iterator **out,
1149 const char *root,
1150 git_iterator_flag_t flags,
1151 const char *start,
1152 const char *end)
1153 {
1154 fs_iterator *fi = git__calloc(1, sizeof(fs_iterator));
1155 GITERR_CHECK_ALLOC(fi);
1156
1157 ITERATOR_BASE_INIT(fi, fs, FS, NULL);
1158
1159 if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
1160 fi->base.flags |= GIT_ITERATOR_IGNORE_CASE;
1161
1162 return fs_iterator__initialize(out, fi, root);
1163 }
1164
1165
1166 typedef struct {
1167 fs_iterator fi;
1168 git_ignores ignores;
1169 int is_ignored;
1170 } workdir_iterator;
1171
1172 GIT_INLINE(bool) workdir_path_is_dotgit(const git_buf *path)
1173 {
1174 size_t len;
1175
1176 if (!path || (len = path->size) < 4)
1177 return false;
1178
1179 if (path->ptr[len - 1] == '/')
1180 len--;
1181
1182 if (tolower(path->ptr[len - 1]) != 't' ||
1183 tolower(path->ptr[len - 2]) != 'i' ||
1184 tolower(path->ptr[len - 3]) != 'g' ||
1185 tolower(path->ptr[len - 4]) != '.')
1186 return false;
1187
1188 return (len == 4 || path->ptr[len - 5] == '/');
1189 }
1190
1191 static int workdir_iterator__enter_dir(fs_iterator *fi)
1192 {
1193 /* only push new ignores if this is not top level directory */
1194 if (fi->stack->next != NULL) {
1195 workdir_iterator *wi = (workdir_iterator *)fi;
1196 ssize_t slash_pos = git_buf_rfind_next(&fi->path, '/');
1197
1198 (void)git_ignore__push_dir(&wi->ignores, &fi->path.ptr[slash_pos + 1]);
1199 }
1200
1201 return 0;
1202 }
1203
1204 static int workdir_iterator__leave_dir(fs_iterator *fi)
1205 {
1206 workdir_iterator *wi = (workdir_iterator *)fi;
1207 git_ignore__pop_dir(&wi->ignores);
1208 return 0;
1209 }
1210
1211 static int workdir_iterator__update_entry(fs_iterator *fi)
1212 {
1213 int error = 0;
1214 workdir_iterator *wi = (workdir_iterator *)fi;
1215
1216 /* skip over .git entries */
1217 if (workdir_path_is_dotgit(&fi->path))
1218 return GIT_ENOTFOUND;
1219
1220 /* reset is_ignored since we haven't checked yet */
1221 wi->is_ignored = -1;
1222
1223 /* check if apparent tree entries are actually submodules */
1224 if (fi->entry.mode != GIT_FILEMODE_TREE)
1225 return 0;
1226
1227 error = git_submodule_lookup(NULL, fi->base.repo, fi->entry.path);
1228 if (error < 0)
1229 giterr_clear();
1230
1231 /* mark submodule (or any dir with .git) as GITLINK and remove slash */
1232 if (!error || error == GIT_EEXISTS) {
1233 fi->entry.mode = S_IFGITLINK;
1234 fi->entry.path[strlen(fi->entry.path) - 1] = '\0';
1235 }
1236
1237 return 0;
1238 }
1239
1240 static void workdir_iterator__free(git_iterator *self)
1241 {
1242 workdir_iterator *wi = (workdir_iterator *)self;
1243 fs_iterator__free(self);
1244 git_ignore__free(&wi->ignores);
1245 }
1246
1247 int git_iterator_for_workdir(
1248 git_iterator **out,
1249 git_repository *repo,
1250 git_iterator_flag_t flags,
1251 const char *start,
1252 const char *end)
1253 {
1254 int error;
1255 workdir_iterator *wi;
1256
1257 if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
1258 return GIT_EBAREREPO;
1259
1260 /* initialize as an fs iterator then do overrides */
1261 wi = git__calloc(1, sizeof(workdir_iterator));
1262 GITERR_CHECK_ALLOC(wi);
1263 ITERATOR_BASE_INIT((&wi->fi), fs, FS, repo);
1264
1265 wi->fi.base.type = GIT_ITERATOR_TYPE_WORKDIR;
1266 wi->fi.cb.free = workdir_iterator__free;
1267 wi->fi.enter_dir_cb = workdir_iterator__enter_dir;
1268 wi->fi.leave_dir_cb = workdir_iterator__leave_dir;
1269 wi->fi.update_entry_cb = workdir_iterator__update_entry;
1270
1271 if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0 ||
1272 (error = git_ignore__for_path(repo, "", &wi->ignores)) < 0)
1273 {
1274 git_iterator_free((git_iterator *)wi);
1275 return error;
1276 }
1277
1278 return fs_iterator__initialize(out, &wi->fi, git_repository_workdir(repo));
1279 }
1280
1281
1282 void git_iterator_free(git_iterator *iter)
1283 {
1284 if (iter == NULL)
1285 return;
1286
1287 iter->cb->free(iter);
1288
1289 git__free(iter->start);
1290 git__free(iter->end);
1291
1292 memset(iter, 0, sizeof(*iter));
1293
1294 git__free(iter);
1295 }
1296
1297 int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
1298 {
1299 bool desire_ignore_case = (ignore_case != 0);
1300
1301 if (iterator__ignore_case(iter) == desire_ignore_case)
1302 return 0;
1303
1304 if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
1305 if (desire_ignore_case)
1306 iter->flags |= GIT_ITERATOR_IGNORE_CASE;
1307 else
1308 iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
1309 } else {
1310 giterr_set(GITERR_INVALID,
1311 "Cannot currently set ignore case on non-empty iterators");
1312 return -1;
1313 }
1314
1315 return 0;
1316 }
1317
1318 git_index *git_iterator_get_index(git_iterator *iter)
1319 {
1320 if (iter->type == GIT_ITERATOR_TYPE_INDEX)
1321 return ((index_iterator *)iter)->index;
1322 return NULL;
1323 }
1324
1325 int git_iterator_current_tree_entry(
1326 const git_tree_entry **tree_entry, git_iterator *iter)
1327 {
1328 if (iter->type != GIT_ITERATOR_TYPE_TREE)
1329 *tree_entry = NULL;
1330 else {
1331 tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
1332 *tree_entry = (tf->current < tf->n_entries) ?
1333 tf->entries[tf->current]->te : NULL;
1334 }
1335
1336 return 0;
1337 }
1338
1339 int git_iterator_current_parent_tree(
1340 const git_tree **tree_ptr,
1341 git_iterator *iter,
1342 const char *parent_path)
1343 {
1344 tree_iterator *ti = (tree_iterator *)iter;
1345 tree_iterator_frame *tf;
1346 const char *scan = parent_path;
1347 const git_tree_entry *te;
1348
1349 *tree_ptr = NULL;
1350
1351 if (iter->type != GIT_ITERATOR_TYPE_TREE)
1352 return 0;
1353
1354 for (tf = ti->root; *scan; ) {
1355 if (!(tf = tf->down) ||
1356 tf->current >= tf->n_entries ||
1357 !(te = tf->entries[tf->current]->te) ||
1358 ti->strncomp(scan, te->filename, te->filename_len) != 0)
1359 return 0;
1360
1361 scan += te->filename_len;
1362 if (*scan == '/')
1363 scan++;
1364 }
1365
1366 *tree_ptr = tf->entries[tf->current]->tree;
1367 return 0;
1368 }
1369
1370 bool git_iterator_current_is_ignored(git_iterator *iter)
1371 {
1372 workdir_iterator *wi = (workdir_iterator *)iter;
1373
1374 if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
1375 return false;
1376
1377 if (wi->is_ignored != -1)
1378 return (bool)(wi->is_ignored != 0);
1379
1380 if (git_ignore__lookup(
1381 &wi->ignores, wi->fi.entry.path, &wi->is_ignored) < 0)
1382 wi->is_ignored = true;
1383
1384 return (bool)wi->is_ignored;
1385 }
1386
1387 int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
1388 {
1389 const git_index_entry *entry;
1390
1391 /* a "done" iterator is after every prefix */
1392 if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
1393 return 1;
1394
1395 /* a NULL prefix is after any valid iterator */
1396 if (!path_prefix)
1397 return -1;
1398
1399 return iter->prefixcomp(entry->path, path_prefix);
1400 }
1401
1402 int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
1403 {
1404 workdir_iterator *wi = (workdir_iterator *)iter;
1405
1406 if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->fi.entry.path)
1407 *path = NULL;
1408 else
1409 *path = &wi->fi.path;
1410
1411 return 0;
1412 }