forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.c
2308 lines (1791 loc) · 55.6 KB
/
iterator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "iterator.h"
#include "tree.h"
#include "index.h"
#define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
#define GIT_ITERATOR_HONOR_IGNORES (1 << 16)
#define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17)
#define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
#define iterator__ignore_case(I) iterator__flag(I,IGNORE_CASE)
#define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES)
#define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
#define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND)
#define iterator__include_conflicts(I) iterator__flag(I,INCLUDE_CONFLICTS)
#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
#define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES)
#define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT)
static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
{
if (ignore_case)
iter->flags |= GIT_ITERATOR_IGNORE_CASE;
else
iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp;
iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp;
iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp;
iter->entry_srch = ignore_case ? git_index_entry_isrch : git_index_entry_srch;
git_vector_set_cmp(&iter->pathlist, (git_vector_cmp)iter->strcomp);
}
static int iterator_range_init(
git_iterator *iter, const char *start, const char *end)
{
if (start && *start) {
iter->start = git__strdup(start);
GITERR_CHECK_ALLOC(iter->start);
iter->start_len = strlen(iter->start);
}
if (end && *end) {
iter->end = git__strdup(end);
GITERR_CHECK_ALLOC(iter->end);
iter->end_len = strlen(iter->end);
}
iter->started = (iter->start == NULL);
iter->ended = false;
return 0;
}
static void iterator_range_free(git_iterator *iter)
{
if (iter->start) {
git__free(iter->start);
iter->start = NULL;
iter->start_len = 0;
}
if (iter->end) {
git__free(iter->end);
iter->end = NULL;
iter->end_len = 0;
}
}
static int iterator_reset_range(
git_iterator *iter, const char *start, const char *end)
{
iterator_range_free(iter);
return iterator_range_init(iter, start, end);
}
static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist)
{
size_t i;
if (git_vector_init(&iter->pathlist, pathlist->count, NULL) < 0)
return -1;
for (i = 0; i < pathlist->count; i++) {
if (!pathlist->strings[i])
continue;
if (git_vector_insert(&iter->pathlist, pathlist->strings[i]) < 0)
return -1;
}
return 0;
}
static int iterator_init_common(
git_iterator *iter,
git_repository *repo,
git_index *index,
git_iterator_options *given_opts)
{
static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT;
git_iterator_options *options = given_opts ? given_opts : &default_opts;
bool ignore_case;
int precompose;
int error;
iter->repo = repo;
iter->index = index;
iter->flags = options->flags;
if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) {
ignore_case = true;
} else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) {
ignore_case = false;
} else if (repo) {
git_index *index;
if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0)
return error;
ignore_case = !!index->ignore_case;
if (ignore_case == 1)
iter->flags |= GIT_ITERATOR_IGNORE_CASE;
else
iter->flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
} else {
ignore_case = false;
}
/* try to look up precompose and set flag if appropriate */
if (repo &&
(iter->flags & GIT_ITERATOR_PRECOMPOSE_UNICODE) == 0 &&
(iter->flags & GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE) == 0) {
if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
giterr_clear();
else if (precompose)
iter->flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
}
if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND))
iter->flags |= GIT_ITERATOR_INCLUDE_TREES;
if ((error = iterator_range_init(iter, options->start, options->end)) < 0 ||
(error = iterator_pathlist_init(iter, &options->pathlist)) < 0)
return error;
iterator_set_ignore_case(iter, ignore_case);
return 0;
}
static void iterator_clear(git_iterator *iter)
{
iter->started = false;
iter->ended = false;
iter->stat_calls = 0;
iter->pathlist_walk_idx = 0;
iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS;
}
GIT_INLINE(bool) iterator_has_started(
git_iterator *iter, const char *path, bool is_submodule)
{
size_t path_len;
if (iter->start == NULL || iter->started == true)
return true;
/* the starting path is generally a prefix - we have started once we
* are prefixed by this path
*/
iter->started = (iter->prefixcomp(path, iter->start) >= 0);
if (iter->started)
return true;
path_len = strlen(path);
/* if, however, we are a submodule, then we support `start` being
* suffixed with a `/` for crazy legacy reasons. match `submod`
* with a start path of `submod/`.
*/
if (is_submodule && iter->start_len && path_len == iter->start_len - 1 &&
iter->start[iter->start_len-1] == '/')
return true;
/* if, however, our current path is a directory, and our starting path
* is _beneath_ that directory, then recurse into the directory (even
* though we have not yet "started")
*/
if (path_len > 0 && path[path_len-1] == '/' &&
iter->strncomp(path, iter->start, path_len) == 0)
return true;
return false;
}
GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path)
{
if (iter->end == NULL)
return false;
else if (iter->ended)
return true;
iter->ended = (iter->prefixcomp(path, iter->end) > 0);
return iter->ended;
}
/* walker for the index and tree iterator that allows it to walk the sorted
* pathlist entries alongside sorted iterator entries.
*/
static bool iterator_pathlist_next_is(git_iterator *iter, const char *path)
{
char *p;
size_t path_len, p_len, cmp_len, i;
int cmp;
if (iter->pathlist.length == 0)
return true;
git_vector_sort(&iter->pathlist);
path_len = strlen(path);
/* for comparison, drop the trailing slash on the current '/' */
if (path_len && path[path_len-1] == '/')
path_len--;
for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) {
p = iter->pathlist.contents[i];
p_len = strlen(p);
if (p_len && p[p_len-1] == '/')
p_len--;
cmp_len = min(path_len, p_len);
/* see if the pathlist entry is a prefix of this path */
cmp = iter->strncomp(p, path, cmp_len);
/* prefix match - see if there's an exact match, or if we were
* given a path that matches the directory
*/
if (cmp == 0) {
/* if this pathlist entry is not suffixed with a '/' then
* it matches a path that is a file or a directory.
* (eg, pathlist = "foo" and path is "foo" or "foo/" or
* "foo/something")
*/
if (p[cmp_len] == '\0' &&
(path[cmp_len] == '\0' || path[cmp_len] == '/'))
return true;
/* if this pathlist entry _is_ suffixed with a '/' then
* it matches only paths that are directories.
* (eg, pathlist = "foo/" and path is "foo/" or "foo/something")
*/
if (p[cmp_len] == '/' && path[cmp_len] == '/')
return true;
}
/* this pathlist entry sorts before the given path, try the next */
else if (cmp < 0) {
iter->pathlist_walk_idx++;
continue;
}
/* this pathlist sorts after the given path, no match. */
else if (cmp > 0) {
break;
}
}
return false;
}
typedef enum {
ITERATOR_PATHLIST_NONE = 0,
ITERATOR_PATHLIST_IS_FILE = 1,
ITERATOR_PATHLIST_IS_DIR = 2,
ITERATOR_PATHLIST_IS_PARENT = 3,
ITERATOR_PATHLIST_FULL = 4,
} iterator_pathlist_search_t;
static iterator_pathlist_search_t iterator_pathlist_search(
git_iterator *iter, const char *path, size_t path_len)
{
const char *p;
size_t idx;
int error;
if (iter->pathlist.length == 0)
return ITERATOR_PATHLIST_FULL;
git_vector_sort(&iter->pathlist);
error = git_vector_bsearch2(&idx, &iter->pathlist,
(git_vector_cmp)iter->strcomp, path);
/* the given path was found in the pathlist. since the pathlist only
* matches directories when they're suffixed with a '/', analyze the
* path string to determine whether it's a directory or not.
*/
if (error == 0) {
if (path_len && path[path_len-1] == '/')
return ITERATOR_PATHLIST_IS_DIR;
return ITERATOR_PATHLIST_IS_FILE;
}
/* at this point, the path we're examining may be a directory (though we
* don't know that yet, since we're avoiding a stat unless it's necessary)
* so walk the pathlist looking for the given path with a '/' after it,
*/
while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) {
if (iter->prefixcomp(p, path) != 0)
break;
/* an exact match would have been matched by the bsearch above */
assert(p[path_len]);
/* is this a literal directory entry (eg `foo/`) or a file beneath */
if (p[path_len] == '/') {
return (p[path_len+1] == '\0') ?
ITERATOR_PATHLIST_IS_DIR :
ITERATOR_PATHLIST_IS_PARENT;
}
if (p[path_len] > '/')
break;
idx++;
}
return ITERATOR_PATHLIST_NONE;
}
/* Empty iterator */
static int empty_iterator_noop(const git_index_entry **e, git_iterator *i)
{
GIT_UNUSED(i);
if (e)
*e = NULL;
return GIT_ITEROVER;
}
static int empty_iterator_advance_over(
const git_index_entry **e,
git_iterator_status_t *s,
git_iterator *i)
{
*s = GIT_ITERATOR_STATUS_EMPTY;
return empty_iterator_noop(e, i);
}
static int empty_iterator_reset(git_iterator *i)
{
GIT_UNUSED(i);
return 0;
}
static void empty_iterator_free(git_iterator *i)
{
GIT_UNUSED(i);
}
typedef struct {
git_iterator base;
git_iterator_callbacks cb;
} empty_iterator;
int git_iterator_for_nothing(
git_iterator **out,
git_iterator_options *options)
{
empty_iterator *iter;
static git_iterator_callbacks callbacks = {
empty_iterator_noop,
empty_iterator_noop,
empty_iterator_noop,
empty_iterator_advance_over,
empty_iterator_reset,
empty_iterator_free
};
*out = NULL;
iter = git__calloc(1, sizeof(empty_iterator));
GITERR_CHECK_ALLOC(iter);
iter->base.type = GIT_ITERATOR_TYPE_EMPTY;
iter->base.cb = &callbacks;
iter->base.flags = options->flags;
*out = &iter->base;
return 0;
}
/* Tree iterator */
typedef struct {
git_tree_entry *tree_entry;
const char *parent_path;
} tree_iterator_entry;
typedef struct {
git_tree *tree;
/* path to this particular frame (folder) */
git_buf path;
/* a sorted list of the entries for this frame (folder), these are
* actually pointers to the iterator's entry pool.
*/
git_vector entries;
tree_iterator_entry *current;
size_t next_idx;
/* on case insensitive iterations, we also have an array of other
* paths that were case insensitively equal to this one, and their
* tree objects. we have coalesced the tree entries into this frame.
* a child `tree_iterator_entry` will contain a pointer to its actual
* parent path.
*/
git_vector similar_trees;
git_array_t(git_buf) similar_paths;
} tree_iterator_frame;
typedef struct {
git_iterator base;
git_tree *root;
git_array_t(tree_iterator_frame) frames;
git_index_entry entry;
git_buf entry_path;
/* a pool of entries to reduce the number of allocations */
git_pool entry_pool;
} tree_iterator;
GIT_INLINE(tree_iterator_frame *) tree_iterator_parent_frame(
tree_iterator *iter)
{
return iter->frames.size > 1 ?
&iter->frames.ptr[iter->frames.size-2] : NULL;
}
GIT_INLINE(tree_iterator_frame *) tree_iterator_current_frame(
tree_iterator *iter)
{
return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL;
}
GIT_INLINE(int) tree_entry_cmp(
const git_tree_entry *a, const git_tree_entry *b, bool icase)
{
return git_path_cmp(
a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
icase ? git__strncasecmp : git__strncmp);
}
GIT_INLINE(int) tree_iterator_entry_cmp(const void *ptr_a, const void *ptr_b)
{
const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a;
const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b;
return tree_entry_cmp(a->tree_entry, b->tree_entry, false);
}
GIT_INLINE(int) tree_iterator_entry_cmp_icase(
const void *ptr_a, const void *ptr_b)
{
const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a;
const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b;
return tree_entry_cmp(a->tree_entry, b->tree_entry, true);
}
static int tree_iterator_entry_sort_icase(const void *ptr_a, const void *ptr_b)
{
const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a;
const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b;
int c = tree_entry_cmp(a->tree_entry, b->tree_entry, true);
/* stabilize the sort order for filenames that are (case insensitively)
* the same by examining the parent path (case sensitively) before
* falling back to a case sensitive sort of the filename.
*/
if (!c && a->parent_path != b->parent_path)
c = git__strcmp(a->parent_path, b->parent_path);
if (!c)
c = tree_entry_cmp(a->tree_entry, b->tree_entry, false);
return c;
}
static int tree_iterator_compute_path(
git_buf *out,
tree_iterator_entry *entry)
{
git_buf_clear(out);
if (entry->parent_path)
git_buf_joinpath(out, entry->parent_path, entry->tree_entry->filename);
else
git_buf_puts(out, entry->tree_entry->filename);
if (git_tree_entry__is_tree(entry->tree_entry))
git_buf_putc(out, '/');
if (git_buf_oom(out))
return -1;
return 0;
}
static int tree_iterator_frame_init(
tree_iterator *iter,
git_tree *tree,
tree_iterator_entry *frame_entry)
{
tree_iterator_frame *new_frame = NULL;
tree_iterator_entry *new_entry;
git_tree *dup = NULL;
git_tree_entry *tree_entry;
git_vector_cmp cmp;
size_t i;
int error = 0;
new_frame = git_array_alloc(iter->frames);
GITERR_CHECK_ALLOC(new_frame);
memset(new_frame, 0, sizeof(tree_iterator_frame));
if ((error = git_tree_dup(&dup, tree)) < 0)
goto done;
memset(new_frame, 0x0, sizeof(tree_iterator_frame));
new_frame->tree = dup;
if (frame_entry &&
(error = tree_iterator_compute_path(&new_frame->path, frame_entry)) < 0)
goto done;
cmp = iterator__ignore_case(&iter->base) ?
tree_iterator_entry_sort_icase : NULL;
if ((error = git_vector_init(
&new_frame->entries, dup->entries.size, cmp)) < 0)
goto done;
git_array_foreach(dup->entries, i, tree_entry) {
new_entry = git_pool_malloc(&iter->entry_pool, 1);
GITERR_CHECK_ALLOC(new_entry);
new_entry->tree_entry = tree_entry;
new_entry->parent_path = new_frame->path.ptr;
if ((error = git_vector_insert(&new_frame->entries, new_entry)) < 0)
goto done;
}
git_vector_set_sorted(&new_frame->entries,
!iterator__ignore_case(&iter->base));
done:
if (error < 0) {
git_tree_free(dup);
git_array_pop(iter->frames);
}
return error;
}
GIT_INLINE(tree_iterator_entry *) tree_iterator_current_entry(
tree_iterator_frame *frame)
{
return frame->current;
}
GIT_INLINE(int) tree_iterator_frame_push_neighbors(
tree_iterator *iter,
tree_iterator_frame *parent_frame,
tree_iterator_frame *frame,
const char *filename)
{
tree_iterator_entry *entry, *new_entry;
git_tree *tree = NULL;
git_tree_entry *tree_entry;
git_buf *path;
size_t new_size, i;
int error = 0;
while (parent_frame->next_idx < parent_frame->entries.length) {
entry = parent_frame->entries.contents[parent_frame->next_idx];
if (strcasecmp(filename, entry->tree_entry->filename) != 0)
break;
if ((error = git_tree_lookup(&tree,
iter->base.repo, entry->tree_entry->oid)) < 0)
break;
if (git_vector_insert(&parent_frame->similar_trees, tree) < 0)
break;
path = git_array_alloc(parent_frame->similar_paths);
GITERR_CHECK_ALLOC(path);
memset(path, 0, sizeof(git_buf));
if ((error = tree_iterator_compute_path(path, entry)) < 0)
break;
GITERR_CHECK_ALLOC_ADD(&new_size,
frame->entries.length, tree->entries.size);
git_vector_size_hint(&frame->entries, new_size);
git_array_foreach(tree->entries, i, tree_entry) {
new_entry = git_pool_malloc(&iter->entry_pool, 1);
GITERR_CHECK_ALLOC(new_entry);
new_entry->tree_entry = tree_entry;
new_entry->parent_path = path->ptr;
if ((error = git_vector_insert(&frame->entries, new_entry)) < 0)
break;
}
if (error)
break;
parent_frame->next_idx++;
}
return error;
}
GIT_INLINE(int) tree_iterator_frame_push(
tree_iterator *iter, tree_iterator_entry *entry)
{
tree_iterator_frame *parent_frame, *frame;
git_tree *tree = NULL;
int error;
if ((error = git_tree_lookup(&tree,
iter->base.repo, entry->tree_entry->oid)) < 0 ||
(error = tree_iterator_frame_init(iter, tree, entry)) < 0)
goto done;
parent_frame = tree_iterator_parent_frame(iter);
frame = tree_iterator_current_frame(iter);
/* if we're case insensitive, then we may have another directory that
* is (case insensitively) equal to this one. coalesce those children
* into this tree.
*/
if (iterator__ignore_case(&iter->base))
error = tree_iterator_frame_push_neighbors(iter,
parent_frame, frame, entry->tree_entry->filename);
done:
git_tree_free(tree);
return error;
}
static void tree_iterator_frame_pop(tree_iterator *iter)
{
tree_iterator_frame *frame;
git_buf *buf = NULL;
git_tree *tree;
size_t i;
assert(iter->frames.size);
frame = git_array_pop(iter->frames);
git_vector_free(&frame->entries);
git_tree_free(frame->tree);
do {
buf = git_array_pop(frame->similar_paths);
git_buf_free(buf);
} while (buf != NULL);
git_array_clear(frame->similar_paths);
git_vector_foreach(&frame->similar_trees, i, tree)
git_tree_free(tree);
git_vector_free(&frame->similar_trees);
git_buf_free(&frame->path);
}
static int tree_iterator_current(
const git_index_entry **out, git_iterator *i)
{
tree_iterator *iter = (tree_iterator *)i;
if (!iterator__has_been_accessed(i))
return iter->base.cb->advance(out, i);
if (!iter->frames.size) {
*out = NULL;
return GIT_ITEROVER;
}
*out = &iter->entry;
return 0;
}
static void tree_iterator_set_current(
tree_iterator *iter,
tree_iterator_frame *frame,
tree_iterator_entry *entry)
{
git_tree_entry *tree_entry = entry->tree_entry;
frame->current = entry;
memset(&iter->entry, 0x0, sizeof(git_index_entry));
iter->entry.mode = tree_entry->attr;
iter->entry.path = iter->entry_path.ptr;
git_oid_cpy(&iter->entry.id, tree_entry->oid);
}
static int tree_iterator_advance(const git_index_entry **out, git_iterator *i)
{
tree_iterator *iter = (tree_iterator *)i;
int error = 0;
iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
/* examine tree entries until we find the next one to return */
while (true) {
tree_iterator_entry *prev_entry, *entry;
tree_iterator_frame *frame;
bool is_tree;
if ((frame = tree_iterator_current_frame(iter)) == NULL) {
error = GIT_ITEROVER;
break;
}
/* no more entries in this frame. pop the frame out */
if (frame->next_idx == frame->entries.length) {
tree_iterator_frame_pop(iter);
continue;
}
/* we may have coalesced the contents of case-insensitively same-named
* directories, so do the sort now.
*/
if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries))
git_vector_sort(&frame->entries);
/* we have more entries in the current frame, that's our next entry */
prev_entry = tree_iterator_current_entry(frame);
entry = frame->entries.contents[frame->next_idx];
frame->next_idx++;
/* we can have collisions when iterating case insensitively. (eg,
* 'A/a' and 'a/A'). squash this one if it's already been seen.
*/
if (iterator__ignore_case(&iter->base) &&
prev_entry &&
tree_iterator_entry_cmp_icase(prev_entry, entry) == 0)
continue;
if ((error = tree_iterator_compute_path(&iter->entry_path, entry)) < 0)
break;
/* if this path is before our start, advance over this entry */
if (!iterator_has_started(&iter->base, iter->entry_path.ptr, false))
continue;
/* if this path is after our end, stop */
if (iterator_has_ended(&iter->base, iter->entry_path.ptr)) {
error = GIT_ITEROVER;
break;
}
/* if we have a list of paths we're interested in, examine it */
if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr))
continue;
is_tree = git_tree_entry__is_tree(entry->tree_entry);
/* if we are *not* including trees then advance over this entry */
if (is_tree && !iterator__include_trees(iter)) {
/* if we've found a tree (and are not returning it to the caller)
* and we are autoexpanding, then we want to return the first
* child. push the new directory and advance.
*/
if (iterator__do_autoexpand(iter)) {
if ((error = tree_iterator_frame_push(iter, entry)) < 0)
break;
}
continue;
}
tree_iterator_set_current(iter, frame, entry);
/* if we are autoexpanding, then push this as a new frame, so that
* the next call to `advance` will dive into this directory.
*/
if (is_tree && iterator__do_autoexpand(iter))
error = tree_iterator_frame_push(iter, entry);
break;
}
if (out)
*out = (error == 0) ? &iter->entry : NULL;
return error;
}
static int tree_iterator_advance_into(
const git_index_entry **out, git_iterator *i)
{
tree_iterator *iter = (tree_iterator *)i;
tree_iterator_frame *frame;
tree_iterator_entry *prev_entry;
int error;
if (out)
*out = NULL;
if ((frame = tree_iterator_current_frame(iter)) == NULL)
return GIT_ITEROVER;
/* get the last seen entry */
prev_entry = tree_iterator_current_entry(frame);
/* it's legal to call advance_into when auto-expand is on. in this case,
* we will have pushed a new (empty) frame on to the stack for this
* new directory. since it's empty, its current_entry should be null.
*/
assert(iterator__do_autoexpand(i) ^ (prev_entry != NULL));
if (prev_entry) {
if (!git_tree_entry__is_tree(prev_entry->tree_entry))
return 0;
if ((error = tree_iterator_frame_push(iter, prev_entry)) < 0)
return error;
}
/* we've advanced into the directory in question, let advance
* find the first entry
*/
return tree_iterator_advance(out, i);
}
static int tree_iterator_advance_over(
const git_index_entry **out,
git_iterator_status_t *status,
git_iterator *i)
{
*status = GIT_ITERATOR_STATUS_NORMAL;
return git_iterator_advance(out, i);
}
static void tree_iterator_clear(tree_iterator *iter)
{
while (iter->frames.size)
tree_iterator_frame_pop(iter);
git_array_clear(iter->frames);
git_pool_clear(&iter->entry_pool);
git_buf_clear(&iter->entry_path);
iterator_clear(&iter->base);
}
static int tree_iterator_init(tree_iterator *iter)
{
int error;
git_pool_init(&iter->entry_pool, sizeof(tree_iterator_entry));
if ((error = tree_iterator_frame_init(iter, iter->root, NULL)) < 0)
return error;
iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
return 0;
}
static int tree_iterator_reset(git_iterator *i)
{
tree_iterator *iter = (tree_iterator *)i;
tree_iterator_clear(iter);
return tree_iterator_init(iter);
}
static void tree_iterator_free(git_iterator *i)
{
tree_iterator *iter = (tree_iterator *)i;
tree_iterator_clear(iter);
git_tree_free(iter->root);
git_buf_free(&iter->entry_path);
}
int git_iterator_for_tree(
git_iterator **out,
git_tree *tree,
git_iterator_options *options)
{
tree_iterator *iter;
int error;
static git_iterator_callbacks callbacks = {
tree_iterator_current,
tree_iterator_advance,
tree_iterator_advance_into,
tree_iterator_advance_over,
tree_iterator_reset,
tree_iterator_free
};
*out = NULL;
if (tree == NULL)
return git_iterator_for_nothing(out, options);
iter = git__calloc(1, sizeof(tree_iterator));
GITERR_CHECK_ALLOC(iter);
iter->base.type = GIT_ITERATOR_TYPE_TREE;
iter->base.cb = &callbacks;
if ((error = iterator_init_common(&iter->base,
git_tree_owner(tree), NULL, options)) < 0 ||
(error = git_tree_dup(&iter->root, tree)) < 0 ||
(error = tree_iterator_init(iter)) < 0)
goto on_error;
*out = &iter->base;
return 0;
on_error:
git_iterator_free(&iter->base);
return error;
}
int git_iterator_current_tree_entry(
const git_tree_entry **tree_entry, git_iterator *i)
{
tree_iterator *iter;
tree_iterator_frame *frame;
tree_iterator_entry *entry;
assert(i->type == GIT_ITERATOR_TYPE_TREE);
iter = (tree_iterator *)i;
frame = tree_iterator_current_frame(iter);
entry = tree_iterator_current_entry(frame);
*tree_entry = entry->tree_entry;
return 0;
}
int git_iterator_current_parent_tree(
const git_tree **parent_tree, git_iterator *i, size_t depth)
{
tree_iterator *iter;
tree_iterator_frame *frame;
assert(i->type == GIT_ITERATOR_TYPE_TREE);