Commit e40f1c2d23c3758968df94a17831ec5432ae6988

Russell Belfer 2013-03-08T16:39:57

Make tree iterator handle icase equivalence There is a serious bug in the previous tree iterator implementation. If case insensitivity resulted in member elements being equivalent to one another, and those member elements were trees, then the children of the colliding elements would be processed in sequence instead of in a single flattened list. This meant that the tree iterator was not truly acting like a case-insensitive list. This completely reworks the tree iterator to manage lists with case insensitive equivalence classes and advance through the items in a unified manner in a single sorted frame. It is possible that at a future date we might want to update this to separate the case insensitive and case sensitive tree iterators so that the case sensitive one could be a minimal amount of code and the insensitive one would always know what it needed to do without checking flags. But there would be so much shared code between the two, that I'm not sure it that's a win. For now, this gets what we need. More tests are needed, though.

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
diff --git a/src/hashsig.c b/src/hashsig.c
index e9c5164..3a75aaa 100644
--- a/src/hashsig.c
+++ b/src/hashsig.c
@@ -6,6 +6,7 @@
  */
 #include "hashsig.h"
 #include "fileops.h"
+#include "util.h"
 
 typedef uint32_t hashsig_t;
 typedef uint64_t hashsig_state;
@@ -19,7 +20,7 @@ typedef uint64_t hashsig_state;
 
 #define HASHSIG_HEAP_SIZE ((1 << 7) - 1)
 
-typedef int (GIT_STDLIB_CALL *hashsig_cmp)(const void *a, const void *b);
+typedef int (*hashsig_cmp)(const void *a, const void *b, void *);
 
 typedef struct {
 	int size, asize;
@@ -53,15 +54,17 @@ static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp)
 	h->cmp   = cmp;
 }
 
-static int GIT_STDLIB_CALL hashsig_cmp_max(const void *a, const void *b)
+static int hashsig_cmp_max(const void *a, const void *b, void *payload)
 {
 	hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
+	GIT_UNUSED(payload);
 	return (av < bv) ? -1 : (av > bv) ? 1 : 0;
 }
 
-static int GIT_STDLIB_CALL hashsig_cmp_min(const void *a, const void *b)
+static int hashsig_cmp_min(const void *a, const void *b, void *payload)
 {
 	hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
+	GIT_UNUSED(payload);
 	return (av > bv) ? -1 : (av < bv) ? 1 : 0;
 }
 
