Commit 0e0589fcc383a0ca96d342896103e01d715df755

Edward Thomson 2016-03-10T00:04:26

iterator: combine fs+workdir iterators more completely Drop some of the layers of indirection between the workdir and the filesystem iterators. This makes the code a little bit easier to follow, and reduces the number of unnecessary allocations a bit as well. (Prior to this, when we filter entries, we would allocate them, filter them and then free them; now we do the filtering before allocation.) Also, rename `git_iterator_advance_over_with_status` to just `git_iterator_advance_over`. Mostly because it's a fucking long-ass function name otherwise.

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
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
diff --git a/src/checkout.c b/src/checkout.c
index bf3de2e..0fbb7fc 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -394,7 +394,7 @@ static int checkout_action_wd_only(
 		git_buf_sets(&data->tmp, wd->path);
 		saved_wd.path = data->tmp.ptr;
 
-		error = git_iterator_advance_over_with_status(
+		error = git_iterator_advance_over(
 			wditem, &untracked_state, workdir);
 		if (error == GIT_ITEROVER)
 			over = true;
@@ -930,7 +930,7 @@ static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, g
 	git_index *index;
 
 	/* Only write conficts from sources that have them: indexes. */
-	if ((index = git_iterator_get_index(data->target)) == NULL)
+	if ((index = git_iterator_index(data->target)) == NULL)
 		return 0;
 
 	data->update_conflicts._cmp = checkout_conflictdata_cmp;
@@ -1080,7 +1080,7 @@ static int checkout_conflicts_coalesce_renames(
 	size_t i, names;
 	int error = 0;
 
-	if ((index = git_iterator_get_index(data->target)) == NULL)
+	if ((index = git_iterator_index(data->target)) == NULL)
 		return 0;
 
 	/* Juggle entries based on renames */
@@ -1138,7 +1138,7 @@ static int checkout_conflicts_mark_directoryfile(
 	const char *path;
 	int prefixed, error = 0;
 
-	if ((index = git_iterator_get_index(data->target)) == NULL)
+	if ((index = git_iterator_index(data->target)) == NULL)
 		return 0;
 
 	len = git_index_entrycount(index);
@@ -2378,7 +2378,7 @@ static int checkout_data_init(
 		if ((error = git_repository_index(&data->index, data->repo)) < 0)
 			goto cleanup;
 
-		if (data->index != git_iterator_get_index(target)) {
+		if (data->index != git_iterator_index(target)) {
 			if ((error = git_index_read(data->index, true)) < 0)
 				goto cleanup;
 
@@ -2600,7 +2600,7 @@ int git_checkout_iterator(
 		(error = checkout_create_conflicts(&data)) < 0)
 		goto cleanup;
 
-	if (data.index != git_iterator_get_index(target) &&
+	if (data.index != git_iterator_index(target) &&
 		(error = checkout_extensions_update_index(&data)) < 0)
 		goto cleanup;
 
diff --git a/src/diff.c b/src/diff.c
index 9ac5b92..a2bfcbf 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -826,8 +826,7 @@ static int maybe_modified(
 	 */
 	} else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
 		bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
-		git_index *index;
-		git_iterator_index(&index, info->new_iter);
+		git_index *index = git_iterator_index(info->new_iter);
 
 		status = GIT_DELTA_UNMODIFIED;
 
@@ -980,15 +979,14 @@ static int iterator_advance_into(
 	return error;
 }
 
-static int iterator_advance_over_with_status(
+static int iterator_advance_over(
 	const git_index_entry **entry,
 	git_iterator_status_t *status,
 	git_iterator *iterator)
 {
-	int error;
+	int error = git_iterator_advance_over(entry, status, iterator);
 
-	if ((error = git_iterator_advance_over_with_status(
-			entry, status, iterator)) == GIT_ITEROVER) {
+	if (error == GIT_ITEROVER) {
 		*entry = NULL;
 		error = 0;
 	}
@@ -1056,7 +1054,7 @@ static int handle_unmatched_new_item(
 				return iterator_advance(&info->nitem, info->new_iter);
 
 			/* iterate into dir looking for an actual untracked file */
-			if ((error = iterator_advance_over_with_status(
+			if ((error = iterator_advance_over(
 					&info->nitem, &untracked_state, info->new_iter)) < 0)
 				return error;
 
diff --git a/src/iterator.c b/src/iterator.c
index 91a7145..ce0fb0e 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -49,15 +49,19 @@
 		git__free(P); return -1; } \
 	} while (0)
 
-#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 GIT_ITERATOR_FIRST_ACCESS   (1 << 15)
+#define GIT_ITERATOR_HONOR_IGNORES  (1 << 16)
+#define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17)
 
-#define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
+#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)
 
 #define iterator__end(I) ((git_iterator *)(I))->end
 #define iterator__past_end(I,PATH) \
@@ -221,6 +225,29 @@ static int iterator__reset_range(
 	return 0;
 }
 
+int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
+{
+	if (ignore_case) {
+		iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
+
+		iter->strcomp = git__strcasecmp;
+		iter->strncomp = git__strncasecmp;
+		iter->prefixcomp = git__prefixcmp_icase;
+		iter->entry_srch = git_index_entry_isrch;
+	} else {
+		iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
+
+		iter->strcomp = git__strcmp;
+		iter->strncomp = git__strncmp;
+		iter->prefixcomp = git__prefixcmp;
+		iter->entry_srch = git_index_entry_srch;
+	}
+
+	iterator_pathlist__update_ignore_case(iter);
+
+	return 0;
+}
+
 static int iterator__update_ignore_case(
 	git_iterator *iter,
 	git_iterator_flag_t flags)
@@ -241,23 +268,7 @@ static int iterator__update_ignore_case(
 		ignore_case = (index->ignore_case == 1);
 	}
 
-	if (ignore_case) {
-		iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
-
-		iter->strcomp = git__strcasecmp;
-		iter->strncomp = git__strncasecmp;
-		iter->prefixcomp = git__prefixcmp_icase;
-	} else {
-		iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
-
-		iter->strcomp = git__strcmp;
-		iter->strncomp = git__strncmp;
-		iter->prefixcomp = git__prefixcmp;
-	}
-
-	iterator_pathlist__update_ignore_case(iter);
-
-	return 0;
+	return git_iterator_set_ignore_case(iter, ignore_case);
 }
 
 GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
@@ -269,14 +280,18 @@ GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
 static int iterator_range_init(
 	git_iterator *iter, const char *start, const char *end)
 {
-	if (start) {
+	if (start && *start) {
 		iter->start = git__strdup(start);
 		GITERR_CHECK_ALLOC(iter->start);
+
+		iter->start_len = strlen(iter->start);
 	}
 
-	if (end) {
+	if (end && *end) {
 		iter->end = git__strdup(end);
 		GITERR_CHECK_ALLOC(iter->end);
+
+		iter->end_len = strlen(iter->end);
 	}
 
 	iter->started = (iter->start == NULL);
@@ -290,11 +305,13 @@ 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;
 	}
 }
 
@@ -333,10 +350,9 @@ static int iterator_init_common(
 	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;
 
-	assert(repo);
-
 	iter->repo = repo;
 	iter->flags = options->flags;
 
@@ -344,7 +360,7 @@ static int iterator_init_common(
 		ignore_case = true;
 	} else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) {
 		ignore_case = false;
-	} else {
+	} else if (repo) {
 		git_index *index;
 
 		if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0)
@@ -356,6 +372,19 @@ static int iterator_init_common(
 			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))
@@ -364,6 +393,7 @@ static int iterator_init_common(
 	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_srch : git_index_entry_isrch;
 
 	if ((error = iterator_range_init(iter, options->start, options->end)) < 0 ||
 		(error = iterator_pathlist_init(iter, &options->pathlist)) < 0)
@@ -376,6 +406,7 @@ 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;
 }
@@ -416,7 +447,7 @@ GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path)
 /* walker for the index iterator that allows it to walk the sorted pathlist
  * entries alongside sorted iterator entries.
  */
-static bool iterator_pathlist_contains(git_iterator *iter, const char *path)
+static bool iterator_pathlist_next_is(git_iterator *iter, const char *path)
 {
 	char *p;
 	size_t path_len, p_len, cmp_len, i;
@@ -483,6 +514,62 @@ static bool iterator_pathlist_contains(git_iterator *iter, const char *path)
 	return false;
 }
 
+typedef enum {
+	ITERATOR_PATHLIST_NOT_FOUND = 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;
+
+	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_NOT_FOUND;
+}
+
 /* Empty iterator */
 
 static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
@@ -684,8 +771,6 @@ static int tree_iterator_frame_init(
 	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;
@@ -911,7 +996,7 @@ static int tree_iterator_advance(const git_index_entry **out, git_iterator *i)
 		}
 
 		/* if we have a list of paths we're interested in, examine it */
-		if (!iterator_pathlist_contains(&iter->base, iter->entry_path.ptr))
+		if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr))
 			continue;
 
 		is_tree = git_tree_entry__is_tree(entry->tree_entry);
@@ -1058,6 +1143,7 @@ int git_iterator_for_tree(
 		tree_iterator_current,
 		tree_iterator_advance,
 		tree_iterator_advance_into,
+		NULL, /* advance_over */
 		tree_iterator_reset,
 		tree_iterator_reset_range,
 		tree_iterator_at_end,
@@ -1124,975 +1210,913 @@ int git_iterator_current_parent_tree(
 	return 0;
 }
 
-/* Index iterator */
+/* Filesystem iterator */
+
+typedef struct {
+	struct stat st;
+	size_t path_len;
+	iterator_pathlist_search_t match;
+	char path[GIT_FLEX_ARRAY];
+} filesystem_iterator_entry;
+
+typedef struct {
+	git_vector entries;
+	git_pool entry_pool;
+	size_t next_idx;
 
+	size_t path_len;
+	int is_ignored;
+} filesystem_iterator_frame;
 
 typedef struct {
 	git_iterator base;
-	git_iterator_callbacks cb;
+	char *root;
+	size_t root_len;
+
+	unsigned int dirload_flags;
+
+	git_tree *tree;
 	git_index *index;
-	git_vector entries;
-	git_vector_cmp entry_srch;
-	size_t current;
-	/* when limiting with a pathlist, this is the current index into it */
-	size_t pathlist_idx;
-	/* when not in autoexpand mode, use these to represent "tree" state */
-	git_buf partial;
-	size_t partial_pos;
-	char restore_terminator;
-	git_index_entry tree_entry;
-} index_iterator;
+	git_vector index_snapshot;
 
-static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
-{
-	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
+	git_array_t(filesystem_iterator_frame) frames;
+	git_ignores ignores;
 
-	if (ie != NULL && iterator__past_end(ii, ie->path)) {
-		ii->current = git_vector_length(&ii->entries);
-		ie = NULL;
-	}
+	/* info about the current entry */
+	git_index_entry entry;
+	git_buf current_path;
+	int current_is_ignored;
 
-	return ie;
+	/* temporary buffer for advance_over */
+	git_buf tmp_buf;
+} filesystem_iterator;
+
+
+GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_parent_frame(
+	filesystem_iterator *iter)
+{
+	return iter->frames.size > 1 ?
+		&iter->frames.ptr[iter->frames.size-2] : NULL;
 }
 
-static const git_index_entry *index_iterator__advance_over_unwanted(
-	index_iterator *ii)
+GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_current_frame(
+	filesystem_iterator *iter)
 {
-	const git_index_entry *ie = index_iterator__index_entry(ii);
-	bool match;
+	return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL;
+}
 
-	while (ie) {
-		if (!iterator__include_conflicts(ii) &&
-				git_index_entry_is_conflict(ie)) {
-			ii->current++;
-			ie = index_iterator__index_entry(ii);
-			continue;
-		}
+GIT_INLINE(filesystem_iterator_entry *) filesystem_iterator_current_entry(
+	filesystem_iterator_frame *frame)
+{
+	return frame->next_idx == 0 ?
+		NULL : frame->entries.contents[frame->next_idx-1];
+}
 
-		/* if we have a pathlist, this entry's path must be in it to be
-		 * returned.  walk the pathlist in unison with the index to
-		 * compare paths.
-		 */
-		if (ii->base.pathlist.length) {
-			match = iterator_pathlist_walk__contains(&ii->base, ie->path);
+static int filesystem_iterator_entry_cmp(const void *_a, const void *_b)
+{
+	const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a;
+	const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b;
 
-			if (!match) {
-				ii->current++;
-				ie = index_iterator__index_entry(ii);
-				continue;
-			}
-		}
+	return git__strcmp(a->path, b->path);
+}
 
-		break;
-	}
+static int filesystem_iterator_entry_cmp_icase(const void *_a, const void *_b)
+{
+	const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a;
+	const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b;
 
-	return ie;
+	return git__strcasecmp(a->path, b->path);
 }
 
-static void index_iterator__next_prefix_tree(index_iterator *ii)
+#define FILESYSTEM_MAX_DEPTH 100
+
+/**
+ * Figure out if an entry is a submodule.
+ *
+ * We consider it a submodule if the path is listed as a submodule in
+ * either the tree or the index.
+ */
+static int is_submodule(
+	bool *out, filesystem_iterator *iter, const char *path, size_t path_len)
 {
-	const char *slash;
+	bool is_submodule = false;
+	int error;
 
-	if (!iterator__include_trees(ii))
-		return;
+	*out = false;
 
-	slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');
+	/* first see if this path is a submodule in HEAD */
+	if (iter->tree) {
+		git_tree_entry *entry;
 
-	if (slash != NULL) {
-		ii->partial_pos = (slash - ii->partial.ptr) + 1;
-		ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
-		ii->partial.ptr[ii->partial_pos] = '\0';
-	} else {
-		ii->partial_pos = ii->partial.size;
-	}
+		error = git_tree_entry_bypath(&entry, iter->tree, path);
 
-	if (index_iterator__index_entry(ii) == NULL)
-		ii->partial_pos = ii->partial.size;
-}
+		if (error < 0 && error != GIT_ENOTFOUND)
+			return error;
 
-static int index_iterator__first_prefix_tree(index_iterator *ii)
-{
-	const git_index_entry *ie = index_iterator__advance_over_unwanted(ii);
-	const char *scan, *prior, *slash;
+		if (!error) {
+			is_submodule = (entry->attr == GIT_FILEMODE_COMMIT);
+			git_tree_entry_free(entry);
+		}
+	}
 
-	if (!ie || !iterator__include_trees(ii))
-		return 0;
+	if (!is_submodule && iter->index) {
+		size_t pos;
 
-	/* find longest common prefix with prior index entry */
-	for (scan = slash = ie->path, prior = ii->partial.ptr;
-		 *scan && *scan == *prior; ++scan, ++prior)
-		if (*scan == '/')
-			slash = scan;
+		error = git_index_snapshot_find(&pos,
+			&iter->index_snapshot, iter->base.entry_srch, path, path_len, 0);
 
-	if (git_buf_sets(&ii->partial, ie->path) < 0)
-		return -1;
+		if (error < 0 && error != GIT_ENOTFOUND)
+			return error;
 
-	ii->partial_pos = (slash - ie->path) + 1;
-	index_iterator__next_prefix_tree(ii);
+		if (!error) {
+			git_index_entry *e = git_vector_get(&iter->index_snapshot, pos);
+			is_submodule = (e->mode == GIT_FILEMODE_COMMIT);
+		}
+	}
 
+	*out = is_submodule;
 	return 0;
 }
 
-#define index_iterator__at_tree(I) \
-	(iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)
+GIT_INLINE(git_dir_flag) filesystem_iterator_dir_flag(git_index_entry *entry)
+{
+#if defined(GIT_WIN32) && !defined(__MINGW32__)
+	return (entry && entry->mode) ?
+		(S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) :
+		GIT_DIR_FLAG_UNKNOWN;
+#else
+	GIT_UNUSED(entry);
+	return GIT_DIR_FLAG_UNKNOWN;
+#endif
+}
 
-static int index_iterator__current(
-	const git_index_entry **entry, git_iterator *self)
+static void filesystem_iterator_frame_push_ignores(
+	filesystem_iterator *iter,
+	filesystem_iterator_entry *frame_entry,
+	filesystem_iterator_frame *new_frame)
 {
-	index_iterator *ii = (index_iterator *)self;
-	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
+	filesystem_iterator_frame *previous_frame;
+	const char *path = frame_entry ? frame_entry->path : "";
 
-	if (ie != NULL && index_iterator__at_tree(ii)) {
-		ii->tree_entry.path = ii->partial.ptr;
-		ie = &ii->tree_entry;
+	if (!iterator__honor_ignores(&iter->base))
+		return;
+
+	if (git_ignore__lookup(&new_frame->is_ignored,
+			&iter->ignores, path, GIT_DIR_FLAG_TRUE) < 0) {
+		giterr_clear();
+		new_frame->is_ignored = GIT_IGNORE_NOTFOUND;
 	}
 
-	if (entry)
-		*entry = ie;
+	/* if this is not the top level directory... */
+	if (frame_entry) {
+		const char *relative_path;
 
-	ii->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+		previous_frame = filesystem_iterator_parent_frame(iter);
 
-	return (ie != NULL) ? 0 : GIT_ITEROVER;
+		/* push new ignores for files in this directory */
+		relative_path = frame_entry->path + previous_frame->path_len;
+
+		/* inherit ignored from parent if no rule specified */
+		if (new_frame->is_ignored <= GIT_IGNORE_NOTFOUND)
+			new_frame->is_ignored = previous_frame->is_ignored;
+
+		git_ignore__push_dir(&iter->ignores, relative_path);
+	}
 }
 
-static int index_iterator__at_end(git_iterator *self)
+static void filesystem_iterator_frame_pop_ignores(
+	filesystem_iterator *iter)
 {
-	index_iterator *ii = (index_iterator *)self;
-	return (ii->current >= git_vector_length(&ii->entries));
+	if (iterator__honor_ignores(&iter->base))
+		git_ignore__pop_dir(&iter->ignores);
 }
 
-static int index_iterator__advance(
-	const git_index_entry **entry, git_iterator *self)
+GIT_INLINE(bool) filesystem_iterator_examine_path(
+	bool *is_dir_out,
+	iterator_pathlist_search_t *match_out,
+	filesystem_iterator *iter,
+	filesystem_iterator_entry *frame_entry,
+	const char *path,
+	size_t path_len)
 {
-	index_iterator *ii = (index_iterator *)self;
-	size_t entrycount = git_vector_length(&ii->entries);
-	const git_index_entry *ie;
+	bool is_dir = 0;
+	iterator_pathlist_search_t match = ITERATOR_PATHLIST_FULL;
 
-	if (!iterator__has_been_accessed(ii))
-		return index_iterator__current(entry, self);
+	*is_dir_out = false;
+	*match_out = ITERATOR_PATHLIST_NOT_FOUND;
 
-	if (index_iterator__at_tree(ii)) {
-		if (iterator__do_autoexpand(ii)) {
-			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
-			index_iterator__next_prefix_tree(ii);
-		} else {
-			/* advance to sibling tree (i.e. find entry with new prefix) */
-			while (ii->current < entrycount) {
-				ii->current++;
+	if (iter->base.start_len) {
+		int cmp = iter->base.strncomp(path, iter->base.start, path_len);
 
-				if (!(ie = git_vector_get(&ii->entries, ii->current)) ||
-					ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
-					break;
-			}
+		/* we haven't stat'ed `path` yet, so we don't yet know if it's a
+		 * directory or not.  special case if the current path may be a
+		 * directory that matches the start prefix.
+		 */
+		if (cmp == 0) {
+			if (iter->base.start[path_len] == '/')
+				is_dir = true;
 
-			if (index_iterator__first_prefix_tree(ii) < 0)
-				return -1;
+			else if (iter->base.start[path_len] != '\0')
+				cmp = -1;
 		}
-	} else {
-		if (ii->current < entrycount)
-			ii->current++;
 
-		if (index_iterator__first_prefix_tree(ii) < 0)
-			return -1;
+		if (cmp < 0)
+			return false;
 	}
 
-	return index_iterator__current(entry, self);
-}
-
-static int index_iterator__advance_into(
-	const git_index_entry **entry, git_iterator *self)
-{
-	index_iterator *ii = (index_iterator *)self;
-	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
+	if (iter->base.end_len) {
+		int cmp = iter->base.strncomp(path, iter->base.end, iter->base.end_len);
 
-	if (ie != NULL && index_iterator__at_tree(ii)) {
-		if (ii->restore_terminator)
-			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
-		index_iterator__next_prefix_tree(ii);
+		if (cmp > 0)
+			return false;
 	}
 
-	return index_iterator__current(entry, self);
-}
-
-static int index_iterator__reset(git_iterator *self)
-{
-	index_iterator *ii = (index_iterator *)self;
-	const git_index_entry *ie;
-
-	ii->current = 0;
-	ii->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
-
-	iterator_pathlist_walk__reset(self);
-
-	/* if we're given a start prefix, find it; if we're given a pathlist, find
-	 * the first of those.  start at the later of the two.
+	/* if we have a pathlist that we're limiting to, examine this path now
+	 * to avoid a `stat` if we're not interested in the path.
 	 */
-	if (ii->base.start)
-		git_index_snapshot_find(
-			&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
-
-	if ((ie = index_iterator__advance_over_unwanted(ii)) == NULL)
-		return 0;
-
-	if (git_buf_sets(&ii->partial, ie->path) < 0)
-		return -1;
-
-	ii->partial_pos = 0;
+	if (iter->base.pathlist.length) {
+		/* if our parent was explicitly included, so too are we */
+		if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT)
+			match = ITERATOR_PATHLIST_FULL;
+		else
+			match = iterator_pathlist_search(&iter->base, path, path_len);
 
-	if (ii->base.start) {
-		size_t startlen = strlen(ii->base.start);
+		if (match == ITERATOR_PATHLIST_NOT_FOUND)
+			return false;
 
-		ii->partial_pos = (startlen > ii->partial.size) ?
-			ii->partial.size : startlen;
+		/* Ensure that the pathlist entry lines up with what we expected */
+		if (match == ITERATOR_PATHLIST_IS_DIR ||
+			match == ITERATOR_PATHLIST_IS_PARENT)
+			is_dir = true;
 	}
 
-	index_iterator__next_prefix_tree(ii);
-
-	return 0;
-}
-
-static int index_iterator__reset_range(
-	git_iterator *self, const char *start, const char *end)
-{
-	if (iterator__reset_range(self, start, end) < 0)
-		return -1;
-
-	return index_iterator__reset(self);
-}
-
-static void index_iterator__free(git_iterator *self)
-{
-	index_iterator *ii = (index_iterator *)self;
-	git_index_snapshot_release(&ii->entries, ii->index);
-	ii->index = NULL;
-	git_buf_free(&ii->partial);
+	*is_dir_out = is_dir;
+	*match_out = match;
+	return true;
 }
 
-int git_iterator_for_index(
-	git_iterator **iter,
-	git_repository *repo,
-	git_index  *index,
-	git_iterator_options *options)
+GIT_INLINE(bool) filesystem_iterator_is_dot_git(
+	filesystem_iterator *iter, const char *path, size_t path_len)
 {
-	int error = 0;
-	index_iterator *ii = git__calloc(1, sizeof(index_iterator));
-	GITERR_CHECK_ALLOC(ii);
-
-	if ((error = git_index_snapshot_new(&ii->entries, index)) < 0) {
-		git__free(ii);
-		return error;
-	}
-	ii->index = index;
-
-	ITERATOR_BASE_INIT(ii, index, INDEX, repo);
-
-	if ((error = iterator__update_ignore_case((git_iterator *)ii, options ? options->flags : 0)) < 0) {
-		git_iterator_free((git_iterator *)ii);
-		return error;
-	}
+	size_t len;
 
-	ii->entry_srch = iterator__ignore_case(ii) ?
-		git_index_entry_isrch : git_index_entry_srch;
+	if (!iterator__ignore_dot_git(&iter->base))
+		return false;
 
-	git_vector_set_cmp(&ii->entries, iterator__ignore_case(ii) ?
-		git_index_entry_icmp : git_index_entry_cmp);
-	git_vector_sort(&ii->entries);
+	if ((len = path_len) < 4)
+		return false;
 
-	git_buf_init(&ii->partial, 0);
-	ii->tree_entry.mode = GIT_FILEMODE_TREE;
+	if (path[len - 1] == '/')
+		len--;
 
-	index_iterator__reset((git_iterator *)ii);
+	if (git__tolower(path[len - 1]) != 't' ||
+		git__tolower(path[len - 2]) != 'i' ||
+		git__tolower(path[len - 3]) != 'g' ||
+		git__tolower(path[len - 4]) != '.')
+		return false;
 
-	*iter = (git_iterator *)ii;
-	return 0;
+	return (len == 4 || path[len - 5] == '/');
 }
 
-
-typedef struct fs_iterator_frame fs_iterator_frame;
-struct fs_iterator_frame {
-	fs_iterator_frame *next;
-	git_vector entries;
-	size_t index;
-	int is_ignored;
-};
-
-typedef struct fs_iterator fs_iterator;
-struct fs_iterator {
-	git_iterator base;
-	git_iterator_callbacks cb;
-	fs_iterator_frame *stack;
-	git_index_entry entry;
-	git_buf path;
-	size_t root_len;
-	uint32_t dirload_flags;
-	int depth;
-	iterator_pathlist__match_t pathlist_match;
-
-	int (*enter_dir_cb)(fs_iterator *self);
-	int (*leave_dir_cb)(fs_iterator *self);
-	int (*update_entry_cb)(fs_iterator *self);
-};
-
-#define FS_MAX_DEPTH 100
-
-typedef struct {
-	struct stat st;
-	iterator_pathlist__match_t pathlist_match;
-	size_t      path_len;
-	char        path[GIT_FLEX_ARRAY];
-} fs_iterator_path_with_stat;
-
-static int fs_iterator_path_with_stat_cmp(const void *a, const void *b)
+static filesystem_iterator_entry *filesystem_iterator_entry_init(
+	filesystem_iterator_frame *frame,
+	const char *path,
+	size_t path_len,
+	struct stat *statbuf,
+	iterator_pathlist_search_t pathlist_match)
 {
-	const fs_iterator_path_with_stat *psa = a, *psb = b;
-	return strcmp(psa->path, psb->path);
-}
+	filesystem_iterator_entry *entry;
+	size_t entry_size;
 
-static int fs_iterator_path_with_stat_cmp_icase(const void *a, const void *b)
-{
-	const fs_iterator_path_with_stat *psa = a, *psb = b;
-	return strcasecmp(psa->path, psb->path);
-}
+	/* Make sure to append two bytes, one for the path's null
+	 * termination, one for a possible trailing '/' for folders.
+	 */
+	if (GIT_ADD_SIZET_OVERFLOW(&entry_size,
+			sizeof(filesystem_iterator_entry), path_len) ||
+		GIT_ADD_SIZET_OVERFLOW(&entry_size, entry_size, 2) ||
+		(entry = git_pool_malloc(&frame->entry_pool, entry_size)) == NULL)
+		return NULL;
 
-static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
-{
-	fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame));
-	git_vector_cmp entry_compare = CASESELECT(
-		iterator__ignore_case(fi),
-		fs_iterator_path_with_stat_cmp_icase,
-		fs_iterator_path_with_stat_cmp);
+	entry->path_len = path_len;
+	entry->match = pathlist_match;
+	memcpy(entry->path, path, path_len);
+	memcpy(&entry->st, statbuf, sizeof(struct stat));
 
-	if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) {
-		git__free(ff);
-		ff = NULL;
-	}
+	/* Suffix directory paths with a '/' */
+	if (S_ISDIR(entry->st.st_mode))
+		entry->path[entry->path_len++] = '/';
 
-	return ff;
-}
+	entry->path[entry->path_len] = '\0';
 
-static void fs_iterator__free_frame(fs_iterator_frame *ff)
-{
-	git_vector_free_deep(&ff->entries);
-	git__free(ff);
+	return entry;
 }
 
-static void fs_iterator__pop_frame(
-	fs_iterator *fi, fs_iterator_frame *ff, bool pop_last)
+static int filesystem_iterator_frame_push(
+	filesystem_iterator *iter,
+	filesystem_iterator_entry *frame_entry)
 {
-	if (fi && fi->stack == ff) {
-		if (!ff->next && !pop_last) {
-			memset(&fi->entry, 0, sizeof(fi->entry));
-			return;
-		}
-
-		if (fi->leave_dir_cb)
-			(void)fi->leave_dir_cb(fi);
+	filesystem_iterator_frame *new_frame = NULL;
+	git_path_diriter diriter = GIT_PATH_DIRITER_INIT;
+	git_buf root = GIT_BUF_INIT;
+	const char *path;
+	filesystem_iterator_entry *entry;
+	struct stat statbuf;
+	size_t path_len;
+	int error;
 
-		fi->stack = ff->next;
-		fi->depth--;
+	if (iter->frames.size == FILESYSTEM_MAX_DEPTH) {
+		giterr_set(GITERR_REPOSITORY,
+			"directory nesting too deep (%d)", iter->frames.size);
+		return -1;
 	}
 
-	fs_iterator__free_frame(ff);
-}
-
-static int fs_iterator__update_entry(fs_iterator *fi);
-static int fs_iterator__advance_over(
-	const git_index_entry **entry, git_iterator *self);
-
-static int fs_iterator__entry_cmp(const void *i, const void *item)
-{
-	const fs_iterator *fi = (const fs_iterator *)i;
-	const fs_iterator_path_with_stat *ps = item;
-	return fi->base.prefixcomp(fi->base.start, ps->path);
-}
+	new_frame = git_array_alloc(iter->frames);
+	GITERR_CHECK_ALLOC(new_frame);
 
-static void fs_iterator__seek_frame_start(
-	fs_iterator *fi, fs_iterator_frame *ff)
-{
-	if (!ff)
-		return;
+	memset(new_frame, 0, sizeof(filesystem_iterator_frame));
 
-	if (fi->base.start)
-		git_vector_bsearch2(
-			&ff->index, &ff->entries, fs_iterator__entry_cmp, fi);
+	if (frame_entry)
+		git_buf_joinpath(&root, iter->root, frame_entry->path);
 	else
-		ff->index = 0;
-}
+		git_buf_puts(&root, iter->root);
 
-static int dirload_with_stat(git_vector *contents, fs_iterator *fi)
-{
-	git_path_diriter diriter = GIT_PATH_DIRITER_INIT;
-	const char *path;
-	size_t start_len = fi->base.start ? strlen(fi->base.start) : 0;
-	size_t end_len = fi->base.end ? strlen(fi->base.end) : 0;
-	fs_iterator_path_with_stat *ps;
-	size_t path_len, cmp_len, ps_size;
-	iterator_pathlist__match_t pathlist_match = ITERATOR_PATHLIST_MATCH;
-	int error;
+	if (git_buf_oom(&root)) {
+		error = -1;
+		goto done;
+	}
+
+	new_frame->path_len = frame_entry ? frame_entry->path_len : 0;
 
 	/* Any error here is equivalent to the dir not existing, skip over it */
 	if ((error = git_path_diriter_init(
-			&diriter, fi->path.ptr, fi->dirload_flags)) < 0) {
+			&diriter, root.ptr, iter->dirload_flags)) < 0) {
 		error = GIT_ENOTFOUND;
 		goto done;
 	}
 
+	if ((error = git_vector_init(&new_frame->entries, 64,
+			iterator__ignore_case(&iter->base) ?
+			filesystem_iterator_entry_cmp_icase :
+			filesystem_iterator_entry_cmp)) < 0)
+		goto done;
+
+	git_pool_init(&new_frame->entry_pool, 1);
+
+	/* check if this directory is ignored */
+	filesystem_iterator_frame_push_ignores(iter, frame_entry, new_frame);
+
 	while ((error = git_path_diriter_next(&diriter)) == 0) {
+		iterator_pathlist_search_t pathlist_match = ITERATOR_PATHLIST_FULL;
+		bool dir_expected = false;
+
 		if ((error = git_path_diriter_fullpath(&path, &path_len, &diriter)) < 0)
 			goto done;
 
-		assert(path_len > fi->root_len);
+		assert(path_len > iter->root_len);
 
 		/* remove the prefix if requested */
-		path += fi->root_len;
-		path_len -= fi->root_len;
-
-		/* skip if before start_stat or after end_stat */
-		cmp_len = min(start_len, path_len);
-		if (cmp_len && fi->base.strncomp(path, fi->base.start, cmp_len) < 0)
-			continue;
-		/* skip if after end_stat */
-		cmp_len = min(end_len, path_len);
-		if (cmp_len && fi->base.strncomp(path, fi->base.end, cmp_len) > 0)
-			continue;
+		path += iter->root_len;
+		path_len -= iter->root_len;
 
-		/* if we have a pathlist that we're limiting to, examine this path.
-		 * if the frame has already deemed us inside the path (eg, we're in
-		 * `foo/bar` and the pathlist previously was detected to say `foo/`)
-		 * then simply continue.  otherwise, examine the pathlist looking for
-		 * this path or children of this path.
+		/* examine start / end and the pathlist to see if this path is in it.
+		 * note that since we haven't yet stat'ed the path, we cannot know
+		 * whether it's a directory yet or not, so this can give us an
+		 * expected type (S_IFDIR or S_IFREG) that we should examine)
 		 */
-		if (fi->base.pathlist.length &&
-			fi->pathlist_match != ITERATOR_PATHLIST_MATCH &&
-			fi->pathlist_match != ITERATOR_PATHLIST_MATCH_DIRECTORY &&
-			!(pathlist_match = iterator_pathlist__match(&fi->base, path, path_len)))
+		if (!filesystem_iterator_examine_path(&dir_expected, &pathlist_match,
+			iter, frame_entry, path, path_len))
 			continue;
 
-		/* Make sure to append two bytes, one for the path's null
-		 * termination, one for a possible trailing '/' for folders.
+		/* TODO: don't need to stat if assume unchanged for this path and
+		 * we have an index, we can just copy the data out of it.
 		 */
-		GITERR_CHECK_ALLOC_ADD(&ps_size, sizeof(fs_iterator_path_with_stat), path_len);
-		GITERR_CHECK_ALLOC_ADD(&ps_size, ps_size, 2);
-
-		ps = git__calloc(1, ps_size);
-		ps->path_len = path_len;
 
-		memcpy(ps->path, path, path_len);
+		if ((error = git_path_diriter_stat(&statbuf, &diriter)) < 0) {
+			/* file was removed between readdir and lstat */
+			if (error == GIT_ENOTFOUND)
+				continue;
 
-		/* TODO: don't stat if assume unchanged for this path */
+			/* treat the file as unreadable */
+			memset(&statbuf, 0, sizeof(statbuf));
+			statbuf.st_mode = GIT_FILEMODE_UNREADABLE;
 
-		if ((error = git_path_diriter_stat(&ps->st, &diriter)) < 0) {
-			if (error == GIT_ENOTFOUND) {
-				/* file was removed between readdir and lstat */
-				git__free(ps);
-				continue;
-			}
+			error = 0;
+		}
 
-			if (pathlist_match == ITERATOR_PATHLIST_MATCH_DIRECTORY) {
-				/* were looking for a directory, but this is a file */
-				git__free(ps);
-				continue;
-			}
+		iter->base.stat_calls++;
 
-			/* Treat the file as unreadable if we get any other error */
-			memset(&ps->st, 0, sizeof(ps->st));
-			ps->st.st_mode = GIT_FILEMODE_UNREADABLE;
+		/* Ignore wacky things in the filesystem */
+		if (!S_ISDIR(statbuf.st_mode) &&
+			!S_ISREG(statbuf.st_mode) &&
+			!S_ISLNK(statbuf.st_mode) &&
+			statbuf.st_mode != GIT_FILEMODE_UNREADABLE)
+			continue;
 
-			giterr_clear();
-			error = 0;
-		} else if (S_ISDIR(ps->st.st_mode)) {
-			/* Suffix directory paths with a '/' */
-			ps->path[ps->path_len++] = '/';
-			ps->path[ps->path_len] = '\0';
-		} else if(!S_ISREG(ps->st.st_mode) && !S_ISLNK(ps->st.st_mode)) {
-			/* Ignore wacky things in the filesystem */
-			git__free(ps);
+		if (filesystem_iterator_is_dot_git(iter, path, path_len))
 			continue;
+
+		/* convert submodules to GITLINK and remove trailing slashes */
+		if (S_ISDIR(statbuf.st_mode)) {
+			bool submodule = false;
+
+			if ((error = is_submodule(&submodule, iter, path, path_len)) < 0)
+				goto done;
+
+			if (submodule)
+				statbuf.st_mode = GIT_FILEMODE_COMMIT;
 		}
 
-		/* record whether this path was explicitly found in the path list
-		 * or whether we're only examining it because something beneath it
-		 * is in the path list.
-		 */
-		ps->pathlist_match = pathlist_match;
-		git_vector_insert(contents, ps);
+		/* Ensure that the pathlist entry lines up with what we expected */
+		if (dir_expected && !S_ISDIR(statbuf.st_mode))
+			continue;
+
+		entry = filesystem_iterator_entry_init(new_frame,
+			path, path_len, &statbuf, pathlist_match);
+		GITERR_CHECK_ALLOC(entry);
+
+		git_vector_insert(&new_frame->entries, entry);
 	}
 
 	if (error == GIT_ITEROVER)
 		error = 0;
 
 	/* sort now that directory suffix is added */
-	git_vector_sort(contents);
+	git_vector_sort(&new_frame->entries);
 
 done:
+	if (error < 0)
+		git_array_pop(iter->frames);
+
+	git_buf_free(&root);
 	git_path_diriter_free(&diriter);
 	return error;
 }
 
-
-static int fs_iterator__expand_dir(fs_iterator *fi)
+GIT_INLINE(void) filesystem_iterator_frame_pop(filesystem_iterator *iter)
 {
-	int error;
-	fs_iterator_frame *ff;
-
-	if (fi->depth > FS_MAX_DEPTH) {
-		giterr_set(GITERR_REPOSITORY,
-			"Directory nesting is too deep (%d)", fi->depth);
-		return -1;
-	}
+	filesystem_iterator_frame *frame;
 
-	ff = fs_iterator__alloc_frame(fi);
-	GITERR_CHECK_ALLOC(ff);
-
-	error = dirload_with_stat(&ff->entries, fi);
-
-	if (error < 0) {
-		git_error_state last_error = { 0 };
-		giterr_state_capture(&last_error, error);
+	assert(iter->frames.size);
 
-		/* these callbacks may clear the error message */
-		fs_iterator__free_frame(ff);
-		fs_iterator__advance_over(NULL, (git_iterator *)fi);
-		/* next time return value we skipped to */
-		fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
+	frame = git_array_pop(iter->frames);
+	filesystem_iterator_frame_pop_ignores(iter);
 
-		return giterr_state_restore(&last_error);
-	}
+	git_pool_clear(&frame->entry_pool);
+	git_vector_free(&frame->entries);
+}
 
-	if (ff->entries.length == 0) {
-		fs_iterator__free_frame(ff);
-		return GIT_ENOTFOUND;
-	}
-	fi->base.stat_calls += ff->entries.length;
+static void filesystem_iterator_set_current(
+	filesystem_iterator *iter,
+	filesystem_iterator_entry *entry)
+{
+	iter->entry.ctime.seconds = entry->st.st_ctime;
+	iter->entry.ctime.nanoseconds = entry->st.st_ctime_nsec;
 
-	fs_iterator__seek_frame_start(fi, ff);
+	iter->entry.mtime.seconds = entry->st.st_mtime;
+	iter->entry.mtime.nanoseconds = entry->st.st_mtime_nsec;
 
-	ff->next  = fi->stack;
-	fi->stack = ff;
-	fi->depth++;
+	iter->entry.dev = entry->st.st_dev;
+	iter->entry.ino = entry->st.st_ino;
+	iter->entry.mode = git_futils_canonical_mode(entry->st.st_mode);
+	iter->entry.uid = entry->st.st_uid;
+	iter->entry.gid = entry->st.st_gid;
+	iter->entry.file_size = entry->st.st_size;
 
-	if (fi->enter_dir_cb && (error = fi->enter_dir_cb(fi)) < 0)
-		return error;
+	iter->entry.path = entry->path;
 
-	return fs_iterator__update_entry(fi);
+	iter->current_is_ignored = GIT_IGNORE_UNCHECKED;
 }
 
-static int fs_iterator__current(
-	const git_index_entry **entry, git_iterator *self)
+static int filesystem_iterator_current(
+	const git_index_entry **out, git_iterator *i)
 {
-	fs_iterator *fi = (fs_iterator *)self;
-	const git_index_entry *fe = (fi->entry.path == NULL) ? NULL : &fi->entry;
-
-	if (entry)
-		*entry = fe;
+	filesystem_iterator *iter = (filesystem_iterator *)i;
 
-	fi->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+	if (!iterator__has_been_accessed(i))
+		return iter->base.cb->advance(out, i);
 
-	return (fe != NULL) ? 0 : GIT_ITEROVER;
-}
+	if (!iter->frames.size) {
+		*out = NULL;
+		return GIT_ITEROVER;
+	}
 
-static int fs_iterator__at_end(git_iterator *self)
-{
-	return (((fs_iterator *)self)->entry.path == NULL);
+	*out = &iter->entry;
+	return 0;
 }
 
-static int fs_iterator__advance_into(
-	const git_index_entry **entry, git_iterator *iter)
+static int filesystem_iterator_advance(
+	const git_index_entry **out, git_iterator *i)
 {
+	filesystem_iterator *iter = (filesystem_iterator *)i;
 	int error = 0;
-	fs_iterator *fi = (fs_iterator *)iter;
 
-	iterator__clear_entry(entry);
-
-	/* Allow you to explicitly advance into a commit/submodule (as well as a
-	 * tree) to avoid cases where an entry is mislabeled as a submodule in
-	 * the working directory.  The fs iterator will never have COMMMIT
-	 * entries on it's own, but a wrapper might add them.
-	 */
-	if (fi->entry.path != NULL &&
-		(fi->entry.mode == GIT_FILEMODE_TREE ||
-		 fi->entry.mode == GIT_FILEMODE_COMMIT))
-		/* returns GIT_ENOTFOUND if the directory is empty */
-		error = fs_iterator__expand_dir(fi);
+	iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
 
-	if (!error && entry)
-		error = fs_iterator__current(entry, iter);
+	/* examine filesystem entries until we find the next one to return */
+	while (true) {
+		filesystem_iterator_frame *frame;
+		filesystem_iterator_entry *entry;
 
-	if (!error && !fi->entry.path)
-		error = GIT_ITEROVER;
+		if ((frame = filesystem_iterator_current_frame(iter)) == NULL) {
+			error = GIT_ITEROVER;
+			break;
+		}
 
-	return error;
-}
+		/* no more entries in this frame.  pop the frame out */
+		if (frame->next_idx == frame->entries.length) {
+			filesystem_iterator_frame_pop(iter);
+			continue;
+		}
 
-static void fs_iterator__advance_over_internal(git_iterator *self)
-{
-	fs_iterator *fi = (fs_iterator *)self;
-	fs_iterator_frame *ff;
-	fs_iterator_path_with_stat *next;
+		/* we have more entries in the current frame, that's our next entry */
+		entry = frame->entries.contents[frame->next_idx];
+		frame->next_idx++;
 
-	while (fi->entry.path != NULL) {
-		ff = fi->stack;
-		next = git_vector_get(&ff->entries, ++ff->index);
+		if (S_ISDIR(entry->st.st_mode)) {
+			if (iterator__do_autoexpand(iter)) {
+				error = filesystem_iterator_frame_push(iter, entry);
 
-		if (next != NULL)
-			break;
+				/* may get GIT_ENOTFOUND due to races or permission problems
+				 * that we want to quietly swallow
+				 */
+				if (error == GIT_ENOTFOUND)
+					continue;
+				else if (error < 0)
+					break;
+			}
 
-		fs_iterator__pop_frame(fi, ff, false);
+			if (!iterator__include_trees(iter))
+				continue;
+		}
+
+		filesystem_iterator_set_current(iter, entry);
+		break;
 	}
+
+	if (out)
+		*out = (error == 0) ? &iter->entry : NULL;
+
+	return error;
 }
 
-static int fs_iterator__advance_over(
-	const git_index_entry **entry, git_iterator *self)
+static int filesystem_iterator_advance_into(
+	const git_index_entry **out, git_iterator *i)
 {
+	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator_frame *frame;
+	filesystem_iterator_entry *prev_entry;
 	int error;
 
-	if (entry != NULL)
-		*entry = NULL;
-
-	fs_iterator__advance_over_internal(self);
-
-	error = fs_iterator__update_entry((fs_iterator *)self);
+	if (out)
+		*out = NULL;
 
-	if (!error && entry != NULL)
-		error = fs_iterator__current(entry, self);
+	if ((frame = filesystem_iterator_current_frame(iter)) == NULL)
+		return GIT_ITEROVER;
 
-	return error;
-}
+	/* get the last seen entry */
+	prev_entry = filesystem_iterator_current_entry(frame);
 
-static int fs_iterator__advance(
-	const git_index_entry **entry, git_iterator *self)
-{
-	fs_iterator *fi = (fs_iterator *)self;
+	/* 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 (!iterator__has_been_accessed(fi))
-		return fs_iterator__current(entry, self);
+	if (prev_entry) {
+		if (prev_entry->st.st_mode != GIT_FILEMODE_COMMIT &&
+			!S_ISDIR(prev_entry->st.st_mode))
+			return 0;
 
-	/* given include_trees & autoexpand, we might have to go into a tree */
-	if (iterator__do_autoexpand(fi) &&
-		fi->entry.path != NULL &&
-		fi->entry.mode == GIT_FILEMODE_TREE)
-	{
-		int error = fs_iterator__advance_into(entry, self);
-		if (error != GIT_ENOTFOUND)
+		if ((error = filesystem_iterator_frame_push(iter, prev_entry)) < 0)
 			return error;
-		/* continue silently past empty directories if autoexpanding */
-		giterr_clear();
 	}
 
-	return fs_iterator__advance_over(entry, self);
+	/* we've advanced into the directory in question, let advance
+	 * find the first entry
+	 */
+	return filesystem_iterator_advance(out, i);
 }
 
-static int fs_iterator__reset(git_iterator *self)
+int git_iterator_current_workdir_path(git_buf **out, git_iterator *i)
 {
-	int error;
-	fs_iterator *fi = (fs_iterator *)self;
+	filesystem_iterator *iter = (filesystem_iterator *)i;
+	const git_index_entry *entry;
 
-	fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
+	if (i->type != GIT_ITERATOR_TYPE_FS &&
+		i->type != GIT_ITERATOR_TYPE_WORKDIR) {
+		*out = NULL;
+		return 0;
+	}
 
-	while (fi->stack != NULL && fi->stack->next != NULL)
-		fs_iterator__pop_frame(fi, fi->stack, false);
-	fi->depth = 0;
+	git_buf_truncate(&iter->current_path, iter->root_len);
 
-	fs_iterator__seek_frame_start(fi, fi->stack);
+	if (git_iterator_current(&entry, i) < 0 ||
+		git_buf_puts(&iter->current_path, entry->path) < 0)
+		return -1;
 
-	error = fs_iterator__update_entry(fi);
-	if (error == GIT_ITEROVER)
-		error = 0;
+	*out = &iter->current_path;
+	return 0;
+}
 
-	return error;
+GIT_INLINE(git_dir_flag) entry_dir_flag(git_index_entry *entry)
+{
+#if defined(GIT_WIN32) && !defined(__MINGW32__)
+	return (entry && entry->mode) ?
+		(S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) :
+		GIT_DIR_FLAG_UNKNOWN;
+#else
+	GIT_UNUSED(entry);
+	return GIT_DIR_FLAG_UNKNOWN;
+#endif
 }
 
-static int fs_iterator__reset_range(
-	git_iterator *self, const char *start, const char *end)
+static void filesystem_iterator_update_ignored(filesystem_iterator *iter)
 {
-	int error;
+	filesystem_iterator_frame *frame;
+	git_dir_flag dir_flag = entry_dir_flag(&iter->entry);
 
-	if ((error = iterator__reset_range(self, start, end)) < 0)
-		return error;
+	if (git_ignore__lookup(&iter->current_is_ignored,
+			&iter->ignores, iter->entry.path, dir_flag) < 0) {
+		giterr_clear();
+		iter->current_is_ignored = GIT_IGNORE_NOTFOUND;
+	}
 
-	return fs_iterator__reset(self);
+	/* use ignore from containing frame stack */
+	if (iter->current_is_ignored <= GIT_IGNORE_NOTFOUND) {
+		frame = filesystem_iterator_current_frame(iter);
+		iter->current_is_ignored = frame->is_ignored;
+	}
 }
 
-static void fs_iterator__free(git_iterator *self)
+GIT_INLINE(bool) filesystem_iterator_current_is_ignored(
+	filesystem_iterator *iter)
 {
-	fs_iterator *fi = (fs_iterator *)self;
+	if (iter->current_is_ignored == GIT_IGNORE_UNCHECKED)
+		filesystem_iterator_update_ignored(iter);
 
-	while (fi->stack != NULL)
-		fs_iterator__pop_frame(fi, fi->stack, true);
-
-	git_buf_free(&fi->path);
+	return (iter->current_is_ignored == GIT_IGNORE_TRUE);
 }
 
-static int fs_iterator__update_entry(fs_iterator *fi)
+bool git_iterator_current_is_ignored(git_iterator *i)
 {
-	fs_iterator_path_with_stat *ps;
-
-	while (true) {
-		memset(&fi->entry, 0, sizeof(fi->entry));
+	if (i->type != GIT_ITERATOR_TYPE_WORKDIR)
+		return false;
 
-		if (!fi->stack)
-			return GIT_ITEROVER;
+	return filesystem_iterator_current_is_ignored((filesystem_iterator *)i);
+}
 
-		ps = git_vector_get(&fi->stack->entries, fi->stack->index);
-		if (!ps)
-			return GIT_ITEROVER;
+bool git_iterator_current_tree_is_ignored(git_iterator *i)
+{
+	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator_frame *frame;
 
-		git_buf_truncate(&fi->path, fi->root_len);
-		if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0)
-			return -1;
+	if (i->type != GIT_ITERATOR_TYPE_WORKDIR)
+		return false;
 
-		if (iterator__past_end(fi, fi->path.ptr + fi->root_len))
-			return GIT_ITEROVER;
+	frame = filesystem_iterator_current_frame(iter);
+	return (frame->is_ignored == GIT_IGNORE_TRUE);
+}
 
-		fi->entry.path = ps->path;
-		fi->pathlist_match = ps->pathlist_match;
-		git_index_entry__init_from_stat(&fi->entry, &ps->st, true);
+static int filesystem_iterator_advance_over(
+	const git_index_entry **out,
+	git_iterator_status_t *status,
+	git_iterator *i)
+{
+	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator_frame *current_frame;
+	filesystem_iterator_entry *current_entry;
+	const git_index_entry *entry = NULL;
+	const char *base;
+	int error = 0;
 
-		/* need different mode here to keep directories during iteration */
-		fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
+	*out = NULL;
+	*status = GIT_ITERATOR_STATUS_NORMAL;
 
-		/* allow wrapper to check/update the entry (can force skip) */
-		if (fi->update_entry_cb &&
-			fi->update_entry_cb(fi) == GIT_ENOTFOUND) {
-			fs_iterator__advance_over_internal(&fi->base);
-			continue;
-		}
+	assert(iterator__has_been_accessed(i));
 
-		/* if this is a tree and trees aren't included, then skip */
-		if (fi->entry.mode == GIT_FILEMODE_TREE && !iterator__include_trees(fi)) {
-			int error = fs_iterator__advance_into(NULL, &fi->base);
+	current_frame = filesystem_iterator_current_frame(iter);
+	assert(current_frame);
+	current_entry = filesystem_iterator_current_entry(current_frame);
+	assert(current_entry);
 
-			if (error != GIT_ENOTFOUND)
-				return error;
+	if ((error = git_iterator_current(&entry, i)) < 0)
+		return error;
 
-			giterr_clear();
-			fs_iterator__advance_over_internal(&fi->base);
-			continue;
-		}
+	if (!S_ISDIR(entry->mode)) {
+		if (filesystem_iterator_current_is_ignored(iter))
+			*status = GIT_ITERATOR_STATUS_IGNORED;
 
-		break;
+		return filesystem_iterator_advance(out, i);
 	}
 
-	return 0;
-}
+	git_buf_clear(&iter->tmp_buf);
+	if ((error = git_buf_puts(&iter->tmp_buf, entry->path)) < 0)
+		return error;
 
-static int fs_iterator__initialize(
-	git_iterator **out, fs_iterator *fi, const char *root)
-{
-	int error;
+	base = iter->tmp_buf.ptr;
+
+	/* scan inside the directory looking for files.  if we find nothing,
+	 * we will remain EMPTY.  if we find any ignored item, upgrade EMPTY to
+	 * IGNORED.  if we find a real actual item, upgrade all the way to NORMAL
+	 * and then stop.
+	 *
+	 * however, if we're here looking for a pathlist item (but are not
+	 * actually in the pathlist ourselves) then start at FILTERED instead of
+	 * EMPTY.  callers then know that this path was not something they asked
+	 * about.
+	 */
+	*status = current_entry->match == ITERATOR_PATHLIST_IS_PARENT ?
+		GIT_ITERATOR_STATUS_FILTERED : GIT_ITERATOR_STATUS_EMPTY;
 
-	if (git_buf_sets(&fi->path, root) < 0 || git_path_to_dir(&fi->path) < 0) {
-		git__free(fi);
-		return -1;
-	}
-	fi->root_len = fi->path.size;
-	fi->pathlist_match = ITERATOR_PATHLIST_MATCH_CHILD;
+	while (entry && !iter->base.prefixcomp(entry->path, base)) {
+		if (filesystem_iterator_current_is_ignored(iter)) {
+			/* if we found an explicitly ignored item, then update from
+			 * EMPTY to IGNORED
+			 */
+			*status = GIT_ITERATOR_STATUS_IGNORED;
+		} else if (S_ISDIR(entry->mode)) {
+			error = filesystem_iterator_advance_into(&entry, i);
 
-	fi->dirload_flags =
-		(iterator__ignore_case(fi) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
-		(iterator__flag(fi, PRECOMPOSE_UNICODE) ?
-			GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);
+			if (!error)
+				continue;
 
-	if ((error = fs_iterator__expand_dir(fi)) < 0) {
-		if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) {
-			giterr_clear();
-			error = 0;
+			/* this directory disappeared, ignore it */
+			else if (error == GIT_ENOTFOUND)
+				error = 0;
+
+			/* a real error occurred */
+			else
+				break;
 		} else {
-			git_iterator_free((git_iterator *)fi);
-			fi = NULL;
+			/* we found a non-ignored item, treat parent as untracked */
+			*status = GIT_ITERATOR_STATUS_NORMAL;
+			break;
 		}
+
+		if ((error = git_iterator_advance(&entry, i)) < 0)
+			break;
+	}
+
+	/* wrap up scan back to base directory */
+	while (entry && !iter->base.prefixcomp(entry->path, base)) {
+		if ((error = git_iterator_advance(&entry, i)) < 0)
+			break;
 	}
 
-	*out = (git_iterator *)fi;
+	if (!error)
+		*out = entry;
+
 	return error;
 }
 
-int git_iterator_for_filesystem(
-	git_iterator **out,
-	const char *root,
-	git_iterator_options *options)
+static void filesystem_iterator_clear(filesystem_iterator *iter)
 {
-	fs_iterator *fi = git__calloc(1, sizeof(fs_iterator));
-	GITERR_CHECK_ALLOC(fi);
+	while (iter->frames.size)
+		filesystem_iterator_frame_pop(iter);
 
-	ITERATOR_BASE_INIT(fi, fs, FS, NULL);
+	git_array_clear(iter->frames);
+	git_ignore__free(&iter->ignores);
 
-	if (options && (options->flags & GIT_ITERATOR_IGNORE_CASE) != 0)
-		fi->base.flags |= GIT_ITERATOR_IGNORE_CASE;
+	git_buf_free(&iter->tmp_buf);
 
-	return fs_iterator__initialize(out, fi, root);
+	iterator_clear(&iter->base);
 }
 
-
-typedef struct {
-	fs_iterator fi;
-	git_ignores ignores;
-	int is_ignored;
-
-	/*
-	 * We may have a tree or the index+snapshot to compare against
-	 * when checking for submodules.
-	 */
-	git_tree *tree;
-	git_index *index;
-	git_vector index_snapshot;
-	git_vector_cmp entry_srch;
-
-} workdir_iterator;
-
-GIT_INLINE(bool) workdir_path_is_dotgit(const git_buf *path)
+static int filesystem_iterator_init(filesystem_iterator *iter)
 {
-	size_t len;
+	int error;
 
-	if (!path || (len = path->size) < 4)
-		return false;
+	if (iterator__honor_ignores(&iter->base) &&
+		(error = git_ignore__for_path(iter->base.repo,
+			".gitignore", &iter->ignores)) < 0)
+		return error;
 
-	if (path->ptr[len - 1] == '/')
-		len--;
+	if ((error = filesystem_iterator_frame_push(iter, NULL)) < 0)
+		return error;
 
-	if (git__tolower(path->ptr[len - 1]) != 't' ||
-		git__tolower(path->ptr[len - 2]) != 'i' ||
-		git__tolower(path->ptr[len - 3]) != 'g' ||
-		git__tolower(path->ptr[len - 4]) != '.')
-		return false;
+	iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
 
-	return (len == 4 || path->ptr[len - 5] == '/');
+	return 0;
 }
 
-/**
- * Figure out if an entry is a submodule.
- *
- * We consider it a submodule if the path is listed as a submodule in
- * either the tree or the index.
- */
-static int is_submodule(workdir_iterator *wi, fs_iterator_path_with_stat *ie)
+static int filesystem_iterator_reset(git_iterator *i)
 {
-	int error, is_submodule = 0;
-
-	if (wi->tree) {
-		git_tree_entry *e;
-
-		/* remove the trailing slash for finding */
-		ie->path[ie->path_len-1] = '\0';
-		error = git_tree_entry_bypath(&e, wi->tree, ie->path);
-		ie->path[ie->path_len-1] = '/';
-		if (error < 0 && error != GIT_ENOTFOUND)
-			return 0;
-		if (!error) {
-			is_submodule = e->attr == GIT_FILEMODE_COMMIT;
-			git_tree_entry_free(e);
-		}
-	}
+	filesystem_iterator *iter = (filesystem_iterator *)i;
 
-	if (!is_submodule && wi->index) {
-		git_index_entry *e;
-		size_t pos;
+	filesystem_iterator_clear(iter);
+	return filesystem_iterator_init(iter);
+}
 
-		error = git_index_snapshot_find(&pos, &wi->index_snapshot, wi->entry_srch, ie->path, ie->path_len-1, 0);
-		if (error < 0 && error != GIT_ENOTFOUND)
-			return 0;
+static int filesystem_iterator_reset_range(
+	git_iterator *i, const char *start, const char *end)
+{
+	if (iterator_range_reset(i, start, end) < 0)
+		return -1;
 
-		if (!error) {
-			e = git_vector_get(&wi->index_snapshot, pos);
+	return filesystem_iterator_reset(i);
+}
 
-			is_submodule = e->mode == GIT_FILEMODE_COMMIT;
-		}
-	}
+static int filesystem_iterator_at_end(git_iterator *i)
+{
+	filesystem_iterator *iter = (filesystem_iterator *)i;
 
-	return is_submodule;
+	return (iter->frames.size == 0);
 }
 
-GIT_INLINE(git_dir_flag) git_entry__dir_flag(git_index_entry *entry) {
-#if defined(GIT_WIN32) && !defined(__MINGW32__)
-	return (entry && entry->mode)
-		? S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE
-		: GIT_DIR_FLAG_UNKNOWN;
-#else
-	GIT_UNUSED(entry);
-	return GIT_DIR_FLAG_UNKNOWN;
-#endif
+static void filesystem_iterator_free(git_iterator *i)
+{
+	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator_clear(iter);
 }
 
-static int workdir_iterator__enter_dir(fs_iterator *fi)
+static int iterator_for_filesystem(
+	git_iterator **out,
+	git_repository *repo,
+	const char *root,
+	git_index *index,
+	git_tree *tree,
+	git_iterator_type_t type,
+	git_iterator_options *options)
 {
-	workdir_iterator *wi = (workdir_iterator *)fi;
-	fs_iterator_frame *ff = fi->stack;
-	size_t pos;
-	fs_iterator_path_with_stat *entry;
-	bool found_submodules = false;
+	filesystem_iterator *iter;
+	size_t root_len;
+	int error;
 
-	git_dir_flag dir_flag = git_entry__dir_flag(&fi->entry);
+	static git_iterator_callbacks callbacks = {
+		filesystem_iterator_current,
+		filesystem_iterator_advance,
+		filesystem_iterator_advance_into,
+		filesystem_iterator_advance_over,
+		filesystem_iterator_reset,
+		filesystem_iterator_reset_range,
+		filesystem_iterator_at_end,
+		filesystem_iterator_free
+	};
 
-	/* check if this directory is ignored */
-	if (git_ignore__lookup(&ff->is_ignored, &wi->ignores, fi->path.ptr + fi->root_len, dir_flag) < 0) {
-		giterr_clear();
-		ff->is_ignored = GIT_IGNORE_NOTFOUND;
-	}
+	*out = NULL;
 
-	/* if this is not the top level directory... */
-	if (ff->next != NULL) {
-		ssize_t slash_pos = git_buf_rfind_next(&fi->path, '/');
+	if (root == NULL)
+		return git_iterator_for_nothing(out, options);
 
-		/* inherit ignored from parent if no rule specified */
-		if (ff->is_ignored <= GIT_IGNORE_NOTFOUND)
-			ff->is_ignored = ff->next->is_ignored;
+	iter = git__calloc(1, sizeof(filesystem_iterator));
+	GITERR_CHECK_ALLOC(iter);
 
-		/* push new ignores for files in this directory */
-		(void)git_ignore__push_dir(&wi->ignores, &fi->path.ptr[slash_pos + 1]);
-	}
+	root_len = strlen(root);
 
-	/* convert submodules to GITLINK and remove trailing slashes */
-	git_vector_foreach(&ff->entries, pos, entry) {
-		if (!S_ISDIR(entry->st.st_mode) || !strcmp(GIT_DIR, entry->path))
-			continue;
+	iter->root = git__malloc(root_len+2);
+	GITERR_CHECK_ALLOC(iter->root);
 
-		if (is_submodule(wi, entry)) {
-			entry->st.st_mode = GIT_FILEMODE_COMMIT;
-			entry->path_len--;
-			entry->path[entry->path_len] = '\0';
-			found_submodules = true;
-		}
-	}
+	memcpy(iter->root, root, root_len);
 
-	/* if we renamed submodules, re-sort and re-seek to start */
-	if (found_submodules) {
-		git_vector_set_sorted(&ff->entries, 0);
-		git_vector_sort(&ff->entries);
-		fs_iterator__seek_frame_start(fi, ff);
+	if (root_len == 0 || root[root_len-1] != '/') {
+		iter->root[root_len] = '/';
+		root_len++;
 	}
+	iter->root[root_len] = '\0';
+	iter->root_len = root_len;
 
-	return 0;
-}
+	if ((error = git_buf_puts(&iter->current_path, iter->root)) < 0)
+		goto on_error;
 
-static int workdir_iterator__leave_dir(fs_iterator *fi)
-{
-	workdir_iterator *wi = (workdir_iterator *)fi;
-	git_ignore__pop_dir(&wi->ignores);
-	return 0;
-}
+	iter->base.type = type;
+	iter->base.cb = &callbacks;
 
-static int workdir_iterator__update_entry(fs_iterator *fi)
-{
-	workdir_iterator *wi = (workdir_iterator *)fi;
 
-	/* skip over .git entries */
-	if (workdir_path_is_dotgit(&fi->path))
-		return GIT_ENOTFOUND;
+	if ((error = iterator_init_common(&iter->base, repo, options)) < 0)
+		goto on_error;
+
+	if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0)
+		goto on_error;
+
+	if ((iter->index = index) != NULL &&
+		(error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0)
+		goto on_error;
+
+	iter->dirload_flags =
+		(iterator__ignore_case(&iter->base) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
+		(iterator__flag(&iter->base, PRECOMPOSE_UNICODE) ?
+			 GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);
 
-	/* reset is_ignored since we haven't checked yet */
-	wi->is_ignored = GIT_IGNORE_UNCHECKED;
+	if ((error = filesystem_iterator_init(iter)) < 0)
+		goto on_error;
 
+	*out = &iter->base;
 	return 0;
+
+on_error:
+	git__free(iter->root);
+	git_buf_free(&iter->current_path);
+	git_iterator_free(&iter->base);
+	return error;
 }
 
-static void workdir_iterator__free(git_iterator *self)
+int git_iterator_for_filesystem(
+	git_iterator **out,
+	const char *root,
+	git_iterator_options *options)
 {
-	workdir_iterator *wi = (workdir_iterator *)self;
-	if (wi->index)
-		git_index_snapshot_release(&wi->index_snapshot, wi->index);
-	git_tree_free(wi->tree);
-	fs_iterator__free(self);
-	git_ignore__free(&wi->ignores);
+	return iterator_for_filesystem(out,
+		NULL, root, NULL, NULL, GIT_ITERATOR_TYPE_FS, options);
 }
 
 int git_iterator_for_workdir_ext(
@@ -2101,254 +2125,354 @@ int git_iterator_for_workdir_ext(
 	const char *repo_workdir,
 	git_index *index,
 	git_tree *tree,
-	git_iterator_options *options)
+	git_iterator_options *given_opts)
 {
-	int error, precompose = 0;
-	workdir_iterator *wi;
+	git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT;
 
 	if (!repo_workdir) {
 		if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
 			return GIT_EBAREREPO;
+
 		repo_workdir = git_repository_workdir(repo);
 	}
 
-	/* initialize as an fs iterator then do overrides */
-	wi = git__calloc(1, sizeof(workdir_iterator));
-	GITERR_CHECK_ALLOC(wi);
-	ITERATOR_BASE_INIT((&wi->fi), fs, FS, repo);
-
-	wi->fi.base.type = GIT_ITERATOR_TYPE_WORKDIR;
-	wi->fi.cb.free = workdir_iterator__free;
-	wi->fi.enter_dir_cb = workdir_iterator__enter_dir;
-	wi->fi.leave_dir_cb = workdir_iterator__leave_dir;
-	wi->fi.update_entry_cb = workdir_iterator__update_entry;
-
-	if ((error = iterator__update_ignore_case((git_iterator *)wi, options ? options->flags : 0)) < 0 ||
-		(error = git_ignore__for_path(repo, ".gitignore", &wi->ignores)) < 0)
-	{
-		git_iterator_free((git_iterator *)wi);
-		return error;
-	}
+	/* upgrade to a workdir iterator, adding necessary internal flags */
+	if (given_opts)
+		memcpy(&options, given_opts, sizeof(git_iterator_options));
 
-	if (tree && (error = git_tree_dup(&wi->tree, tree)) < 0)
-		return error;
+	options.flags |= GIT_ITERATOR_HONOR_IGNORES |
+		GIT_ITERATOR_IGNORE_DOT_GIT;
 
-	wi->index = index;
-	if (index && (error = git_index_snapshot_new(&wi->index_snapshot, index)) < 0) {
-		git_iterator_free((git_iterator *)wi);
-		return error;
-	}
-	wi->entry_srch = iterator__ignore_case(wi) ?
-		git_index_entry_isrch : git_index_entry_srch;
+	return iterator_for_filesystem(out,
+		repo, repo_workdir, index, tree, GIT_ITERATOR_TYPE_WORKDIR, &options);
+}
 
 
-	/* try to look up precompose and set flag if appropriate */
-	if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
-		giterr_clear();
-	else if (precompose)
-		wi->fi.base.flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
+/* Index iterator */
 
-	return fs_iterator__initialize(out, &wi->fi, repo_workdir);
-}
 
-void git_iterator_free(git_iterator *iter)
+typedef struct {
+	git_iterator base;
+	git_iterator_callbacks cb;
+	git_index *index;
+	git_vector entries;
+	git_vector_cmp entry_srch;
+	size_t current;
+	/* when limiting with a pathlist, this is the current index into it */
+	size_t pathlist_idx;
+	/* when not in autoexpand mode, use these to represent "tree" state */
+	git_buf partial;
+	size_t partial_pos;
+	char restore_terminator;
+	git_index_entry tree_entry;
+} index_iterator;
+
+static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
 {
-	if (iter == NULL)
-		return;
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
-	iter->cb->free(iter);
+	if (ie != NULL && iterator__past_end(ii, ie->path)) {
+		ii->current = git_vector_length(&ii->entries);
+		ie = NULL;
+	}
 
-	git_vector_free(&iter->pathlist);
-	git__free(iter->start);
-	git__free(iter->end);
+	return ie;
+}
 
-	memset(iter, 0, sizeof(*iter));
+static const git_index_entry *index_iterator__advance_over_unwanted(
+	index_iterator *ii)
+{
+	const git_index_entry *ie = index_iterator__index_entry(ii);
+	bool match;
 
-	git__free(iter);
+	while (ie) {
+		if (!iterator__include_conflicts(ii) &&
+				git_index_entry_is_conflict(ie)) {
+			ii->current++;
+			ie = index_iterator__index_entry(ii);
+			continue;
+		}
+
+		/* if we have a pathlist, this entry's path must be in it to be
+		 * returned.  walk the pathlist in unison with the index to
+		 * compare paths.
+		 */
+		if (ii->base.pathlist.length) {
+			match = iterator_pathlist_walk__contains(&ii->base, ie->path);
+
+			if (!match) {
+				ii->current++;
+				ie = index_iterator__index_entry(ii);
+				continue;
+			}
+		}
+
+		break;
+	}
+
+	return ie;
 }
 
-int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
+static void index_iterator__next_prefix_tree(index_iterator *ii)
 {
-	bool desire_ignore_case  = (ignore_case != 0);
+	const char *slash;
 
-	if (iterator__ignore_case(iter) == desire_ignore_case)
-		return 0;
+	if (!iterator__include_trees(ii))
+		return;
 
-	if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
-		if (desire_ignore_case)
-			iter->flags |= GIT_ITERATOR_IGNORE_CASE;
-		else
-			iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
+	slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');
+
+	if (slash != NULL) {
+		ii->partial_pos = (slash - ii->partial.ptr) + 1;
+		ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
+		ii->partial.ptr[ii->partial_pos] = '\0';
 	} else {
-		giterr_set(GITERR_INVALID,
-			"Cannot currently set ignore case on non-empty iterators");
-		return -1;
+		ii->partial_pos = ii->partial.size;
 	}
 
-	return 0;
+	if (index_iterator__index_entry(ii) == NULL)
+		ii->partial_pos = ii->partial.size;
 }
 
-git_index *git_iterator_get_index(git_iterator *iter)
+static int index_iterator__first_prefix_tree(index_iterator *ii)
 {
-	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
-		return ((index_iterator *)iter)->index;
-	return NULL;
+	const git_index_entry *ie = index_iterator__advance_over_unwanted(ii);
+	const char *scan, *prior, *slash;
+
+	if (!ie || !iterator__include_trees(ii))
+		return 0;
+
+	/* find longest common prefix with prior index entry */
+	for (scan = slash = ie->path, prior = ii->partial.ptr;
+		 *scan && *scan == *prior; ++scan, ++prior)
+		if (*scan == '/')
+			slash = scan;
+
+	if (git_buf_sets(&ii->partial, ie->path) < 0)
+		return -1;
+
+	ii->partial_pos = (slash - ie->path) + 1;
+	index_iterator__next_prefix_tree(ii);
+
+	return 0;
 }
 
-static void workdir_iterator_update_is_ignored(workdir_iterator *wi)
+#define index_iterator__at_tree(I) \
+	(iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)
+
+static int index_iterator__current(
+	const git_index_entry **entry, git_iterator *self)
 {
-	git_dir_flag dir_flag = git_entry__dir_flag(&wi->fi.entry);
+	index_iterator *ii = (index_iterator *)self;
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
-	if (git_ignore__lookup(&wi->is_ignored, &wi->ignores, wi->fi.entry.path, dir_flag) < 0) {
-		giterr_clear();
-		wi->is_ignored = GIT_IGNORE_NOTFOUND;
+	if (ie != NULL && index_iterator__at_tree(ii)) {
+		ii->tree_entry.path = ii->partial.ptr;
+		ie = &ii->tree_entry;
 	}
 
-	/* use ignore from containing frame stack */
-	if (wi->is_ignored <= GIT_IGNORE_NOTFOUND)
-		wi->is_ignored = wi->fi.stack->is_ignored;
+	if (entry)
+		*entry = ie;
+
+	ii->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+
+	return (ie != NULL) ? 0 : GIT_ITEROVER;
 }
 
-bool git_iterator_current_is_ignored(git_iterator *iter)
+static int index_iterator__at_end(git_iterator *self)
 {
-	workdir_iterator *wi = (workdir_iterator *)iter;
+	index_iterator *ii = (index_iterator *)self;
+	return (ii->current >= git_vector_length(&ii->entries));
+}
 
-	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
-		return false;
+static int index_iterator__advance(
+	const git_index_entry **entry, git_iterator *self)
+{
+	index_iterator *ii = (index_iterator *)self;
+	size_t entrycount = git_vector_length(&ii->entries);
+	const git_index_entry *ie;
 
-	if (wi->is_ignored != GIT_IGNORE_UNCHECKED)
-		return (bool)(wi->is_ignored == GIT_IGNORE_TRUE);
+	if (!iterator__has_been_accessed(ii))
+		return index_iterator__current(entry, self);
+
+	if (index_iterator__at_tree(ii)) {
+		if (iterator__do_autoexpand(ii)) {
+			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
+			index_iterator__next_prefix_tree(ii);
+		} else {
+			/* advance to sibling tree (i.e. find entry with new prefix) */
+			while (ii->current < entrycount) {
+				ii->current++;
 
-	workdir_iterator_update_is_ignored(wi);
+				if (!(ie = git_vector_get(&ii->entries, ii->current)) ||
+					ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
+					break;
+			}
 
-	return (bool)(wi->is_ignored == GIT_IGNORE_TRUE);
+			if (index_iterator__first_prefix_tree(ii) < 0)
+				return -1;
+		}
+	} else {
+		if (ii->current < entrycount)
+			ii->current++;
+
+		if (index_iterator__first_prefix_tree(ii) < 0)
+			return -1;
+	}
+
+	return index_iterator__current(entry, self);
 }
 
-bool git_iterator_current_tree_is_ignored(git_iterator *iter)
+static int index_iterator__advance_into(
+	const git_index_entry **entry, git_iterator *self)
 {
-	workdir_iterator *wi = (workdir_iterator *)iter;
+	index_iterator *ii = (index_iterator *)self;
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
-	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
-		return false;
+	if (ie != NULL && index_iterator__at_tree(ii)) {
+		if (ii->restore_terminator)
+			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
+		index_iterator__next_prefix_tree(ii);
+	}
 
-	return (bool)(wi->fi.stack->is_ignored == GIT_IGNORE_TRUE);
+	return index_iterator__current(entry, self);
 }
 
-int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
+static int index_iterator__reset(git_iterator *self)
 {
-	const git_index_entry *entry;
+	index_iterator *ii = (index_iterator *)self;
+	const git_index_entry *ie;
 
-	/* a "done" iterator is after every prefix */
-	if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
-		return 1;
+	ii->current = 0;
+	ii->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
 
-	/* a NULL prefix is after any valid iterator */
-	if (!path_prefix)
+	iterator_pathlist_walk__reset(self);
+
+	/* if we're given a start prefix, find it; if we're given a pathlist, find
+	 * the first of those.  start at the later of the two.
+	 */
+	if (ii->base.start)
+		git_index_snapshot_find(
+			&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
+
+	if ((ie = index_iterator__advance_over_unwanted(ii)) == NULL)
+		return 0;
+
+	if (git_buf_sets(&ii->partial, ie->path) < 0)
 		return -1;
 
-	return iter->prefixcomp(entry->path, path_prefix);
-}
+	ii->partial_pos = 0;
 
-int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
-{
-	workdir_iterator *wi = (workdir_iterator *)iter;
+	if (ii->base.start) {
+		size_t startlen = strlen(ii->base.start);
 
-	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->fi.entry.path)
-		*path = NULL;
-	else
-		*path = &wi->fi.path;
+		ii->partial_pos = (startlen > ii->partial.size) ?
+			ii->partial.size : startlen;
+	}
+
+	index_iterator__next_prefix_tree(ii);
 
 	return 0;
 }
 
-int git_iterator_index(git_index **out, git_iterator *iter)
+static int index_iterator__reset_range(
+	git_iterator *self, const char *start, const char *end)
 {
-	workdir_iterator *wi = (workdir_iterator *)iter;
-
-	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
-		*out = NULL;
+	if (iterator__reset_range(self, start, end) < 0)
+		return -1;
 
-	*out = wi->index;
+	return index_iterator__reset(self);
+}
 
-	return 0;
+static void index_iterator__free(git_iterator *self)
+{
+	index_iterator *ii = (index_iterator *)self;
+	git_index_snapshot_release(&ii->entries, ii->index);
+	ii->index = NULL;
+	git_buf_free(&ii->partial);
 }
 
-int git_iterator_advance_over_with_status(
-	const git_index_entry **entryptr,
-	git_iterator_status_t *status,
-	git_iterator *iter)
+int git_iterator_for_index(
+	git_iterator **iter,
+	git_repository *repo,
+	git_index  *index,
+	git_iterator_options *options)
 {
 	int error = 0;
-	workdir_iterator *wi = (workdir_iterator *)iter;
-	char *base = NULL;
-	const git_index_entry *entry;
-
-	*status = GIT_ITERATOR_STATUS_NORMAL;
+	index_iterator *ii = git__calloc(1, sizeof(index_iterator));
+	GITERR_CHECK_ALLOC(ii);
 
-	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
-		return git_iterator_advance(entryptr, iter);
-	if ((error = git_iterator_current(&entry, iter)) < 0)
+	if ((error = git_index_snapshot_new(&ii->entries, index)) < 0) {
+		git__free(ii);
 		return error;
+	}
+	ii->index = index;
 
-	if (!S_ISDIR(entry->mode)) {
-		workdir_iterator_update_is_ignored(wi);
-		if (wi->is_ignored == GIT_IGNORE_TRUE)
-			*status = GIT_ITERATOR_STATUS_IGNORED;
-		return git_iterator_advance(entryptr, iter);
+	ITERATOR_BASE_INIT(ii, index, INDEX, repo);
+
+	if ((error = iterator__update_ignore_case((git_iterator *)ii, options ? options->flags : 0)) < 0) {
+		git_iterator_free((git_iterator *)ii);
+		return error;
 	}
 
-	*status = GIT_ITERATOR_STATUS_EMPTY;
+	ii->entry_srch = iterator__ignore_case(ii) ?
+		git_index_entry_isrch : git_index_entry_srch;
 
-	base = git__strdup(entry->path);
-	GITERR_CHECK_ALLOC(base);
+	git_vector_set_cmp(&ii->entries, iterator__ignore_case(ii) ?
+		git_index_entry_icmp : git_index_entry_cmp);
+	git_vector_sort(&ii->entries);
 
-	/* scan inside directory looking for a non-ignored item */
-	while (entry && !iter->prefixcomp(entry->path, base)) {
-		workdir_iterator_update_is_ignored(wi);
+	git_buf_init(&ii->partial, 0);
+	ii->tree_entry.mode = GIT_FILEMODE_TREE;
 
-		/* if we found an explicitly ignored item, then update from
-		 * EMPTY to IGNORED
-		 */
-		if (wi->is_ignored == GIT_IGNORE_TRUE)
-			*status = GIT_ITERATOR_STATUS_IGNORED;
-		else if (S_ISDIR(entry->mode)) {
-			error = git_iterator_advance_into(&entry, iter);
+	index_iterator__reset((git_iterator *)ii);
 
-			if (!error)
-				continue;
+	*iter = (git_iterator *)ii;
+	return 0;
+}
 
-			else if (error == GIT_ENOTFOUND) {
-				/* we entered this directory only hoping to find child matches to
-				 * our pathlist (eg, this is `foo` and we had a pathlist entry for
-				 * `foo/bar`).  it should not be ignored, it should be excluded.
-				 */
-				if (wi->fi.pathlist_match == ITERATOR_PATHLIST_MATCH_CHILD)
-					*status = GIT_ITERATOR_STATUS_FILTERED;
-				else
-					wi->is_ignored = GIT_IGNORE_TRUE; /* mark empty dirs ignored */
 
-				error = 0;
-			} else
-				break; /* real error, stop here */
-		} else {
-			/* we found a non-ignored item, treat parent as untracked */
-			*status = GIT_ITERATOR_STATUS_NORMAL;
-			break;
-		}
+void git_iterator_free(git_iterator *iter)
+{
+	if (iter == NULL)
+		return;
 
-		if ((error = git_iterator_advance(&entry, iter)) < 0)
-			break;
-	}
+	iter->cb->free(iter);
 
-	/* wrap up scan back to base directory */
-	while (entry && !iter->prefixcomp(entry->path, base))
-		if ((error = git_iterator_advance(&entry, iter)) < 0)
-			break;
+	git_vector_free(&iter->pathlist);
+	git__free(iter->start);
+	git__free(iter->end);
 
-	*entryptr = entry;
-	git__free(base);
+	memset(iter, 0, sizeof(*iter));
 
-	return error;
+	git__free(iter);
+}
+
+int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
+{
+	const git_index_entry *entry;
+
+	/* a "done" iterator is after every prefix */
+	if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
+		return 1;
+
+	/* a NULL prefix is after any valid iterator */
+	if (!path_prefix)
+		return -1;
+
+	return iter->prefixcomp(entry->path, path_prefix);
+}
+
+git_index *git_iterator_index(git_iterator *iter)
+{
+	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
+		return ((index_iterator *)iter)->index;
+
+	if (iter->type == GIT_ITERATOR_TYPE_FS ||
+		iter->type == GIT_ITERATOR_TYPE_WORKDIR)
+		return ((filesystem_iterator *)iter)->index;
+
+	return NULL;
 }
 
 int git_iterator_walk(
diff --git a/src/iterator.h b/src/iterator.h
index 8cd774b..d64d63f 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -34,10 +34,19 @@ typedef enum {
 	GIT_ITERATOR_DONT_AUTOEXPAND  = (1u << 3),
 	/** convert precomposed unicode to decomposed unicode */
 	GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
+	/** never convert precomposed unicode to decomposed unicode */
+	GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
 	/** include conflicts */
-	GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 5),
+	GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
 } git_iterator_flag_t;
 
+typedef enum {
+	GIT_ITERATOR_STATUS_NORMAL = 0,
+	GIT_ITERATOR_STATUS_IGNORED = 1,
+	GIT_ITERATOR_STATUS_EMPTY = 2,
+	GIT_ITERATOR_STATUS_FILTERED = 3
+} git_iterator_status_t;
+
 typedef struct {
 	const char *start;
 	const char *end;
@@ -57,6 +66,8 @@ typedef struct {
 	int (*current)(const git_index_entry **, git_iterator *);
 	int (*advance)(const git_index_entry **, git_iterator *);
 	int (*advance_into)(const git_index_entry **, git_iterator *);
+	int (*advance_over)(
+		const git_index_entry **, git_iterator_status_t *, git_iterator *);
 	int (*reset)(git_iterator *);
 	int (*reset_range)(git_iterator *, const char *start, const char *end);
 	int (*at_end)(git_iterator *);
@@ -67,8 +78,13 @@ struct git_iterator {
 	git_iterator_type_t type;
 	git_iterator_callbacks *cb;
 	git_repository *repo;
+
 	char *start;
+	size_t start_len;
+
 	char *end;
+	size_t end_len;
+
 	bool started;
 	bool ended;
 	git_vector pathlist;
@@ -76,6 +92,7 @@ struct git_iterator {
 	int (*strcomp)(const char *a, const char *b);
 	int (*strncomp)(const char *a, const char *b, size_t n);
 	int (*prefixcomp)(const char *str, const char *prefix);
+	int (*entry_srch)(const void *key, const void *array_member);
 	size_t stat_calls;
 	unsigned int flags;
 };
@@ -183,6 +200,28 @@ GIT_INLINE(int) git_iterator_advance_into(
 	return iter->cb->advance_into(entry, iter);
 }
 
+/* Advance over a directory and check if it contains no files or just
+ * ignored files.
+ *
+ * In a tree or the index, all directories will contain files, but in the
+ * working directory it is possible to have an empty directory tree or a
+ * tree that only contains ignored files.  Many Git operations treat these
+ * cases specially.  This advances over a directory (presumably an
+ * untracked directory) but checks during the scan if there are any files
+ * and any non-ignored files.
+ */
+GIT_INLINE(int) git_iterator_advance_over(
+	const git_index_entry **entry,
+	git_iterator_status_t *status,
+	git_iterator *iter)
+{
+	if (iter->cb->advance_over)
+		return iter->cb->advance_over(entry, status, iter);
+
+	*status = GIT_ITERATOR_STATUS_NORMAL;
+	return git_iterator_advance(entry, iter);
+}
+
 /**
  * Advance into a tree or skip over it if it is empty.
  *
@@ -273,35 +312,12 @@ extern int git_iterator_cmp(
 extern int git_iterator_current_workdir_path(
 	git_buf **path, git_iterator *iter);
 
-/* Return index pointer if index iterator, else NULL */
-extern git_index *git_iterator_get_index(git_iterator *iter);
-
-typedef enum {
-	GIT_ITERATOR_STATUS_NORMAL = 0,
-	GIT_ITERATOR_STATUS_IGNORED = 1,
-	GIT_ITERATOR_STATUS_EMPTY = 2,
-	GIT_ITERATOR_STATUS_FILTERED = 3
-} git_iterator_status_t;
-
-/* Advance over a directory and check if it contains no files or just
- * ignored files.
- *
- * In a tree or the index, all directories will contain files, but in the
- * working directory it is possible to have an empty directory tree or a
- * tree that only contains ignored files.  Many Git operations treat these
- * cases specially.  This advances over a directory (presumably an
- * untracked directory) but checks during the scan if there are any files
- * and any non-ignored files.
- */
-extern int git_iterator_advance_over_with_status(
-	const git_index_entry **entry, git_iterator_status_t *status, git_iterator *iter);
-
 /**
  * Retrieve the index stored in the iterator.
  *
- * Only implemented for the workdir iterator
+ * Only implemented for the workdir and index iterators.
  */
-extern int git_iterator_index(git_index **out, git_iterator *iter);
+extern git_index *git_iterator_index(git_iterator *iter);
 
 typedef int (*git_iterator_walk_cb)(
 	const git_index_entry **entries,