Commit 34a4ad46e8ff306dca989bb591dc415e38480064

Vicent Martí 2013-01-16T15:52:58

Merge pull request #1211 from arrbee/fix-icase-status-file Fix case insensitivity issues in git_status_file

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
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 7726a65..3861102 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -209,6 +209,15 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
 GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry);
 
 /**
+ * Compare two tree entries
+ *
+ * @param e1 first tree entry
+ * @param e2 second tree entry
+ * @return <0 if e1 is before e2, 0 if e1 == e2, >0 if e1 is after e2
+ */
+GIT_EXTERN(int) git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2);
+
+/**
  * Convert a tree entry to the git_object it points too.
  *
  * You must call `git_object_free()` on the object when you are done with it.
diff --git a/src/checkout.c b/src/checkout.c
index d5a471d..411bf3b 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -224,7 +224,7 @@ static int checkout_action_wd_only(
 	if (!git_pathspec_match_path(
 			pathspec, wd->path,
 			(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
-			workdir->ignore_case))
+			git_iterator_ignore_case(workdir)))
 		return 0;
 
 	/* check if item is tracked in the index but not in the checkout diff */
@@ -1130,7 +1130,7 @@ static int checkout_data_init(
 		if ((error = git_config_refresh(cfg)) < 0)
 			goto cleanup;
 
-		if (git_iterator_inner_type(target) == GIT_ITERATOR_INDEX) {
+		if (git_iterator_inner_type(target) == GIT_ITERATOR_TYPE_INDEX) {
 			/* if we are iterating over the index, don't reload */
 			data->index = git_iterator_index_get_index(target);
 			GIT_REFCOUNT_INC(data->index);
@@ -1208,6 +1208,7 @@ int git_checkout_iterator(
 	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
 	uint32_t *actions = NULL;
 	size_t *counts = NULL;
+	git_iterator_flag_t iterflags = 0;
 
 	/* initialize structures and options */
 	error = checkout_data_init(&data, target, opts);
@@ -1228,18 +1229,21 @@ int git_checkout_iterator(
 		diff_opts.pathspec = data.opts.paths;
 
 	/* set up iterators */
+
+	iterflags = git_iterator_ignore_case(target) ?
+		GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
+
 	if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 ||
 		(error = git_iterator_for_workdir_range(
-			&workdir, data.repo, data.pfx, data.pfx)) < 0 ||
+			&workdir, data.repo, iterflags, data.pfx, data.pfx)) < 0 ||
 		(error = git_iterator_for_tree_range(
-			&baseline, data.opts.baseline, data.pfx, data.pfx)) < 0)
+			&baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0)
 		goto cleanup;
 
 	/* Handle case insensitivity for baseline if necessary */
-	if (workdir->ignore_case && !baseline->ignore_case) {
+	if (git_iterator_ignore_case(workdir) != git_iterator_ignore_case(baseline))
 		if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0)
 			goto cleanup;
-	}
 
 	/* Generate baseline-to-target diff which will include an entry for
 	 * every possible update that might need to be made.
diff --git a/src/diff.c b/src/diff.c
index 5e34b92..4b60935 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -418,7 +418,7 @@ static int maybe_modified(
 	git_delta_t status = GIT_DELTA_MODIFIED;
 	unsigned int omode = oitem->mode;
 	unsigned int nmode = nitem->mode;
-	bool new_is_workdir = (new_iter->type == GIT_ITERATOR_WORKDIR);
+	bool new_is_workdir = (new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
 
 	GIT_UNUSED(old_iter);
 
@@ -556,7 +556,9 @@ static int diff_list_init_from_iterators(
 
 	/* Use case-insensitive compare if either iterator has
 	 * the ignore_case bit set */
-	if (!old_iter->ignore_case && !new_iter->ignore_case) {
+	if (!git_iterator_ignore_case(old_iter) &&
+		!git_iterator_ignore_case(new_iter))
+	{
 		diff->opts.flags &= ~GIT_DIFF_DELTAS_ARE_ICASE;
 
 		diff->strcomp    = git__strcmp;
@@ -714,7 +716,7 @@ int git_diff__from_iterators(
 			else if (git_iterator_current_is_ignored(new_iter))
 				delta_type = GIT_DELTA_IGNORED;
 
-			else if (new_iter->type != GIT_ITERATOR_WORKDIR)
+			else if (new_iter->type != GIT_ITERATOR_TYPE_WORKDIR)
 				delta_type = GIT_DELTA_ADDED;
 
 			if (diff_delta__from_one(diff, delta_type, nitem) < 0)
@@ -786,8 +788,8 @@ int git_diff_tree_to_tree(
 	assert(diff && repo);
 
 	DIFF_FROM_ITERATORS(
-		git_iterator_for_tree_range(&a, old_tree, pfx, pfx),
-		git_iterator_for_tree_range(&b, new_tree, pfx, pfx)
+		git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
+		git_iterator_for_tree_range(&b, new_tree, 0, pfx, pfx)
 	);
 
 	return error;
@@ -808,8 +810,8 @@ int git_diff_tree_to_index(
 		return error;
 
 	DIFF_FROM_ITERATORS(
-		git_iterator_for_tree_range(&a, old_tree, pfx, pfx),
-	    git_iterator_for_index_range(&b, index, pfx, pfx)
+		git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
+	    git_iterator_for_index_range(&b, index, 0, pfx, pfx)
 	);
 
 	return error;
@@ -829,8 +831,8 @@ int git_diff_index_to_workdir(
 		return error;
 
 	DIFF_FROM_ITERATORS(
-		git_iterator_for_index_range(&a, index, pfx, pfx),
-	    git_iterator_for_workdir_range(&b, repo, pfx, pfx)
+		git_iterator_for_index_range(&a, index, 0, pfx, pfx),
+	    git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx)
 	);
 
 	return error;
@@ -848,8 +850,8 @@ int git_diff_tree_to_workdir(
 	assert(diff && repo);
 
 	DIFF_FROM_ITERATORS(
-		git_iterator_for_tree_range(&a, old_tree, pfx, pfx),
-	    git_iterator_for_workdir_range(&b, repo, pfx, pfx)
+		git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
+	    git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx)
 	);
 
 	return error;
diff --git a/src/diff_output.c b/src/diff_output.c
index e79bf30..8a7a7a2 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -495,7 +495,7 @@ static void diff_patch_init(
 		patch->old_src = patch->diff->old_src;
 		patch->new_src = patch->diff->new_src;
 	} else {
-		patch->old_src = patch->new_src = GIT_ITERATOR_TREE;
+		patch->old_src = patch->new_src = GIT_ITERATOR_TYPE_TREE;
 	}
 }
 
@@ -578,7 +578,7 @@ static int diff_patch_load(
 	 */
 
 	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
-		patch->old_src == GIT_ITERATOR_WORKDIR) {
+		patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
 			goto cleanup;
@@ -587,7 +587,7 @@ static int diff_patch_load(
 	}
 
 	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
-		patch->new_src == GIT_ITERATOR_WORKDIR) {
+		patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
 			goto cleanup;
@@ -596,7 +596,7 @@ static int diff_patch_load(
 	}
 
 	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
-		patch->old_src != GIT_ITERATOR_WORKDIR) {
+		patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->old_file,
 				&patch->old_data, &patch->old_blob)) < 0)
@@ -606,7 +606,7 @@ static int diff_patch_load(
 	}
 
 	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
-		patch->new_src != GIT_ITERATOR_WORKDIR) {
+		patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->new_file,
 				&patch->new_data, &patch->new_blob)) < 0)