@@ -69,7 +72,7 @@ static void hashsig_heap_up(hashsig_heap *h, int el)
 {
 	int parent_el = HEAP_PARENT_OF(el);
 
-	while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el]) > 0) {
+	while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el], NULL) > 0) {
 		hashsig_t t = h->values[el];
 		h->values[el] = h->values[parent_el];
 		h->values[parent_el] = t;
@@ -92,10 +95,10 @@ static void hashsig_heap_down(hashsig_heap *h, int el)
 		lv = h->values[lel];
 		rv = h->values[rel];
 
-		if (h->cmp(&v, &lv) < 0 && h->cmp(&v, &rv) < 0)
+		if (h->cmp(&v, &lv, NULL) < 0 && h->cmp(&v, &rv, NULL) < 0)
 			break;
 
-		swapel = (h->cmp(&lv, &rv) < 0) ? lel : rel;
+		swapel = (h->cmp(&lv, &rv, NULL) < 0) ? lel : rel;
 
 		h->values[el] = h->values[swapel];
 		h->values[swapel] = v;
@@ -107,13 +110,13 @@ static void hashsig_heap_down(hashsig_heap *h, int el)
 static void hashsig_heap_sort(hashsig_heap *h)
 {
 	/* only need to do this at the end for signature comparison */
-	qsort(h->values, h->size, sizeof(hashsig_t), h->cmp);
+	git__qsort_r(h->values, h->size, sizeof(hashsig_t), h->cmp, NULL);
 }
 
 static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val)
 {
 	/* if heap is full, pop top if new element should replace it */
-	if (h->size == h->asize && h->cmp(&val, &h->values[0]) > 0) {
+	if (h->size == h->asize && h->cmp(&val, &h->values[0], NULL) > 0) {
 		h->size--;
 		h->values[0] = h->values[h->size];
 		hashsig_heap_down(h, 0);
@@ -343,7 +346,7 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
 	/* hash heaps are sorted - just look for overlap vs total */
 
 	for (i = 0, j = 0; i < a->size && j < b->size; ) {
-		cmp = a->cmp(&a->values[i], &b->values[j]);
+		cmp = a->cmp(&a->values[i], &b->values[j], NULL);
 
 		if (cmp < 0)
 			++i;
diff --git a/src/iterator.c b/src/iterator.c
index 6c77647..84664c0 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -94,7 +94,7 @@ static int iterator_update_ignore_case(
 	else if (ignore_case == 0)
 		iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
 
-	iter->prefixcomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ?
+	iter->prefixcomp = iterator__ignore_case(iter) ?
 		git__prefixcmp_icase : git__prefixcmp;
 
 	return error;
@@ -162,41 +162,38 @@ int git_iterator_for_nothing(
 }
 
 
+
+typedef struct {
+	size_t parent_entry_index;  /* index in parent entries array */
+	size_t parent_tree_index;   /* index in parent entry tree */
+	git_tree *tree; /* this tree if this is tree (only valid while current) */
+} tree_iterator_entry;
+
 typedef struct tree_iterator_frame tree_iterator_frame;
 struct tree_iterator_frame {
-	tree_iterator_frame *next, *prev;
-	git_tree *tree;
+	tree_iterator_frame *parent, *child;
+
+	size_t n_entries; /* items in this frame */
+	size_t current;   /* start of currently active range in frame */
+	size_t next;      /* start of next range in frame */
+
 	const char *start;
 	size_t startlen;
-	size_t index;
-	/* secondary tree index for case-insensitive sort */
-	void **icase_map;
-	void *icase_data[GIT_FLEX_ARRAY];
+
+	tree_iterator_entry entries[GIT_FLEX_ARRAY];
 };
 
 typedef struct {
 	git_iterator base;
 	git_iterator_callbacks cb;
-	tree_iterator_frame *stack, *tail;
+	tree_iterator_frame *head, *top;
 	git_index_entry entry;
 	git_buf path;
 	bool path_has_filename;
+	int (*strcomp)(const char *a, const char *b);
+	int (*strncomp)(const char *a, const char *b, size_t sz);
 } tree_iterator;
 
-GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti)
-{
-	tree_iterator_frame *tf = ti->stack;
-	size_t entries = git_tree_entrycount(tf->tree), idx = tf->index;
-
-	if (idx >= entries)
-		return NULL;
-
-	if (tf->icase_map)
-		idx = (size_t)tf->icase_map[idx];
-
-	return git_tree_entry_byindex(tf->tree, idx);
-}
-
 static char *tree_iterator__current_filename(
 	tree_iterator *ti, const git_tree_entry *te)
 {
@@ -213,193 +210,232 @@ static char *tree_iterator__current_filename(
 	return ti->path.ptr;
 }
 
-static void tree_iterator__free_frame(tree_iterator_frame *tf)
+static int tree_iterator__create_top_frame(tree_iterator *ti, git_tree *tree)
 {
-	if (!tf)
-		return;
+	size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
+	tree_iterator_frame *top = git__calloc(sz, sizeof(char));
+	GITERR_CHECK_ALLOC(top);
 
-	git_tree_free(tf->tree);
-	tf->tree = NULL;
+	top->n_entries = 1;
+	top->next = 1;
+	top->start = ti->base.start;
+	top->startlen = top->start ? strlen(top->start) : 0;
+	top->entries[0].tree = tree;
 
-	git__free(tf);
+	ti->head = ti->top = top;
+
+	return 0;
 }
 
-static bool tree_iterator__pop_frame(tree_iterator *ti)
+static const git_tree_entry *tree_iterator__get_tree_entry(
+	tree_iterator_frame *tf, const tree_iterator_entry *entry, size_t i)
 {
-	tree_iterator_frame *tf = ti->stack;
-
-	/* don't free the initial tree/frame */
-	if (!tf->next)
-		return false;
+	git_tree *tree;
 
-	ti->stack = tf->next;
-	ti->stack->prev = NULL;
+	if (!entry) {
+		if (i >= tf->n_entries)
+			return NULL;
+		entry = &tf->entries[i];
+	}
 
-	tree_iterator__free_frame(tf);
+	tree = tf->parent->entries[entry->parent_entry_index].tree;
+	if (!tree)
+		return NULL;
 
-	return true;
+	return git_tree_entry_byindex(tree, entry->parent_tree_index);
 }
 
-static int tree_iterator__to_end(tree_iterator *ti)
+static int tree_iterator__entry_cmp(const void *a, const void *b, void *p)
 {
-	while (tree_iterator__pop_frame(ti)) /* pop all */;
-	ti->stack->index = git_tree_entrycount(ti->stack->tree);
-	return 0;
+	tree_iterator_frame *tf = p;
+	const git_tree_entry *ae = tree_iterator__get_tree_entry(tf, a, 0);
+	const git_tree_entry *be = tree_iterator__get_tree_entry(tf, b, 0);
+	size_t common_len = min(ae->filename_len, be->filename_len);
+	int cmp = git__strncasecmp(ae->filename, be->filename, common_len);
+
+	if (!cmp) {
+		char a_next = ae->filename[common_len];
+		char b_next = be->filename[common_len];
+
+		if (!a_next && ae->attr == GIT_FILEMODE_TREE)
+			a_next = '/';
+		if (!b_next && be->attr == GIT_FILEMODE_TREE)
+			b_next = '/';
+
+		cmp = (int)a_next - (int)b_next;
+	}
+
+	return cmp;
 }
 
-static int tree_iterator__current(
-	const git_index_entry **entry, git_iterator *self)
+static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
 {
-	tree_iterator *ti = (tree_iterator *)self;
-	const git_tree_entry *te = tree_iterator__tree_entry(ti);
-
-	if (entry)
-		*entry = NULL;
-
-	if (te == NULL)
-		return 0;
+	/* find next and load trees for current range */
+	int error = 0;
+	const git_tree_entry *te, *last_te = NULL;
 
-	ti->entry.mode = te->attr;
-	git_oid_cpy(&ti->entry.oid, &te->oid);
+	tf->next = tf->current;
 
-	ti->entry.path = tree_iterator__current_filename(ti, te);
-	if (ti->entry.path == NULL)
-		return -1;
+	while (tf->next < tf->n_entries) {
+		if (!(te = tree_iterator__get_tree_entry(tf, 0, tf->next)) ||
+			(last_te && ti->strcomp(last_te->filename, te->filename) != 0))
+			break;
 
-	if (iterator__past_end(ti, ti->entry.path))
-		return tree_iterator__to_end(ti);
+		if (git_tree_entry__is_tree(te) &&
+			(error = git_tree_lookup(
+				&tf->entries[tf->next].tree, ti->base.repo, &te->oid)) < 0)
+			break;
 
-	if (entry)
-		*entry = &ti->entry;
+		tf->next++;
+		last_te = te;
+	}
 
-	return 0;
-}
+	if (last_te && !tree_iterator__current_filename(ti, last_te))
+		return -1;
 
-static int tree_iterator__at_end(git_iterator *self)
-{
-	return (tree_iterator__tree_entry((tree_iterator *)self) == NULL);
+	return error;
 }
 
-static int tree_iterator__icase_map_cmp(const void *a, const void *b, void *data)
+GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
 {
-	git_tree *tree = data;
-	const git_tree_entry *te1 = git_tree_entry_byindex(tree, (size_t)a);
-	const git_tree_entry *te2 = git_tree_entry_byindex(tree, (size_t)b);
-
-	return te1 ? (te2 ? git_tree_entry_icmp(te1, te2) : 1) : -1;
+	return (ti->head->current < ti->head->n_entries &&
+			ti->head->entries[ti->head->current].tree != NULL);
 }
 
-static int tree_iterator__frame_start_icmp(const void *key, const void *el)
+static int tree_iterator__push_frame(tree_iterator *ti)
 {
-	const tree_iterator_frame *tf = (const tree_iterator_frame *)key;
-	const git_tree_entry *te = git_tree_entry_byindex(tf->tree, (size_t)el);
-	size_t minlen = min(tf->startlen, te->filename_len);
+	int error = 0;
+	tree_iterator_frame *tf = ti->head, *new_tf = NULL;
+	size_t i, n_entries = 0, sz = sizeof(tree_iterator_frame);
+	const git_tree_entry *te;
 
-	return git__strncasecmp(tf->start, te->filename, minlen);
-}
+	/* if current item in head is not a tree, do nothing */
+	if (tf->current >= tf->n_entries || !tf->entries[tf->current].tree)
+		return 0;
 
-static void tree_iterator__frame_seek_start(tree_iterator_frame *tf)
-{
-	if (!tf->start)
-		tf->index = 0;
-	else if (!tf->icase_map)
-		tf->index = git_tree__prefix_position(tf->tree, tf->start);
-	else {
-		if (!git__bsearch(
-				tf->icase_map, git_tree_entrycount(tf->tree),
-				tf, tree_iterator__frame_start_icmp, &tf->index))
-		{
-			while (tf->index > 0) {
-				/* move back while previous entry is still prefixed */
-				if (tree_iterator__frame_start_icmp(
-						tf, (const void *)(tf->index - 1)))
-					break;
-				tf->index--;
-			}
+	/* build frame - sum tree entries from parent range */
+	for (i = tf->current; i < tf->next; ++i)
+		n_entries += git_tree_entrycount(tf->entries[i].tree);
+	sz += n_entries * sizeof(tree_iterator_entry);
+	new_tf = git__calloc(sz, sizeof(char));
+	GITERR_CHECK_ALLOC(new_tf);
+
+	/* populate frame and entries */
+	new_tf->parent = tf;
+	new_tf->n_entries = n_entries;
+
+	for (i = tf->current, n_entries = 0; i < tf->next; ++i) {
+		git_tree *tree = tf->entries[i].tree;
+		size_t j, max_j = git_tree_entrycount(tree);
+
+		for (j = 0; j < max_j; ++j) {
+			new_tf->entries[n_entries].parent_entry_index = i;
+			new_tf->entries[n_entries].parent_tree_index = j;
+			n_entries++;
 		}
 	}
-}
-
-static int tree_iterator__push_frame(
-	tree_iterator *ti, git_tree *tree, const char *start)
-{
-	size_t i, max_i = git_tree_entrycount(tree);
-	tree_iterator_frame *tf =
-		git__calloc(1, sizeof(tree_iterator_frame) + max_i * sizeof(void *));
-	GITERR_CHECK_ALLOC(tf);
-
-	tf->tree  = tree;
 
-	tf->next  = ti->stack;
-	ti->stack = tf;
-	if (tf->next)
-		tf->next->prev = tf;
-	else
-		ti->tail = tf;
+	/* if ignore_case, sort entries case insensitively */
+	if (iterator__ignore_case(ti))
+		git__qsort_r(
+			new_tf->entries, new_tf->n_entries, sizeof(tree_iterator_entry),
+			tree_iterator__entry_cmp, new_tf);
+
+	/* pick new_tf->current based on "start" (or start at zero) */
+	if (tf->startlen > 0) {
+		/* find first item >= start */
+		for (i = 0; i < new_tf->n_entries; ++i) {
+			if (!(te = tree_iterator__get_tree_entry(new_tf, NULL, i)))
+				break;
+			sz = min(tf->startlen, te->filename_len);
+			if (ti->strncomp(tf->start, te->filename, sz) <= 0 &&
+				(tf->startlen <= te->filename_len ||
+				 tf->start[te->filename_len] == '/'))
+				break;
+		}
+		new_tf->current = i;
 
-	if (start && *start) {
-		tf->start    = start;
-		tf->startlen = strlen(start);
+		if ((new_tf->start = strchr(tf->start, '/')) != NULL) {
+			new_tf->start++;
+			new_tf->startlen = strlen(new_tf->start);
+		}
 	}
 
 	ti->path_has_filename = false;
 
-	if (!max_i)
-		return 0;
+	/* find next and load trees for current range */
+	if ((error = tree_iterator__set_next(ti, new_tf)) < 0)
+		return error;
 
-	/* build secondary index if iterator is case-insensitive */
-	if (iterator__ignore_case(ti)) {
-		tf->icase_map = tf->icase_data;
+	tf->child = new_tf;
+	ti->head  = new_tf;
 
-		for (i = 0; i < max_i; ++i)
-			tf->icase_map[i] = (void *)i;
+	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
+		return tree_iterator__push_frame(ti);
 
-		git__tsort_r(
-			tf->icase_map, max_i, tree_iterator__icase_map_cmp, tf->tree);
-	}
+	return 0;
+}
 
-	tree_iterator__frame_seek_start(tf);
+static bool tree_iterator__move_to_next(tree_iterator_frame *tf)
+{
+	while (tf->current < tf->next) {
+		if (tf->parent && tf->entries[tf->current].tree) {
+			git_tree_free(tf->entries[tf->current].tree);
+			tf->entries[tf->current].tree = NULL;
+		}
+		tf->current++;
+	}
 
-	return 0;
+	return (tf->current < tf->n_entries);
 }
 
-static int tree_iterator__expand_tree(tree_iterator *ti)
+static bool tree_iterator__pop_frame(tree_iterator *ti)
 {
-	int error;
-	git_tree *subtree;
-	const git_tree_entry *te = tree_iterator__tree_entry(ti);
-	const char *relpath;
+	tree_iterator_frame *tf = ti->head;
 
-	while (te != NULL && git_tree_entry__is_tree(te)) {
-		relpath = tree_iterator__current_filename(ti, te);
+	if (!tf->parent)
+		return false;
 
-		/* check that we have not passed the range end */
-		if (iterator__past_end(ti, relpath))
-			return tree_iterator__to_end(ti);
+	tree_iterator__move_to_next(tf);
 
-		if ((error = git_tree_lookup(&subtree, ti->base.repo, &te->oid)) < 0)
-			return error;
+	ti->head = tf->parent;
+	ti->head->child = NULL;
+	git__free(tf);
 
-		relpath = NULL;
+	git_buf_rtruncate_at_char(&ti->path, '/');
 
-		/* apply range start to new frame if relevant */
-		if (ti->stack->start &&
-			ti->base.prefixcomp(ti->stack->start, te->filename) == 0)
-		{
-			if (ti->stack->start[te->filename_len] == '/')
-				relpath = ti->stack->start + te->filename_len + 1;
-		}
+	return true;
+}
 
-		if ((error = tree_iterator__push_frame(ti, subtree, relpath)) < 0)
-			return error;
+static int tree_iterator__current(
+	const git_index_entry **entry, git_iterator *self)
+{
+	tree_iterator *ti = (tree_iterator *)self;
+	tree_iterator_frame *tf = ti->head;
+	const git_tree_entry *te;
 
-		/* if including trees, then one expansion is always enough */
-		if (iterator__include_trees(ti))
-			break;
+	if (entry)
+		*entry = NULL;
+
+	if (!(te = tree_iterator__get_tree_entry(tf, NULL, tf->current)))
+		return 0;
 
-		te = tree_iterator__tree_entry(ti);
+	ti->entry.mode = te->attr;
+	git_oid_cpy(&ti->entry.oid, &te->oid);
+
+	ti->entry.path = tree_iterator__current_filename(ti, te);
+	if (ti->entry.path == NULL)
+		return -1;
+
+	if (iterator__past_end(ti, ti->entry.path)) {
+		while (tree_iterator__pop_frame(ti)) /* pop to top */;
+		ti->head->current = ti->head->n_entries;
+		return 0;
 	}
 
+	if (entry)
+		*entry = &ti->entry;
+
 	return 0;
 }
 
@@ -408,16 +444,12 @@ static int tree_iterator__advance_into(
 {
 	int error = 0;
 	tree_iterator *ti = (tree_iterator *)self;
-	const git_tree_entry *te = tree_iterator__tree_entry(ti);
 
 	if (entry)
 		*entry = NULL;
 
-	/* if DONT_AUTOEXPAND is off, the following will always be false */
-	if (te && git_tree_entry__is_tree(te))
-		error = tree_iterator__expand_tree(ti);
-
-	if (!error && entry)
+	if (tree_iterator__at_tree(ti) &&
+		!(error = tree_iterator__push_frame(ti)))
 		error = tree_iterator__current(entry, self);
 
 	return error;
@@ -426,14 +458,18 @@ static int tree_iterator__advance_into(
 static int tree_iterator__advance(
 	const git_index_entry **entry, git_iterator *self)
 {
+	int error;
 	tree_iterator *ti = (tree_iterator *)self;
-	const git_tree_entry *te = tree_iterator__tree_entry(ti);
+	tree_iterator_frame *tf = ti->head;
 
-	if (entry != NULL)
+	if (entry)
 		*entry = NULL;
 
-	/* given include_trees & autoexpand, we might have to go into a tree */
-	if (te && git_tree_entry__is_tree(te) && iterator__do_autoexpand(ti))
+	if (tf->current > tf->n_entries)
+		return 0;
+
+	if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
+		tree_iterator__at_tree(ti))
 		return tree_iterator__advance_into(entry, self);
 
 	if (ti->path_has_filename) {
@@ -441,19 +477,16 @@ static int tree_iterator__advance(
 		ti->path_has_filename = false;
 	}
 
-	while (1) {
-		++ti->stack->index;
-
-		if ((te = tree_iterator__tree_entry(ti)) != NULL)
-			break;
-
-		if (!tree_iterator__pop_frame(ti))
-			break; /* no frames left to pop */
+	/* scan forward and up, advancing in frame or popping frame when done */
+	while (!tree_iterator__move_to_next(tf) && tree_iterator__pop_frame(ti))
+		tf = ti->head;
 
-		git_buf_rtruncate_at_char(&ti->path, '/');
-	}
+	/* find next and load trees */
+	if ((error = tree_iterator__set_next(ti, tf)) < 0)
+		return error;
 
-	if (te && git_tree_entry__is_tree(te) && !iterator__include_trees(ti))
+	/* deal with include_trees / auto_expand as needed */
+	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
 		return tree_iterator__advance_into(entry, self);
 
 	return tree_iterator__current(entry, self);
@@ -463,44 +496,45 @@ static int tree_iterator__seek(git_iterator *self, const char *prefix)
 {
 	GIT_UNUSED(self);
 	GIT_UNUSED(prefix);
-	/* pop stack until matches prefix */
-	/* seek item in current frame matching prefix */
-	/* push stack which matches prefix */
 	return -1;
 }
 
-static void tree_iterator__free(git_iterator *self)
+static int tree_iterator__reset(
+	git_iterator *self, const char *start, const char *end)
 {
 	tree_iterator *ti = (tree_iterator *)self;
 
-	while (tree_iterator__pop_frame(ti)) /* pop all */;
+	while (tree_iterator__pop_frame(ti)) /* pop to top */;
+	ti->top->current = 0;
 
-	tree_iterator__free_frame(ti->stack);
-	ti->stack = ti->tail = NULL;
+	if (iterator__reset_range(self, start, end) < 0)
+		return -1;
+	git_buf_clear(&ti->path);
 
-	git_buf_free(&ti->path);
+	return tree_iterator__push_frame(ti); /* re-expand top tree */
 }
 
-static int tree_iterator__reset(
-	git_iterator *self, const char *start, const char *end)
+static int tree_iterator__at_end(git_iterator *self)
 {
 	tree_iterator *ti = (tree_iterator *)self;
+	return (ti->head->current >= ti->head->n_entries);
+}
 
-	while (tree_iterator__pop_frame(ti)) /* pop all */;
-
-	if (iterator__reset_range(self, start, end) < 0)
-		return -1;
-
-	/* reset start position */
-	tree_iterator__frame_seek_start(ti->stack);
+static void tree_iterator__free(git_iterator *self)
+{
+	tree_iterator *ti = (tree_iterator *)self;
 
-	git_buf_clear(&ti->path);
-	ti->path_has_filename = false;
+	while (tree_iterator__pop_frame(ti)) /* pop to top */;
 
-	if (iterator__do_autoexpand(ti) && !iterator__include_trees(ti))
-		return tree_iterator__expand_tree(ti);
+	/* free base frame */
+	if (ti->head) {
+		git_tree_free(ti->head->entries[0].tree);
+		ti->head->entries[0].tree = NULL;
+		git__free(ti->head);
+	}
+	ti->head = ti->top = NULL;
 
-	return 0;
+	git_buf_free(&ti->path);
 }
 
 int git_iterator_for_tree(
@@ -520,15 +554,19 @@ int git_iterator_for_tree(
 		return error;
 
 	ITERATOR_BASE_INIT(ti, tree, TREE);
-
 	ti->base.repo = git_tree_owner(tree);
 
-	if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0 ||
-		(error = tree_iterator__push_frame(ti, tree, ti->base.start)) < 0)
+	if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0)
 		goto fail;
 
-	if (iterator__do_autoexpand(ti) && !iterator__include_trees(ti) &&
-		(error = tree_iterator__expand_tree(ti)) < 0)
+	if (iterator__ignore_case(ti)) {
+		ti->strcomp = git__strcasecmp; ti->strncomp = git__strncasecmp;
+	} else {
+		ti->strcomp = git__strcmp; ti->strncomp = git__strncmp;
+	}
+
+	if ((error = tree_iterator__create_top_frame(ti, tree)) < 0 ||
+		(error = tree_iterator__push_frame(ti)) < 0) /* expand top right now */
 		goto fail;
 
 	*iter = (git_iterator *)ti;
@@ -1216,8 +1254,14 @@ git_index *git_iterator_get_index(git_iterator *iter)
 int git_iterator_current_tree_entry(
 	const git_tree_entry **tree_entry, git_iterator *iter)
 {
-	*tree_entry = (iter->type != GIT_ITERATOR_TYPE_TREE) ? NULL :
-		tree_iterator__tree_entry((tree_iterator *)iter);
+	if (iter->type != GIT_ITERATOR_TYPE_TREE)
+		*tree_entry = NULL;
+	else {
+		tree_iterator *ti = (tree_iterator *)iter;
+		*tree_entry =
+			tree_iterator__get_tree_entry(ti->head, NULL, ti->head->current);
+	}
+
 	return 0;
 }
 
@@ -1229,39 +1273,28 @@ int git_iterator_current_parent_tree(
 	tree_iterator *ti = (tree_iterator *)iter;
 	tree_iterator_frame *tf;
 	const char *scan = parent_path;
-	int (*strncomp)(const char *a, const char *b, size_t sz);
+	const git_tree_entry *te;
 
-	if (iter->type != GIT_ITERATOR_TYPE_TREE || ti->stack == NULL)
-		goto notfound;
+	*tree_ptr = NULL;
 
-	strncomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ?
-		git__strncasecmp : git__strncmp;
+	if (iter->type != GIT_ITERATOR_TYPE_TREE)
+		return 0;
 
-	for (tf = ti->tail; tf != NULL; tf = tf->prev) {
-		const git_tree_entry *te;
+	tf = ti->top;
 
-		if (!*scan) {
-			*tree_ptr = tf->tree;
+	while (*scan) {
+		/* get entry of this parent that child is currently on */
+		if (!(tf = tf->child) ||
+			!(te = tree_iterator__get_tree_entry(tf, NULL, tf->current)) ||
+			ti->strncomp(scan, te->filename, te->filename_len) != 0)
 			return 0;
-		}
-
-		te = git_tree_entry_byindex(tf->tree,
-			tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index);
-
-		if (strncomp(scan, te->filename, te->filename_len) != 0)
-			goto notfound;
 
 		scan += te->filename_len;
-
-		if (*scan) {
-			if (*scan != '/')
-				goto notfound;
+		if (*scan == '/')
 			scan++;
-		}
 	}
 
-notfound:
-	*tree_ptr = NULL;
+	*tree_ptr = tf->entries[tf->current].tree;
 	return 0;
 }
 
diff --git a/src/posix.h b/src/posix.h
index 9dd0c94..719c8a0 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -32,14 +32,13 @@ typedef int git_file;
  * Standard POSIX Methods
  *
  * All the methods starting with the `p_` prefix are
- * direct ports of the standard POSIX methods. 
+ * direct ports of the standard POSIX methods.
  *
  * Some of the methods are slightly wrapped to provide
  * saner defaults. Some of these methods are emulated
  * in Windows platforns.
  *
  * Use your manpages to check the docs on these.
- * Straightforward 
  */
 
 extern int p_read(git_file fd, void *buf, size_t cnt);
diff --git a/src/unix/posix.h b/src/unix/posix.h
index c738b53..f4886c5 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -8,6 +8,7 @@
 #define INCLUDE_posix__w32_h__
 
 #include <stdio.h>
+#include <sys/param.h>
 
 #define p_lstat(p,b) lstat(p,b)
 #define p_readlink(a, b, c) readlink(a, b, c)
diff --git a/src/util.c b/src/util.c
index 059108e..561288f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -607,3 +607,33 @@ size_t git__unescape(char *str)
 
 	return (pos - str);
 }
+
+#if defined(GIT_WIN32) || defined(BSD)
+typedef struct {
+	git__qsort_r_cmp cmp;
+	void *payload;
+} git__qsort_r_glue;
+
+static int GIT_STDLIB_CALL git__qsort_r_glue_cmp(
+	void *payload, const void *a, const void *b)
+{
+	git__qsort_r_glue *glue = payload;
+	return glue->cmp(a, b, glue->payload);
+}
+#endif
+
+void git__qsort_r(
+	void *els, size_t nel, size_t elsize, git__qsort_r_cmp cmp, void *payload)
+{
+#if defined(GIT_WIN32)
+	git__qsort_r_glue glue = { cmp, payload };
+	qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
+#else
+#if defined(BSD)
+	git__qsort_r_glue glue = { cmp, payload };
+	qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
+#else
+	qsort_r(els, nel, elsize, cmp, payload);
+#endif
+#endif
+}
diff --git a/src/util.h b/src/util.h
index 9dbcb6a..e77f17e 100644
--- a/src/util.h
+++ b/src/util.h
@@ -151,6 +151,10 @@ typedef int (*git__tsort_r_cmp)(const void *a, const void *b, void *payload);
 extern void git__tsort_r(
 	void **dst, size_t size, git__tsort_r_cmp cmp, void *payload);
 
+typedef int (*git__qsort_r_cmp)(const void *a, const void *b, void *payload);
+
+extern void git__qsort_r(
+	void *els, size_t nel, size_t elsize, git__qsort_r_cmp cmp, void *payload);
 
 /**
  * @param position If non-NULL, this will be set to the position where the
diff --git a/tests-clar/repo/iterator.c b/tests-clar/repo/iterator.c
index 27ab4fe..3d72d7a 100644
--- a/tests-clar/repo/iterator.c
+++ b/tests-clar/repo/iterator.c
@@ -6,7 +6,6 @@ static git_repository *g_repo;
 
 void test_repo_iterator__initialize(void)
 {
-	g_repo = cl_git_sandbox_init("icase");
 }
 
 void test_repo_iterator__cleanup(void)
@@ -16,7 +15,11 @@ void test_repo_iterator__cleanup(void)
 }
 
 static void expect_iterator_items(
-	git_iterator *i, int expected_flat, int expected_total)
+	git_iterator *i,
+	int expected_flat,
+	const char **expected_flat_paths,
+	int expected_total,
+	const char **expected_total_paths)
 {
 	const git_index_entry *entry;
 	int count;
@@ -29,11 +32,21 @@ static void expect_iterator_items(
 		if (no_trees)
 			cl_assert(entry->mode != GIT_FILEMODE_TREE);
 
-		count++;
+		if (expected_flat_paths) {
+			const char *expect_path = expected_flat_paths[count];
+			size_t expect_len = strlen(expect_path);
+
+			cl_assert_equal_s(expect_path, entry->path);
+
+			if (expect_path[expect_len - 1] == '/')
+				cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
+			else
+				cl_assert(entry->mode != GIT_FILEMODE_TREE);
+		}
 
 		cl_git_pass(git_iterator_advance(&entry, i));
 
-		if (count > expected_flat)
+		if (++count > expected_flat)
 			break;
 	}
 
@@ -48,14 +61,24 @@ static void expect_iterator_items(
 		if (no_trees)
 			cl_assert(entry->mode != GIT_FILEMODE_TREE);
 
-		count++;
+		if (expected_total_paths) {
+			const char *expect_path = expected_total_paths[count];
+			size_t expect_len = strlen(expect_path);
+
+			cl_assert_equal_s(expect_path, entry->path);
+
+			if (expect_path[expect_len - 1] == '/')
+				cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
+			else
+				cl_assert(entry->mode != GIT_FILEMODE_TREE);
+		}
 
 		if (entry->mode == GIT_FILEMODE_TREE)
 			cl_git_pass(git_iterator_advance_into(&entry, i));
 		else
 			cl_git_pass(git_iterator_advance(&entry, i));
 
-		if (count > expected_total)
+		if (++count > expected_total)
 			break;
 	}
 
@@ -84,23 +107,25 @@ void test_repo_iterator__index(void)
 	git_iterator *i;
 	git_index *index;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	cl_git_pass(git_repository_index(&index, g_repo));
 
 	/* autoexpand with no tree entries for index */
 	cl_git_pass(git_iterator_for_index(&i, index, 0, NULL, NULL));
-	expect_iterator_items(i, 20, 20);
+	expect_iterator_items(i, 20, NULL, 20, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
-	expect_iterator_items(i, 22, 22);
+	expect_iterator_items(i, 22, NULL, 22, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
-	expect_iterator_items(i, 12, 22);
+	expect_iterator_items(i, 12, NULL, 22, NULL);
 	git_iterator_free(i);
 
 	git_index_free(index);
@@ -112,6 +137,8 @@ void test_repo_iterator__index_icase(void)
 	git_index *index;
 	unsigned int caps;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	cl_git_pass(git_repository_index(&index, g_repo));
 	caps = git_index_caps(index);
 
@@ -120,32 +147,32 @@ void test_repo_iterator__index_icase(void)
 
 	/* autoexpand with no tree entries over range */
 	cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D"));
-	expect_iterator_items(i, 7, 7);
+	expect_iterator_items(i, 7, NULL, 7, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z"));
-	expect_iterator_items(i, 3, 3);
+	expect_iterator_items(i, 3, NULL, 3, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 8, 8);
+	expect_iterator_items(i, 8, NULL, 8, NULL);
 	git_iterator_free(i);
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 4, 4);
+	expect_iterator_items(i, 4, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 5, 8);
+	expect_iterator_items(i, 5, NULL, 8, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 4);
+	expect_iterator_items(i, 1, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	/* force case insensitivity */
@@ -153,33 +180,33 @@ void test_repo_iterator__index_icase(void)
 
 	/* autoexpand with no tree entries over range */
 	cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D"));
-	expect_iterator_items(i, 13, 13);
+	expect_iterator_items(i, 13, NULL, 13, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z"));
-	expect_iterator_items(i, 5, 5);
+	expect_iterator_items(i, 5, NULL, 5, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 14, 14);
+	expect_iterator_items(i, 14, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 6, 6);
+	expect_iterator_items(i, 6, NULL, 6, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 9, 14);
+	expect_iterator_items(i, 9, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_index(
 		&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 6);
+	expect_iterator_items(i, 1, NULL, 6, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_index_set_caps(index, caps));
@@ -191,23 +218,25 @@ void test_repo_iterator__tree(void)
 	git_iterator *i;
 	git_tree *head;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	cl_git_pass(git_repository_head_tree(&head, g_repo));
 
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_tree(&i, head, 0, NULL, NULL));
-	expect_iterator_items(i, 20, 20);
+	expect_iterator_items(i, 20, NULL, 20, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
-	expect_iterator_items(i, 22, 22);
+	expect_iterator_items(i, 22, NULL, 22, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
-	expect_iterator_items(i, 12, 22);
+	expect_iterator_items(i, 12, NULL, 22, NULL);
 	git_iterator_free(i);
 
 	git_tree_free(head);
@@ -219,94 +248,171 @@ void test_repo_iterator__tree_icase(void)
 	git_tree *head;
 	git_iterator_flag_t flag;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	cl_git_pass(git_repository_head_tree(&head, g_repo));
 
 	flag = GIT_ITERATOR_DONT_IGNORE_CASE;
 
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D"));
-	expect_iterator_items(i, 7, 7);
+	expect_iterator_items(i, 7, NULL, 7, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z"));
-	expect_iterator_items(i, 3, 3);
+	expect_iterator_items(i, 3, NULL, 3, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 8, 8);
+	expect_iterator_items(i, 8, NULL, 8, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 4, 4);
+	expect_iterator_items(i, 4, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 5, 8);
+	expect_iterator_items(i, 5, NULL, 8, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 4);
+	expect_iterator_items(i, 1, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	flag = GIT_ITERATOR_IGNORE_CASE;
 
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D"));
-	expect_iterator_items(i, 13, 13);
+	expect_iterator_items(i, 13, NULL, 13, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z"));
-	expect_iterator_items(i, 5, 5);
+	expect_iterator_items(i, 5, NULL, 5, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 14, 14);
+	expect_iterator_items(i, 14, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 6, 6);
+	expect_iterator_items(i, 6, NULL, 6, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 9, 14);
+	expect_iterator_items(i, 9, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_tree(
 		&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 6);
+	expect_iterator_items(i, 1, NULL, 6, NULL);
+	git_iterator_free(i);
+}
+
+void test_repo_iterator__tree_more(void)
+{
+	git_iterator *i;
+	git_tree *head;
+	static const char *expect_basic[] = {
+		"current_file",
+		"file_deleted",
+		"modified_file",
+		"staged_changes",
+		"staged_changes_file_deleted",
+		"staged_changes_modified_file",
+		"staged_delete_file_deleted",
+		"staged_delete_modified_file",
+		"subdir.txt",
+		"subdir/current_file",
+		"subdir/deleted_file",
+		"subdir/modified_file",
+		NULL,
+	};
+	static const char *expect_trees[] = {
+		"current_file",
+		"file_deleted",
+		"modified_file",
+		"staged_changes",
+		"staged_changes_file_deleted",
+		"staged_changes_modified_file",
+		"staged_delete_file_deleted",
+		"staged_delete_modified_file",
+		"subdir.txt",
+		"subdir/",
+		"subdir/current_file",
+		"subdir/deleted_file",
+		"subdir/modified_file",
+		NULL,
+	};
+	static const char *expect_noauto[] = {
+		"current_file",
+		"file_deleted",
+		"modified_file",
+		"staged_changes",
+		"staged_changes_file_deleted",
+		"staged_changes_modified_file",
+		"staged_delete_file_deleted",
+		"staged_delete_modified_file",
+		"subdir.txt",
+		"subdir/",
+		NULL
+	};
+
+	g_repo = cl_git_sandbox_init("status");
+
+	cl_git_pass(git_repository_head_tree(&head, g_repo));
+
+	/* auto expand with no tree entries */
+	cl_git_pass(git_iterator_for_tree(&i, head, 0, NULL, NULL));
+	expect_iterator_items(i, 12, expect_basic, 12, expect_basic);
+	git_iterator_free(i);
+
+	/* auto expand with tree entries */
+	cl_git_pass(git_iterator_for_tree(
+		&i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
+	expect_iterator_items(i, 13, expect_trees, 13, expect_trees);
+	git_iterator_free(i);
+
+	/* no auto expand (implies trees included) */
+	cl_git_pass(git_iterator_for_tree(
+		&i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
+	expect_iterator_items(i, 10, expect_noauto, 13, expect_trees);
 	git_iterator_free(i);
+
+	git_tree_free(head);
 }
 
 void test_repo_iterator__workdir(void)
 {
 	git_iterator *i;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_workdir(&i, g_repo, 0, NULL, NULL));
-	expect_iterator_items(i, 20, 20);
+	expect_iterator_items(i, 20, NULL, 20, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
-	expect_iterator_items(i, 22, 22);
+	expect_iterator_items(i, 22, NULL, 22, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
-	expect_iterator_items(i, 12, 22);
+	expect_iterator_items(i, 12, NULL, 22, NULL);
 	git_iterator_free(i);
 }
 
@@ -315,69 +421,71 @@ void test_repo_iterator__workdir_icase(void)
 	git_iterator *i;
 	git_iterator_flag_t flag;
 
+	g_repo = cl_git_sandbox_init("icase");
+
 	flag = GIT_ITERATOR_DONT_IGNORE_CASE;
 
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D"));
-	expect_iterator_items(i, 7, 7);
+	expect_iterator_items(i, 7, NULL, 7, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z"));
-	expect_iterator_items(i, 3, 3);
+	expect_iterator_items(i, 3, NULL, 3, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 8, 8);
+	expect_iterator_items(i, 8, NULL, 8, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 4, 4);
+	expect_iterator_items(i, 4, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 5, 8);
+	expect_iterator_items(i, 5, NULL, 8, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 4);
+	expect_iterator_items(i, 1, NULL, 4, NULL);
 	git_iterator_free(i);
 
 	flag = GIT_ITERATOR_IGNORE_CASE;
 
 	/* auto expand with no tree entries */
 	cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D"));
-	expect_iterator_items(i, 13, 13);
+	expect_iterator_items(i, 13, NULL, 13, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z"));
-	expect_iterator_items(i, 5, 5);
+	expect_iterator_items(i, 5, NULL, 5, NULL);
 	git_iterator_free(i);
 
 	/* auto expand with tree entries */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
-	expect_iterator_items(i, 14, 14);
+	expect_iterator_items(i, 14, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
-	expect_iterator_items(i, 6, 6);
+	expect_iterator_items(i, 6, NULL, 6, NULL);
 	git_iterator_free(i);
 
 	/* no auto expand (implies trees included) */
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
-	expect_iterator_items(i, 9, 14);
+	expect_iterator_items(i, 9, NULL, 14, NULL);
 	git_iterator_free(i);
 
 	cl_git_pass(git_iterator_for_workdir(
 		&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
-	expect_iterator_items(i, 1, 6);
+	expect_iterator_items(i, 1, NULL, 6, NULL);
 	git_iterator_free(i);
 }