@@ -1666,32 +1666,28 @@ int git_diff__paired_foreach(
 	int cmp;
 	git_diff_delta *i2h, *w2i;
 	size_t i, j, i_max, j_max;
-	bool icase = false;
+	int (*strcomp)(const char *, const char *);
 
 	i_max = idx2head ? idx2head->deltas.length : 0;
 	j_max = wd2idx   ? wd2idx->deltas.length   : 0;
 
-	if (idx2head && wd2idx &&
-		(0 != (idx2head->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) ||
-		 0 != (wd2idx->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE)))
-	{
-		/* Then use the ignore-case sorter... */
-		icase = true;
-
-		/* and assert that both are ignore-case sorted. If this function
-		 * ever needs to support merge joining result sets that are not sorted
-		 * by the same function, then it will need to be extended to do a spool
-		 * and sort on one of the results before merge joining */
-		assert(0 != (idx2head->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) &&
-			0 != (wd2idx->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE));
-	}
+   /* Get appropriate strcmp function */
+   strcomp = idx2head ? idx2head->strcomp : wd2idx ? wd2idx->strcomp : NULL;
+
+   /* Assert both iterators use matching ignore-case. If this function ever
+    * supports merging diffs that are not sorted by the same function, then
+    * it will need to spool and sort on one of the results before merging
+    */
+   if (idx2head && wd2idx) {
+       assert(idx2head->strcomp == wd2idx->strcomp);
+   }
 
 	for (i = 0, j = 0; i < i_max || j < j_max; ) {
 		i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL;
 		w2i = wd2idx   ? GIT_VECTOR_GET(&wd2idx->deltas,j)   : NULL;
 
 		cmp = !w2i ? -1 : !i2h ? 1 :
-			STRCMP_CASESELECT(icase, i2h->old_file.path, w2i->old_file.path);
+			strcomp(i2h->old_file.path, w2i->old_file.path);
 
 		if (cmp < 0) {
 			if (cb(i2h, NULL, payload))
diff --git a/src/iterator.c b/src/iterator.c
index 08e2e79..56b2629 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -24,14 +24,14 @@
 #define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \
 	(P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \
 	GITERR_CHECK_ALLOC(P); \
-	(P)->base.type    = GIT_ITERATOR_ ## NAME_UC; \
+	(P)->base.type    = GIT_ITERATOR_TYPE_ ## NAME_UC; \
 	(P)->base.cb    = &(P)->cb; \
 	ITERATOR_SET_CB(P,NAME_LC); \
 	(P)->base.start   = start ? git__strdup(start) : NULL; \
 	(P)->base.end     = end ? git__strdup(end) : NULL; \
-	(P)->base.ignore_case = false; \
 	if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
 		git__free(P); return -1; } \
+	(P)->base.prefixcomp = git__prefixcmp; \
 	} while (0)
 
 static int iterator__reset_range(
@@ -54,6 +54,34 @@ static int iterator__reset_range(
 	return 0;
 }
 
+static int iterator_update_ignore_case(
+	git_iterator *iter,
+	git_iterator_flag_t flags)
+{
+	int error = 0, ignore_case = -1;
+
+	if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
+		ignore_case = true;
+	else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0)
+		ignore_case = false;
+	else {
+		git_index *index;
+
+		if (!(error = git_repository_index__weakptr(&index, iter->repo)))
+			ignore_case = (index->ignore_case != false);
+	}
+
+	if (ignore_case > 0)
+		iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
+	else if (ignore_case == 0)
+		iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
+
+	iter->prefixcomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ?
+		git__prefixcmp_icase : git__prefixcmp;
+
+	return error;
+}
+
 static int empty_iterator__no_item(
 	git_iterator *iter, const git_index_entry **entry)
 {
@@ -91,13 +119,14 @@ typedef struct {
 	git_iterator_callbacks cb;
 } empty_iterator;
 
-int git_iterator_for_nothing(git_iterator **iter)
+int git_iterator_for_nothing(git_iterator **iter, git_iterator_flag_t flags)
 {
 	empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
 	GITERR_CHECK_ALLOC(i);
 
-	i->base.type  = GIT_ITERATOR_EMPTY;
+	i->base.type  = GIT_ITERATOR_TYPE_EMPTY;
 	i->base.cb    = &i->cb;
+	i->base.flags = flags;
 	i->cb.current = empty_iterator__no_item;
 	i->cb.at_end  = empty_iterator__at_end;
 	i->cb.advance = empty_iterator__no_item;
@@ -116,7 +145,10 @@ struct tree_iterator_frame {
 	tree_iterator_frame *next, *prev;
 	git_tree *tree;
 	char *start;
+	size_t startlen;
 	size_t index;
+	void **icase_map;
+	void *icase_data[GIT_FLEX_ARRAY];
 };
 
 typedef struct {
@@ -130,7 +162,13 @@ typedef struct {
 
 GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti)
 {
-	return git_tree_entry_byindex(ti->stack->tree, ti->stack->index);
+	tree_iterator_frame *tf = ti->stack;
+
+	if (tf->index >= git_tree_entrycount(tf->tree))
+		return NULL;
+
+	return git_tree_entry_byindex(
+		tf->tree, tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index);
 }
 
 static char *tree_iterator__current_filename(
@@ -149,7 +187,10 @@ static void tree_iterator__free_frame(tree_iterator_frame *tf)
 {
 	if (!tf)
 		return;
+
 	git_tree_free(tf->tree);
+	tf->tree = NULL;
+
 	git__free(tf);
 }
 
@@ -195,7 +236,7 @@ static int tree_iterator__current(
 	if (ti->entry.path == NULL)
 		return -1;
 
-	if (ti->base.end && git__prefixcmp(ti->entry.path, ti->base.end) > 0)
+	if (ti->base.end && ti->base.prefixcomp(ti->entry.path, ti->base.end) > 0)
 		return tree_iterator__to_end(ti);
 
 	if (entry)
@@ -209,10 +250,50 @@ static int tree_iterator__at_end(git_iterator *self)
 	return (tree_iterator__tree_entry((tree_iterator *)self) == NULL);
 }
 
+static int tree_iterator__icase_map_cmp(const void *a, const void *b, void *data)
+{
+	git_tree *tree = data;
+	const git_tree_entry *te1 = git_tree_entry_byindex(tree, (size_t)a);
+	const git_tree_entry *te2 = git_tree_entry_byindex(tree, (size_t)b);
+	return te1 ? (te2 ? git_tree_entry_icmp(te1, te2) : 1) : -1;
+}
+
+static int tree_iterator__frame_start_icmp(const void *key, const void *element)
+{
+	const tree_iterator_frame *tf = (const tree_iterator_frame *)key;
+	const git_tree_entry *te = git_tree_entry_byindex(tf->tree, (size_t)element);
+
+	return memcmp(tf->start, te->filename, min(tf->startlen, te->filename_len));
+}
+
+static void tree_iterator__frame_seek_start(tree_iterator_frame *tf)
+{
+	if (!tf->start)
+		tf->index = 0;
+	else if (!tf->icase_map)
+		tf->index = git_tree__prefix_position(tf->tree, tf->start);
+	else {
+		if (!git__bsearch(
+				tf->icase_map, git_tree_entrycount(tf->tree),
+				tf, tree_iterator__frame_start_icmp, &tf->index))
+		{
+			while (tf->index > 0) {
+				/* move back while previous entry is still prefixed */
+				if (tree_iterator__frame_start_icmp(
+						tf, (const void *)(tf->index - 1)))
+					break;
+				tf->index--;
+			}
+		}
+	}
+}
+
 static tree_iterator_frame *tree_iterator__alloc_frame(
-	git_tree *tree, char *start)
+	tree_iterator *ti, git_tree *tree, char *start)
 {
-	tree_iterator_frame *tf = git__calloc(1, sizeof(tree_iterator_frame));
+	size_t i, max_i = git_tree_entrycount(tree);
+	tree_iterator_frame *tf =
+		git__calloc(1, sizeof(tree_iterator_frame) + max_i * sizeof(void *));
 	if (!tf)
 		return NULL;
 
@@ -220,9 +301,24 @@ static tree_iterator_frame *tree_iterator__alloc_frame(
 
 	if (start && *start) {
 		tf->start = start;
-		tf->index = git_tree__prefix_position(tree, start);
+		tf->startlen = strlen(start);
 	}
 
+	if (!max_i)
+		return tf;
+
+	if ((ti->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0) {
+		tf->icase_map = tf->icase_data;
+
+		for (i = 0; i < max_i; ++i)
+			tf->icase_map[i] = (void *)i;
+
+		git__tsort_r(
+			tf->icase_map, max_i, tree_iterator__icase_map_cmp, tf->tree);
+	}
+
+	tree_iterator__frame_seek_start(tf);
+
 	return tf;
 }
 
@@ -240,7 +336,7 @@ static int tree_iterator__expand_tree(tree_iterator *ti)
 
 		/* check that we have not passed the range end */
 		if (ti->base.end != NULL &&
-			git__prefixcmp(ti->path.ptr, ti->base.end) > 0)
+			ti->base.prefixcomp(ti->path.ptr, ti->base.end) > 0)
 			return tree_iterator__to_end(ti);
 
 		if ((error = git_tree_lookup(&subtree, ti->base.repo, &te->oid)) < 0)
@@ -250,14 +346,13 @@ static int tree_iterator__expand_tree(tree_iterator *ti)
 
 		/* apply range start to new frame if relevant */
 		if (ti->stack->start &&
-			git__prefixcmp(ti->stack->start, te->filename) == 0)
+			ti->base.prefixcomp(ti->stack->start, te->filename) == 0)
 		{
-			size_t namelen = strlen(te->filename);
-			if (ti->stack->start[namelen] == '/')
-				relpath = ti->stack->start + namelen + 1;
+			if (ti->stack->start[te->filename_len] == '/')
+				relpath = ti->stack->start + te->filename_len + 1;
 		}
 
-		if ((tf = tree_iterator__alloc_frame(subtree, relpath)) == NULL)
+		if ((tf = tree_iterator__alloc_frame(ti, subtree, relpath)) == NULL)
 			return -1;
 
 		tf->next  = ti->stack;
@@ -286,8 +381,9 @@ static int tree_iterator__advance(
 	}
 
 	while (1) {
-		te = git_tree_entry_byindex(ti->stack->tree, ++ti->stack->index);
-		if (te != NULL)
+		++ti->stack->index;
+
+		if ((te = tree_iterator__tree_entry(ti)) != NULL)
 			break;
 
 		if (!tree_iterator__pop_frame(ti))
@@ -337,8 +433,8 @@ static int tree_iterator__reset(
 	if (iterator__reset_range(self, start, end) < 0)
 		return -1;
 
-	ti->stack->index =
-		git_tree__prefix_position(ti->stack->tree, ti->base.start);
+	/* reset start position */
+	tree_iterator__frame_seek_start(ti->stack);
 
 	git_buf_clear(&ti->path);
 	ti->path_has_filename = false;
@@ -349,6 +445,7 @@ static int tree_iterator__reset(
 int git_iterator_for_tree_range(
 	git_iterator **iter,
 	git_tree *tree,
+	git_iterator_flag_t flags,
 	const char *start,
 	const char *end)
 {
@@ -356,7 +453,7 @@ int git_iterator_for_tree_range(
 	tree_iterator *ti;
 
 	if (tree == NULL)
-		return git_iterator_for_nothing(iter);
+		return git_iterator_for_nothing(iter, flags);
 
 	if ((error = git_tree__dup(&tree, tree)) < 0)
 		return error;
@@ -364,13 +461,20 @@ int git_iterator_for_tree_range(
 	ITERATOR_BASE_INIT(ti, tree, TREE);
 
 	ti->base.repo = git_tree_owner(tree);
-	ti->stack = ti->tail = tree_iterator__alloc_frame(tree, ti->base.start);
+
+	if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0)
+		goto fail;
+
+	ti->stack = ti->tail = tree_iterator__alloc_frame(ti, tree, ti->base.start);
 
 	if ((error = tree_iterator__expand_tree(ti)) < 0)
-		git_iterator_free((git_iterator *)ti);
-	else
-		*iter = (git_iterator *)ti;
+		goto fail;
+
+	*iter = (git_iterator *)ti;
+	return 0;
 
+fail:
+	git_iterator_free((git_iterator *)ti);
 	return error;
 }
 
@@ -411,7 +515,7 @@ static void index_iterator__skip_conflicts(
 
 		if (ie == NULL ||
 			(ii->base.end != NULL &&
-			ITERATOR_PREFIXCMP(ii->base, ie->path, ii->base.end) > 0)) {
+			 ii->base.prefixcomp(ie->path, ii->base.end) > 0)) {
 			ii->current = entrycount;
 			break;
 		}
@@ -466,15 +570,21 @@ static void index_iterator__free(git_iterator *self)
 int git_iterator_for_index_range(
 	git_iterator **iter,
 	git_index  *index,
+	git_iterator_flag_t flags,
 	const char *start,
 	const char *end)
 {
 	index_iterator *ii;
 
+	GIT_UNUSED(flags);
+
 	ITERATOR_BASE_INIT(ii, index, INDEX);
 
 	ii->base.repo = git_index_owner(index);
-	ii->base.ignore_case = index->ignore_case;
+	if (index->ignore_case) {
+		ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
+		ii->base.prefixcomp = git__prefixcmp_icase;
+	}
 	ii->index = index;
 	GIT_REFCOUNT_INC(index);
 
@@ -485,20 +595,6 @@ int git_iterator_for_index_range(
 	return 0;
 }
 
-int git_iterator_for_repo_index_range(
-	git_iterator **iter,
-	git_repository *repo,
-	const char *start,
-	const char *end)
-{
-	int error;
-	git_index *index;
-
-	if ((error = git_repository_index__weakptr(&index, repo)) < 0)
-		return error;
-
-	return git_iterator_for_index_range(iter, index, start, end);
-}
 
 typedef struct workdir_iterator_frame workdir_iterator_frame;
 struct workdir_iterator_frame {
@@ -544,7 +640,8 @@ static workdir_iterator_frame *workdir_iterator__alloc_frame(
 	workdir_iterator *wi)
 {
 	workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
-	git_vector_cmp entry_compare = CASESELECT(wi->base.ignore_case,
+	git_vector_cmp entry_compare = CASESELECT(
+		(wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0,
 		git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
 
 	if (wf == NULL)
@@ -606,7 +703,8 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi)
 	GITERR_CHECK_ALLOC(wf);
 
 	error = git_path_dirload_with_stat(
-		wi->path.ptr, wi->root_len, wi->base.ignore_case,
+		wi->path.ptr, wi->root_len,
+		(wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0,
 		wi->base.start, wi->base.end, &wf->entries);
 
 	if (error < 0 || wf->entries.length == 0) {
@@ -744,8 +842,8 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
 	if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0)
 		return -1;
 
-	if (wi->base.end && ITERATOR_PREFIXCMP(
-			wi->base, wi->path.ptr + wi->root_len, wi->base.end) > 0)
+	if (wi->base.end &&
+		wi->base.prefixcomp(wi->path.ptr + wi->root_len, wi->base.end) > 0)
 		return 0;
 
 	wi->entry.path = ps->path;
@@ -789,12 +887,12 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
 int git_iterator_for_workdir_range(
 	git_iterator **iter,
 	git_repository *repo,
+	git_iterator_flag_t flags,
 	const char *start,
 	const char *end)
 {
 	int error;
 	workdir_iterator *wi;
-	git_index *index;
 
 	assert(iter && repo);
 
@@ -805,13 +903,8 @@ int git_iterator_for_workdir_range(
 	ITERATOR_BASE_INIT(wi, workdir, WORKDIR);
 	wi->base.repo = repo;
 
-	if ((error = git_repository_index__weakptr(&index, repo)) < 0) {
-		git_iterator_free((git_iterator *)wi);
-		return error;
-	}
-
-	/* Match ignore_case flag for iterator to that of the index */
-	wi->base.ignore_case = index->ignore_case;
+	if ((error = iterator_update_ignore_case((git_iterator *)wi, flags)) < 0)
+		goto fail;
 
 	if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 ||
 		git_path_to_dir(&wi->path) < 0 ||
@@ -822,23 +915,24 @@ int git_iterator_for_workdir_range(
 	}
 
 	wi->root_len = wi->path.size;
-	wi->entrycmp = wi->base.ignore_case ?
+	wi->entrycmp = (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0 ?
 		workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case;
 
 	if ((error = workdir_iterator__expand_dir(wi)) < 0) {
-		if (error == GIT_ENOTFOUND)
-			error = 0;
-		else {
-			git_iterator_free((git_iterator *)wi);
-			wi = NULL;
-		}
+		if (error != GIT_ENOTFOUND)
+			goto fail;
+		giterr_clear();
 	}
 
 	*iter = (git_iterator *)wi;
+	return 0;
 
+fail:
+	git_iterator_free((git_iterator *)wi);
 	return error;
 }
 
+
 typedef struct {
 	/* replacement callbacks */
 	git_iterator_callbacks cb;
@@ -913,12 +1007,12 @@ void git_iterator_spoolandsort_pop(git_iterator *self)
 {
 	spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
 
-	if (self->type != GIT_ITERATOR_SPOOLANDSORT)
+	if (self->type != GIT_ITERATOR_TYPE_SPOOLANDSORT)
 		return;
 
 	self->cb   = scb->orig;
 	self->type = scb->orig_type;
-	self->ignore_case = !self->ignore_case;
+	self->flags ^= GIT_ITERATOR_IGNORE_CASE;
 
 	spoolandsort_iterator__free_callbacks(scb);
 }
@@ -935,9 +1029,14 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case)
 	spoolandsort_callbacks *scb;
 	int (*entrycomp)(const void *a, const void *b);
 
-	if (iter->ignore_case == ignore_case)
+	if (((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) == (ignore_case != 0))
 		return 0;
 
+	if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
+		iter->flags = (iter->flags ^ GIT_ITERATOR_IGNORE_CASE);
+		return 0;
+	}
+
 	scb = git__calloc(1, sizeof(spoolandsort_callbacks));
 	GITERR_CHECK_ALLOC(scb);
 
@@ -978,8 +1077,8 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case)
 	git_vector_sort(&scb->entries);
 
 	iter->cb   = (git_iterator_callbacks *)scb;
-	iter->type = GIT_ITERATOR_SPOOLANDSORT;
-	iter->ignore_case = !iter->ignore_case;
+	iter->type = GIT_ITERATOR_TYPE_SPOOLANDSORT;
+	iter->flags ^= GIT_ITERATOR_IGNORE_CASE;
 
 	return 0;
 
@@ -988,13 +1087,29 @@ fail:
 	return -1;
 }
 
+
+void git_iterator_free(git_iterator *iter)
+{
+	if (iter == NULL)
+		return;
+
+	iter->cb->free(iter);
+
+	git__free(iter->start);
+	git__free(iter->end);
+
+	memset(iter, 0, sizeof(*iter));
+
+	git__free(iter);
+}
+
 git_index *git_iterator_index_get_index(git_iterator *iter)
 {
-	if (iter->type == GIT_ITERATOR_INDEX)
+	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
 		return ((index_iterator *)iter)->index;
 
-	if (iter->type == GIT_ITERATOR_SPOOLANDSORT &&
-		((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_INDEX)
+	if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT &&
+		((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_TYPE_INDEX)
 		return ((index_iterator *)iter)->index;
 
 	return NULL;
@@ -1002,7 +1117,7 @@ git_index *git_iterator_index_get_index(git_iterator *iter)
 
 git_iterator_type_t git_iterator_inner_type(git_iterator *iter)
 {
-	if (iter->type == GIT_ITERATOR_SPOOLANDSORT)
+	if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT)
 		return ((spoolandsort_callbacks *)iter->cb)->orig_type;
 
 	return iter->type;
@@ -1011,7 +1126,7 @@ git_iterator_type_t git_iterator_inner_type(git_iterator *iter)
 int git_iterator_current_tree_entry(
 	git_iterator *iter, const git_tree_entry **tree_entry)
 {
-	*tree_entry = (iter->type != GIT_ITERATOR_TREE) ? NULL :
+	*tree_entry = (iter->type != GIT_ITERATOR_TYPE_TREE) ? NULL :
 		tree_iterator__tree_entry((tree_iterator *)iter);
 	return 0;
 }
@@ -1024,10 +1139,14 @@ int git_iterator_current_parent_tree(
 	tree_iterator *ti = (tree_iterator *)iter;
 	tree_iterator_frame *tf;
 	const char *scan = parent_path;
+	int (*strncomp)(const char *a, const char *b, size_t sz);
 
-	if (iter->type != GIT_ITERATOR_TREE || ti->stack == NULL)
+	if (iter->type != GIT_ITERATOR_TYPE_TREE || ti->stack == NULL)
 		goto notfound;
 
+	strncomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ?
+		git__strncasecmp : git__strncmp;
+
 	for (tf = ti->tail; tf != NULL; tf = tf->prev) {
 		const git_tree_entry *te;
 
@@ -1036,9 +1155,10 @@ int git_iterator_current_parent_tree(
 			return 0;
 		}
 
-		te = git_tree_entry_byindex(tf->tree, tf->index);
+		te = git_tree_entry_byindex(tf->tree,
+			tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index);
 
-		if (strncmp(scan, te->filename, te->filename_len) != 0)
+		if (strncomp(scan, te->filename, te->filename_len) != 0)
 			goto notfound;
 
 		scan += te->filename_len;
@@ -1059,7 +1179,7 @@ int git_iterator_current_is_ignored(git_iterator *iter)
 {
 	workdir_iterator *wi = (workdir_iterator *)iter;
 
-	if (iter->type != GIT_ITERATOR_WORKDIR)
+	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
 		return 0;
 
 	if (wi->is_ignored != -1)
@@ -1076,7 +1196,7 @@ int git_iterator_advance_into_directory(
 {
 	workdir_iterator *wi = (workdir_iterator *)iter;
 
-	if (iter->type == GIT_ITERATOR_WORKDIR &&
+	if (iter->type == GIT_ITERATOR_TYPE_WORKDIR &&
 		wi->entry.path &&
 		(wi->entry.mode == GIT_FILEMODE_TREE ||
 		 wi->entry.mode == GIT_FILEMODE_COMMIT))
@@ -1089,8 +1209,7 @@ int git_iterator_advance_into_directory(
 	return entry ? git_iterator_current(iter, entry) : 0;
 }
 
-int git_iterator_cmp(
-	git_iterator *iter, const char *path_prefix)
+int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
 {
 	const git_index_entry *entry;
 
@@ -1103,14 +1222,14 @@ int git_iterator_cmp(
 	if (!path_prefix)
 		return -1;
 
-	return ITERATOR_PREFIXCMP(*iter, entry->path, path_prefix);
+	return iter->prefixcomp(entry->path, path_prefix);
 }
 
 int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path)
 {
 	workdir_iterator *wi = (workdir_iterator *)iter;
 
-	if (iter->type != GIT_ITERATOR_WORKDIR || !wi->entry.path)
+	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->entry.path)
 		*path = NULL;
 	else
 		*path = &wi->path;
diff --git a/src/iterator.h b/src/iterator.h
index 8a4356e..a9bccfc 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -12,20 +12,26 @@
 #include "vector.h"
 #include "buffer.h"
 
-#define ITERATOR_PREFIXCMP(ITER, STR, PREFIX)	(((ITER).ignore_case) ? \
+#define ITERATOR_PREFIXCMP(ITER, STR, PREFIX)	\
+	(((ITER).flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? \
 	git__prefixcmp_icase((STR), (PREFIX)) : \
 	git__prefixcmp((STR), (PREFIX)))
 
 typedef struct git_iterator git_iterator;
 
 typedef enum {
-	GIT_ITERATOR_EMPTY = 0,
-	GIT_ITERATOR_TREE = 1,
-	GIT_ITERATOR_INDEX = 2,
-	GIT_ITERATOR_WORKDIR = 3,
-	GIT_ITERATOR_SPOOLANDSORT = 4
+	GIT_ITERATOR_TYPE_EMPTY = 0,
+	GIT_ITERATOR_TYPE_TREE = 1,
+	GIT_ITERATOR_TYPE_INDEX = 2,
+	GIT_ITERATOR_TYPE_WORKDIR = 3,
+	GIT_ITERATOR_TYPE_SPOOLANDSORT = 4
 } git_iterator_type_t;
 
+typedef enum {
+	GIT_ITERATOR_IGNORE_CASE = (1 << 0), /* ignore_case */
+	GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1), /* force ignore_case off */
+} git_iterator_flag_t;
+
 typedef struct {
 	int (*current)(git_iterator *, const git_index_entry **);
 	int (*at_end)(git_iterator *);
@@ -41,50 +47,60 @@ struct git_iterator {
 	git_repository *repo;
 	char *start;
 	char *end;
-	bool ignore_case;
+	int (*prefixcomp)(const char *str, const char *prefix);
+	unsigned int flags;
 };
 
-extern int git_iterator_for_nothing(git_iterator **iter);
+extern int git_iterator_for_nothing(
+	git_iterator **out, git_iterator_flag_t flags);
 
+/* tree iterators will match the ignore_case value from the index of the
+ * repository, unless you override with a non-zero flag value
+ */
 extern int git_iterator_for_tree_range(
-	git_iterator **iter, git_tree *tree,
-	const char *start, const char *end);
+	git_iterator **out,
+	git_tree *tree,
+	git_iterator_flag_t flags,
+	const char *start,
+	const char *end);
 
-GIT_INLINE(int) git_iterator_for_tree(
-	git_iterator **iter, git_tree *tree)
+GIT_INLINE(int) git_iterator_for_tree(git_iterator **out, git_tree *tree)
 {
-	return git_iterator_for_tree_range(iter, tree, NULL, NULL);
+	return git_iterator_for_tree_range(out, tree, 0, NULL, NULL);
 }
 
+/* index iterators will take the ignore_case value from the index; the
+ * ignore_case flags are not used
+ */
 extern int git_iterator_for_index_range(
-	git_iterator **iter, git_index *index, const char *start, const char *end);
-
-GIT_INLINE(int) git_iterator_for_index(
-	git_iterator **iter, git_index *index)
-{
-	return git_iterator_for_index_range(iter, index, NULL, NULL);
-}
-
-extern int git_iterator_for_repo_index_range(
-	git_iterator **iter, git_repository *repo,
-	const char *start, const char *end);
+	git_iterator **out,
+	git_index *index,
+	git_iterator_flag_t flags,
+	const char *start,
+	const char *end);
 
-GIT_INLINE(int) git_iterator_for_repo_index(
-	git_iterator **iter, git_repository *repo)
+GIT_INLINE(int) git_iterator_for_index(git_iterator **out, git_index *index)
 {
-	return git_iterator_for_repo_index_range(iter, repo, NULL, NULL);
+	return git_iterator_for_index_range(out, index, 0, NULL, NULL);
 }
 
+/* workdir iterators will match the ignore_case value from the index of the
+ * repository, unless you override with a non-zero flag value
+ */
 extern int git_iterator_for_workdir_range(
-	git_iterator **iter, git_repository *repo,
-	const char *start, const char *end);
+	git_iterator **out,
+	git_repository *repo,
+	git_iterator_flag_t flags,
+	const char *start,
+	const char *end);
 
-GIT_INLINE(int) git_iterator_for_workdir(
-	git_iterator **iter, git_repository *repo)
+GIT_INLINE(int) git_iterator_for_workdir(git_iterator **out, git_repository *repo)
 {
-	return git_iterator_for_workdir_range(iter, repo, NULL, NULL);
+	return git_iterator_for_workdir_range(out, repo, 0, NULL, NULL);
 }
 
+extern void git_iterator_free(git_iterator *iter);
+
 /* Spool all iterator values, resort with alternative ignore_case value
  * and replace callbacks with spoolandsort alternates.
  */
@@ -130,21 +146,6 @@ GIT_INLINE(int) git_iterator_reset(
 	return iter->cb->reset(iter, start, end);
 }
 
-GIT_INLINE(void) git_iterator_free(git_iterator *iter)
-{
-	if (iter == NULL)
-		return;
-
-	iter->cb->free(iter);
-
-	git__free(iter->start);
-	git__free(iter->end);
-
-	memset(iter, 0, sizeof(*iter));
-
-	git__free(iter);
-}
-
 GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter)
 {
 	return iter->type;
@@ -155,6 +156,16 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
 	return iter->repo;
 }
 
+GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
+{
+	return iter->flags;
+}
+
+GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter)
+{
+	return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0);
+}
+
 extern int git_iterator_current_tree_entry(
 	git_iterator *iter, const git_tree_entry **tree_entry);
 
diff --git a/src/path.c b/src/path.c
index 0fd367e..5de58cc 100644
--- a/src/path.c
+++ b/src/path.c
@@ -701,6 +701,30 @@ int git_path_cmp(
 	return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
 }
 
+int git_path_icmp(
+	const char *name1, size_t len1, int isdir1,
+	const char *name2, size_t len2, int isdir2)
+{
+	unsigned char c1, c2;
+	size_t len = len1 < len2 ? len1 : len2;
+	int cmp;
+
+	cmp = strncasecmp(name1, name2, len);
+	if (cmp)
+		return cmp;
+
+	c1 = name1[len];
+	c2 = name2[len];
+
+	if (c1 == '\0' && isdir1)
+		c1 = '/';
+
+	if (c2 == '\0' && isdir2)
+		c2 = '/';
+
+	return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
+}
+
 int git_path_direach(
 	git_buf *path,
 	int (*fn)(void *, git_buf *),
diff --git a/src/path.h b/src/path.h
index de0a40b..feefd65 100644
--- a/src/path.h
+++ b/src/path.h
@@ -261,12 +261,17 @@ extern int git_path_direach(
 	void *state);
 
 /**
- * Sort function to order two paths.
+ * Sort function to order two paths
  */
 extern int git_path_cmp(
 	const char *name1, size_t len1, int isdir1,
 	const char *name2, size_t len2, int isdir2);
 
+/** Path sort function that is case insensitive */
+extern int git_path_icmp(
+	const char *name1, size_t len1, int isdir1,
+	const char *name2, size_t len2, int isdir2);
+
 /**
  * Invoke callback up path directory by directory until the ceiling is
  * reached (inclusive of a final call at the root_path).
diff --git a/src/status.c b/src/status.c
index 777d850..282cb39 100644
--- a/src/status.c
+++ b/src/status.c
@@ -196,21 +196,24 @@ struct status_file_info {
 	char *expected;
 	unsigned int count;
 	unsigned int status;
+	int fnm_flags;
 	int ambiguous;
 };
 
 static int get_one_status(const char *path, unsigned int status, void *data)
 {
 	struct status_file_info *sfi = data;
+	int (*strcomp)(const char *a, const char *b);
 
 	sfi->count++;
 	sfi->status = status;
 
+	strcomp = (sfi->fnm_flags & FNM_CASEFOLD) ? git__strcasecmp : git__strcmp;
+
 	if (sfi->count > 1 ||
-		(strcmp(sfi->expected, path) != 0 &&
-		p_fnmatch(sfi->expected, path, 0) != 0)) {
-		giterr_set(GITERR_INVALID,
-			"Ambiguous path '%s' given to git_status_file", sfi->expected);
+		(strcomp(sfi->expected, path) != 0 &&
+		 p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0))
+	{
 		sfi->ambiguous = true;
 		return GIT_EAMBIGUOUS;
 	}
@@ -226,11 +229,17 @@ int git_status_file(
 	int error;
 	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
 	struct status_file_info sfi = {0};
+	git_index *index;
 
 	assert(status_flags && repo && path);
 
+	if ((error = git_repository_index__weakptr(&index, repo)) < 0)
+		return error;
+
 	if ((sfi.expected = git__strdup(path)) == NULL)
 		return -1;
+	if (index->ignore_case)
+		sfi.fnm_flags = FNM_CASEFOLD;
 
 	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
 	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
@@ -242,8 +251,11 @@ int git_status_file(
 
 	error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi);
 
-	if (error < 0 && sfi.ambiguous)
+	if (error < 0 && sfi.ambiguous) {
+		giterr_set(GITERR_INVALID,
+			"Ambiguous path '%s' given to git_status_file", sfi.expected);
 		error = GIT_EAMBIGUOUS;
+	}
 
 	if (!error && !sfi.count) {
 		git_buf full = GIT_BUF_INIT;
diff --git a/src/submodule.c b/src/submodule.c
index a723266..5283322 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1130,10 +1130,12 @@ static int load_submodule_config_from_index(
 	git_repository *repo, git_oid *gitmodules_oid)
 {
 	int error;
+	git_index *index;
 	git_iterator *i;
 	const git_index_entry *entry;
 
-	if ((error = git_iterator_for_repo_index(&i, repo)) < 0)
+	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
+		(error = git_iterator_for_index(&i, index)) < 0)
 		return error;
 
 	error = git_iterator_current(i, &entry);
diff --git a/src/tree.c b/src/tree.c
index cd1cd60..c34e9b9 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -55,14 +55,23 @@ static int valid_entry_name(const char *filename)
 		  strcmp(filename, DOT_GIT) != 0));
 }
 
-static int entry_sort_cmp(const void *a, const void *b)
+int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
 {
-	const git_tree_entry *entry_a = (const git_tree_entry *)(a);
-	const git_tree_entry *entry_b = (const git_tree_entry *)(b);
-
 	return git_path_cmp(
-		entry_a->filename, entry_a->filename_len, git_tree_entry__is_tree(entry_a),
-		entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b));
+		e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
+		e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+}
+
+int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
+{
+	return git_path_icmp(
+		e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
+		e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+}
+
+static int entry_sort_cmp(const void *a, const void *b)
+{
+	return git_tree_entry_cmp((const git_tree_entry *)a, (const git_tree_entry *)b);
 }
 
 static git_tree_entry *alloc_entry(const char *filename)
diff --git a/src/tree.h b/src/tree.h
index 6f05f5a..27afd4f 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -39,6 +39,8 @@ GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e)
 	return (S_ISDIR(e->attr) && !S_ISGITLINK(e->attr));
 }
 
+extern int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2);
+
 void git_tree__free(git_tree *tree);
 int git_tree__parse(git_tree *tree, git_odb_object *obj);
 
diff --git a/src/tsort.c b/src/tsort.c
index 634fe26..97473be 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -23,9 +23,8 @@
 #	define MIN(x,y) (((x) < (y) ? (x) : (y)))
 #endif
 
-typedef int (*cmp_ptr_t)(const void *, const void *);
-
-static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
+static int binsearch(
+	void **dst, const void *x, size_t size, git__tsort_r_cmp cmp, void *payload)
 {
 	int l, c, r;
 	void *lx, *cx;
@@ -38,12 +37,12 @@ static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
 	lx = dst[l];
 
 	/* check for beginning conditions */
-	if (cmp(x, lx) < 0)
+	if (cmp(x, lx, payload) < 0)
 		return 0;
 
-	else if (cmp(x, lx) == 0) {
+	else if (cmp(x, lx, payload) == 0) {
 		int i = 1;
-		while (cmp(x, dst[i]) == 0)
+		while (cmp(x, dst[i], payload) == 0)
 			i++;
 		return i;
 	}
@@ -51,7 +50,7 @@ static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
 	/* guaranteed not to be >= rx */
 	cx = dst[c];
 	while (1) {
-		const int val = cmp(x, cx);
+		const int val = cmp(x, cx, payload);
 		if (val < 0) {
 			if (c - l <= 1) return c;
 			r = c;
@@ -62,7 +61,7 @@ static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
 		} else {
 			do {
 				cx = dst[++c];
-			} while (cmp(x, cx) == 0);
+			} while (cmp(x, cx, payload) == 0);
 			return c;
 		}
 		c = l + ((r - l) >> 1);
@@ -71,7 +70,8 @@ static int binsearch(void **dst, const void *x, size_t size, cmp_ptr_t cmp)
 }
 
 /* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
-static void bisort(void **dst, size_t start, size_t size, cmp_ptr_t cmp)
+static void bisort(
+	void **dst, size_t start, size_t size, git__tsort_r_cmp cmp, void *payload)
 {
 	size_t i;
 	void *x;
@@ -80,12 +80,12 @@ static void bisort(void **dst, size_t start, size_t size, cmp_ptr_t cmp)
 	for (i = start; i < size; i++) {
 		int j;
 		/* If this entry is already correct, just move along */
-		if (cmp(dst[i - 1], dst[i]) <= 0)
+		if (cmp(dst[i - 1], dst[i], payload) <= 0)
 			continue;
 
 		/* Else we need to find the right place, shift everything over, and squeeze in */
 		x = dst[i];
-		location = binsearch(dst, x, i, cmp);
+		location = binsearch(dst, x, i, cmp, payload);
 		for (j = (int)i - 1; j >= location; j--) {
 			dst[j + 1] = dst[j];
 		}
@@ -102,7 +102,8 @@ struct tsort_run {
 
 struct tsort_store {
 	size_t alloc;
-	cmp_ptr_t cmp;
+	git__tsort_r_cmp cmp;
+	void *payload;
 	void **storage;
 };
 
@@ -118,7 +119,8 @@ static void reverse_elements(void **dst, ssize_t start, ssize_t end)
 	}
 }
 
-static ssize_t count_run(void **dst, ssize_t start, ssize_t size, struct tsort_store *store)
+static ssize_t count_run(
+	void **dst, ssize_t start, ssize_t size, struct tsort_store *store)
 {
 	ssize_t curr = start + 2;
 
@@ -126,7 +128,7 @@ static ssize_t count_run(void **dst, ssize_t start, ssize_t size, struct tsort_s
 		return 1;
 
 	if (start >= size - 2) {
-		if (store->cmp(dst[size - 2], dst[size - 1]) > 0) {
+		if (store->cmp(dst[size - 2], dst[size - 1], store->payload) > 0) {
 			void *tmp = dst[size - 1];
 			dst[size - 1] = dst[size - 2];
 			dst[size - 2] = tmp;
@@ -135,13 +137,15 @@ static ssize_t count_run(void **dst, ssize_t start, ssize_t size, struct tsort_s
 		return 2;
 	}
 
-	if (store->cmp(dst[start], dst[start + 1]) <= 0) {
-		while (curr < size - 1 && store->cmp(dst[curr - 1], dst[curr]) <= 0)
+	if (store->cmp(dst[start], dst[start + 1], store->payload) <= 0) {
+		while (curr < size - 1 &&
+				store->cmp(dst[curr - 1], dst[curr], store->payload) <= 0)
 			curr++;
 
 		return curr - start;
 	} else {
-		while (curr < size - 1 && store->cmp(dst[curr - 1], dst[curr]) > 0)
+		while (curr < size - 1 &&
+				store->cmp(dst[curr - 1], dst[curr], store->payload) > 0)
 			curr++;
 
 		/* reverse in-place */
@@ -219,7 +223,7 @@ static void merge(void **dst, const struct tsort_run *stack, ssize_t stack_curr,
 
 		for (k = curr; k < curr + A + B; k++) {
 			if ((i < A) && (j < curr + A + B)) {
-				if (store->cmp(storage[i], dst[j]) <= 0)
+				if (store->cmp(storage[i], dst[j], store->payload) <= 0)
 					dst[k] = storage[i++];
 				else
 					dst[k] = dst[j++];
@@ -235,7 +239,7 @@ static void merge(void **dst, const struct tsort_run *stack, ssize_t stack_curr,
 
 		for (k = curr + A + B - 1; k >= curr; k--) {
 			if ((i >= 0) && (j >= curr)) {
-				if (store->cmp(dst[j], storage[i]) > 0)
+				if (store->cmp(dst[j], storage[i], store->payload) > 0)
 					dst[k] = dst[j--];
 				else
 					dst[k] = storage[i--];
@@ -307,7 +311,7 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr,
 	if (run < minrun) run = minrun;\
 	if (run > (ssize_t)size - curr) run = size - curr;\
 	if (run > len) {\
-		bisort(&dst[curr], len, run, cmp);\
+		bisort(&dst[curr], len, run, cmp, payload);\
 		len = run;\
 	}\
 	run_stack[stack_curr].start = curr;\
@@ -329,7 +333,8 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr,
 }\
 while (0)
 
-void git__tsort(void **dst, size_t size, cmp_ptr_t cmp)
+void git__tsort_r(
+	void **dst, size_t size, git__tsort_r_cmp cmp, void *payload)
 {
 	struct tsort_store _store, *store = &_store;
 	struct tsort_run run_stack[128];
@@ -340,7 +345,7 @@ void git__tsort(void **dst, size_t size, cmp_ptr_t cmp)
 	ssize_t minrun;
 
 	if (size < 64) {
-		bisort(dst, 1, size, cmp);
+		bisort(dst, 1, size, cmp, payload);
 		return;
 	}
 
@@ -351,6 +356,7 @@ void git__tsort(void **dst, size_t size, cmp_ptr_t cmp)
 	store->alloc = 0;
 	store->storage = NULL;
 	store->cmp = cmp;
+	store->payload = payload;
 
 	PUSH_NEXT();
 	PUSH_NEXT();
@@ -365,3 +371,13 @@ void git__tsort(void **dst, size_t size, cmp_ptr_t cmp)
 		PUSH_NEXT();
 	}
 }
+
+static int tsort_r_cmp(const void *a, const void *b, void *payload)
+{
+	return ((git__tsort_cmp)payload)(a, b);
+}
+
+void git__tsort(void **dst, size_t size, git__tsort_cmp cmp)
+{
+	git__tsort_r(dst, size, tsort_r_cmp, cmp);
+}
diff --git a/src/util.c b/src/util.c
index 51173fa..30c4dc6 100644
--- a/src/util.c
+++ b/src/util.c
@@ -462,7 +462,7 @@ uint32_t git__hash(const void *key, int len, uint32_t seed)
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
- */ 
+ */
 int git__bsearch(
 	void **array,
 	size_t array_len,
@@ -493,6 +493,37 @@ int git__bsearch(
 	return (cmp == 0) ? 0 : -1;
 }
 
+int git__bsearch_r(
+	void **array,
+	size_t array_len,
+	const void *key,
+	int (*compare_r)(const void *, const void *, void *),
+	void *payload,
+	size_t *position)
+{
+	unsigned int lim;
+	int cmp = -1;
+	void **part, **base = array;
+
+	for (lim = (unsigned int)array_len; lim != 0; lim >>= 1) {
+		part = base + (lim >> 1);
+		cmp = (*compare_r)(key, *part, payload);
+		if (cmp == 0) {
+			base = part;
+			break;
+		}
+		if (cmp > 0) { /* key > p; take right partition */
+			base = part + 1;
+			lim--;
+		} /* else take left partition */
+	}
+
+	if (position)
+		*position = (base - array);
+
+	return (cmp == 0) ? 0 : -1;
+}
+
 /**
  * A strcmp wrapper
  *
diff --git a/src/util.h b/src/util.h
index ee0d0e3..9bcd320 100644
--- a/src/util.h
+++ b/src/util.h
@@ -119,7 +119,15 @@ GIT_INLINE(const char *) git__next_line(const char *s)
 	return s;
 }
 
-extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *));
+typedef int (*git__tsort_cmp)(const void *a, const void *b);
+
+extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
+
+typedef int (*git__tsort_r_cmp)(const void *a, const void *b, void *payload);
+
+extern void git__tsort_r(
+	void **dst, size_t size, git__tsort_r_cmp cmp, void *payload);
+
 
 /**
  * @param position If non-NULL, this will be set to the position where the
@@ -130,7 +138,15 @@ extern int git__bsearch(
 	void **array,
 	size_t array_len,
 	const void *key,
-	int (*compare)(const void *, const void *),
+	int (*compare)(const void *key, const void *element),
+	size_t *position);
+
+extern int git__bsearch_r(
+	void **array,
+	size_t array_len,
+	const void *key,
+	int (*compare_r)(const void *key, const void *element, void *payload),
+	void *payload,
 	size_t *position);
 
 extern int git__strcmp_cb(const void *a, const void *b);
diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c
index b579063..566503b 100644
--- a/tests-clar/diff/iterator.c
+++ b/tests-clar/diff/iterator.c
@@ -35,7 +35,8 @@ static void tree_iterator_test(
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
 	cl_assert(t = resolve_commit_oid_to_tree(repo, treeish));
-	cl_git_pass(git_iterator_for_tree_range(&i, t, start, end));
+	cl_git_pass(git_iterator_for_tree_range(
+		&i, t, GIT_ITERATOR_DONT_IGNORE_CASE, start, end));
 
 	/* test loop */
 	cl_git_pass(git_iterator_current(i, &entry));
@@ -304,7 +305,8 @@ void test_diff_iterator__tree_special_functions(void)
 		repo, "24fa9a9fc4e202313e24b648087495441dab432b");
 	cl_assert(t != NULL);
 
-	cl_git_pass(git_iterator_for_tree_range(&i, t, NULL, NULL));
+	cl_git_pass(git_iterator_for_tree_range(
+		&i, t, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
@@ -355,12 +357,14 @@ static void index_iterator_test(
 	const char **expected_names,
 	const char **expected_oids)
 {
+	git_index *index;
 	git_iterator *i;
 	const git_index_entry *entry;
 	int count = 0;
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
-	cl_git_pass(git_iterator_for_repo_index_range(&i, repo, start, end));
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
@@ -378,6 +382,7 @@ static void index_iterator_test(
 	}
 
 	git_iterator_free(i);
+	git_index_free(index);
 
 	cl_assert_equal_i(expected_count, count);
 }
@@ -533,7 +538,7 @@ static void workdir_iterator_test(
 	int count = 0, count_all = 0, count_all_post_reset = 0;
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
-	cl_git_pass(git_iterator_for_workdir_range(&i, repo, start, end));
+	cl_git_pass(git_iterator_for_workdir_range(&i, repo, 0, start, end));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
@@ -731,7 +736,7 @@ void test_diff_iterator__workdir_builtin_ignores(void)
 	cl_git_mkfile("attr/sub/.git", "whatever");
 
 	cl_git_pass(
-		git_iterator_for_workdir_range(&i, repo, "dir", "sub/sub/file"));
+		git_iterator_for_workdir_range(&i, repo, 0, "dir", "sub/sub/file"));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	for (idx = 0; entry != NULL; ++idx) {
@@ -750,3 +755,155 @@ void test_diff_iterator__workdir_builtin_ignores(void)
 
 	git_iterator_free(i);
 }
+
+static void check_wd_first_through_third_range(
+	git_repository *repo, const char *start, const char *end)
+{
+	git_iterator *i;
+	const git_index_entry *entry;
+	int idx;
+	static const char *expected[] = { "FIRST", "second", "THIRD", NULL };
+
+	cl_git_pass(git_iterator_for_workdir_range(
+		&i, repo, GIT_ITERATOR_IGNORE_CASE, start, end));
+	cl_git_pass(git_iterator_current(i, &entry));
+
+	for (idx = 0; entry != NULL; ++idx) {
+		cl_assert_equal_s(expected[idx], entry->path);
+
+		if (S_ISDIR(entry->mode))
+			cl_git_pass(git_iterator_advance_into_directory(i, &entry));
+		else
+			cl_git_pass(git_iterator_advance(i, &entry));
+	}
+
+	cl_assert(expected[idx] == NULL);
+
+	git_iterator_free(i);
+}
+
+void test_diff_iterator__workdir_handles_icase_range(void)
+{
+	git_repository *repo;
+
+	repo = cl_git_sandbox_init("empty_standard_repo");
+	cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt");
+
+	cl_git_mkfile("empty_standard_repo/before", "whatever\n");
+	cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n");
+	cl_git_mkfile("empty_standard_repo/second", "whatever\n");
+	cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n");
+	cl_git_mkfile("empty_standard_repo/zafter", "whatever\n");
+	cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n");
+
+	check_wd_first_through_third_range(repo, "first", "third");
+	check_wd_first_through_third_range(repo, "FIRST", "THIRD");
+	check_wd_first_through_third_range(repo, "first", "THIRD");
+	check_wd_first_through_third_range(repo, "FIRST", "third");
+	check_wd_first_through_third_range(repo, "FirSt", "tHiRd");
+}
+
+static void check_tree_range(
+	git_repository *repo,
+	const char *start,
+	const char *end,
+	bool ignore_case,
+	int expected_count)
+{
+	git_tree *head;
+	git_iterator *i;
+	const git_index_entry *entry;
+	int count;
+
+	cl_git_pass(git_repository_head_tree(&head, repo));
+
+	cl_git_pass(git_iterator_for_tree_range(
+		&i, head,
+		ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE,
+		start, end));
+
+	cl_git_pass(git_iterator_current(i, &entry));
+
+	for (count = 0; entry != NULL; ) {
+		++count;
+		cl_git_pass(git_iterator_advance(i, &entry));
+	}
+
+	cl_assert_equal_i(expected_count, count);
+
+	git_iterator_free(i);
+	git_tree_free(head);
+}
+
+void test_diff_iterator__tree_handles_icase_range(void)
+{
+	git_repository *repo;
+
+	repo = cl_git_sandbox_init("testrepo");
+
+	check_tree_range(repo, "B", "C", false, 0);
+	check_tree_range(repo, "B", "C", true, 1);
+	check_tree_range(repo, "a", "z", false, 3);
+	check_tree_range(repo, "a", "z", true, 4);
+}
+
+static void check_index_range(
+	git_repository *repo,
+	const char *start,
+	const char *end,
+	bool ignore_case,
+	int expected_count)
+{
+	git_index *index;
+	git_iterator *i;
+	const git_index_entry *entry;
+	int count, caps;
+	bool is_ignoring_case;
+
+	cl_git_pass(git_repository_index(&index, repo));
+
+	caps = git_index_caps(index);
+	is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0);
+
+	if (ignore_case != is_ignoring_case)
+		cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE));
+
+	cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end));
+
+	cl_assert(git_iterator_ignore_case(i) == ignore_case);
+
+	cl_git_pass(git_iterator_current(i, &entry));
+
+	for (count = 0; entry != NULL; ) {
+		++count;
+		cl_git_pass(git_iterator_advance(i, &entry));
+	}
+
+	cl_assert_equal_i(expected_count, count);
+
+	git_iterator_free(i);
+	git_index_free(index);
+}
+
+void test_diff_iterator__index_handles_icase_range(void)
+{
+	git_repository *repo;
+	git_index *index;
+	git_tree *head;
+
+	repo = cl_git_sandbox_init("testrepo");
+
+	/* reset index to match HEAD */
+	cl_git_pass(git_repository_head_tree(&head, repo));
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_index_read_tree(index, head));
+	cl_git_pass(git_index_write(index));
+	git_tree_free(head);
+	git_index_free(index);
+
+	/* do some ranged iterator checks toggling case sensitivity */
+	check_index_range(repo, "B", "C", false, 0);
+	check_index_range(repo, "B", "C", true, 1);
+	check_index_range(repo, "a", "z", false, 3);
+	check_index_range(repo, "a", "z", true, 4);
+}
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 6786b91..ead1bc7 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -580,3 +580,90 @@ void test_status_worktree__conflicted_item(void)
 	git_index_free(index);
 }
 
+static void stage_and_commit(git_repository *repo, const char *path)
+{
+	git_oid tree_oid, commit_oid;
+	git_tree *tree;
+	git_signature *signature;
+	git_index *index;
+
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_index_add_from_workdir(index, path));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_index_write_tree(&tree_oid, index));
+	git_index_free(index);
+
+	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
+
+	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
+
+	cl_git_pass(git_commit_create_v(
+		&commit_oid,
+		repo,
+		"HEAD",
+		signature,
+		signature,
+		NULL,
+		"Initial commit\n\0",
+		tree,
+		0));
+
+	git_tree_free(tree);
+	git_signature_free(signature);
+}
+
+static void assert_ignore_case(
+	bool should_ignore_case,
+	int expected_lower_cased_file_status,
+	int expected_camel_cased_file_status)
+{
+	git_config *config;
+	unsigned int status;
+	git_buf lower_case_path = GIT_BUF_INIT, camel_case_path = GIT_BUF_INIT;
+	git_repository *repo, *repo2;
+
+	repo = cl_git_sandbox_init("empty_standard_repo");
+	cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt");
+
+	cl_git_pass(git_repository_config(&config, repo));
+	cl_git_pass(git_config_set_bool(config, "core.ignorecase", should_ignore_case));
+	git_config_free(config);
+
+	cl_git_pass(git_buf_joinpath(&lower_case_path,
+		git_repository_workdir(repo), "plop"));
+
+	cl_git_mkfile(git_buf_cstr(&lower_case_path), "");
+
+	stage_and_commit(repo, "plop");
+
+	cl_git_pass(git_repository_open(&repo2, "./empty_standard_repo"));
+
+	cl_git_pass(git_status_file(&status, repo2, "plop"));
+	cl_assert_equal_i(GIT_STATUS_CURRENT, status);
+
+	cl_git_pass(git_buf_joinpath(&camel_case_path,
+		git_repository_workdir(repo), "Plop"));
+
+	cl_git_pass(p_rename(git_buf_cstr(&lower_case_path), git_buf_cstr(&camel_case_path)));
+
+	cl_git_pass(git_status_file(&status, repo2, "plop"));
+	cl_assert_equal_i(expected_lower_cased_file_status, status);
+
+	cl_git_pass(git_status_file(&status, repo2, "Plop"));
+	cl_assert_equal_i(expected_camel_cased_file_status, status);
+
+	git_repository_free(repo2);
+	git_buf_free(&lower_case_path);
+	git_buf_free(&camel_case_path);
+}
+
+void test_status_worktree__file_status_honors_core_ignorecase_true(void)
+{
+	assert_ignore_case(true, GIT_STATUS_CURRENT, GIT_STATUS_CURRENT);
+}
+
+void test_status_worktree__file_status_honors_core_ignorecase_false(void)
+{
+	assert_ignore_case(false, GIT_STATUS_WT_DELETED, GIT_STATUS_WT_NEW);
+}