Commit 28fd7206b1359b6564bad3c66382b47a8f2e3eb1

Vicent Marti 2014-04-18T12:33:19

Merge pull request #2108 from libgit2/rb/threadsafe-index-iterator Make index iterator thread safe

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
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 23c5af1..918e5b8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -305,6 +305,11 @@ ELSE ()
 	ENDIF ()
 	IF (APPLE) # Apple deprecated OpenSSL
 		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
+
+		# With clang, disable some annoying extra warnings
+		IF (NOT CMAKE_COMPILER_IS_GNUCC)
+			SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-const-variable -Wno-unused-function")
+		ENDIF()
 	ENDIF ()
 	IF (PROFILE)
 		SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
diff --git a/include/git2/index.h b/include/git2/index.h
index dd6a28e..05e58a6 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -77,7 +77,12 @@ typedef struct git_index_entry {
 #define GIT_IDXENTRY_VALID     (0x8000)
 #define GIT_IDXENTRY_STAGESHIFT 12
 
-#define GIT_IDXENTRY_STAGE(E) (((E)->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT)
+#define GIT_IDXENTRY_STAGE(E) \
+	(((E)->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT)
+
+#define GIT_IDXENTRY_STAGE_SET(E,S) do { \
+	(E)->flags = ((E)->flags & ~GIT_IDXENTRY_STAGEMASK) | \
+		(((S) & 0x03) << GIT_IDXENTRY_STAGESHIFT); } while (0)
 
 /**
  * Bitmasks for on-disk fields of `git_index_entry`'s `flags_extended`
@@ -327,12 +332,14 @@ GIT_EXTERN(size_t) git_index_entrycount(const git_index *index);
 
 /**
  * Clear the contents (all the entries) of an index object.
- * This clears the index object in memory; changes must be manually
- * written to disk for them to take effect.
+ *
+ * This clears the index object in memory; changes must be explicitly
+ * written to disk for them to take effect persistently.
  *
  * @param index an existing index object
+ * @return 0 on success, error code < 0 on failure
  */
-GIT_EXTERN(void) git_index_clear(git_index *index);
+GIT_EXTERN(int) git_index_clear(git_index *index);
 
 /**
  * Get a pointer to one of the entries in the index
@@ -568,8 +575,7 @@ GIT_EXTERN(int) git_index_update_all(
  * @param at_pos the address to which the position of the index entry is written (optional)
  * @param index an existing index object
  * @param path path to search
- * @return a zero-based position in the index if found;
- * GIT_ENOTFOUND otherwise
+ * @return a zero-based position in the index if found; GIT_ENOTFOUND otherwise
  */
 GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *path);
 
@@ -613,6 +619,7 @@ GIT_EXTERN(int) git_index_conflict_add(
  * @param their_out Pointer to store the their entry
  * @param index an existing index object
  * @param path path to search
+ * @return 0 or an error code
  */
 GIT_EXTERN(int) git_index_conflict_get(
 	const git_index_entry **ancestor_out,
@@ -625,16 +632,18 @@ GIT_EXTERN(int) git_index_conflict_get(
  * Removes the index entries that represent a conflict of a single file.
  *
  * @param index an existing index object
- * @param path to search
+ * @param path path to remove conflicts for
+ * @return 0 or an error code
  */
 GIT_EXTERN(int) git_index_conflict_remove(git_index *index, const char *path);
 
 /**
- * Remove all conflicts in the index (entries with a stage greater than 0.)
+ * Remove all conflicts in the index (entries with a stage greater than 0).
  *
  * @param index an existing index object
+ * @return 0 or an error code
  */
-GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index);
+GIT_EXTERN(int) git_index_conflict_cleanup(git_index *index);
 
 /**
  * Determine if the index contains entries representing file conflicts.
@@ -644,9 +653,12 @@ GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index);
 GIT_EXTERN(int) git_index_has_conflicts(const git_index *index);
 
 /**
- * Create an iterator for the conflicts in the index.  You may not modify the
- * index while iterating, the results are undefined.
+ * Create an iterator for the conflicts in the index.
+ *
+ * The index must not be modified while iterating; the results are undefined.
  *
+ * @param iterator_out The newly created conflict iterator
+ * @param index The index to scan
  * @return 0 or an error code
  */
 GIT_EXTERN(int) git_index_conflict_iterator_new(
diff --git a/include/git2/sys/diff.h b/include/git2/sys/diff.h
new file mode 100644
index 0000000..bc6cdf3
--- /dev/null
+++ b/include/git2/sys/diff.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sys_git_diff_h__
+#define INCLUDE_sys_git_diff_h__
+
+#include "git2/common.h"
+#include "git2/types.h"
+#include "git2/oid.h"
+
+/**
+ * @file git2/sys/diff.h
+ * @brief Low-level Git diff utilities
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Diff print callback that writes to a git_buf.
+ *
+ * This function is provided not for you to call it directly, but instead
+ * so you can use it as a function pointer to the `git_diff_print` or
+ * `git_patch_print` APIs.  When using those APIs, you specify a callback
+ * to actually handle the diff and/or patch data.
+ *
+ * Use this callback to easily write that data to a `git_buf` buffer.  You
+ * must pass a `git_buf *` value as the payload to the `git_diff_print`
+ * and/or `git_patch_print` function.  The data will be appended to the
+ * buffer (after any existing content).
+ */
+GIT_EXTERN(int) git_diff_print_callback__to_buf(
+	const git_diff_delta *delta,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
+	void *payload); /*< payload must be a `git_buf *` */
+
+/**
+ * Diff print callback that writes to stdio FILE handle.
+ *
+ * This function is provided not for you to call it directly, but instead
+ * so you can use it as a function pointer to the `git_diff_print` or
+ * `git_patch_print` APIs.  When using those APIs, you specify a callback
+ * to actually handle the diff and/or patch data.
+ *
+ * Use this callback to easily write that data to a stdio FILE handle.  You
+ * must pass a `FILE *` value (such as `stdout` or `stderr` or the return
+ * value from `fopen()`) as the payload to the `git_diff_print`
+ * and/or `git_patch_print` function.  If you pass NULL, this will write
+ * data to `stdout`.
+ */
+GIT_EXTERN(int) git_diff_print_callback__to_file_handle(
+	const git_diff_delta *delta,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
+	void *payload); /*< payload must be a `FILE *` */
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/attr.c b/src/attr.c
index d8a171d..6228743 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -2,7 +2,7 @@
 #include "repository.h"
 #include "sysdir.h"
 #include "config.h"
-#include "attr.h"
+#include "attr_file.h"
 #include "ignore.h"
 #include "git2/oid.h"
 #include <ctype.h>
@@ -33,6 +33,7 @@ static int collect_attr_files(
 	const char *path,
 	git_vector *files);
 
+static void release_attr_files(git_vector *files);
 
 int git_attr_get(
 	const char **value,
@@ -59,6 +60,7 @@ int git_attr_get(
 	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
 		goto cleanup;
 
+	memset(&attr, 0, sizeof(attr));
 	attr.name = name;
 	attr.name_hash = git_attr_file__name_hash(name);
 
@@ -76,7 +78,7 @@ int git_attr_get(
 	}
 
 cleanup:
-	git_vector_free(&files);
+	release_attr_files(&files);
 	git_attr_path__free(&path);
 
 	return error;
@@ -152,7 +154,7 @@ int git_attr_get_many(
 	}
 
 cleanup:
-	git_vector_free(&files);
+	release_attr_files(&files);
 	git_attr_path__free(&path);
 	git__free(info);
 
@@ -181,12 +183,10 @@ int git_attr_foreach(
 	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
 		return -1;
 
-	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
+	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0 ||
+		(error = git_strmap_alloc(&seen)) < 0)
 		goto cleanup;
 
-	seen = git_strmap_alloc();
-	GITERR_CHECK_ALLOC(seen);
-
 	git_vector_foreach(&files, i, file) {
 
 		git_attr_file__foreach_matching_rule(file, &path, j, rule) {
@@ -211,13 +211,12 @@ int git_attr_foreach(
 
 cleanup:
 	git_strmap_free(seen);
-	git_vector_free(&files);
+	release_attr_files(&files);
 	git_attr_path__free(&path);
 
 	return error;
 }
 
-
 int git_attr_add_macro(
 	git_repository *repo,
 	const char *name,
@@ -252,237 +251,6 @@ int git_attr_add_macro(
 	return error;
 }
 
-bool git_attr_cache__is_cached(
-	git_repository *repo, git_attr_file_source source, const char *path)
-{
-	git_buf cache_key = GIT_BUF_INIT;
-	git_strmap *files = git_repository_attr_cache(repo)->files;
-	const char *workdir = git_repository_workdir(repo);
-	bool rval;
-
-	if (workdir && git__prefixcmp(path, workdir) == 0)
-		path += strlen(workdir);
-	if (git_buf_printf(&cache_key, "%d#%s", (int)source, path) < 0)
-		return false;
-
-	rval = git_strmap_exists(files, git_buf_cstr(&cache_key));
-
-	git_buf_free(&cache_key);
-
-	return rval;
-}
-
-static int load_attr_file(
-	const char **data,
-	git_futils_filestamp *stamp,
-	const char *filename)
-{
-	int error;
-	git_buf content = GIT_BUF_INIT;
-
-	error = git_futils_filestamp_check(stamp, filename);
-	if (error < 0)
-		return error;
-
-	/* if error == 0, then file is up to date. By returning GIT_ENOTFOUND,
-	 * we tell the caller not to reparse this file...
-	 */
-	if (!error)
-		return GIT_ENOTFOUND;
-
-	error = git_futils_readbuffer(&content, filename);
-	if (error < 0) {
-		/* convert error into ENOTFOUND so failed permissions / invalid
-		 * file type don't actually stop the operation in progress.
-		 */
-		return GIT_ENOTFOUND;
-
-		/* TODO: once warnings are available, issue a warning callback */
-	}
-
-	*data = git_buf_detach(&content);
-
-	return 0;
-}
-
-static int load_attr_blob_from_index(
-	const char **content,
-	git_blob **blob,
-	git_repository *repo,
-	const git_oid *old_oid,
-	const char *relfile)
-{
-	int error;
-	size_t pos;
-	git_index *index;
-	const git_index_entry *entry;
-
-	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
-		(error = git_index_find(&pos, index, relfile)) < 0)
-		return error;
-
-	entry = git_index_get_byindex(index, pos);
-
-	if (old_oid && git_oid__cmp(old_oid, &entry->id) == 0)
-		return GIT_ENOTFOUND;
-
-	if ((error = git_blob_lookup(blob, repo, &entry->id)) < 0)
-		return error;
-
-	*content = git_blob_rawcontent(*blob);
-	return 0;
-}
-
-static int load_attr_from_cache(
-	git_attr_file **file,
-	git_attr_cache *cache,
-	git_attr_file_source source,
-	const char *relative_path)
-{
-	git_buf  cache_key = GIT_BUF_INIT;
-	khiter_t cache_pos;
-
-	*file = NULL;
-
-	if (!cache || !cache->files)
-		return 0;
-
-	if (git_buf_printf(&cache_key, "%d#%s", (int)source, relative_path) < 0)
-		return -1;
-
-	cache_pos = git_strmap_lookup_index(cache->files, cache_key.ptr);
-
-	git_buf_free(&cache_key);
-
-	if (git_strmap_valid_index(cache->files, cache_pos))
-		*file = git_strmap_value_at(cache->files, cache_pos);
-
-	return 0;
-}
-
-int git_attr_cache__internal_file(
-	git_repository *repo,
-	const char *filename,
-	git_attr_file **file)
-{
-	int error = 0;
-	git_attr_cache *cache = git_repository_attr_cache(repo);
-	khiter_t cache_pos = git_strmap_lookup_index(cache->files, filename);
-
-	if (git_strmap_valid_index(cache->files, cache_pos)) {
-		*file = git_strmap_value_at(cache->files, cache_pos);
-		return 0;
-	}
-
-	if (git_attr_file__new(file, 0, filename, &cache->pool) < 0)
-		return -1;
-
-	git_strmap_insert(cache->files, (*file)->key + 2, *file, error);
-	if (error > 0)
-		error = 0;
-
-	return error;
-}
-
-int git_attr_cache__push_file(
-	git_repository *repo,
-	const char *base,
-	const char *filename,
-	git_attr_file_source source,
-	git_attr_file_parser parse,
-	void* parsedata,
-	git_vector *stack)
-{
-	int error = 0;
-	git_buf path = GIT_BUF_INIT;
-	const char *workdir = git_repository_workdir(repo);
-	const char *relfile, *content = NULL;
-	git_attr_cache *cache = git_repository_attr_cache(repo);
-	git_attr_file *file = NULL;
-	git_blob *blob = NULL;
-	git_futils_filestamp stamp;
-
-	assert(filename && stack);
-
-	/* join base and path as needed */
-	if (base != NULL && git_path_root(filename) < 0) {
-		if (git_buf_joinpath(&path, base, filename) < 0)
-			return -1;
-		filename = path.ptr;
-	}
-
-	relfile = filename;
-	if (workdir && git__prefixcmp(relfile, workdir) == 0)
-		relfile += strlen(workdir);
-
-	/* check cache */
-	if (load_attr_from_cache(&file, cache, source, relfile) < 0)
-		return -1;
-
-	/* if not in cache, load data, parse, and cache */
-
-	if (source == GIT_ATTR_FILE_FROM_FILE) {
-		git_futils_filestamp_set(
-			&stamp, file ? &file->cache_data.stamp : NULL);
-
-		error = load_attr_file(&content, &stamp, filename);
-	} else {
-		error = load_attr_blob_from_index(&content, &blob,
-			repo, file ? &file->cache_data.oid : NULL, relfile);
-	}
-
-	if (error) {
-		/* not finding a file is not an error for this function */
-		if (error == GIT_ENOTFOUND) {
-			giterr_clear();
-			error = 0;
-		}
-		goto finish;
-	}
-
-	/* if we got here, we have to parse and/or reparse the file */
-	if (file)
-		git_attr_file__clear_rules(file);
-	else {
-		error = git_attr_file__new(&file, source, relfile, &cache->pool);
-		if (error < 0)
-			goto finish;
-	}
-
-	if (parse && (error = parse(repo, parsedata, content, file)) < 0)
-		goto finish;
-
-	git_strmap_insert(cache->files, file->key, file, error); //-V595
-	if (error > 0)
-		error = 0;
-
-	/* remember "cache buster" file signature */
-	if (blob)
-		git_oid_cpy(&file->cache_data.oid, git_object_id((git_object *)blob));
-	else
-		git_futils_filestamp_set(&file->cache_data.stamp, &stamp);
-
-finish:
-	/* push file onto vector if we found one*/
-	if (!error && file != NULL)
-		error = git_vector_insert(stack, file);
-
-	if (error != 0)
-		git_attr_file__free(file);
-
-	if (blob)
-		git_blob_free(blob);
-	else
-		git__free((void *)content);
-
-	git_buf_free(&path);
-
-	return error;
-}
-
-#define push_attr_file(R,S,B,F) \
-	git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,git_attr_file__parse_buffer,NULL,(S))
-
 typedef struct {
 	git_repository *repo;
 	uint32_t flags;
@@ -491,7 +259,7 @@ typedef struct {
 	git_vector *files;
 } attr_walk_up_info;
 
-int git_attr_cache__decide_sources(
+static int attr_decide_sources(
 	uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
 {
 	int count = 0;
@@ -499,42 +267,76 @@ int git_attr_cache__decide_sources(
 	switch (flags & 0x03) {
 	case GIT_ATTR_CHECK_FILE_THEN_INDEX:
 		if (has_wd)
-			srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
 		if (has_index)
-			srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
 		break;
 	case GIT_ATTR_CHECK_INDEX_THEN_FILE:
 		if (has_index)
-			srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
 		if (has_wd)
-			srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
 		break;
 	case GIT_ATTR_CHECK_INDEX_ONLY:
 		if (has_index)
-			srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
 		break;
 	}
 
 	return count;
 }
 
+static int push_attr_file(
+	git_repository *repo,
+	git_vector *list,
+	git_attr_file_source source,
+	const char *base,
+	const char *filename)
+{
+	int error = 0;
+	git_attr_file *file = NULL;
+
+	error = git_attr_cache__get(
+		&file, repo, source, base, filename, git_attr_file__parse_buffer);
+	if (error < 0)
+		return error;
+
+	if (file != NULL) {
+		if ((error = git_vector_insert(list, file)) < 0)
+			git_attr_file__free(file);
+	}
+
+	return error;
+}
+
 static int push_one_attr(void *ref, git_buf *path)
 {
 	int error = 0, n_src, i;
 	attr_walk_up_info *info = (attr_walk_up_info *)ref;
 	git_attr_file_source src[2];
 
-	n_src = git_attr_cache__decide_sources(
+	n_src = attr_decide_sources(
 		info->flags, info->workdir != NULL, info->index != NULL, src);
 
 	for (i = 0; !error && i < n_src; ++i)
-		error = git_attr_cache__push_file(
-			info->repo, path->ptr, GIT_ATTR_FILE, src[i],
-			git_attr_file__parse_buffer, NULL, info->files);
+		error = push_attr_file(
+			info->repo, info->files, src[i], path->ptr, GIT_ATTR_FILE);
 
 	return error;
 }
 
+static void release_attr_files(git_vector *files)
+{
+	size_t i;
+	git_attr_file *file;
+
+	git_vector_foreach(files, i, file) {
+		git_attr_file__free(file);
+		files->contents[i] = NULL;
+	}
+	git_vector_free(files);
+}
+
 static int collect_attr_files(
 	git_repository *repo,
 	uint32_t flags,
@@ -546,9 +348,8 @@ static int collect_attr_files(
 	const char *workdir = git_repository_workdir(repo);
 	attr_walk_up_info info = { NULL };
 
-	if (git_attr_cache__init(repo) < 0 ||
-		git_vector_init(files, 4, NULL) < 0)
-		return -1;
+	if ((error = git_attr_cache__init(repo)) < 0)
+		return error;
 
 	/* Resolve path in a non-bare repo */
 	if (workdir != NULL)
@@ -566,7 +367,8 @@ static int collect_attr_files(
 	 */
 
 	error = push_attr_file(
-		repo, files, git_repository_path(repo), GIT_ATTR_FILE_INREPO);
+		repo, files, GIT_ATTR_FILE__FROM_FILE,
+		git_repository_path(repo), GIT_ATTR_FILE_INREPO);
 	if (error < 0)
 		goto cleanup;
 
@@ -583,7 +385,8 @@ static int collect_attr_files(
 
 	if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
 		error = push_attr_file(
-			repo, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
+			repo, files, GIT_ATTR_FILE__FROM_FILE,
+			NULL, git_repository_attr_cache(repo)->cfg_attr_file);
 		if (error < 0)
 			goto cleanup;
 	}
@@ -591,7 +394,8 @@ static int collect_attr_files(
 	if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
 		error = git_sysdir_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
 		if (!error)
-			error = push_attr_file(repo, files, NULL, dir.ptr);
+			error = push_attr_file(
+				repo, files, GIT_ATTR_FILE__FROM_FILE, NULL, dir.ptr);
 		else if (error == GIT_ENOTFOUND) {
 			giterr_clear();
 			error = 0;
@@ -600,153 +404,8 @@ static int collect_attr_files(
 
  cleanup:
 	if (error < 0)
-		git_vector_free(files);
+		release_attr_files(files);
 	git_buf_free(&dir);
 
 	return error;
 }
-
-static int attr_cache__lookup_path(
-	char **out, git_config *cfg, const char *key, const char *fallback)
-{
-	git_buf buf = GIT_BUF_INIT;
-	int error;
-	const git_config_entry *entry = NULL;
-
-	*out = NULL;
-
-	if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
-		return error;
-
-	if (entry) {
-		const char *cfgval = entry->value;
-
-		/* expand leading ~/ as needed */
-		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
-			!git_sysdir_find_global_file(&buf, &cfgval[2]))
-			*out = git_buf_detach(&buf);
-		else if (cfgval)
-			*out = git__strdup(cfgval);
-
-	}
-	else if (!git_sysdir_find_xdg_file(&buf, fallback))
-		*out = git_buf_detach(&buf);
-
-	git_buf_free(&buf);
-
-	return error;
-}
-
-int git_attr_cache__init(git_repository *repo)
-{
-	int ret;
-	git_attr_cache *cache = git_repository_attr_cache(repo);
-	git_config *cfg;
-
-	if (cache->initialized)
-		return 0;
-
-	/* cache config settings for attributes and ignores */
-	if (git_repository_config__weakptr(&cfg, repo) < 0)
-		return -1;
-
-	ret = attr_cache__lookup_path(
-		&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
-	if (ret < 0)
-		return ret;
-
-	ret = attr_cache__lookup_path(
-		&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
-	if (ret < 0)
-		return ret;
-
-	/* allocate hashtable for attribute and ignore file contents */
-	if (cache->files == NULL) {
-		cache->files = git_strmap_alloc();
-		GITERR_CHECK_ALLOC(cache->files);
-	}
-
-	/* allocate hashtable for attribute macros */
-	if (cache->macros == NULL) {
-		cache->macros = git_strmap_alloc();
-		GITERR_CHECK_ALLOC(cache->macros);
-	}
-
-	/* allocate string pool */
-	if (git_pool_init(&cache->pool, 1, 0) < 0)
-		return -1;
-
-	cache->initialized = 1;
-
-	/* insert default macros */
-	return git_attr_add_macro(repo, "binary", "-diff -crlf -text");
-}
-
-void git_attr_cache_flush(
-	git_repository *repo)
-{
-	git_attr_cache *cache;
-
-	if (!repo)
-		return;
-
-	cache = git_repository_attr_cache(repo);
-
-	if (cache->files != NULL) {
-		git_attr_file *file;
-
-		git_strmap_foreach_value(cache->files, file, {
-			git_attr_file__free(file);
-		});
-
-		git_strmap_free(cache->files);
-	}
-
-	if (cache->macros != NULL) {
-		git_attr_rule *rule;
-
-		git_strmap_foreach_value(cache->macros, rule, {
-			git_attr_rule__free(rule);
-		});
-
-		git_strmap_free(cache->macros);
-	}
-
-	git_pool_clear(&cache->pool);
-
-	git__free(cache->cfg_attr_file);
-	cache->cfg_attr_file = NULL;
-
-	git__free(cache->cfg_excl_file);
-	cache->cfg_excl_file = NULL;
-
-	cache->initialized = 0;
-}
-
-int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
-{
-	git_strmap *macros = git_repository_attr_cache(repo)->macros;
-	int error;
-
-	/* TODO: generate warning log if (macro->assigns.length == 0) */
-	if (macro->assigns.length == 0)
-		return 0;
-
-	git_strmap_insert(macros, macro->match.pattern, macro, error);
-	return (error < 0) ? -1 : 0;
-}
-
-git_attr_rule *git_attr_cache__lookup_macro(
-	git_repository *repo, const char *name)
-{
-	git_strmap *macros = git_repository_attr_cache(repo)->macros;
-	khiter_t pos;
-
-	pos = git_strmap_lookup_index(macros, name);
-
-	if (!git_strmap_valid_index(macros, pos))
-		return NULL;
-
-	return (git_attr_rule *)git_strmap_value_at(macros, pos);
-}
-
diff --git a/src/attr.h b/src/attr.h
index 19c979b..f9f216d 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -8,38 +8,6 @@
 #define INCLUDE_attr_h__
 
 #include "attr_file.h"
-
-#define GIT_ATTR_CONFIG       "core.attributesfile"
-#define GIT_IGNORE_CONFIG     "core.excludesfile"
-
-typedef int (*git_attr_file_parser)(
-	git_repository *, void *, const char *, git_attr_file *);
-
-extern int git_attr_cache__insert_macro(
-	git_repository *repo, git_attr_rule *macro);
-
-extern git_attr_rule *git_attr_cache__lookup_macro(
-	git_repository *repo, const char *name);
-
-extern int git_attr_cache__push_file(
-	git_repository *repo,
-	const char *base,
-	const char *filename,
-	git_attr_file_source source,
-	git_attr_file_parser parse,
-	void *parsedata, /* passed through to parse function */
-	git_vector *stack);
-
-extern int git_attr_cache__internal_file(
-	git_repository *repo,
-	const char *key,
-	git_attr_file **file_ptr);
-
-/* returns true if path is in cache */
-extern bool git_attr_cache__is_cached(
-	git_repository *repo, git_attr_file_source source, const char *path);
-
-extern int git_attr_cache__decide_sources(
-	uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs);
+#include "attrcache.h"
 
 #endif
diff --git a/src/attr_file.c b/src/attr_file.c
index ea92336..65bbf78 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -1,85 +1,239 @@
 #include "common.h"
 #include "repository.h"
 #include "filebuf.h"
-#include "attr.h"
+#include "attr_file.h"
+#include "attrcache.h"
 #include "git2/blob.h"
 #include "git2/tree.h"
+#include "index.h"
 #include <ctype.h>
 
-static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
-static void git_attr_rule__clear(git_attr_rule *rule);
-static bool parse_optimized_patterns(
-	git_attr_fnmatch *spec,
-	git_pool *pool,
-	const char *pattern);
+static void attr_file_free(git_attr_file *file)
+{
+	bool unlock = !git_mutex_lock(&file->lock);
+	git_attr_file__clear_rules(file, false);
+	git_pool_clear(&file->pool);
+	if (unlock)
+		git_mutex_unlock(&file->lock);
+	git_mutex_free(&file->lock);
+
+	git__memzero(file, sizeof(*file));
+	git__free(file);
+}
 
 int git_attr_file__new(
-	git_attr_file **attrs_ptr,
-	git_attr_file_source from,
-	const char *path,
-	git_pool *pool)
+	git_attr_file **out,
+	git_attr_file_entry *entry,
+	git_attr_file_source source)
 {
-	git_attr_file *attrs = NULL;
-
-	attrs = git__calloc(1, sizeof(git_attr_file));
+	git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
 	GITERR_CHECK_ALLOC(attrs);
 
-	if (pool)
-		attrs->pool = pool;
-	else {
-		attrs->pool = git__calloc(1, sizeof(git_pool));
-		if (!attrs->pool || git_pool_init(attrs->pool, 1, 0) < 0)
-			goto fail;
-		attrs->pool_is_allocated = true;
+	if (git_mutex_init(&attrs->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to initialize lock");
+		git__free(attrs);
+		return -1;
 	}
 
-	if (path) {
-		size_t len = strlen(path);
+	if (git_pool_init(&attrs->pool, 1, 0) < 0) {
+		attr_file_free(attrs);
+		return -1;
+	}
+
+	GIT_REFCOUNT_INC(attrs);
+	attrs->entry  = entry;
+	attrs->source = source;
+	*out = attrs;
+	return 0;
+}
 
-		attrs->key = git_pool_malloc(attrs->pool, (uint32_t)len + 3);
-		GITERR_CHECK_ALLOC(attrs->key);
+int git_attr_file__clear_rules(git_attr_file *file, bool need_lock)
+{
+	unsigned int i;
+	git_attr_rule *rule;
 
-		attrs->key[0] = '0' + (char)from;
-		attrs->key[1] = '#';
-		memcpy(&attrs->key[2], path, len);
-		attrs->key[len + 2] = '\0';
+	if (need_lock && git_mutex_lock(&file->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock attribute file");
+		return -1;
 	}
 
-	if (git_vector_init(&attrs->rules, 4, NULL) < 0)
-		goto fail;
+	git_vector_foreach(&file->rules, i, rule)
+		git_attr_rule__free(rule);
+	git_vector_free(&file->rules);
+
+	if (need_lock)
+		git_mutex_unlock(&file->lock);
 
-	*attrs_ptr = attrs;
 	return 0;
+}
 
-fail:
-	git_attr_file__free(attrs);
-	attrs_ptr = NULL;
-	return -1;
+void git_attr_file__free(git_attr_file *file)
+{
+	if (!file)
+		return;
+	GIT_REFCOUNT_DEC(file, attr_file_free);
 }
 
-int git_attr_file__parse_buffer(
-	git_repository *repo, void *parsedata, const char *buffer, git_attr_file *attrs)
+static int attr_file_oid_from_index(
+	git_oid *oid, git_repository *repo, const char *path)
+{
+	int error;
+	git_index *idx;
+	size_t pos;
+	const git_index_entry *entry;
+
+	if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
+		(error = git_index__find_pos(&pos, idx, path, 0, 0)) < 0)
+		return error;
+
+	if (!(entry = git_index_get_byindex(idx, pos)))
+		return GIT_ENOTFOUND;
+
+	*oid = entry->id;
+	return 0;
+}
+
+int git_attr_file__load(
+	git_attr_file **out,
+	git_repository *repo,
+	git_attr_file_entry *entry,
+	git_attr_file_source source,
+	git_attr_file_parser parser)
 {
 	int error = 0;
-	const char *scan = NULL, *context = NULL;
-	git_attr_rule *rule = NULL;
+	git_blob *blob = NULL;
+	git_buf content = GIT_BUF_INIT;
+	const char *data = NULL;
+	git_attr_file *file;
+	struct stat st;
+
+	*out = NULL;
+
+	switch (source) {
+	case GIT_ATTR_FILE__IN_MEMORY:
+		/* in-memory attribute file doesn't need data */
+		break;
+	case GIT_ATTR_FILE__FROM_INDEX: {
+		git_oid id;
+
+		if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
+			(error = git_blob_lookup(&blob, repo, &id)) < 0)
+			return error;
+
+		data = git_blob_rawcontent(blob);
+		break;
+	}
+	case GIT_ATTR_FILE__FROM_FILE: {
+		int fd;
+
+		if (p_stat(entry->fullpath, &st) < 0)
+			return git_path_set_error(errno, entry->fullpath, "stat");
+		if (S_ISDIR(st.st_mode))
+			return GIT_ENOTFOUND;
+
+		/* For open or read errors, return ENOTFOUND to skip item */
+		/* TODO: issue warning when warning API is available */
+
+		if ((fd = git_futils_open_ro(entry->fullpath)) < 0)
+			return GIT_ENOTFOUND;
+
+		error = git_futils_readbuffer_fd(&content, fd, (size_t)st.st_size);
+		p_close(fd);
 
-	GIT_UNUSED(parsedata);
+		if (error < 0)
+			return GIT_ENOTFOUND;
 
-	assert(buffer && attrs);
+		data = content.ptr;
+		break;
+	}
+	default:
+		giterr_set(GITERR_INVALID, "Unknown file source %d", source);
+		return -1;
+	}
 
-	scan = buffer;
+	if ((error = git_attr_file__new(&file, entry, source)) < 0)
+		goto cleanup;
+
+	if (parser && (error = parser(repo, file, data)) < 0) {
+		git_attr_file__free(file);
+		goto cleanup;
+	}
+
+	/* write cache breaker */
+	if (source == GIT_ATTR_FILE__FROM_INDEX)
+		git_oid_cpy(&file->cache_data.oid, git_blob_id(blob));
+	else if (source == GIT_ATTR_FILE__FROM_FILE)
+		git_futils_filestamp_set_from_stat(&file->cache_data.stamp, &st);
+	/* else always cacheable */
+
+	*out = file;
+
+cleanup:
+	git_blob_free(blob);
+	git_buf_free(&content);
+
+	return error;
+}
+
+int git_attr_file__out_of_date(git_repository *repo, git_attr_file *file)
+{
+	if (!file)
+		return 1;
+
+	switch (file->source) {
+	case GIT_ATTR_FILE__IN_MEMORY:
+		return 0;
+
+	case GIT_ATTR_FILE__FROM_FILE:
+		return git_futils_filestamp_check(
+			&file->cache_data.stamp, file->entry->fullpath);
+
+	case GIT_ATTR_FILE__FROM_INDEX: {
+		int error;
+		git_oid id;
+
+		if ((error = attr_file_oid_from_index(
+				&id, repo, file->entry->path)) < 0)
+			return error;
+
+		return (git_oid__cmp(&file->cache_data.oid, &id) != 0);
+	}
+
+	default:
+		giterr_set(GITERR_INVALID, "Invalid file type %d", file->source);
+		return -1;
+	}
+}
+
+static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
+static void git_attr_rule__clear(git_attr_rule *rule);
+static bool parse_optimized_patterns(
+	git_attr_fnmatch *spec,
+	git_pool *pool,
+	const char *pattern);
+
+int git_attr_file__parse_buffer(
+	git_repository *repo, git_attr_file *attrs, const char *data)
+{
+	int error = 0;
+	const char *scan = data, *context = NULL;
+	git_attr_rule *rule = NULL;
 
 	/* if subdir file path, convert context for file paths */
-	if (attrs->key &&
-		git_path_root(attrs->key + 2) < 0 &&
-		git__suffixcmp(attrs->key, "/" GIT_ATTR_FILE) == 0)
-		context = attrs->key + 2;
+	if (attrs->entry &&
+		git_path_root(attrs->entry->path) < 0 &&
+		!git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
+		context = attrs->entry->path;
+
+	if (git_mutex_lock(&attrs->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock attribute file");
+		return -1;
+	}
 
 	while (!error && *scan) {
 		/* allocate rule if needed */
 		if (!rule) {
-			if (!(rule = git__calloc(1, sizeof(git_attr_rule)))) {
+			if (!(rule = git__calloc(1, sizeof(*rule)))) {
 				error = -1;
 				break;
 			}
@@ -89,9 +243,9 @@ int git_attr_file__parse_buffer(
 
 		/* parse the next "pattern attr attr attr" line */
 		if (!(error = git_attr_fnmatch__parse(
-				&rule->match, attrs->pool, context, &scan)) &&
+				&rule->match, &attrs->pool, context, &scan)) &&
 			!(error = git_attr_assignment__parse(
-				repo, attrs->pool, &rule->assigns, &scan)))
+				repo, &attrs->pool, &rule->assigns, &scan)))
 		{
 			if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
 				/* should generate error/warning if this is coming from any
@@ -112,62 +266,12 @@ int git_attr_file__parse_buffer(
 		}
 	}
 
+	git_mutex_unlock(&attrs->lock);
 	git_attr_rule__free(rule);
 
 	return error;
 }
 
-int git_attr_file__new_and_load(
-	git_attr_file **attrs_ptr,
-	const char *path)
-{
-	int error;
-	git_buf content = GIT_BUF_INIT;
-
-	if ((error = git_attr_file__new(attrs_ptr, 0, path, NULL)) < 0)
-		return error;
-
-	if (!(error = git_futils_readbuffer(&content, path)))
-		error = git_attr_file__parse_buffer(
-			NULL, NULL, git_buf_cstr(&content), *attrs_ptr);
-
-	git_buf_free(&content);
-
-	if (error) {
-		git_attr_file__free(*attrs_ptr);
-		*attrs_ptr = NULL;
-	}
-
-	return error;
-}
-
-void git_attr_file__clear_rules(git_attr_file *file)
-{
-	unsigned int i;
-	git_attr_rule *rule;
-
-	git_vector_foreach(&file->rules, i, rule)
-		git_attr_rule__free(rule);
-
-	git_vector_free(&file->rules);
-}
-
-void git_attr_file__free(git_attr_file *file)
-{
-	if (!file)
-		return;
-
-	git_attr_file__clear_rules(file);
-
-	if (file->pool_is_allocated) {
-		git_pool_clear(file->pool);
-		git__free(file->pool);
-	}
-	file->pool = NULL;
-
-	git__free(file);
-}
-
 uint32_t git_attr_file__name_hash(const char *name)
 {
 	uint32_t h = 5381;
@@ -178,7 +282,6 @@ uint32_t git_attr_file__name_hash(const char *name)
 	return h;
 }
 
-
 int git_attr_file__lookup_one(
 	git_attr_file *file,
 	const git_attr_path *path,
@@ -207,25 +310,63 @@ int git_attr_file__lookup_one(
 	return 0;
 }
 
+int git_attr_file__load_standalone(git_attr_file **out, const char *path)
+{
+	int error;
+	git_attr_file *file;
+	git_buf content = GIT_BUF_INIT;
+
+	error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE);
+	if (error < 0)
+		return error;
+
+	error = git_attr_cache__alloc_file_entry(
+		&file->entry, NULL, path, &file->pool);
+	if (error < 0) {
+		git_attr_file__free(file);
+		return error;
+	}
+	/* because the cache entry is allocated from the file's own pool, we
+	 * don't have to free it - freeing file+pool will free cache entry, too.
+	 */
+
+	if (!(error = git_futils_readbuffer(&content, path))) {
+		error = git_attr_file__parse_buffer(NULL, file, content.ptr);
+		git_buf_free(&content);
+	}
+
+	if (error < 0)
+		git_attr_file__free(file);
+	else
+		*out = file;
+
+	return error;
+}
 
 bool git_attr_fnmatch__match(
 	git_attr_fnmatch *match,
 	const git_attr_path *path)
 {
-	int fnm;
-	int icase_flags = (match->flags & GIT_ATTR_FNMATCH_ICASE) ? FNM_CASEFOLD : 0;
+	const char *filename;
+	int flags = 0;
 
-	if (match->flags & GIT_ATTR_FNMATCH_DIRECTORY && !path->is_dir)
+	if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir)
 		return false;
 
-	if (match->flags & GIT_ATTR_FNMATCH_FULLPATH)
-		fnm = p_fnmatch(match->pattern, path->path, FNM_PATHNAME | icase_flags);
-	else if (path->is_dir)
-		fnm = p_fnmatch(match->pattern, path->basename, FNM_LEADING_DIR | icase_flags);
-	else
-		fnm = p_fnmatch(match->pattern, path->basename, icase_flags);
+	if (match->flags & GIT_ATTR_FNMATCH_ICASE)
+		flags |= FNM_CASEFOLD;
 
-	return (fnm == FNM_NOMATCH) ? false : true;
+	if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) {
+		filename = path->path;
+		flags |= FNM_PATHNAME;
+	} else {
+		filename = path->basename;
+
+		if (path->is_dir)
+			flags |= FNM_LEADING_DIR;
+	}
+
+	return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
 }
 
 bool git_attr_rule__match(
@@ -240,7 +381,6 @@ bool git_attr_rule__match(
 	return matched;
 }
 
-
 git_attr_assignment *git_attr_rule__lookup_assignment(
 	git_attr_rule *rule, const char *name)
 {
@@ -339,7 +479,7 @@ void git_attr_path__free(git_attr_path *info)
 int git_attr_fnmatch__parse(
 	git_attr_fnmatch *spec,
 	git_pool *pool,
-	const char *source,
+	const char *context,
 	const char **base)
 {
 	const char *pattern, *scan;
@@ -407,21 +547,21 @@ int git_attr_fnmatch__parse(
 	}
 
 	if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 &&
-		source != NULL && git_path_root(pattern) < 0)
+		context != NULL && git_path_root(pattern) < 0)
 	{
-        /* use context path minus the trailing filename */
-        char *slash = strrchr(source, '/');
-        size_t sourcelen = slash ? slash - source + 1 : 0;
+		/* use context path minus the trailing filename */
+		char *slash = strrchr(context, '/');
+		size_t contextlen = slash ? slash - context + 1 : 0;
 
 		/* given an unrooted fullpath match from a file inside a repo,
 		 * prefix the pattern with the relative directory of the source file
 		 */
 		spec->pattern = git_pool_malloc(
-			pool, (uint32_t)(sourcelen + spec->length + 1));
+			pool, (uint32_t)(contextlen + spec->length + 1));
 		if (spec->pattern) {
-			memcpy(spec->pattern, source, sourcelen);
-			memcpy(spec->pattern + sourcelen, pattern, spec->length);
-			spec->length += sourcelen;
+			memcpy(spec->pattern, context, contextlen);
+			memcpy(spec->pattern + contextlen, pattern, spec->length);
+			spec->length += contextlen;
 			spec->pattern[spec->length] = '\0';
 		}
 	} else {
@@ -434,6 +574,7 @@ int git_attr_fnmatch__parse(
 	} else {
 		/* strip '\' that might have be used for internal whitespace */
 		spec->length = git__unescape(spec->pattern);
+		/* TODO: convert remaining '\' into '/' for POSIX ??? */
 	}
 
 	return 0;
diff --git a/src/attr_file.h b/src/attr_file.h
index 3bc7c6c..c906be4 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -35,6 +35,14 @@
 	(GIT_ATTR_FNMATCH_ALLOWSPACE | \
 	 GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO)
 
+typedef enum {
+	GIT_ATTR_FILE__IN_MEMORY  = 0,
+	GIT_ATTR_FILE__FROM_FILE  = 1,
+	GIT_ATTR_FILE__FROM_INDEX = 2,
+
+	GIT_ATTR_FILE_NUM_SOURCES = 3
+} git_attr_file_source;
+
 extern const char *git_attr__true;
 extern const char *git_attr__false;
 extern const char *git_attr__unset;
@@ -63,17 +71,32 @@ typedef struct {
 	const char *value;
 } git_attr_assignment;
 
+typedef struct git_attr_file_entry git_attr_file_entry;
+
 typedef struct {
-	char *key;				/* cache "source#path" this was loaded from */
-	git_vector rules;		/* vector of <rule*> or <fnmatch*> */
-	git_pool *pool;
-	bool pool_is_allocated;
+	git_refcount rc;
+	git_mutex lock;
+	git_attr_file_entry *entry;
+	git_attr_file_source source;
+	git_vector rules;			/* vector of <rule*> or <fnmatch*> */
+	git_pool pool;
 	union {
 		git_oid oid;
 		git_futils_filestamp stamp;
 	} cache_data;
 } git_attr_file;
 
+struct git_attr_file_entry {
+	git_attr_file *file[GIT_ATTR_FILE_NUM_SOURCES];
+	const char *path; /* points into fullpath */
+	char fullpath[GIT_FLEX_ARRAY];
+};
+
+typedef int (*git_attr_file_parser)(
+	git_repository *repo,
+	git_attr_file *file,
+	const char *data);
+
 typedef struct {
 	git_buf  full;
 	char    *path;
@@ -81,29 +104,37 @@ typedef struct {
 	int      is_dir;
 } git_attr_path;
 
-typedef enum {
-	GIT_ATTR_FILE_FROM_FILE = 0,
-	GIT_ATTR_FILE_FROM_INDEX = 1
-} git_attr_file_source;
-
 /*
  * git_attr_file API
  */
 
-extern int git_attr_file__new(
-	git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool);
+int git_attr_file__new(
+	git_attr_file **out,
+	git_attr_file_entry *entry,
+	git_attr_file_source source);
+
+void git_attr_file__free(git_attr_file *file);
+
+int git_attr_file__load(
+	git_attr_file **out,
+	git_repository *repo,
+	git_attr_file_entry *ce,
+	git_attr_file_source source,
+	git_attr_file_parser parser);
 
-extern int git_attr_file__new_and_load(
-	git_attr_file **attrs_ptr, const char *path);
+int git_attr_file__load_standalone(
+	git_attr_file **out, const char *path);
 
-extern void git_attr_file__free(git_attr_file *file);
+int git_attr_file__out_of_date(
+	git_repository *repo, git_attr_file *file);
 
-extern void git_attr_file__clear_rules(git_attr_file *file);
+int git_attr_file__parse_buffer(
+	git_repository *repo, git_attr_file *attrs, const char *data);
 
-extern int git_attr_file__parse_buffer(
-	git_repository *repo, void *parsedata, const char *buf, git_attr_file *file);
+int git_attr_file__clear_rules(
+	git_attr_file *file, bool need_lock);
 
-extern int git_attr_file__lookup_one(
+int git_attr_file__lookup_one(
 	git_attr_file *file,
 	const git_attr_path *path,
 	const char *attr,
@@ -114,7 +145,7 @@ extern int git_attr_file__lookup_one(
 	git_vector_rforeach(&(file)->rules, (iter), (rule)) \
 		if (git_attr_rule__match((rule), (path)))
 
-extern uint32_t git_attr_file__name_hash(const char *name);
+uint32_t git_attr_file__name_hash(const char *name);
 
 
 /*
diff --git a/src/attrcache.c b/src/attrcache.c
new file mode 100644
index 0000000..88b68eb
--- /dev/null
+++ b/src/attrcache.c
@@ -0,0 +1,448 @@
+#include "common.h"
+#include "repository.h"
+#include "attr_file.h"
+#include "config.h"
+#include "sysdir.h"
+#include "ignore.h"
+
+GIT__USE_STRMAP;
+
+GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
+{
+	GIT_UNUSED(cache); /* avoid warning if threading is off */
+
+	if (git_mutex_lock(&cache->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to get attr cache lock");
+		return -1;
+	}
+	return 0;
+}
+
+GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache)
+{
+	GIT_UNUSED(cache); /* avoid warning if threading is off */
+	git_mutex_unlock(&cache->lock);
+}
+
+GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry(
+	git_attr_cache *cache, const char *path)
+{
+	khiter_t pos = git_strmap_lookup_index(cache->files, path);
+
+	if (git_strmap_valid_index(cache->files, pos))
+		return git_strmap_value_at(cache->files, pos);
+	else
+		return NULL;
+}
+
+int git_attr_cache__alloc_file_entry(
+	git_attr_file_entry **out,
+	const char *base,
+	const char *path,
+	git_pool *pool)
+{
+	size_t baselen = 0, pathlen = strlen(path);
+	size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1;
+	git_attr_file_entry *ce;
+
+	if (base != NULL && git_path_root(path) < 0) {
+		baselen = strlen(base);
+		cachesize += baselen;
+
+		if (baselen && base[baselen - 1] != '/')
+			cachesize++;
+	}
+
+	ce = git_pool_mallocz(pool, cachesize);
+	GITERR_CHECK_ALLOC(ce);
+
+	if (baselen) {
+		memcpy(ce->fullpath, base, baselen);
+
+		if (base[baselen - 1] != '/')
+			ce->fullpath[baselen++] = '/';
+	}
+	memcpy(&ce->fullpath[baselen], path, pathlen);
+
+	ce->path = &ce->fullpath[baselen];
+	*out = ce;
+
+	return 0;
+}
+
+/* call with attrcache locked */
+static int attr_cache_make_entry(
+	git_attr_file_entry **out, git_repository *repo, const char *path)
+{
+	int error = 0;
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_attr_file_entry *entry = NULL;
+
+	error = git_attr_cache__alloc_file_entry(
+		&entry, git_repository_workdir(repo), path, &cache->pool);
+
+	if (!error) {
+		git_strmap_insert(cache->files, entry->path, entry, error);
+		if (error > 0)
+			error = 0;
+	}
+
+	*out = entry;
+	return error;
+}
+
+/* insert entry or replace existing if we raced with another thread */
+static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
+{
+	git_attr_file_entry *entry;
+	git_attr_file *old;
+
+	if (attr_cache_lock(cache) < 0)
+		return -1;
+
+	entry = attr_cache_lookup_entry(cache, file->entry->path);
+
+	GIT_REFCOUNT_OWN(file, entry);
+	GIT_REFCOUNT_INC(file);
+
+	old = git__compare_and_swap(
+		&entry->file[file->source], entry->file[file->source], file);
+
+	if (old) {
+		GIT_REFCOUNT_OWN(old, NULL);
+		git_attr_file__free(old);
+	}
+
+	attr_cache_unlock(cache);
+	return 0;
+}
+
+static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
+{
+	int error = 0;
+	git_attr_file_entry *entry;
+
+	if (!file)
+		return 0;
+	if ((error = attr_cache_lock(cache)) < 0)
+		return error;
+
+	if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
+		file = git__compare_and_swap(&entry->file[file->source], file, NULL);
+
+	attr_cache_unlock(cache);
+
+	if (file) {
+		GIT_REFCOUNT_OWN(file, NULL);
+		git_attr_file__free(file);
+	}
+
+	return error;
+}
+
+/* Look up cache entry and file.
+ * - If entry is not present, create it while the cache is locked.
+ * - If file is present, increment refcount before returning it, so the
+ *   cache can be unlocked and it won't go away.
+ */
+static int attr_cache_lookup(
+	git_attr_file **out_file,
+	git_attr_file_entry **out_entry,
+	git_repository *repo,
+	git_attr_file_source source,
+	const char *base,
+	const char *filename)
+{
+	int error = 0;
+	git_buf path = GIT_BUF_INIT;
+	const char *wd = git_repository_workdir(repo), *relfile;
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_attr_file_entry *entry = NULL;
+	git_attr_file *file = NULL;
+
+	/* join base and path as needed */
+	if (base != NULL && git_path_root(filename) < 0) {
+		if (git_buf_joinpath(&path, base, filename) < 0)
+			return -1;
+		filename = path.ptr;
+	}
+
+	relfile = filename;
+	if (wd && !git__prefixcmp(relfile, wd))
+		relfile += strlen(wd);
+
+	/* check cache for existing entry */
+	if ((error = attr_cache_lock(cache)) < 0)
+		goto cleanup;
+
+	entry = attr_cache_lookup_entry(cache, relfile);
+	if (!entry) {
+		if ((error = attr_cache_make_entry(&entry, repo, relfile)) < 0)
+			goto cleanup;
+	} else if (entry->file[source] != NULL) {
+		file = entry->file[source];
+		GIT_REFCOUNT_INC(file);
+	}
+
+	attr_cache_unlock(cache);
+
+cleanup:
+	*out_file  = file;
+	*out_entry = entry;
+
+	git_buf_free(&path);
+	return error;
+}
+
+int git_attr_cache__get(
+	git_attr_file **out,
+	git_repository *repo,
+	git_attr_file_source source,
+	const char *base,
+	const char *filename,
+	git_attr_file_parser parser)
+{
+	int error = 0;
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_attr_file_entry *entry = NULL;
+	git_attr_file *file = NULL, *updated = NULL;
+
+	if ((error = attr_cache_lookup(
+			&file, &entry, repo, source, base, filename)) < 0)
+		return error;
+
+	/* load file if we don't have one or if existing one is out of date */
+	if (!file || (error = git_attr_file__out_of_date(repo, file)) > 0)
+		error = git_attr_file__load(&updated, repo, entry, source, parser);
+
+	/* if we loaded the file, insert into and/or update cache */
+	if (updated) {
+		if ((error = attr_cache_upsert(cache, updated)) < 0)
+			git_attr_file__free(updated);
+		else {
+			git_attr_file__free(file); /* offset incref from lookup */
+			file = updated;
+		}
+	}
+
+	/* if file could not be loaded */
+	if (error < 0) {
+		/* remove existing entry */
+		if (file) {
+			git_attr_file__free(file); /* offset incref from lookup */
+			attr_cache_remove(cache, file);
+			file = NULL;
+		}
+		/* no error if file simply doesn't exist */
+		if (error == GIT_ENOTFOUND) {
+			giterr_clear();
+			error = 0;
+		}
+	}
+
+	*out = file;
+	return error;
+}
+
+bool git_attr_cache__is_cached(
+	git_repository *repo,
+	git_attr_file_source source,
+	const char *filename)
+{
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_strmap *files;
+	khiter_t pos;
+	git_attr_file_entry *entry;
+
+	if (!(cache = git_repository_attr_cache(repo)) ||
+		!(files = cache->files))
+		return false;
+
+	pos = git_strmap_lookup_index(files, filename);
+	if (!git_strmap_valid_index(files, pos))
+		return false;
+
+	entry = git_strmap_value_at(files, pos);
+
+	return entry && (entry->file[source] != NULL);
+}
+
+
+static int attr_cache__lookup_path(
+	char **out, git_config *cfg, const char *key, const char *fallback)
+{
+	git_buf buf = GIT_BUF_INIT;
+	int error;
+	const git_config_entry *entry = NULL;
+
+	*out = NULL;
+
+	if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
+		return error;
+
+	if (entry) {
+		const char *cfgval = entry->value;
+
+		/* expand leading ~/ as needed */
+		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
+			!git_sysdir_find_global_file(&buf, &cfgval[2]))
+			*out = git_buf_detach(&buf);
+		else if (cfgval)
+			*out = git__strdup(cfgval);
+	}
+	else if (!git_sysdir_find_xdg_file(&buf, fallback))
+		*out = git_buf_detach(&buf);
+
+	git_buf_free(&buf);
+
+	return error;
+}
+
+static void attr_cache__free(git_attr_cache *cache)
+{
+	bool unlock;
+
+	if (!cache)
+		return;
+
+	unlock = (git_mutex_lock(&cache->lock) == 0);
+
+	if (cache->files != NULL) {
+		git_attr_file_entry *entry;
+		git_attr_file *file;
+		int i;
+
+		git_strmap_foreach_value(cache->files, entry, {
+			for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) {
+				if ((file = git__swap(entry->file[i], NULL)) != NULL) {
+					GIT_REFCOUNT_OWN(file, NULL);
+					git_attr_file__free(file);
+				}
+			}
+		});
+		git_strmap_free(cache->files);
+	}
+
+	if (cache->macros != NULL) {
+		git_attr_rule *rule;
+
+		git_strmap_foreach_value(cache->macros, rule, {
+			git_attr_rule__free(rule);
+		});
+		git_strmap_free(cache->macros);
+	}
+
+	git_pool_clear(&cache->pool);
+
+	git__free(cache->cfg_attr_file);
+	cache->cfg_attr_file = NULL;
+
+	git__free(cache->cfg_excl_file);
+	cache->cfg_excl_file = NULL;
+
+	if (unlock)
+		git_mutex_unlock(&cache->lock);
+	git_mutex_free(&cache->lock);
+
+	git__free(cache);
+}
+
+int git_attr_cache__do_init(git_repository *repo)
+{
+	int ret = 0;
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_config *cfg;
+
+	if (cache)
+		return 0;
+
+	if ((ret = git_repository_config__weakptr(&cfg, repo)) < 0)
+		return ret;
+
+	cache = git__calloc(1, sizeof(git_attr_cache));
+	GITERR_CHECK_ALLOC(cache);
+
+	/* set up lock */
+	if (git_mutex_init(&cache->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to initialize lock for attr cache");
+		git__free(cache);
+		return -1;
+	}
+
+	/* cache config settings for attributes and ignores */
+	ret = attr_cache__lookup_path(
+		&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
+	if (ret < 0)
+		goto cancel;
+
+	ret = attr_cache__lookup_path(
+		&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
+	if (ret < 0)
+		goto cancel;
+
+	/* allocate hashtable for attribute and ignore file contents,
+	 * hashtable for attribute macros, and string pool
+	 */
+	if ((ret = git_strmap_alloc(&cache->files)) < 0 ||
+		(ret = git_strmap_alloc(&cache->macros)) < 0 ||
+		(ret = git_pool_init(&cache->pool, 1, 0)) < 0)
+		goto cancel;
+
+	cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
+	if (cache)
+		goto cancel; /* raced with another thread, free this but no error */
+
+	/* insert default macros */
+	return git_attr_add_macro(repo, "binary", "-diff -crlf -text");
+
+cancel:
+	attr_cache__free(cache);
+	return ret;
+}
+
+void git_attr_cache_flush(git_repository *repo)
+{
+	git_attr_cache *cache;
+
+	/* this could be done less expensively, but for now, we'll just free
+	 * the entire attrcache and let the next use reinitialize it...
+	 */
+	if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
+		attr_cache__free(cache);
+}
+
+int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
+{
+	git_attr_cache *cache = git_repository_attr_cache(repo);
+	git_strmap *macros = cache->macros;
+	int error;
+
+	/* TODO: generate warning log if (macro->assigns.length == 0) */
+	if (macro->assigns.length == 0)
+		return 0;
+
+	if (git_mutex_lock(&cache->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to get attr cache lock");
+		error = -1;
+	} else {
+		git_strmap_insert(macros, macro->match.pattern, macro, error);
+		git_mutex_unlock(&cache->lock);
+	}
+
+	return (error < 0) ? -1 : 0;
+}
+
+git_attr_rule *git_attr_cache__lookup_macro(
+	git_repository *repo, const char *name)
+{
+	git_strmap *macros = git_repository_attr_cache(repo)->macros;
+	khiter_t pos;
+
+	pos = git_strmap_lookup_index(macros, name);
+
+	if (!git_strmap_valid_index(macros, pos))
+		return NULL;
+
+	return (git_attr_rule *)git_strmap_value_at(macros, pos);
+}
+
diff --git a/src/attrcache.h b/src/attrcache.h
index 077633b..be0a22f 100644
--- a/src/attrcache.h
+++ b/src/attrcache.h
@@ -7,18 +7,50 @@
 #ifndef INCLUDE_attrcache_h__
 #define INCLUDE_attrcache_h__
 
-#include "pool.h"
+#include "attr_file.h"
 #include "strmap.h"
 
+#define GIT_ATTR_CONFIG       "core.attributesfile"
+#define GIT_IGNORE_CONFIG     "core.excludesfile"
+
 typedef struct {
-	int initialized;
-	git_pool pool;
-	git_strmap *files;	 /* hash path to git_attr_file of rules */
-	git_strmap *macros;	 /* hash name to vector<git_attr_assignment> */
 	char *cfg_attr_file; /* cached value of core.attributesfile */
 	char *cfg_excl_file; /* cached value of core.excludesfile */
+	git_strmap *files;	 /* hash path to git_attr_cache_entry records */
+	git_strmap *macros;	 /* hash name to vector<git_attr_assignment> */
+	git_mutex lock;
+	git_pool  pool;
 } git_attr_cache;
 
-extern int git_attr_cache__init(git_repository *repo);
+extern int git_attr_cache__do_init(git_repository *repo);
+
+#define git_attr_cache__init(REPO) \
+	(git_repository_attr_cache(REPO) ? 0 : git_attr_cache__do_init(REPO))
+
+/* get file - loading and reload as needed */
+extern int git_attr_cache__get(
+	git_attr_file **file,
+	git_repository *repo,
+	git_attr_file_source source,
+	const char *base,
+	const char *filename,
+	git_attr_file_parser parser);
+
+extern bool git_attr_cache__is_cached(
+	git_repository *repo,
+	git_attr_file_source source,
+	const char *path);
+
+extern int git_attr_cache__alloc_file_entry(
+	git_attr_file_entry **out,
+	const char *base,
+	const char *path,
+	git_pool *pool);
+
+extern int git_attr_cache__insert_macro(
+	git_repository *repo, git_attr_rule *macro);
+
+extern git_attr_rule *git_attr_cache__lookup_macro(
+	git_repository *repo, const char *name);
 
 #endif
diff --git a/src/checkout.c b/src/checkout.c
index 141fc13..0b38522 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -277,19 +277,23 @@ static int checkout_action_wd_only(
 
 	/* check if item is tracked in the index but not in the checkout diff */
 	if (data->index != NULL) {
+		size_t pos;
+
+		error = git_index__find_pos(
+			&pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
+
 		if (wd->mode != GIT_FILEMODE_TREE) {
-			if (!(error = git_index_find(NULL, data->index, wd->path))) {
+			if (!error) { /* found by git_index__find_pos call */
 				notify = GIT_CHECKOUT_NOTIFY_DIRTY;
 				remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
 			} else if (error != GIT_ENOTFOUND)
 				return error;
 			else
-				giterr_clear();
+				error = 0; /* git_index__find_pos does not set error msg */
 		} else {
 			/* for tree entries, we have to see if there are any index
 			 * entries that are contained inside that tree
 			 */
-			size_t pos = git_index__prefix_position(data->index, wd->path);
 			const git_index_entry *e = git_index_get_byindex(data->index, pos);
 
 			if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
@@ -653,10 +657,13 @@ static int checkout_conflictdata_cmp(const void *a, const void *b)
 	return diff;
 }
 
-int checkout_conflictdata_empty(const git_vector *conflicts, size_t idx)
+int checkout_conflictdata_empty(
+	const git_vector *conflicts, size_t idx, void *payload)
 {
 	checkout_conflictdata *conflict;
 
+	GIT_UNUSED(payload);
+
 	if ((conflict = git_vector_get(conflicts, idx)) == NULL)
 		return -1;
 
@@ -954,7 +961,8 @@ static int checkout_conflicts_coalesce_renames(
 			ancestor_conflict->one_to_two = 1;
 	}
 
-	git_vector_remove_matching(&data->conflicts, checkout_conflictdata_empty);
+	git_vector_remove_matching(
+		&data->conflicts, checkout_conflictdata_empty, NULL);
 
 done:
 	return error;
diff --git a/src/common.h b/src/common.h
index d389cf8..9c8bdc1 100644
--- a/src/common.h
+++ b/src/common.h
@@ -46,6 +46,7 @@
 # include <unistd.h>
 # ifdef GIT_THREADS
 #	include <pthread.h>
+#	include <sched.h>
 # endif
 #define GIT_STDLIB_CALL
 
diff --git a/src/config_file.c b/src/config_file.c
index aedf2cb..bb26aa8 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -180,11 +180,15 @@ static int config_open(git_config_backend *cfg, git_config_level_t level)
 
 	b->level = level;
 
-	b->values = git_strmap_alloc();
-	GITERR_CHECK_ALLOC(b->values);
+	if ((res = git_strmap_alloc(&b->values)) < 0)
+		return res;
 
 	git_array_init(b->readers);
 	reader = git_array_alloc(b->readers);
+	if (!reader) {
+		git_strmap_free(b->values);
+		return -1;
+	}
 	memset(reader, 0, sizeof(struct reader));
 
 	reader->file_path = git__strdup(b->file_path);
@@ -205,6 +209,7 @@ static int config_open(git_config_backend *cfg, git_config_level_t level)
 
 	reader = git_array_get(b->readers, 0);
 	git_buf_free(&reader->buffer);
+
 	return res;
 }
 
@@ -218,8 +223,10 @@ static int config_refresh(git_config_backend *cfg)
 
 	for (i = 0; i < git_array_size(b->readers); i++) {
 		reader = git_array_get(b->readers, i);
+
 		res = git_futils_readbuffer_updated(
-			&reader->buffer, reader->file_path, &reader->file_mtime, &reader->file_size, &updated);
+			&reader->buffer, reader->file_path,
+			&reader->file_mtime, &reader->file_size, &updated);
 
 		if (res < 0)
 			return (res == GIT_ENOTFOUND) ? 0 : res;
@@ -233,10 +240,9 @@ static int config_refresh(git_config_backend *cfg)
 
 	/* need to reload - store old values and prep for reload */
 	old_values = b->values;
-	b->values = git_strmap_alloc();
-	GITERR_CHECK_ALLOC(b->values);
-
-	if ((res = config_parse(b, reader, b->level, 0)) < 0) {
+	if ((res = git_strmap_alloc(&b->values)) < 0) {
+		b->values = old_values;
+	} else if ((res = config_parse(b, reader, b->level, 0)) < 0) {
 		free_vars(b->values);
 		b->values = old_values;
 	} else {
diff --git a/src/diff.c b/src/diff.c
index cb05a5f..0d1aed4 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -318,6 +318,31 @@ static const char *diff_mnemonic_prefix(
 	return pfx;
 }
 
+static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
+{
+	if (!ignore_case) {
+		diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
+
+		diff->strcomp    = git__strcmp;
+		diff->strncomp   = git__strncmp;
+		diff->pfxcomp    = git__prefixcmp;
+		diff->entrycomp  = git_index_entry_cmp;
+
+		git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
+	} else {
+		diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
+
+		diff->strcomp    = git__strcasecmp;
+		diff->strncomp   = git__strncasecmp;
+		diff->pfxcomp    = git__prefixcmp_icase;
+		diff->entrycomp  = git_index_entry_icmp;
+
+		git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
+	}
+
+	git_vector_sort(&diff->deltas);
+}
+
 static git_diff *diff_list_alloc(
 	git_repository *repo,
 	git_iterator *old_iter,
@@ -344,24 +369,10 @@ static git_diff *diff_list_alloc(
 
 	/* Use case-insensitive compare if either iterator has
 	 * the ignore_case bit set */
-	if (!git_iterator_ignore_case(old_iter) &&
-		!git_iterator_ignore_case(new_iter)) {
-		diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
-
-		diff->strcomp    = git__strcmp;
-		diff->strncomp   = git__strncmp;
-		diff->pfxcomp    = git__prefixcmp;
-		diff->entrycomp  = git_index_entry__cmp;
-	} else {
-		diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
-
-		diff->strcomp    = git__strcasecmp;
-		diff->strncomp   = git__strncasecmp;
-		diff->pfxcomp    = git__prefixcmp_icase;
-		diff->entrycomp  = git_index_entry__cmp_icase;
-
-		git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
-	}
+	diff_set_ignore_case(
+		diff,
+		git_iterator_ignore_case(old_iter) ||
+		git_iterator_ignore_case(new_iter));
 
 	return diff;
 }
@@ -1183,39 +1194,25 @@ int git_diff_tree_to_index(
 	const git_diff_options *opts)
 {
 	int error = 0;
-	bool reset_index_ignore_case = false;
+	bool index_ignore_case = false;
 
 	assert(diff && repo);
 
 	if (!index && (error = diff_load_index(&index, repo)) < 0)
 		return error;
 
-	if (index->ignore_case) {
-		git_index__set_ignore_case(index, false);
-		reset_index_ignore_case = true;
-	}
+	index_ignore_case = index->ignore_case;
 
 	DIFF_FROM_ITERATORS(
-		git_iterator_for_tree(&a, old_tree, 0, pfx, pfx),
-		git_iterator_for_index(&b, index, 0, pfx, pfx)
+		git_iterator_for_tree(
+			&a, old_tree, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx),
+		git_iterator_for_index(
+			&b, index, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx)
 	);
 
-	if (reset_index_ignore_case) {
-		git_index__set_ignore_case(index, true);
-
-		if (!error) {
-			git_diff *d = *diff;
-
-			d->opts.flags |= GIT_DIFF_IGNORE_CASE;
-			d->strcomp    = git__strcasecmp;
-			d->strncomp   = git__strncasecmp;
-			d->pfxcomp    = git__prefixcmp_icase;
-			d->entrycomp  = git_index_entry__cmp_icase;
-
-			git_vector_set_cmp(&d->deltas, git_diff_delta__casecmp);
-			git_vector_sort(&d->deltas);
-		}
-	}
+	/* if index is in case-insensitive order, re-sort deltas to match */
+	if (!error && index_ignore_case)
+		diff_set_ignore_case(*diff, true);
 
 	return error;
 }
diff --git a/src/diff_driver.c b/src/diff_driver.c
index 4c9a0af..8136e0d 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -66,7 +66,7 @@ git_diff_driver_registry *git_diff_driver_registry_new()
 	if (!reg)
 		return NULL;
 
-	if ((reg->drivers = git_strmap_alloc()) == NULL) {
+	if (git_strmap_alloc(&reg->drivers) < 0) {
 		git_diff_driver_registry_free(reg);
 		return NULL;
 	}
diff --git a/src/diff_print.c b/src/diff_print.c
index 1a09bed..a7f7b6f 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -8,6 +8,7 @@
 #include "diff.h"
 #include "diff_patch.h"
 #include "fileops.h"
+#include "git2/sys/diff.h"
 
 typedef struct {
 	git_diff *diff;
@@ -435,7 +436,7 @@ int git_patch_print(
 	return error;
 }
 
-static int diff_print_to_buffer_cb(
+int git_diff_print_callback__to_buf(
 	const git_diff_delta *delta,
 	const git_diff_hunk *hunk,
 	const git_diff_line *line,
@@ -444,6 +445,11 @@ static int diff_print_to_buffer_cb(
 	git_buf *output = payload;
 	GIT_UNUSED(delta); GIT_UNUSED(hunk);
 
+	if (!output) {
+		giterr_set(GITERR_INVALID, "Buffer pointer must be provided");
+		return -1;
+	}
+
 	if (line->origin == GIT_DIFF_LINE_ADDITION ||
 		line->origin == GIT_DIFF_LINE_DELETION ||
 		line->origin == GIT_DIFF_LINE_CONTEXT)
@@ -452,10 +458,28 @@ static int diff_print_to_buffer_cb(
 	return git_buf_put(output, line->content, line->content_len);
 }
 
+int git_diff_print_callback__to_file_handle(
+	const git_diff_delta *delta,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
+	void *payload)
+{
+	FILE *fp = payload ? payload : stdout;
+
+	GIT_UNUSED(delta); GIT_UNUSED(hunk);
+
+	if (line->origin == GIT_DIFF_LINE_CONTEXT ||
+		line->origin == GIT_DIFF_LINE_ADDITION ||
+		line->origin == GIT_DIFF_LINE_DELETION)
+		fputc(line->origin, fp);
+	fwrite(line->content, 1, line->content_len, fp);
+	return 0;
+}
+
 /* print a git_patch to a git_buf */
 int git_patch_to_buf(
 	git_buf *out,
 	git_patch *patch)
 {
-	return git_patch_print(patch, diff_print_to_buffer_cb, out);
+	return git_patch_print(patch, git_diff_print_callback__to_buf, out);
 }
diff --git a/src/fileops.c b/src/fileops.c
index 5709499..13b8f6a 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -132,6 +132,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
 
 	if (read_size != (ssize_t)len) {
 		giterr_set(GITERR_OS, "Failed to read descriptor");
+		git_buf_free(buf);
 		return -1;
 	}
 
@@ -804,10 +805,8 @@ int git_futils_filestamp_check(
 	if (stamp == NULL)
 		return 1;
 
-	if (p_stat(path, &st) < 0) {
-		giterr_set(GITERR_OS, "Could not stat '%s'", path);
+	if (p_stat(path, &st) < 0)
 		return GIT_ENOTFOUND;
-	}
 
 	if (stamp->mtime == (git_time_t)st.st_mtime &&
 		stamp->size  == (git_off_t)st.st_size   &&
@@ -831,3 +830,16 @@ void git_futils_filestamp_set(
 	else
 		memset(target, 0, sizeof(*target));
 }
+
+
+void git_futils_filestamp_set_from_stat(
+	git_futils_filestamp *stamp, struct stat *st)
+{
+	if (st) {
+		stamp->mtime = (git_time_t)st->st_mtime;
+		stamp->size  = (git_off_t)st->st_size;
+		stamp->ino   = (unsigned int)st->st_ino;
+	} else {
+		memset(stamp, 0, sizeof(*stamp));
+	}
+}
diff --git a/src/fileops.h b/src/fileops.h
index 6a65235..62227ab 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -292,13 +292,14 @@ typedef struct {
  * Compare stat information for file with reference info.
  *
  * This function updates the file stamp to current data for the given path
- * and returns 0 if the file is up-to-date relative to the prior setting or
- * 1 if the file has been changed.  (This also may return GIT_ENOTFOUND if
- * the file doesn't exist.)
+ * and returns 0 if the file is up-to-date relative to the prior setting,
+ * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't
+ * exist.  This will not call giterr_set, so you must set the error if you
+ * plan to return an error.
  *
  * @param stamp File stamp to be checked
  * @param path Path to stat and check if changed
- * @return 0 if up-to-date, 1 if out-of-date, <0 on error
+ * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat
  */
 extern int git_futils_filestamp_check(
 	git_futils_filestamp *stamp, const char *path);
@@ -316,4 +317,10 @@ extern int git_futils_filestamp_check(
 extern void git_futils_filestamp_set(
 	git_futils_filestamp *tgt, const git_futils_filestamp *src);
 
+/**
+ * Set file stamp data from stat structure
+ */
+extern void git_futils_filestamp_set_from_stat(
+	git_futils_filestamp *stamp, struct stat *st);
+
 #endif /* INCLUDE_fileops_h__ */
diff --git a/src/global.c b/src/global.c
index c26b4b3..15baf1e 100644
--- a/src/global.c
+++ b/src/global.c
@@ -16,8 +16,9 @@ git_mutex git__mwindow_mutex;
 
 #define MAX_SHUTDOWN_CB 8
 
-git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
-git_atomic git__n_shutdown_callbacks;
+static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
+static git_atomic git__n_shutdown_callbacks;
+static git_atomic git__n_inits;
 
 void git__on_shutdown(git_global_shutdown_fn callback)
 {
@@ -74,7 +75,6 @@ static void git__shutdown(void)
 
 static DWORD _tls_index;
 static DWORD _mutex = 0;
-static DWORD _n_inits = 0;
 
 static int synchronized_threads_init()
 {
@@ -101,7 +101,7 @@ int git_threads_init(void)
 	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
 
 	/* Only do work on a 0 -> 1 transition of the refcount */
-	if (1 == ++_n_inits)
+	if (1 == git_atomic_inc(&git__n_inits))
 		error = synchronized_threads_init();
 
 	/* Exit the lock */
@@ -124,7 +124,7 @@ void git_threads_shutdown(void)
 	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
 
 	/* Only do work on a 1 -> 0 transition of the refcount */
-	if (0 == --_n_inits)
+	if (0 == git_atomic_dec(&git__n_inits))
 		synchronized_threads_shutdown();
 
 	/* Exit the lock */
@@ -135,7 +135,7 @@ git_global_st *git__global_state(void)
 {
 	void *ptr;
 
-	assert(_n_inits);
+	assert(git_atomic_get(&git__n_inits) > 0);
 
 	if ((ptr = TlsGetValue(_tls_index)) != NULL)
 		return ptr;
@@ -153,7 +153,6 @@ git_global_st *git__global_state(void)
 
 static pthread_key_t _tls_key;
 static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
-static git_atomic git__n_inits;
 int init_error = 0;
 
 static void cb__free_status(void *st)
@@ -204,7 +203,7 @@ git_global_st *git__global_state(void)
 {
 	void *ptr;
 
-	assert(git__n_inits.val);
+	assert(git_atomic_get(&git__n_inits) > 0);
 
 	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
 		return ptr;
@@ -224,14 +223,15 @@ static git_global_st __state;
 
 int git_threads_init(void)
 {
-	/* noop */
+	git_atomic_inc(&git__n_inits);
 	return 0;
 }
 
 void git_threads_shutdown(void)
 {
 	/* Shut down any subsystems that have global state */
-	git__shutdown();
+	if (0 == git_atomic_dec(&git__n_inits))
+		git__shutdown();
 }
 
 git_global_st *git__global_state(void)
diff --git a/src/ignore.c b/src/ignore.c
index 5cf4fca..deae204 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -1,7 +1,7 @@
 #include "git2/ignore.h"
 #include "common.h"
 #include "ignore.h"
-#include "attr.h"
+#include "attrcache.h"
 #include "path.h"
 #include "config.h"
 
@@ -10,26 +10,26 @@
 #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
 
 static int parse_ignore_file(
-	git_repository *repo, void *parsedata, const char *buffer, git_attr_file *ignores)
+	git_repository *repo, git_attr_file *attrs, const char *data)
 {
 	int error = 0;
-	git_attr_fnmatch *match = NULL;
-	const char *scan = NULL, *context = NULL;
 	int ignore_case = false;
+	const char *scan = data, *context = NULL;
+	git_attr_fnmatch *match = NULL;
 
-	/* Prefer to have the caller pass in a git_ignores as the parsedata
-	 * object.  If they did not, then look up the value of ignore_case */
-	if (parsedata != NULL)
-		ignore_case = ((git_ignores *)parsedata)->ignore_case;
-	else if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
-		return error;
+	if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
+		giterr_clear();
 
-	if (ignores->key &&
-		git_path_root(ignores->key + 2) < 0 &&
-		git__suffixcmp(ignores->key, "/" GIT_IGNORE_FILE) == 0)
-		context = ignores->key + 2;
+	/* if subdir file path, convert context for file paths */
+	if (attrs->entry &&
+		git_path_root(attrs->entry->path) < 0 &&
+		!git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE))
+		context = attrs->entry->path;
 
-	scan = buffer;
+	if (git_mutex_lock(&attrs->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock ignore file");
+		return -1;
+	}
 
 	while (!error && *scan) {
 		if (!match) {
@@ -40,7 +40,7 @@ static int parse_ignore_file(
 		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG;
 
 		if (!(error = git_attr_fnmatch__parse(
-			match, ignores->pool, context, &scan)))
+			match, &attrs->pool, context, &scan)))
 		{
 			match->flags |= GIT_ATTR_FNMATCH_IGNORE;
 
@@ -48,7 +48,7 @@ static int parse_ignore_file(
 				match->flags |= GIT_ATTR_FNMATCH_ICASE;
 
 			scan = git__next_line(scan);
-			error = git_vector_insert(&ignores->rules, match);
+			error = git_vector_insert(&attrs->rules, match);
 		}
 
 		if (error != 0) {
@@ -62,33 +62,55 @@ static int parse_ignore_file(
 		}
 	}
 
+	git_mutex_unlock(&attrs->lock);
 	git__free(match);
 
 	return error;
 }
 
-#define push_ignore_file(R,IGN,S,B,F) \
-	git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,parse_ignore_file,(IGN),(S))
+static int push_ignore_file(
+	git_ignores *ignores,
+	git_vector *which_list,
+	const char *base,
+	const char *filename)
+{
+	int error = 0;
+	git_attr_file *file = NULL;
+
+	error = git_attr_cache__get(
+		&file, ignores->repo, GIT_ATTR_FILE__FROM_FILE,
+		base, filename, parse_ignore_file);
+	if (error < 0)
+		return error;
+
+	if (file != NULL) {
+		if ((error = git_vector_insert(which_list, file)) < 0)
+			git_attr_file__free(file);
+	}
+
+	return error;
+}
 
 static int push_one_ignore(void *payload, git_buf *path)
 {
 	git_ignores *ign = payload;
-
 	ign->depth++;
-
-	return push_ignore_file(
-		ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
+	return push_ignore_file(ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
 }
 
-static int get_internal_ignores(git_attr_file **ign, git_repository *repo)
+static int get_internal_ignores(git_attr_file **out, git_repository *repo)
 {
 	int error;
 
-	if (!(error = git_attr_cache__init(repo)))
-		error = git_attr_cache__internal_file(repo, GIT_IGNORE_INTERNAL, ign);
+	if ((error = git_attr_cache__init(repo)) < 0)
+		return error;
+
+	error = git_attr_cache__get(
+		out, repo, GIT_ATTR_FILE__IN_MEMORY, NULL, GIT_IGNORE_INTERNAL, NULL);
 
-	if (!error && !(*ign)->rules.length)
-		error = parse_ignore_file(repo, NULL, GIT_IGNORE_DEFAULT_RULES, *ign);
+	/* if internal rules list is empty, insert default rules */
+	if (!error && !(*out)->rules.length)
+		error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES);
 
 	return error;
 }
@@ -103,19 +125,15 @@ int git_ignore__for_path(
 
 	assert(ignores);
 
+	memset(ignores, 0, sizeof(*ignores));
 	ignores->repo = repo;
-	git_buf_init(&ignores->dir, 0);
-	ignores->ign_internal = NULL;
-	ignores->depth = 0;
 
 	/* Read the ignore_case flag */
 	if ((error = git_repository__cvar(
 			&ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0)
 		goto cleanup;
 
-	if ((error = git_vector_init(&ignores->ign_path, 8, NULL)) < 0 ||
-		(error = git_vector_init(&ignores->ign_global, 2, NULL)) < 0 ||
-		(error = git_attr_cache__init(repo)) < 0)
+	if ((error = git_attr_cache__init(repo)) < 0)
 		goto cleanup;
 
 	/* given a unrooted path in a non-bare repo, resolve it */
@@ -127,8 +145,7 @@ int git_ignore__for_path(
 		goto cleanup;
 
 	/* set up internals */
-	error = get_internal_ignores(&ignores->ign_internal, repo);
-	if (error < 0)
+	if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0)
 		goto cleanup;
 
 	/* load .gitignore up the path */
@@ -140,14 +157,16 @@ int git_ignore__for_path(
 	}
 
 	/* load .git/info/exclude */
-	error = push_ignore_file(repo, ignores, &ignores->ign_global,
+	error = push_ignore_file(
+		ignores, &ignores->ign_global,
 		git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
 	if (error < 0)
 		goto cleanup;
 
 	/* load core.excludesfile */
 	if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
-		error = push_ignore_file(repo, ignores, &ignores->ign_global, NULL,
+		error = push_ignore_file(
+			ignores, &ignores->ign_global, NULL,
 			git_repository_attr_cache(repo)->cfg_excl_file);
 
 cleanup:
@@ -165,33 +184,34 @@ int git_ignore__push_dir(git_ignores *ign, const char *dir)
 	ign->depth++;
 
 	return push_ignore_file(
-		ign->repo, ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
+		ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
 }
 
 int git_ignore__pop_dir(git_ignores *ign)
 {
 	if (ign->ign_path.length > 0) {
 		git_attr_file *file = git_vector_last(&ign->ign_path);
-		const char *start, *end, *scan;
-		size_t keylen;
+		const char *start = file->entry->path, *end;
 
-		/* - ign->dir looks something like "a/b/" (or "a/b/c/d/")
-		 * - file->key looks something like "0#a/b/.gitignore
+		/* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
+		 * - file->path looks something like "a/b/.gitignore
 		 *
-		 * We are popping the last directory off ign->dir.  We also want to
-		 * remove the file from the vector if the directory part of the key
-		 * matches the ign->dir path.  We need to test if the "a/b" part of
+		 * We are popping the last directory off ign->dir.  We also want
+		 * to remove the file from the vector if the popped directory
+		 * matches the ignore path.  We need to test if the "a/b" part of
 		 * the file key matches the path we are about to pop.
 		 */
 
-		for (start = end = scan = &file->key[2]; *scan; ++scan)
-			if (*scan == '/')
-				end = scan; /* point 'end' to last '/' in key */
-		keylen = (end - start) + 1;
+		if ((end = strrchr(start, '/')) != NULL) {
+			size_t dirlen = (end - start) + 1;
 
-		if (ign->dir.size >= keylen &&
-			!memcmp(ign->dir.ptr + ign->dir.size - keylen, start, keylen))
-			git_vector_pop(&ign->ign_path);
+			if (ign->dir.size >= dirlen &&
+				!memcmp(ign->dir.ptr + ign->dir.size - dirlen, start, dirlen))
+			{
+				git_vector_pop(&ign->ign_path);
+				git_attr_file__free(file);
+			}
+		}
 	}
 
 	if (--ign->depth > 0) {
@@ -204,19 +224,33 @@ int git_ignore__pop_dir(git_ignores *ign)
 
 void git_ignore__free(git_ignores *ignores)
 {
-	/* don't need to free ignores->ign_internal since it is in cache */
+	unsigned int i;
+	git_attr_file *file;
+
+	git_attr_file__free(ignores->ign_internal);
+
+	git_vector_foreach(&ignores->ign_path, i, file) {
+		git_attr_file__free(file);
+		ignores->ign_path.contents[i] = NULL;
+	}
 	git_vector_free(&ignores->ign_path);
+
+	git_vector_foreach(&ignores->ign_global, i, file) {
+		git_attr_file__free(file);
+		ignores->ign_global.contents[i] = NULL;
+	}
 	git_vector_free(&ignores->ign_global);
+
 	git_buf_free(&ignores->dir);
 }
 
 static bool ignore_lookup_in_rules(
-	git_vector *rules, git_attr_path *path, int *ignored)
+	git_attr_file *file, git_attr_path *path, int *ignored)
 {
 	size_t j;
 	git_attr_fnmatch *match;
 
-	git_vector_rforeach(rules, j, match) {
+	git_vector_rforeach(&file->rules, j, match) {
 		if (git_attr_fnmatch__match(match, path)) {
 			*ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0);
 			return true;
@@ -238,19 +272,18 @@ int git_ignore__lookup(
 		return -1;
 
 	/* first process builtins - success means path was found */
-	if (ignore_lookup_in_rules(
-			&ignores->ign_internal->rules, &path, ignored))
+	if (ignore_lookup_in_rules(ignores->ign_internal, &path, ignored))
 		goto cleanup;
 
 	/* next process files in the path */
 	git_vector_foreach(&ignores->ign_path, i, file) {
-		if (ignore_lookup_in_rules(&file->rules, &path, ignored))
+		if (ignore_lookup_in_rules(file, &path, ignored))
 			goto cleanup;
 	}
 
 	/* last process global ignores */
 	git_vector_foreach(&ignores->ign_global, i, file) {
-		if (ignore_lookup_in_rules(&file->rules, &path, ignored))
+		if (ignore_lookup_in_rules(file, &path, ignored))
 			goto cleanup;
 	}
 
@@ -261,32 +294,33 @@ cleanup:
 	return 0;
 }
 
-int git_ignore_add_rule(
-	git_repository *repo,
-	const char *rules)
+int git_ignore_add_rule(git_repository *repo, const char *rules)
 {
 	int error;
-	git_attr_file *ign_internal;
+	git_attr_file *ign_internal = NULL;
 
-	if (!(error = get_internal_ignores(&ign_internal, repo)))
-		error = parse_ignore_file(repo, NULL, rules, ign_internal);
+	if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
+		return error;
+
+	error = parse_ignore_file(repo, ign_internal, rules);
+	git_attr_file__free(ign_internal);
 
 	return error;
 }
 
-int git_ignore_clear_internal_rules(
-	git_repository *repo)
+int git_ignore_clear_internal_rules(git_repository *repo)
 {
 	int error;
 	git_attr_file *ign_internal;
 
-	if (!(error = get_internal_ignores(&ign_internal, repo))) {
-		git_attr_file__clear_rules(ign_internal);
+	if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
+		return error;
 
-		return parse_ignore_file(
-			repo, NULL, GIT_IGNORE_DEFAULT_RULES, ign_internal);
-	}
+	if (!(error = git_attr_file__clear_rules(ign_internal, true)))
+		error = parse_ignore_file(
+			repo, ign_internal, GIT_IGNORE_DEFAULT_RULES);
 
+	git_attr_file__free(ign_internal);
 	return error;
 }
 
@@ -331,19 +365,18 @@ int git_ignore_path_is_ignored(
 			break;
 
 		/* first process builtins - success means path was found */
-		if (ignore_lookup_in_rules(
-				&ignores.ign_internal->rules, &path, ignored))
+		if (ignore_lookup_in_rules(ignores.ign_internal, &path, ignored))
 			goto cleanup;
 
 		/* next process files in the path */
 		git_vector_foreach(&ignores.ign_path, i, file) {
-			if (ignore_lookup_in_rules(&file->rules, &path, ignored))
+			if (ignore_lookup_in_rules(file, &path, ignored))
 				goto cleanup;
 		}
 
 		/* last process global ignores */
 		git_vector_foreach(&ignores.ign_global, i, file) {
-			if (ignore_lookup_in_rules(&file->rules, &path, ignored))
+			if (ignore_lookup_in_rules(file, &path, ignored))
 				goto cleanup;
 		}
 
diff --git a/src/index.c b/src/index.c
index b0b5eae..083c01f 100644
--- a/src/index.c
+++ b/src/index.c
@@ -90,13 +90,24 @@ struct entry_long {
 
 struct entry_srch_key {
 	const char *path;
-	size_t path_len;
+	size_t pathlen;
 	int stage;
 };
 
+struct entry_internal {
+	git_index_entry entry;
+	size_t pathlen;
+	char path[GIT_FLEX_ARRAY];
+};
+
+struct reuc_entry_internal {
+	git_index_reuc_entry entry;
+	size_t pathlen;
+	char path[GIT_FLEX_ARRAY];
+};
+
 /* local declarations */
 static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size);
-static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size);
 static int read_header(struct index_header *dest, const void *buffer);
 
 static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
@@ -106,15 +117,15 @@ static int write_index(git_index *index, git_filebuf *file);
 static void index_entry_free(git_index_entry *entry);
 static void index_entry_reuc_free(git_index_reuc_entry *reuc);
 
-static int index_srch(const void *key, const void *array_member)
+int git_index_entry_srch(const void *key, const void *array_member)
 {
 	const struct entry_srch_key *srch_key = key;
-	const git_index_entry *entry = array_member;
+	const struct entry_internal *entry = array_member;
 	int cmp;
 	size_t len1, len2, len;
 
-	len1 = srch_key->path_len;
-	len2 = strlen(entry->path);
+	len1 = srch_key->pathlen;
+	len2 = entry->pathlen;
 	len = len1 < len2 ? len1 : len2;
 
 	cmp = memcmp(srch_key->path, entry->path, len);
@@ -126,20 +137,20 @@ static int index_srch(const void *key, const void *array_member)
 		return 1;
 
 	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
-		return srch_key->stage - GIT_IDXENTRY_STAGE(entry);
+		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
 
 	return 0;
 }
 
-static int index_isrch(const void *key, const void *array_member)
+int git_index_entry_isrch(const void *key, const void *array_member)
 {
 	const struct entry_srch_key *srch_key = key;
-	const git_index_entry *entry = array_member;
+	const struct entry_internal *entry = array_member;
 	int cmp;
 	size_t len1, len2, len;
 
-	len1 = srch_key->path_len;
-	len2 = strlen(entry->path);
+	len1 = srch_key->pathlen;
+	len2 = entry->pathlen;
 	len = len1 < len2 ? len1 : len2;
 
 	cmp = strncasecmp(srch_key->path, entry->path, len);
@@ -152,36 +163,26 @@ static int index_isrch(const void *key, const void *array_member)
 		return 1;
 
 	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
-		return srch_key->stage - GIT_IDXENTRY_STAGE(entry);
+		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
 
 	return 0;
 }
 
-static int index_cmp_path(const void *a, const void *b)
-{
-	return strcmp((const char *)a, (const char *)b);
-}
-
-static int index_icmp_path(const void *a, const void *b)
-{
-	return strcasecmp((const char *)a, (const char *)b);
-}
-
-static int index_srch_path(const void *path, const void *array_member)
+static int index_entry_srch_path(const void *path, const void *array_member)
 {
 	const git_index_entry *entry = array_member;
 
 	return strcmp((const char *)path, entry->path);
 }
 
-static int index_isrch_path(const void *path, const void *array_member)
+static int index_entry_isrch_path(const void *path, const void *array_member)
 {
 	const git_index_entry *entry = array_member;
 
 	return strcasecmp((const char *)path, entry->path);
 }
 
-static int index_cmp(const void *a, const void *b)
+int git_index_entry_cmp(const void *a, const void *b)
 {
 	int diff;
 	const git_index_entry *entry_a = a;
@@ -195,7 +196,7 @@ static int index_cmp(const void *a, const void *b)
 	return diff;
 }
 
-static int index_icmp(const void *a, const void *b)
+int git_index_entry_icmp(const void *a, const void *b)
 {
 	int diff;
 	const git_index_entry *entry_a = a;
@@ -286,17 +287,12 @@ static int reuc_icmp(const void *a, const void *b)
 
 static void index_entry_reuc_free(git_index_reuc_entry *reuc)
 {
-	if (!reuc)
-		return;
-	git__free(reuc->path);
 	git__free(reuc);
 }
 
 static void index_entry_free(git_index_entry *entry)
 {
-	if (!entry)
-		return;
-	git__free(entry->path);
+	memset(&entry->id, 0, sizeof(entry->id));
 	git__free(entry);
 }
 
@@ -325,18 +321,69 @@ static unsigned int index_merge_mode(
 	return git_index__create_mode(mode);
 }
 
-void git_index__set_ignore_case(git_index *index, bool ignore_case)
+static int index_sort_if_needed(git_index *index, bool need_lock)
 {
-	index->ignore_case = ignore_case;
+	/* not truly threadsafe because between when this checks and/or
+	 * sorts the array another thread could come in and unsort it
+	 */
 
-	index->entries_cmp_path = ignore_case ? index_icmp_path : index_cmp_path;
-	index->entries_search = ignore_case ? index_isrch : index_srch;
-	index->entries_search_path = ignore_case ? index_isrch_path : index_srch_path;
+	if (git_vector_is_sorted(&index->entries))
+		return 0;
+
+	if (need_lock && git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to lock index");
+		return -1;
+	}
 
-	git_vector_set_cmp(&index->entries, ignore_case ? index_icmp : index_cmp);
 	git_vector_sort(&index->entries);
 
-	index->reuc_search = ignore_case ? reuc_isrch : reuc_srch;
+	if (need_lock)
+		git_mutex_unlock(&index->lock);
+
+	return 0;
+}
+
+GIT_INLINE(int) index_find_in_entries(
+	size_t *out, git_vector *entries, git_vector_cmp entry_srch,
+	const char *path, size_t path_len, int stage)
+{
+	struct entry_srch_key srch_key;
+	srch_key.path = path;
+	srch_key.pathlen = !path_len ? strlen(path) : path_len;
+	srch_key.stage = stage;
+	return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
+}
+
+GIT_INLINE(int) index_find(
+	size_t *out, git_index *index,
+	const char *path, size_t path_len, int stage, bool need_lock)
+{
+	if (index_sort_if_needed(index, need_lock) < 0)
+		return -1;
+
+	return index_find_in_entries(
+		out, &index->entries, index->entries_search, path, path_len, stage);
+}
+
+void git_index__set_ignore_case(git_index *index, bool ignore_case)
+{
+	index->ignore_case = ignore_case;
+
+	if (ignore_case) {
+		index->entries_cmp_path    = git__strcasecmp_cb;
+		index->entries_search      = git_index_entry_isrch;
+		index->entries_search_path = index_entry_isrch_path;
+		index->reuc_search         = reuc_isrch;
+	} else {
+		index->entries_cmp_path    = git__strcmp_cb;
+		index->entries_search      = git_index_entry_srch;
+		index->entries_search_path = index_entry_srch_path;
+		index->reuc_search         = reuc_srch;
+	}
+
+	git_vector_set_cmp(&index->entries,
+		ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
+	index_sort_if_needed(index, true);
 
 	git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
 	git_vector_sort(&index->reuc);
@@ -345,41 +392,51 @@ void git_index__set_ignore_case(git_index *index, bool ignore_case)
 int git_index_open(git_index **index_out, const char *index_path)
 {
 	git_index *index;
-	int error;
+	int error = -1;
 
 	assert(index_out);
 
 	index = git__calloc(1, sizeof(git_index));
 	GITERR_CHECK_ALLOC(index);
 
+	if (git_mutex_init(&index->lock)) {
+		giterr_set(GITERR_OS, "Failed to initialize lock");
+		git__free(index);
+		return -1;
+	}
+
 	if (index_path != NULL) {
 		index->index_file_path = git__strdup(index_path);
-		GITERR_CHECK_ALLOC(index->index_file_path);
+		if (!index->index_file_path)
+			goto fail;
 
 		/* Check if index file is stored on disk already */
 		if (git_path_exists(index->index_file_path) == true)
 			index->on_disk = 1;
 	}
 
-	if (git_vector_init(&index->entries, 32, index_cmp) < 0 ||
-		git_vector_init(&index->names, 32, conflict_name_cmp) < 0 ||
-		git_vector_init(&index->reuc, 32, reuc_cmp) < 0)
-		return -1;
+	if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 ||
+		git_vector_init(&index->names, 8, conflict_name_cmp) < 0 ||
+		git_vector_init(&index->reuc, 8, reuc_cmp) < 0 ||
+		git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0)
+		goto fail;
 
-	index->entries_cmp_path = index_cmp_path;
-	index->entries_search = index_srch;
-	index->entries_search_path = index_srch_path;
+	index->entries_cmp_path = git__strcmp_cb;
+	index->entries_search = git_index_entry_srch;
+	index->entries_search_path = index_entry_srch_path;
 	index->reuc_search = reuc_srch;
 
-	if ((index_path != NULL) && ((error = git_index_read(index, true)) < 0)) {
-		git_index_free(index);
-		return error;
-	}
+	if (index_path != NULL && (error = git_index_read(index, true)) < 0)
+		goto fail;
 
 	*index_out = index;
 	GIT_REFCOUNT_INC(index);
 
 	return 0;
+
+fail:
+	git_index_free(index);
+	return error;
 }
 
 int git_index_new(git_index **out)
@@ -389,12 +446,19 @@ int git_index_new(git_index **out)
 
 static void index_free(git_index *index)
 {
+	/* index iterators increment the refcount of the index, so if we
+	 * get here then there should be no outstanding iterators.
+	 */
+	assert(!git_atomic_get(&index->readers));
+
 	git_index_clear(index);
 	git_vector_free(&index->entries);
 	git_vector_free(&index->names);
 	git_vector_free(&index->reuc);
+	git_vector_free(&index->deleted);
 
 	git__free(index->index_file_path);
+	git_mutex_free(&index->lock);
 
 	git__memzero(index, sizeof(*index));
 	git__free(index);
@@ -408,28 +472,71 @@ void git_index_free(git_index *index)
 	GIT_REFCOUNT_DEC(index, index_free);
 }
 
-static void index_entries_free(git_vector *entries)
+/* call with locked index */
+static void index_free_deleted(git_index *index)
 {
+	int readers = (int)git_atomic_get(&index->readers);
 	size_t i;
 
-	for (i = 0; i < entries->length; ++i)
-		index_entry_free(git__swap(entries->contents[i], NULL));
+	if (readers > 0 || !index->deleted.length)
+		return;
+
+	for (i = 0; i < index->deleted.length; ++i) {
+		git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
+		index_entry_free(ie);
+	}
 
-	git_vector_clear(entries);
+	git_vector_clear(&index->deleted);
 }
 
-void git_index_clear(git_index *index)
+/* call with locked index */
+static int index_remove_entry(git_index *index, size_t pos)
 {
+	int error = 0;
+	git_index_entry *entry = git_vector_get(&index->entries, pos);
+
+	if (entry != NULL)
+		git_tree_cache_invalidate_path(index->tree, entry->path);
+
+	error = git_vector_remove(&index->entries, pos);
+
+	if (!error) {
+		if (git_atomic_get(&index->readers) > 0) {
+			error = git_vector_insert(&index->deleted, entry);
+		} else {
+			index_entry_free(entry);
+		}
+	}
+
+	return error;
+}
+
+int git_index_clear(git_index *index)
+{
+	int error = 0;
+
 	assert(index);
 
-	index_entries_free(&index->entries);
+	git_tree_cache_free(index->tree);
+	index->tree = NULL;
+
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
+		return -1;
+	}
+
+	while (!error && index->entries.length > 0)
+		error = index_remove_entry(index, index->entries.length - 1);
+	index_free_deleted(index);
+
 	git_index_reuc_clear(index);
 	git_index_name_clear(index);
 
 	git_futils_filestamp_set(&index->stamp, NULL);
 
-	git_tree_cache_free(index->tree);
-	index->tree = NULL;
+	git_mutex_unlock(&index->lock);
+
+	return error;
 }
 
 static int create_index_error(int error, const char *msg)
@@ -495,20 +602,29 @@ int git_index_read(git_index *index, int force)
 
 	if (!index->on_disk) {
 		if (force)
-			git_index_clear(index);
+			return git_index_clear(index);
 		return 0;
 	}
 
 	updated = git_futils_filestamp_check(&stamp, index->index_file_path);
-	if (updated < 0 || (!updated && !force))
+	if (updated < 0) {
+		giterr_set(
+			GITERR_INDEX,
+			"Failed to read index: '%s' no longer exists",
+			index->index_file_path);
 		return updated;
+	}
+	if (!updated && !force)
+		return 0;
 
 	error = git_futils_readbuffer(&buffer, index->index_file_path);
 	if (error < 0)
 		return error;
 
-	git_index_clear(index);
-	error = parse_index(index, buffer.ptr, buffer.size);
+	error = git_index_clear(index);
+
+	if (!error)
+		error = parse_index(index, buffer.ptr, buffer.size);
 
 	if (!error)
 		git_futils_filestamp_set(&index->stamp, &stamp);
@@ -538,7 +654,8 @@ int git_index_write(git_index *index)
 		return create_index_error(-1,
 			"Failed to read index: The index is in-memory only");
 
-	git_vector_sort(&index->entries);
+	if (index_sort_if_needed(index, true) < 0)
+		return -1;
 	git_vector_sort(&index->reuc);
 
 	if ((error = git_filebuf_open(
@@ -557,11 +674,11 @@ int git_index_write(git_index *index)
 	if ((error = git_filebuf_commit(&file)) < 0)
 		return error;
 
-	error = git_futils_filestamp_check(&index->stamp, index->index_file_path);
-	if (error < 0)
-		return error;
+	if (git_futils_filestamp_check(&index->stamp, index->index_file_path) < 0)
+		/* index could not be read from disk! */;
+	else
+		index->on_disk = 1;
 
-	index->on_disk = 1;
 	return 0;
 }
 
@@ -586,7 +703,8 @@ int git_index_write_tree(git_oid *oid, git_index *index)
 	return git_tree__write_index(oid, index, repo);
 }
 
-int git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo)
+int git_index_write_tree_to(
+	git_oid *oid, git_index *index, git_repository *repo)
 {
 	assert(oid && index && repo);
 	return git_tree__write_index(oid, index, repo);
@@ -602,7 +720,8 @@ const git_index_entry *git_index_get_byindex(
 	git_index *index, size_t n)
 {
 	assert(index);
-	git_vector_sort(&index->entries);
+	if (index_sort_if_needed(index, true) < 0)
+		return NULL;
 	return git_vector_get(&index->entries, n);
 }
 
@@ -613,7 +732,7 @@ const git_index_entry *git_index_get_bypath(
 
 	assert(index);
 
-	if (git_index__find(&pos, index, path, 0, stage) < 0) {
+	if (index_find(&pos, index, path, 0, stage, true) < 0) {
 		giterr_set(GITERR_INDEX, "Index does not contain %s", path);
 		return NULL;
 	}
@@ -637,20 +756,19 @@ void git_index_entry__init_from_stat(
 	entry->file_size = st->st_size;
 }
 
-int git_index_entry__cmp(const void *a, const void *b)
+static git_index_entry *index_entry_alloc(const char *path)
 {
-	const git_index_entry *entry_a = a;
-	const git_index_entry *entry_b = b;
-
-	return strcmp(entry_a->path, entry_b->path);
-}
+	size_t pathlen = strlen(path);
+	struct entry_internal *entry =
+		git__calloc(sizeof(struct entry_internal) + pathlen + 1, 1);
+	if (!entry)
+		return NULL;
 
-int git_index_entry__cmp_icase(const void *a, const void *b)
-{
-	const git_index_entry *entry_a = a;
-	const git_index_entry *entry_b = b;
+	entry->pathlen = pathlen;
+	memcpy(entry->path, path, pathlen);
+	entry->entry.path = entry->path;
 
-	return strcasecmp(entry_a->path, entry_b->path);
+	return (git_index_entry *)entry;
 }
 
 static int index_entry_init(
@@ -672,19 +790,31 @@ static int index_entry_init(
 	if (error < 0)
 		return error;
 
-	entry = git__calloc(1, sizeof(git_index_entry));
+	entry = index_entry_alloc(rel_path);
 	GITERR_CHECK_ALLOC(entry);
 
-	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
-
 	entry->id = oid;
-	entry->path = git__strdup(rel_path);
-	GITERR_CHECK_ALLOC(entry->path);
+	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
 
-	*entry_out = entry;
+	*entry_out = (git_index_entry *)entry;
 	return 0;
 }
 
+static git_index_reuc_entry *reuc_entry_alloc(const char *path)
+{
+	size_t pathlen = strlen(path);
+	struct reuc_entry_internal *entry =
+		git__calloc(sizeof(struct reuc_entry_internal) + pathlen + 1, 1);
+	if (!entry)
+		return NULL;
+
+	entry->pathlen = pathlen;
+	memcpy(entry->path, path, pathlen);
+	entry->entry.path = entry->path;
+
+	return (git_index_reuc_entry *)entry;
+}
+
 static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
 	const char *path,
 	int ancestor_mode, const git_oid *ancestor_oid,
@@ -695,15 +825,9 @@ static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
 
 	assert(reuc_out && path);
 
-	*reuc_out = NULL;
-
-	reuc = git__calloc(1, sizeof(git_index_reuc_entry));
+	*reuc_out = reuc = reuc_entry_alloc(path);
 	GITERR_CHECK_ALLOC(reuc);
 
-	reuc->path = git__strdup(path);
-	if (reuc->path == NULL)
-		return -1;
-
 	if ((reuc->mode[0] = ancestor_mode) > 0)
 		git_oid_cpy(&reuc->oid[0], ancestor_oid);
 
@@ -713,26 +837,31 @@ static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
 	if ((reuc->mode[2] = their_mode) > 0)
 		git_oid_cpy(&reuc->oid[2], their_oid);
 
-	*reuc_out = reuc;
 	return 0;
 }
 
-static git_index_entry *index_entry_dup(const git_index_entry *source_entry)
+static void index_entry_cpy(git_index_entry *tgt, const git_index_entry *src)
+{
+	char *tgt_path = tgt->path;
+	memcpy(tgt, src, sizeof(*tgt));
+	tgt->path = tgt_path; /* reset to existing path data */
+}
+
+static int index_entry_dup(git_index_entry **out, const git_index_entry *src)
 {
 	git_index_entry *entry;
 
-	entry = git__malloc(sizeof(git_index_entry));
-	if (!entry)
-		return NULL;
+	if (!src) {
+		*out = NULL;
+		return 0;
+	}
 
-	memcpy(entry, source_entry, sizeof(git_index_entry));
+	*out = entry = index_entry_alloc(src->path);
+	GITERR_CHECK_ALLOC(entry);
 
-	/* duplicate the path string so we own it */
-	entry->path = git__strdup(entry->path);
-	if (!entry->path)
-		return NULL;
+	index_entry_cpy(entry, src);
 
-	return entry;
+	return 0;
 }
 
 static int has_file_name(git_index *index,
@@ -744,20 +873,22 @@ static int has_file_name(git_index *index,
 	const char *name = entry->path;
 
 	while (pos < index->entries.length) {
-		git_index_entry *p = index->entries.contents[pos++];
+		struct entry_internal *p = index->entries.contents[pos++];
 
-		if (len >= strlen(p->path))
+		if (len >= p->pathlen)
 			break;
 		if (memcmp(name, p->path, len))
 			break;
-		if (GIT_IDXENTRY_STAGE(p) != stage)
+		if (GIT_IDXENTRY_STAGE(&p->entry) != stage)
 			continue;
 		if (p->path[len] != '/')
 			continue;
 		retval = -1;
 		if (!ok_to_replace)
 			break;
-		git_vector_remove(&index->entries, --pos);
+
+		if (index_remove_entry(index, --pos) < 0)
+			break;
 	}
 	return retval;
 }
@@ -775,7 +906,7 @@ static int has_dir_name(git_index *index,
 	const char *slash = name + strlen(name);
 
 	for (;;) {
-		size_t len, position;
+		size_t len, pos;
 
 		for (;;) {
 			if (*--slash == '/')
@@ -785,12 +916,13 @@ static int has_dir_name(git_index *index,
 		}
 		len = slash - name;
 
-		if (git_index__find(&position, index, name, len, stage) == 0) {
+		if (!index_find(&pos, index, name, len, stage, false)) {
 			retval = -1;
 			if (!ok_to_replace)
 				break;
 
-			git_vector_remove(&index->entries, position);
+			if (index_remove_entry(index, pos) < 0)
+				break;
 			continue;
 		}
 
@@ -799,20 +931,19 @@ static int has_dir_name(git_index *index,
 		 * already matches the sub-directory, then we know
 		 * we're ok, and we can exit.
 		 */
-		while (position < index->entries.length) {
-			git_index_entry *p = index->entries.contents[position];
+		for (; pos < index->entries.length; ++pos) {
+			struct entry_internal *p = index->entries.contents[pos];
 
-			if ((strlen(p->path) <= len) ||
-			    (p->path[len] != '/') ||
+			if (p->pathlen <= len ||
+			    p->path[len] != '/' ||
 			    memcmp(p->path, name, len))
 				break; /* not our subdirectory */
 
-			if (GIT_IDXENTRY_STAGE(p) == stage)
+			if (GIT_IDXENTRY_STAGE(&p->entry) == stage)
 				return retval;
-
-			position++;
 		}
 	}
+
 	return retval;
 }
 
@@ -823,22 +954,41 @@ static int check_file_directory_collision(git_index *index,
 	retval = retval + has_dir_name(index, entry, ok_to_replace);
 
 	if (retval) {
-		giterr_set(GITERR_INDEX, "'%s' appears as both a file an a directory", entry->path);
+		giterr_set(GITERR_INDEX,
+			"'%s' appears as both a file and a directory", entry->path);
 		return -1;
 	}
 
 	return 0;
 }
 
-static int index_insert(git_index *index, git_index_entry *entry, int replace)
+static int index_no_dups(void **old, void *new)
+{
+	const git_index_entry *entry = new;
+	GIT_UNUSED(old);
+	giterr_set(GITERR_INDEX, "'%s' appears multiple times at stage %d",
+		entry->path, GIT_IDXENTRY_STAGE(entry));
+	return GIT_EEXISTS;
+}
+
+/* index_insert takes ownership of the new entry - if it can't insert
+ * it, then it will return an error **and also free the entry**.  When
+ * it replaces an existing entry, it will update the entry_ptr with the
+ * actual entry in the index (and free the passed in one).
+ */
+static int index_insert(
+	git_index *index, git_index_entry **entry_ptr, int replace)
 {
+	int error = 0;
 	size_t path_length, position;
-	git_index_entry **existing = NULL;
+	git_index_entry *existing = NULL, *entry;
+
+	assert(index && entry_ptr);
 
-	assert(index && entry && entry->path != NULL);
+	entry = *entry_ptr;
 
 	/* make sure that the path length flag is correct */
-	path_length = strlen(entry->path);
+	path_length = ((struct entry_internal *)entry)->pathlen;
 
 	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;
 
@@ -847,30 +997,51 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
 	else
 		entry->flags |= GIT_IDXENTRY_NAMEMASK;
 
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to acquire index lock");
+		return -1;
+	}
+
+	git_vector_sort(&index->entries);
+
 	/* look if an entry with this path already exists */
-	if (!git_index__find(
-			&position, index, entry->path, 0, GIT_IDXENTRY_STAGE(entry))) {
-		existing = (git_index_entry **)&index->entries.contents[position];
+	if (!index_find(
+			&position, index, entry->path, 0, GIT_IDXENTRY_STAGE(entry), false)) {
+		existing = index->entries.contents[position];
 		/* update filemode to existing values if stat is not trusted */
-		entry->mode = index_merge_mode(index, *existing, entry->mode);
+		entry->mode = index_merge_mode(index, existing, entry->mode);
 	}
 
-	if (check_file_directory_collision(index, entry, position, replace) < 0)
-		return -1;
+	/* look for tree / blob name collisions, removing conflicts if requested */
+	error = check_file_directory_collision(index, entry, position, replace);
+	if (error < 0)
+		/* skip changes */;
 
-	/* if replacing is not requested or no existing entry exists, just
-	 * insert entry at the end; the index is no longer sorted
+	/* if we are replacing an existing item, overwrite the existing entry
+	 * and return it in place of the passed in one.
 	 */
-	if (!replace || !existing)
-		return git_vector_insert(&index->entries, entry);
+	else if (existing) {
+		if (replace)
+			index_entry_cpy(existing, entry);
+		index_entry_free(entry);
+		*entry_ptr = entry = existing;
+	}
+	else {
+		/* if replace is not requested or no existing entry exists, insert
+		 * at the sorted position.  (Since we re-sort after each insert to
+		 * check for dups, this is actually cheaper in the long run.)
+		 */
+		error = git_vector_insert_sorted(&index->entries, entry, index_no_dups);
+	}
 
-	/* exists, replace it (preserving name from existing entry) */
-	git__free(entry->path);
-	entry->path = (*existing)->path;
-	git__free(*existing);
-	*existing = entry;
+	if (error < 0) {
+		index_entry_free(*entry_ptr);
+		*entry_ptr = NULL;
+	}
 
-	return 0;
+	git_mutex_unlock(&index->lock);
+
+	return error;
 }
 
 static int index_conflict_to_reuc(git_index *index, const char *path)
@@ -907,19 +1078,15 @@ int git_index_add_bypath(git_index *index, const char *path)
 	assert(index && path);
 
 	if ((ret = index_entry_init(&entry, index, path)) < 0 ||
-		(ret = index_insert(index, entry, 1)) < 0)
-		goto on_error;
+		(ret = index_insert(index, &entry, 1)) < 0)
+		return ret;
 
 	/* Adding implies conflict was resolved, move conflict entries to REUC */
 	if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
-		goto on_error;
+		return ret;
 
 	git_tree_cache_invalidate_path(index->tree, entry->path);
 	return 0;
-
-on_error:
-	index_entry_free(entry);
-	return ret;
 }
 
 int git_index_remove_bypath(git_index *index, const char *path)
@@ -942,14 +1109,11 @@ int git_index_add(git_index *index, const git_index_entry *source_entry)
 	git_index_entry *entry = NULL;
 	int ret;
 
-	entry = index_entry_dup(source_entry);
-	if (entry == NULL)
-		return -1;
+	assert(index && source_entry && source_entry->path);
 
-	if ((ret = index_insert(index, entry, 1)) < 0) {
-		index_entry_free(entry);
+	if ((ret = index_entry_dup(&entry, source_entry)) < 0 ||
+		(ret = index_insert(index, &entry, 1)) < 0)
 		return ret;
-	}
 
 	git_tree_cache_invalidate_path(index->tree, entry->path);
 	return 0;
@@ -957,25 +1121,23 @@ int git_index_add(git_index *index, const git_index_entry *source_entry)
 
 int git_index_remove(git_index *index, const char *path, int stage)
 {
-	size_t position;
 	int error;
-	git_index_entry *entry;
+	size_t position;
 
-	if (git_index__find(&position, index, path, 0, stage) < 0) {
-		giterr_set(GITERR_INDEX, "Index does not contain %s at stage %d",
-			path, stage);
-		return GIT_ENOTFOUND;
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
+		return -1;
 	}
 
-	entry = git_vector_get(&index->entries, position);
-	if (entry != NULL)
-		git_tree_cache_invalidate_path(index->tree, entry->path);
-
-	error = git_vector_remove(&index->entries, position);
-
-	if (!error)
-		index_entry_free(entry);
+	if (index_find(&position, index, path, 0, stage, false) < 0) {
+		giterr_set(
+			GITERR_INDEX, "Index does not contain %s at stage %d", path, stage);
+		error = GIT_ENOTFOUND;
+	} else {
+		error = index_remove_entry(index, position);
+	}
 
+	git_mutex_unlock(&index->lock);
 	return error;
 }
 
@@ -986,14 +1148,16 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
 	size_t pos;
 	git_index_entry *entry;
 
-	if (git_buf_sets(&pfx, dir) < 0 || git_path_to_dir(&pfx) < 0)
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
 		return -1;
+	}
 
-	git_vector_sort(&index->entries);
-
-	pos = git_index__prefix_position(index, pfx.ptr);
+	if (!(error = git_buf_sets(&pfx, dir)) &&
+		!(error = git_path_to_dir(&pfx)))
+		index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false);
 
-	while (1) {
+	while (!error) {
 		entry = git_vector_get(&index->entries, pos);
 		if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
 			break;
@@ -1003,35 +1167,22 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
 			continue;
 		}
 
-		git_tree_cache_invalidate_path(index->tree, entry->path);
-
-		if ((error = git_vector_remove(&index->entries, pos)) < 0)
-			break;
-		index_entry_free(entry);
+		error = index_remove_entry(index, pos);
 
-		/* removed entry at 'pos' so we don't need to increment it */
+		/* removed entry at 'pos' so we don't need to increment */
 	}
 
+	git_mutex_unlock(&index->lock);
 	git_buf_free(&pfx);
 
 	return error;
 }
 
-int git_index__find(
+int git_index__find_pos(
 	size_t *out, git_index *index, const char *path, size_t path_len, int stage)
 {
-	struct entry_srch_key srch_key;
-
-	assert(path);
-
-	git_vector_sort(&index->entries);
-
-	srch_key.path = path;
-	srch_key.path_len = !path_len ? strlen(path) : path_len;
-	srch_key.stage = stage;
-
-	return git_vector_bsearch2(
-		out, &index->entries, index->entries_search, &srch_key);
+	assert(index && path);
+	return index_find(out, index, path, path_len, stage, true);
 }
 
 int git_index_find(size_t *at_pos, git_index *index, const char *path)
@@ -1040,7 +1191,14 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path)
 
 	assert(index && path);
 
-	if (git_vector_bsearch2(&pos, &index->entries, index->entries_search_path, path) < 0) {
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
+		return -1;
+	}
+
+	if (git_vector_bsearch2(
+			&pos, &index->entries, index->entries_search_path, path) < 0) {
+		git_mutex_unlock(&index->lock);
 		giterr_set(GITERR_INDEX, "Index does not contain %s", path);
 		return GIT_ENOTFOUND;
 	}
@@ -1048,37 +1206,20 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path)
 	/* Since our binary search only looked at path, we may be in the
 	 * middle of a list of stages.
 	 */
-	while (pos > 0) {
-		const git_index_entry *prev = git_vector_get(&index->entries, pos-1);
+	for (; pos > 0; --pos) {
+		const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
 
 		if (index->entries_cmp_path(prev->path, path) != 0)
 			break;
-
-		--pos;
 	}
 
 	if (at_pos)
 		*at_pos = pos;
 
+	git_mutex_unlock(&index->lock);
 	return 0;
 }
 
-size_t git_index__prefix_position(git_index *index, const char *path)
-{
-	struct entry_srch_key srch_key;
-	size_t pos;
-
-	srch_key.path = path;
-	srch_key.path_len = strlen(path);
-	srch_key.stage = 0;
-
-	git_vector_sort(&index->entries);
-	git_vector_bsearch2(
-		&pos, &index->entries, index->entries_search, &srch_key);
-
-	return pos;
-}
-
 int git_index_conflict_add(git_index *index,
 	const git_index_entry *ancestor_entry,
 	const git_index_entry *our_entry,
@@ -1090,21 +1231,22 @@ int git_index_conflict_add(git_index *index,
 
 	assert (index);
 
-	if ((ancestor_entry != NULL && (entries[0] = index_entry_dup(ancestor_entry)) == NULL) ||
-		(our_entry != NULL && (entries[1] = index_entry_dup(our_entry)) == NULL) ||
-		(their_entry != NULL && (entries[2] = index_entry_dup(their_entry)) == NULL))
-		return -1;
+	if ((ret = index_entry_dup(&entries[0], ancestor_entry)) < 0 ||
+		(ret = index_entry_dup(&entries[1], our_entry)) < 0 ||
+		(ret = index_entry_dup(&entries[2], their_entry)) < 0)
+		goto on_error;
 
 	for (i = 0; i < 3; i++) {
 		if (entries[i] == NULL)
 			continue;
 
 		/* Make sure stage is correct */
-		entries[i]->flags = (entries[i]->flags & ~GIT_IDXENTRY_STAGEMASK) |
-			((i+1) << GIT_IDXENTRY_STAGESHIFT);
+		GIT_IDXENTRY_STAGE_SET(entries[i], i + 1);
 
-		if ((ret = index_insert(index, entries[i], 1)) < 0)
+		if ((ret = index_insert(index, &entries[i], 1)) < 0)
 			goto on_error;
+
+		entries[i] = NULL; /* don't free if later entry fails */
 	}
 
 	return 0;
@@ -1194,23 +1336,24 @@ int git_index_conflict_get(
 	return 0;
 }
 
-int git_index_conflict_remove(git_index *index, const char *path)
+static int index_conflict_remove(git_index *index, const char *path)
 {
-	size_t pos, posmax;
+	size_t pos = 0;
 	git_index_entry *conflict_entry;
 	int error = 0;
 
-	assert(index && path);
-
-	if (git_index_find(&pos, index, path) < 0)
+	if (path != NULL && git_index_find(&pos, index, path) < 0)
 		return GIT_ENOTFOUND;
 
-	posmax = git_index_entrycount(index);
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to lock index");
+		return -1;
+	}
 
-	while (pos < posmax) {
-		conflict_entry = git_vector_get(&index->entries, pos);
+	while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) {
 
-		if (index->entries_cmp_path(conflict_entry->path, path) != 0)
+		if (path != NULL &&
+			index->entries_cmp_path(conflict_entry->path, path) != 0)
 			break;
 
 		if (GIT_IDXENTRY_STAGE(conflict_entry) == 0) {
@@ -1218,32 +1361,25 @@ int git_index_conflict_remove(git_index *index, const char *path)
 			continue;
 		}
 
-		if ((error = git_vector_remove(&index->entries, pos)) < 0)
-			return error;
-
-		index_entry_free(conflict_entry);
-		posmax--;
+		if ((error = index_remove_entry(index, pos)) < 0)
+			break;
 	}
 
-	return 0;
+	git_mutex_unlock(&index->lock);
+
+	return error;
 }
 
-static int index_conflicts_match(const git_vector *v, size_t idx)
+int git_index_conflict_remove(git_index *index, const char *path)
 {
-	git_index_entry *entry = git_vector_get(v, idx);
-
-	if (GIT_IDXENTRY_STAGE(entry) > 0) {
-		index_entry_free(entry);
-		return 1;
-	}
-
-	return 0;
+	assert(index && path);
+	return index_conflict_remove(index, path);
 }
 
-void git_index_conflict_cleanup(git_index *index)
+int git_index_conflict_cleanup(git_index *index)
 {
 	assert(index);
-	git_vector_remove_matching(&index->entries, index_conflicts_match);
+	return index_conflict_remove(index, NULL);
 }
 
 int git_index_has_conflicts(const git_index *index)
@@ -1338,32 +1474,36 @@ const git_index_name_entry *git_index_name_get_byindex(
 	return git_vector_get(&index->names, n);
 }
 
+static void index_name_entry_free(git_index_name_entry *ne)
+{
+	if (!ne)
+		return;
+	git__free(ne->ancestor);
+	git__free(ne->ours);
+	git__free(ne->theirs);
+	git__free(ne);
+}
+
 int git_index_name_add(git_index *index,
 	const char *ancestor, const char *ours, const char *theirs)
 {
 	git_index_name_entry *conflict_name;
 
-	assert ((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
+	assert((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
 
 	conflict_name = git__calloc(1, sizeof(git_index_name_entry));
 	GITERR_CHECK_ALLOC(conflict_name);
 
-	if (ancestor) {
-		conflict_name->ancestor = git__strdup(ancestor);
-		GITERR_CHECK_ALLOC(conflict_name->ancestor);
-	}
-
-	if (ours) {
-		conflict_name->ours = git__strdup(ours);
-		GITERR_CHECK_ALLOC(conflict_name->ours);
-	}
-
-	if (theirs) {
-		conflict_name->theirs = git__strdup(theirs);
-		GITERR_CHECK_ALLOC(conflict_name->theirs);
+	if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) ||
+		(ours     && !(conflict_name->ours     = git__strdup(ours))) ||
+		(theirs   && !(conflict_name->theirs   = git__strdup(theirs))) ||
+		git_vector_insert(&index->names, conflict_name) < 0)
+	{
+		index_name_entry_free(conflict_name);
+		return -1;
 	}
 
-	return git_vector_insert(&index->names, conflict_name);
+	return 0;
 }
 
 void git_index_name_clear(git_index *index)
@@ -1373,18 +1513,8 @@ void git_index_name_clear(git_index *index)
 
 	assert(index);
 
-	git_vector_foreach(&index->names, i, conflict_name) {
-		if (conflict_name->ancestor)
-			git__free(conflict_name->ancestor);
-
-		if (conflict_name->ours)
-			git__free(conflict_name->ours);
-
-		if (conflict_name->theirs)
-			git__free(conflict_name->theirs);
-
-		git__free(conflict_name);
-	}
+	git_vector_foreach(&index->names, i, conflict_name)
+		index_name_entry_free(conflict_name);
 
 	git_vector_clear(&index->names);
 }
@@ -1412,7 +1542,6 @@ static int index_reuc_insert(
 		return git_vector_insert(&index->reuc, reuc);
 
 	/* exists, replace it */
-	git__free((*existing)->path);
 	git__free(*existing);
 	*existing = reuc;
 
@@ -1429,15 +1558,13 @@ int git_index_reuc_add(git_index *index, const char *path,
 
 	assert(index && path);
 
-	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode, ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
+	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
+			ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
 		(error = index_reuc_insert(index, reuc, 1)) < 0)
-	{
 		index_entry_reuc_free(reuc);
-		return error;
-	}
 
 	return error;
-} 
+}
 
 int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
 {
@@ -1522,13 +1649,9 @@ static int read_reuc(git_index *index, const char *buffer, size_t size)
 		if (size <= len)
 			return index_error_invalid("reading reuc entries");
 
-		lost = git__calloc(1, sizeof(git_index_reuc_entry));
+		lost = reuc_entry_alloc(buffer);
 		GITERR_CHECK_ALLOC(lost);
 
-		/* read NUL-terminated pathname for entry */
-		lost->path = git__strdup(buffer);
-		GITERR_CHECK_ALLOC(lost->path);
-
 		size -= len;
 		buffer += len;
 
@@ -1626,41 +1749,41 @@ static int read_conflict_names(git_index *index, const char *buffer, size_t size
 	return 0;
 }
 
-static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size)
+static size_t read_entry(
+	git_index_entry **out, const void *buffer, size_t buffer_size)
 {
 	size_t path_length, entry_size;
 	uint16_t flags_raw;
 	const char *path_ptr;
 	const struct entry_short *source = buffer;
+	git_index_entry entry = {{0}};
 
 	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
 		return 0;
 
-	memset(dest, 0x0, sizeof(git_index_entry));
-
-	dest->ctime.seconds = (git_time_t)ntohl(source->ctime.seconds);
-	dest->ctime.nanoseconds = ntohl(source->ctime.nanoseconds);
-	dest->mtime.seconds = (git_time_t)ntohl(source->mtime.seconds);
-	dest->mtime.nanoseconds = ntohl(source->mtime.nanoseconds);
-	dest->dev = ntohl(source->dev);
-	dest->ino = ntohl(source->ino);
-	dest->mode = ntohl(source->mode);
-	dest->uid = ntohl(source->uid);
-	dest->gid = ntohl(source->gid);
-	dest->file_size = ntohl(source->file_size);
-	git_oid_cpy(&dest->id, &source->oid);
-	dest->flags = ntohs(source->flags);
-
-	if (dest->flags & GIT_IDXENTRY_EXTENDED) {
+	entry.ctime.seconds = (git_time_t)ntohl(source->ctime.seconds);
+	entry.ctime.nanoseconds = ntohl(source->ctime.nanoseconds);
+	entry.mtime.seconds = (git_time_t)ntohl(source->mtime.seconds);
+	entry.mtime.nanoseconds = ntohl(source->mtime.nanoseconds);
+	entry.dev = ntohl(source->dev);
+	entry.ino = ntohl(source->ino);
+	entry.mode = ntohl(source->mode);
+	entry.uid = ntohl(source->uid);
+	entry.gid = ntohl(source->gid);
+	entry.file_size = ntohl(source->file_size);
+	git_oid_cpy(&entry.id, &source->oid);
+	entry.flags = ntohs(source->flags);
+
+	if (entry.flags & GIT_IDXENTRY_EXTENDED) {
 		const struct entry_long *source_l = (const struct entry_long *)source;
 		path_ptr = source_l->path;
 
 		flags_raw = ntohs(source_l->flags_extended);
-		memcpy(&dest->flags_extended, &flags_raw, 2);
+		memcpy(&entry.flags_extended, &flags_raw, 2);
 	} else
 		path_ptr = source->path;
 
-	path_length = dest->flags & GIT_IDXENTRY_NAMEMASK;
+	path_length = entry.flags & GIT_IDXENTRY_NAMEMASK;
 
 	/* if this is a very long string, we must find its
 	 * real length without overflowing */
@@ -1674,7 +1797,7 @@ static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffe
 		path_length = path_end - path_ptr;
 	}
 
-	if (dest->flags & GIT_IDXENTRY_EXTENDED)
+	if (entry.flags & GIT_IDXENTRY_EXTENDED)
 		entry_size = long_entry_size(path_length);
 	else
 		entry_size = short_entry_size(path_length);
@@ -1682,8 +1805,10 @@ static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffe
 	if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
 		return 0;
 
-	dest->path = git__strdup(path_ptr);
-	assert(dest->path);
+	entry.path = (char *)path_ptr;
+
+	if (index_entry_dup(out, &entry) < 0)
+		return 0;
 
 	return entry_size;
 }
@@ -1749,6 +1874,7 @@ static size_t read_extension(git_index *index, const char *buffer, size_t buffer
 
 static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 {
+	int error = 0;
 	unsigned int i;
 	struct index_header header = { 0 };
 	git_oid checksum_calculated, checksum_expected;
@@ -1768,35 +1894,41 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 	git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
 
 	/* Parse header */
-	if (read_header(&header, buffer) < 0)
-		return -1;
+	if ((error = read_header(&header, buffer)) < 0)
+		return error;
 
 	seek_forward(INDEX_HEADER_SIZE);
 
-	git_vector_clear(&index->entries);
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Unable to acquire index lock");
+		return -1;
+	}
+
+	assert(!index->entries.length);
 
 	/* Parse all the entries */
 	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
-		size_t entry_size;
 		git_index_entry *entry;
-
-		entry = git__malloc(sizeof(git_index_entry));
-		GITERR_CHECK_ALLOC(entry);
-
-		entry_size = read_entry(entry, buffer, buffer_size);
+		size_t entry_size = read_entry(&entry, buffer, buffer_size);
 
 		/* 0 bytes read means an object corruption */
-		if (entry_size == 0)
-			return index_error_invalid("invalid entry");
+		if (entry_size == 0) {
+			error = index_error_invalid("invalid entry");
+			goto done;
+		}
 
-		if (git_vector_insert(&index->entries, entry) < 0)
-			return -1;
+		if ((error = git_vector_insert(&index->entries, entry)) < 0) {
+			index_entry_free(entry);
+			goto done;
+		}
 
 		seek_forward(entry_size);
 	}
 
-	if (i != header.entry_count)
-		return index_error_invalid("header entries changed while parsing");
+	if (i != header.entry_count) {
+		error = index_error_invalid("header entries changed while parsing");
+		goto done;
+	}
 
 	/* There's still space for some extensions! */
 	while (buffer_size > INDEX_FOOTER_SIZE) {
@@ -1805,20 +1937,28 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 		extension_size = read_extension(index, buffer, buffer_size);
 
 		/* see if we have read any bytes from the extension */
-		if (extension_size == 0)
-			return index_error_invalid("extension is truncated");
+		if (extension_size == 0) {
+			error = index_error_invalid("extension is truncated");
+			goto done;
+		}
 
 		seek_forward(extension_size);
 	}
 
-	if (buffer_size != INDEX_FOOTER_SIZE)
-		return index_error_invalid("buffer size does not match index footer size");
+	if (buffer_size != INDEX_FOOTER_SIZE) {
+		error = index_error_invalid(
+			"buffer size does not match index footer size");
+		goto done;
+	}
 
 	/* 160-bit SHA-1 over the content of the index file before this checksum. */
 	git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);
 
-	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0)
-		return index_error_invalid("calculated checksum does not match expected");
+	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) {
+		error = index_error_invalid(
+			"calculated checksum does not match expected");
+		goto done;
+	}
 
 #undef seek_forward
 
@@ -1826,9 +1966,11 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 	 * in-memory index is supposed to be case-insensitive
 	 */
 	git_vector_set_sorted(&index->entries, !index->ignore_case);
-	git_vector_sort(&index->entries);
+	error = index_sort_if_needed(index, false);
 
-	return 0;
+done:
+	git_mutex_unlock(&index->lock);
+	return error;
 }
 
 static bool is_index_extended(git_index *index)
@@ -1856,7 +1998,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
 	size_t path_len, disk_size;
 	char *path;
 
-	path_len = strlen(entry->path);
+	path_len = ((struct entry_internal *)entry)->pathlen;
 
 	if (entry->flags & GIT_IDXENTRY_EXTENDED)
 		disk_size = long_entry_size(path_len);
@@ -1913,22 +2055,30 @@ static int write_entries(git_index *index, git_filebuf *file)
 {
 	int error = 0;
 	size_t i;
-	git_vector case_sorted;
+	git_vector case_sorted, *entries;
 	git_index_entry *entry;
-	git_vector *out = &index->entries;
+
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
+		return -1;
+	}
 
 	/* If index->entries is sorted case-insensitively, then we need
 	 * to re-sort it case-sensitively before writing */
 	if (index->ignore_case) {
-		git_vector_dup(&case_sorted, &index->entries, index_cmp);
+		git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp);
 		git_vector_sort(&case_sorted);
-		out = &case_sorted;
+		entries = &case_sorted;
+	} else {
+		entries = &index->entries;
 	}
 
-	git_vector_foreach(out, i, entry)
+	git_vector_foreach(entries, i, entry)
 		if ((error = write_disk_entry(file, entry)) < 0)
 			break;
 
+	git_mutex_unlock(&index->lock);
+
 	if (index->ignore_case)
 		git_vector_free(&case_sorted);
 
@@ -2100,7 +2250,7 @@ int git_index_entry_stage(const git_index_entry *entry)
 typedef struct read_tree_data {
 	git_vector *old_entries;
 	git_vector *new_entries;
-	git_vector_cmp entries_search;
+	git_vector_cmp entry_cmp;
 } read_tree_data;
 
 static int read_tree_cb(
@@ -2109,6 +2259,7 @@ static int read_tree_cb(
 	read_tree_data *data = payload;
 	git_index_entry *entry = NULL, *old_entry;
 	git_buf path = GIT_BUF_INIT;
+	size_t pos;
 
 	if (git_tree_entry__is_tree(tentry))
 		return 0;
@@ -2116,30 +2267,24 @@ static int read_tree_cb(
 	if (git_buf_joinpath(&path, root, tentry->filename) < 0)
 		return -1;
 
-	entry = git__calloc(1, sizeof(git_index_entry));
+	entry = index_entry_alloc(path.ptr);
 	GITERR_CHECK_ALLOC(entry);
 
 	entry->mode = tentry->attr;
 	entry->id = tentry->oid;
 
 	/* look for corresponding old entry and copy data to new entry */
-	if (data->old_entries) {
-		size_t pos;
-		struct entry_srch_key skey;
-
-		skey.path = path.ptr;
-		skey.path_len = strlen(path.ptr);
-		skey.stage = 0;
-
-		if (!git_vector_bsearch2(
-				&pos, data->old_entries, data->entries_search, &skey) &&
-			(old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
-			entry->mode == old_entry->mode &&
-			git_oid_equal(&entry->id, &old_entry->id))
-		{
-			memcpy(entry, old_entry, sizeof(*entry));
-			entry->flags_extended = 0;
-		}
+	if (data->old_entries != NULL &&
+		!index_find_in_entries(
+			&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) &&
+		(old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
+		entry->mode == old_entry->mode &&
+		git_oid_equal(&entry->id, &old_entry->id))
+	{
+		char *oldpath = entry->path;
+		memcpy(entry, old_entry, sizeof(*entry));
+		entry->path = oldpath;
+		entry->flags_extended = 0;
 	}
 
 	if (path.size < GIT_IDXENTRY_NAMEMASK)
@@ -2147,7 +2292,6 @@ static int read_tree_cb(
 	else
 		entry->flags = GIT_IDXENTRY_NAMEMASK;
 
-	entry->path = git_buf_detach(&path);
 	git_buf_free(&path);
 
 	if (git_vector_insert(data->new_entries, entry) < 0) {
@@ -2168,16 +2312,25 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
 
 	data.old_entries = &index->entries;
 	data.new_entries = &entries;
-	data.entries_search = index->entries_search;
+	data.entry_cmp   = index->entries_search;
 
-	git_vector_sort(&index->entries);
+	if (index_sort_if_needed(index, true) < 0)
+		return -1;
 
 	error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data);
 
 	if (!error) {
 		git_vector_sort(&entries);
-		git_index_clear(index);
-		git_vector_swap(&entries, &index->entries);
+
+		if ((error = git_index_clear(index)) < 0)
+			/* well, this isn't good */;
+		else if (git_mutex_lock(&index->lock) < 0) {
+			giterr_set(GITERR_OS, "Unable to acquire index lock");
+			error = -1;
+		} else {
+			git_vector_swap(&entries, &index->entries);
+			git_mutex_unlock(&index->lock);
+		}
 	}
 
 	git_vector_free(&entries);
@@ -2247,7 +2400,7 @@ int git_index_add_all(
 		/* skip ignored items that are not already in the index */
 		if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
 			git_iterator_current_is_ignored(wditer) &&
-			git_index__find(&existing, index, wd->path, 0, 0) < 0)
+			index_find(&existing, index, wd->path, 0, 0, true) < 0)
 			continue;
 
 		/* issue notification callback if requested */
@@ -2269,17 +2422,14 @@ int git_index_add_all(
 			break;
 
 		/* make the new entry to insert */
-		if ((entry = index_entry_dup(wd)) == NULL) {
-			error = -1;
+		if ((error = index_entry_dup(&entry, wd)) < 0)
 			break;
-		}
+
 		entry->id = blobid;
 
 		/* add working directory item to index */
-		if ((error = index_insert(index, entry, 1)) < 0) {
-			index_entry_free(entry);
+		if ((error = index_insert(index, &entry, 1)) < 0)
 			break;
-		}
 
 		git_tree_cache_invalidate_path(index->tree, wd->path);
 
@@ -2411,3 +2561,48 @@ int git_index_update_all(
 
 	return error;
 }
+
+int git_index_snapshot_new(git_vector *snap, git_index *index)
+{
+	int error;
+
+	GIT_REFCOUNT_INC(index);
+
+	if (git_mutex_lock(&index->lock) < 0) {
+		giterr_set(GITERR_OS, "Failed to lock index");
+		return -1;
+	}
+
+	git_atomic_inc(&index->readers);
+	git_vector_sort(&index->entries);
+
+	error = git_vector_dup(snap, &index->entries, index->entries._cmp);
+
+	git_mutex_unlock(&index->lock);
+
+	if (error < 0)
+		git_index_free(index);
+
+	return error;
+}
+
+void git_index_snapshot_release(git_vector *snap, git_index *index)
+{
+	git_vector_free(snap);
+
+	git_atomic_dec(&index->readers);
+
+	if (!git_mutex_lock(&index->lock)) {
+		index_free_deleted(index); /* try to free pending deleted items */
+		git_mutex_unlock(&index->lock);
+	}
+
+	git_index_free(index);
+}
+
+int git_index_snapshot_find(
+	size_t *out, git_vector *entries, git_vector_cmp entry_srch,
+	const char *path, size_t path_len, int stage)
+{
+	return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
+}
diff --git a/src/index.h b/src/index.h
index 17f04f0..50a0b4b 100644
--- a/src/index.h
+++ b/src/index.h
@@ -21,12 +21,15 @@ struct git_index {
 	git_refcount rc;
 
 	char *index_file_path;
-
 	git_futils_filestamp stamp;
+
 	git_vector entries;
 
-	unsigned int on_disk:1;
+	git_mutex  lock;    /* lock held while entries is being changed */
+	git_vector deleted; /* deleted entries if readers > 0 */
+	git_atomic readers; /* number of active iterators */
 
+	unsigned int on_disk:1;
 	unsigned int ignore_case:1;
 	unsigned int distrust_filemode:1;
 	unsigned int no_symlinks:1;
@@ -50,12 +53,21 @@ struct git_index_conflict_iterator {
 extern void git_index_entry__init_from_stat(
 	git_index_entry *entry, struct stat *st, bool trust_mode);
 
-extern size_t git_index__prefix_position(git_index *index, const char *path);
+/* Index entry comparison functions for array sorting */
+extern int git_index_entry_cmp(const void *a, const void *b);
+extern int git_index_entry_icmp(const void *a, const void *b);
 
-extern int git_index_entry__cmp(const void *a, const void *b);
-extern int git_index_entry__cmp_icase(const void *a, const void *b);
+/* Index entry search functions for search using a search spec */
+extern int git_index_entry_srch(const void *a, const void *b);
+extern int git_index_entry_isrch(const void *a, const void *b);
 
-extern int git_index__find(
+/* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
+ * (but not setting an error message).
+ *
+ * `at_pos` is set to the position where it is or would be inserted.
+ * Pass `path_len` as strlen of path or 0 to call strlen internally.
+ */
+extern int git_index__find_pos(
 	size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
 
 extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
@@ -69,4 +81,16 @@ GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
 
 extern int git_index__changed_relative_to(git_index *index, const git_futils_filestamp *fs);
 
+/* Copy the current entries vector *and* increment the index refcount.
+ * Call `git_index__release_snapshot` when done.
+ */
+extern int git_index_snapshot_new(git_vector *snap, git_index *index);
+extern void git_index_snapshot_release(git_vector *snap, git_index *index);
+
+/* Allow searching in a snapshot; entries must already be sorted! */
+extern int git_index_snapshot_find(
+	size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
+	const char *path, size_t path_len, int stage);
+
+
 #endif
diff --git a/src/iterator.c b/src/iterator.c
index a7a4491..63c14f9 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -644,6 +644,8 @@ typedef struct {
 	git_iterator base;
 	git_iterator_callbacks cb;
 	git_index *index;
+	git_vector entries;
+	git_vector_cmp entry_srch;
 	size_t current;
 	/* when not in autoexpand mode, use these to represent "tree" state */
 	git_buf partial;
@@ -654,10 +656,10 @@ typedef struct {
 
 static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
 {
-	const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
 	if (ie != NULL && iterator__past_end(ii, ie->path)) {
-		ii->current = git_index_entrycount(ii->index);
+		ii->current = git_vector_length(&ii->entries);
 		ie = NULL;
 	}
 
@@ -726,7 +728,7 @@ static int index_iterator__current(
 	const git_index_entry **entry, git_iterator *self)
 {
 	index_iterator *ii = (index_iterator *)self;
-	const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
 	if (ie != NULL && index_iterator__at_tree(ii)) {
 		ii->tree_entry.path = ii->partial.ptr;
@@ -744,14 +746,14 @@ static int index_iterator__current(
 static int index_iterator__at_end(git_iterator *self)
 {
 	index_iterator *ii = (index_iterator *)self;
-	return (ii->current >= git_index_entrycount(ii->index));
+	return (ii->current >= git_vector_length(&ii->entries));
 }
 
 static int index_iterator__advance(
 	const git_index_entry **entry, git_iterator *self)
 {
 	index_iterator *ii = (index_iterator *)self;
-	size_t entrycount = git_index_entrycount(ii->index);
+	size_t entrycount = git_vector_length(&ii->entries);
 	const git_index_entry *ie;
 
 	if (!iterator__has_been_accessed(ii))
@@ -766,7 +768,7 @@ static int index_iterator__advance(
 			while (ii->current < entrycount) {
 				ii->current++;
 
-				if (!(ie = git_index_get_byindex(ii->index, ii->current)) ||
+				if (!(ie = git_vector_get(&ii->entries, ii->current)) ||
 					ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
 					break;
 			}
@@ -789,7 +791,7 @@ 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_index_get_byindex(ii->index, ii->current);
+	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
 
 	if (ie != NULL && index_iterator__at_tree(ii)) {
 		if (ii->restore_terminator)
@@ -815,8 +817,11 @@ static int index_iterator__reset(
 	if (iterator__reset_range(self, start, end) < 0)
 		return -1;
 
-	ii->current = ii->base.start ?
-		git_index__prefix_position(ii->index, ii->base.start) : 0;
+	ii->current = 0;
+
+	if (ii->base.start)
+		git_index_snapshot_find(
+			&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
 
 	if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
 		return 0;
@@ -841,9 +846,8 @@ static int index_iterator__reset(
 static void index_iterator__free(git_iterator *self)
 {
 	index_iterator *ii = (index_iterator *)self;
-	git_index_free(ii->index);
+	git_index_snapshot_release(&ii->entries, ii->index);
 	ii->index = NULL;
-
 	git_buf_free(&ii->partial);
 }
 
@@ -854,18 +858,29 @@ int git_iterator_for_index(
 	const char *start,
 	const char *end)
 {
+	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, git_index_owner(index));
 
-	if (index->ignore_case) {
-		ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
-		ii->base.prefixcomp = git__prefixcmp_icase;
+	if ((error = iterator__update_ignore_case((git_iterator *)ii, flags)) < 0) {
+		git_iterator_free((git_iterator *)ii);
+		return error;
 	}
 
-	ii->index = index;
-	GIT_REFCOUNT_INC(index);
+	ii->entry_srch = iterator__ignore_case(ii) ?
+		git_index_entry_isrch : git_index_entry_srch;
+
+	git_vector_set_cmp(&ii->entries, iterator__ignore_case(ii) ?
+		git_index_entry_icmp : git_index_entry_cmp);
+	git_vector_sort(&ii->entries);
 
 	git_buf_init(&ii->partial, 0);
 	ii->tree_entry.mode = GIT_FILEMODE_TREE;
@@ -873,7 +888,6 @@ int git_iterator_for_index(
 	index_iterator__reset((git_iterator *)ii, NULL, NULL);
 
 	*iter = (git_iterator *)ii;
-
 	return 0;
 }
 
diff --git a/src/merge.c b/src/merge.c
index 10c19b5..2e40b6d 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -995,10 +995,12 @@ static void merge_diff_list_coalesce_renames(
 	}
 }
 
-static int merge_diff_empty(const git_vector *conflicts, size_t idx)
+static int merge_diff_empty(const git_vector *conflicts, size_t idx, void *p)
 {
 	git_merge_diff *conflict = conflicts->contents[idx];
 
+	GIT_UNUSED(p);
+
 	return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) &&
 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) &&
 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry));
@@ -1079,7 +1081,7 @@ int git_merge_diff_list__find_renames(
 	merge_diff_list_coalesce_renames(diff_list, similarity_ours, similarity_theirs, opts);
 
 	/* And remove any entries that were merged and are now empty. */
-	git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty);
+	git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty, NULL);
 
 done:
 	if (cache != NULL) {
@@ -1228,7 +1230,7 @@ done:
 	return error;
 }
 
-GIT_INLINE(int) index_entry_dup(
+GIT_INLINE(int) index_entry_dup_pool(
 	git_index_entry *out,
 	git_pool *pool,
 	const git_index_entry *src)
@@ -1274,9 +1276,9 @@ static git_merge_diff *merge_diff_from_index_entries(
 	if ((conflict = git_pool_malloc(pool, sizeof(git_merge_diff))) == NULL)
 		return NULL;
 
-	if (index_entry_dup(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 ||
-		index_entry_dup(&conflict->our_entry, pool, entries[TREE_IDX_OURS]) < 0 ||
-		index_entry_dup(&conflict->their_entry, pool, entries[TREE_IDX_THEIRS]) < 0)
+	if (index_entry_dup_pool(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 ||
+		index_entry_dup_pool(&conflict->our_entry, pool, entries[TREE_IDX_OURS]) < 0 ||
+		index_entry_dup_pool(&conflict->their_entry, pool, entries[TREE_IDX_THEIRS]) < 0)
 		return NULL;
 
 	conflict->our_status = merge_delta_type_from_index_entries(
@@ -1316,7 +1318,7 @@ static int merge_diff_list_insert_unmodified(
 	entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry));
 	GITERR_CHECK_ALLOC(entry);
 
-	if ((error = index_entry_dup(entry, &diff_list->pool, tree_items[0])) >= 0)
+	if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
 		error = git_vector_insert(&diff_list->staged, entry);
 
 	return error;
@@ -1330,7 +1332,7 @@ int git_merge_diff_list__find_differences(
 {
 	git_iterator *iterators[3] = {0};
 	const git_index_entry *items[3] = {0}, *best_cur_item, *cur_items[3];
-	git_vector_cmp entry_compare = git_index_entry__cmp;
+	git_vector_cmp entry_compare = git_index_entry_cmp;
 	struct merge_diff_df_data df_data = {0};
 	int cur_item_modified;
 	size_t i, j;
diff --git a/src/pathspec.c b/src/pathspec.c
index 4714884..09650de 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -445,7 +445,7 @@ static int pathspec_match_from_iterator(
 		/* check if path is ignored and untracked */
 		if (index != NULL &&
 			git_iterator_current_is_ignored(iter) &&
-			git_index__find(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
+			git_index__find_pos(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
 			continue;
 
 		/* mark the matched pattern as used */
diff --git a/src/repository.h b/src/repository.h
index 99923b6..86db488 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -108,7 +108,7 @@ struct git_repository {
 	git_submodule_cache *_submodules;
 
 	git_cache objects;
-	git_attr_cache attrcache;
+	git_attr_cache *attrcache;
 	git_diff_driver_registry *diff_drivers;
 
 	char *path_repository;
@@ -123,7 +123,7 @@ struct git_repository {
 
 GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
 {
-	return &repo->attrcache;
+	return repo->attrcache;
 }
 
 int git_repository_head_tree(git_tree **tree, git_repository *repo);
diff --git a/src/sortedcache.c b/src/sortedcache.c
index 13f0921..c6b2261 100644
--- a/src/sortedcache.c
+++ b/src/sortedcache.c
@@ -20,7 +20,7 @@ int git_sortedcache_new(
 
 	if (git_pool_init(&sc->pool, 1, 0) < 0 ||
 		git_vector_init(&sc->items, 4, item_cmp) < 0 ||
-		(sc->map = git_strmap_alloc()) == NULL)
+		git_strmap_alloc(&sc->map) < 0)
 		goto fail;
 
 	if (git_rwlock_init(&sc->lock)) {
@@ -39,8 +39,7 @@ int git_sortedcache_new(
 	return 0;
 
 fail:
-	if (sc->map)
-		git_strmap_free(sc->map);
+	git_strmap_free(sc->map);
 	git_vector_free(&sc->items);
 	git_pool_clear(&sc->pool);
 	git__free(sc);
@@ -233,9 +232,8 @@ unlock:
 
 void git_sortedcache_updated(git_sortedcache *sc)
 {
-	 /* update filestamp to latest value */
-	if (git_futils_filestamp_check(&sc->stamp, sc->path) < 0)
-		giterr_clear();
+	/* update filestamp to latest value */
+	git_futils_filestamp_check(&sc->stamp, sc->path);
 }
 
 /* release all items in sorted cache */
diff --git a/src/strmap.h b/src/strmap.h
index 8276ab4..8985aaf 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -22,7 +22,9 @@ typedef khiter_t git_strmap_iter;
 #define GIT__USE_STRMAP \
 	__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
 
-#define git_strmap_alloc()  kh_init(str)
+#define git_strmap_alloc(hp) \
+	((*(hp) = kh_init(str)) == NULL) ? giterr_set_oom(), -1 : 0
+
 #define git_strmap_free(h)  kh_destroy(str, h), h = NULL
 #define git_strmap_clear(h) kh_clear(str, h)
 
diff --git a/src/submodule.c b/src/submodule.c
index bea096d..5ddbfe8 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1638,8 +1638,7 @@ static int submodule_cache_alloc(
 		return -1;
 	}
 
-	cache->submodules = git_strmap_alloc();
-	if (!cache->submodules) {
+	if (git_strmap_alloc(&cache->submodules) < 0) {
 		submodule_cache_free(cache);
 		return -1;
 	}
@@ -1694,8 +1693,6 @@ static int submodule_cache_refresh(git_submodule_cache *cache, int refresh)
 		update_gitmod = (wd != NULL) ?
 			git_futils_filestamp_check(&cache->gitmodules_stamp, path.ptr) :
 			(cache->gitmodules_stamp.mtime != 0);
-		if (update_gitmod < 0)
-			giterr_clear();
 	}
 
 	/* clear submodule flags that are to be refreshed */
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 914c135..50d8610 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -47,6 +47,12 @@ typedef git_atomic git_atomic_ssize;
 #define git_thread_exit(status)	pthread_exit(status)
 #define git_thread_join(id, status) pthread_join(id, status)
 
+#if defined(GIT_WIN32)
+#define git_thread_yield() Sleep(0)
+#else
+#define git_thread_yield() sched_yield()
+#endif
+
 /* Pthreads Mutex */
 #define git_mutex pthread_mutex_t
 #define git_mutex_init(a)	pthread_mutex_init(a, NULL)
@@ -176,6 +182,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
 #define git_thread_kill(thread) (void)0
 #define git_thread_exit(status) (void)0
 #define git_thread_join(id, status) (void)0
+#define git_thread_yield() (void)0
 
 /* Pthreads Mutex */
 #define git_mutex unsigned int
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 1d39971..49afd6e 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -7,23 +7,16 @@
 
 #include "tree-cache.h"
 
-static git_tree_cache *find_child(const git_tree_cache *tree, const char *path)
+static git_tree_cache *find_child(
+	const git_tree_cache *tree, const char *path, const char *end)
 {
-	size_t i, dirlen;
-	const char *end;
-
-	end = strchr(path, '/');
-	if (end == NULL) {
-		end = strrchr(path, '\0');
-	}
-
-	dirlen = end - path;
+	size_t i, dirlen = end ? (size_t)(end - path) : strlen(path);
 
 	for (i = 0; i < tree->children_count; ++i) {
-		const char *childname = tree->children[i]->name;
+		git_tree_cache *child = tree->children[i];
 
-		if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen))
-			return tree->children[i];
+		if (child->namelen == dirlen && !memcmp(path, child->name, dirlen))
+			return child;
 	}
 
 	return NULL;
@@ -44,7 +37,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
 		if (end == NULL) /* End of path */
 			break;
 
-		tree = find_child(tree, ptr);
+		tree = find_child(tree, ptr, end);
 		if (tree == NULL) /* We don't have that tree */
 			return;
 
@@ -64,10 +57,9 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char 
 	while (1) {
 		end = strchr(ptr, '/');
 
-		tree = find_child(tree, ptr);
-		if (tree == NULL) { /* Can't find it */
+		tree = find_child(tree, ptr, end);
+		if (tree == NULL) /* Can't find it */
 			return NULL;
-		}
 
 		if (end == NULL || *end + 1 == '\0')
 			return tree;
@@ -100,6 +92,7 @@ static int read_tree_internal(git_tree_cache **out,
 	tree->parent = parent;
 
 	/* NUL-terminated tree name */
+	tree->namelen = name_len;
 	memcpy(tree->name, name_start, name_len);
 	tree->name[name_len] = '\0';
 
diff --git a/src/tree-cache.h b/src/tree-cache.h
index 805483a..90c82db 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -18,6 +18,7 @@ struct git_tree_cache {
 
 	ssize_t entries;
 	git_oid oid;
+	size_t namelen;
 	char name[GIT_FLEX_ARRAY];
 };
 
diff --git a/src/util.c b/src/util.c
index 3767b89..3985825 100644
--- a/src/util.c
+++ b/src/util.c
@@ -542,10 +542,12 @@ int git__bsearch_r(
  */
 int git__strcmp_cb(const void *a, const void *b)
 {
-	const char *stra = (const char *)a;
-	const char *strb = (const char *)b;
+	return strcmp((const char *)a, (const char *)b);
+}
 
-	return strcmp(stra, strb);
+int git__strcasecmp_cb(const void *a, const void *b)
+{
+	return strcasecmp((const char *)a, (const char *)b);
 }
 
 int git__parse_bool(int *out, const char *value)
diff --git a/src/util.h b/src/util.h
index 5c2c563..d94463c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -196,6 +196,7 @@ extern int git__bsearch_r(
 	size_t *position);
 
 extern int git__strcmp_cb(const void *a, const void *b);
+extern int git__strcasecmp_cb(const void *a, const void *b);
 
 extern int git__strcmp(const char *a, const char *b);
 extern int git__strcasecmp(const char *a, const char *b);
diff --git a/src/vector.c b/src/vector.c
index 37ea07f..c769b69 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -177,7 +177,8 @@ void git_vector_sort(git_vector *v)
 	if (git_vector_is_sorted(v) || !v->_cmp)
 		return;
 
-	git__tsort(v->contents, v->length, v->_cmp);
+	if (v->length > 1)
+		git__tsort(v->contents, v->length, v->_cmp);
 	git_vector_set_sorted(v, 1);
 }
 
@@ -276,14 +277,16 @@ void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *))
 }
 
 void git_vector_remove_matching(
-	git_vector *v, int (*match)(const git_vector *v, size_t idx))
+	git_vector *v,
+	int (*match)(const git_vector *v, size_t idx, void *payload),
+	void *payload)
 {
 	size_t i, j;
 
 	for (i = 0, j = 0; j < v->length; ++j) {
 		v->contents[i] = v->contents[j];
 
-		if (!match(v, i))
+		if (!match(v, i, payload))
 			i++;
 	}
 
@@ -339,3 +342,18 @@ int git_vector_set(void **old, git_vector *v, size_t position, void *value)
 
 	return 0;
 }
+
+int git_vector_verify_sorted(const git_vector *v)
+{
+	size_t i;
+
+	if (!git_vector_is_sorted(v))
+		return -1;
+
+	for (i = 1; i < v->length; ++i) {
+		if (v->_cmp(v->contents[i - 1], v->contents[i]) > 0)
+			return -1;
+	}
+
+	return 0;
+}
diff --git a/src/vector.h b/src/vector.h
index 682b6ad..aac46c4 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -85,8 +85,11 @@ int git_vector_insert_sorted(git_vector *v, void *element,
 int git_vector_remove(git_vector *v, size_t idx);
 void git_vector_pop(git_vector *v);
 void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *));
+
 void git_vector_remove_matching(
-	git_vector *v, int (*match)(const git_vector *v, size_t idx));
+	git_vector *v,
+	int (*match)(const git_vector *v, size_t idx, void *payload),
+	void *payload);
 
 int git_vector_resize_to(git_vector *v, size_t new_length);
 int git_vector_set(void **old, git_vector *v, size_t position, void *value);
@@ -108,4 +111,7 @@ GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
 	}
 }
 
+/* Just use this in tests, not for realz. returns -1 if not sorted */
+int git_vector_verify_sorted(const git_vector *v);
+
 #endif
diff --git a/tests/attr/file.c b/tests/attr/file.c
index 4eb1d22..1f4108c 100644
--- a/tests/attr/file.c
+++ b/tests/attr/file.c
@@ -11,9 +11,9 @@ void test_attr_file__simple_read(void)
 	git_attr_assignment *assign;
 	git_attr_rule *rule;
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0")));
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
 
-	cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2);
+	cl_assert_equal_s(cl_fixture("attr/attr0"), file->entry->path);
 	cl_assert(file->rules.length == 1);
 
 	rule = get_rule(0);
@@ -37,9 +37,9 @@ void test_attr_file__match_variants(void)
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1")));
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
 
-	cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2);
+	cl_assert_equal_s(cl_fixture("attr/attr1"), file->entry->path);
 	cl_assert(file->rules.length == 10);
 
 	/* let's do a thorough check of this rule, then just verify
@@ -121,9 +121,9 @@ void test_attr_file__assign_variants(void)
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
 
-	cl_assert_equal_s(cl_fixture("attr/attr2"), file->key + 2);
+	cl_assert_equal_s(cl_fixture("attr/attr2"), file->entry->path);
 	cl_assert(file->rules.length == 11);
 
 	check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL);
@@ -187,8 +187,8 @@ void test_attr_file__check_attr_examples(void)
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
-	cl_assert_equal_s(cl_fixture("attr/attr3"), file->key + 2);
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
+	cl_assert_equal_s(cl_fixture("attr/attr3"), file->entry->path);
 	cl_assert(file->rules.length == 3);
 
 	rule = get_rule(0);
diff --git a/tests/attr/lookup.c b/tests/attr/lookup.c
index 200bdd2..030ea07 100644
--- a/tests/attr/lookup.c
+++ b/tests/attr/lookup.c
@@ -9,8 +9,8 @@ void test_attr_lookup__simple(void)
 	git_attr_path path;
 	const char *value = NULL;
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0")));
-	cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2);
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
+	cl_assert_equal_s(cl_fixture("attr/attr0"), file->entry->path);
 	cl_assert(file->rules.length == 1);
 
 	cl_git_pass(git_attr_path__init(&path, "test", NULL));
@@ -129,8 +129,8 @@ void test_attr_lookup__match_variants(void)
 		{ NULL, NULL, 0, NULL }
 	};
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1")));
-	cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2);
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
+	cl_assert_equal_s(cl_fixture("attr/attr1"), file->entry->path);
 	cl_assert(file->rules.length == 10);
 
 	cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL));
@@ -190,7 +190,7 @@ void test_attr_lookup__assign_variants(void)
 		{ NULL, NULL, 0, NULL }
 	};
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
 	cl_assert(file->rules.length == 11);
 
 	run_test_cases(file, cases, 0);
@@ -225,7 +225,7 @@ void test_attr_lookup__check_attr_examples(void)
 		{ NULL, NULL, 0, NULL }
 	};
 
-	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
+	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
 	cl_assert(file->rules.length == 3);
 
 	run_test_cases(file, cases, 0);
@@ -250,9 +250,9 @@ void test_attr_lookup__from_buffer(void)
 		{ NULL, NULL, 0, NULL }
 	};
 
-	cl_git_pass(git_attr_file__new(&file, 0, NULL, NULL));
+	cl_git_pass(git_attr_file__new(&file, NULL, 0));
 
-	cl_git_pass(git_attr_file__parse_buffer(NULL, NULL, "a* foo\nabc bar\n* baz", file));
+	cl_git_pass(git_attr_file__parse_buffer(NULL, file, "a* foo\nabc bar\n* baz"));
 
 	cl_assert(file->rules.length == 3);
 
diff --git a/tests/attr/repo.c b/tests/attr/repo.c
index 49cccdc..71dc7a5 100644
--- a/tests/attr/repo.c
+++ b/tests/attr/repo.c
@@ -68,9 +68,12 @@ void test_attr_repo__get_one(void)
 		attr_check_expected(scan->expected, scan->expected_str, scan->attr, value);
 	}
 
-	cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/attributes"));
-	cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitattributes"));
-	cl_assert(git_attr_cache__is_cached(g_repo, 0, "sub/.gitattributes"));
+	cl_assert(git_attr_cache__is_cached(
+		g_repo, GIT_ATTR_FILE__FROM_FILE, ".git/info/attributes"));
+	cl_assert(git_attr_cache__is_cached(
+		g_repo, GIT_ATTR_FILE__FROM_FILE, ".gitattributes"));
+	cl_assert(git_attr_cache__is_cached(
+		g_repo, GIT_ATTR_FILE__FROM_FILE, "sub/.gitattributes"));
 }
 
 void test_attr_repo__get_many(void)
diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h
index c2489db..d395bd6 100644
--- a/tests/clar_libgit2.h
+++ b/tests/clar_libgit2.h
@@ -11,7 +11,7 @@
  *
  * Use this wrapper around all `git_` library calls that return error codes!
  */
-#define cl_git_pass(expr) cl_git_pass_(expr, __FILE__, __LINE__)
+#define cl_git_pass(expr) cl_git_pass_((expr), __FILE__, __LINE__)
 
 #define cl_git_pass_(expr, file, line) do { \
 	int _lg2_error; \
diff --git a/tests/core/strmap.c b/tests/core/strmap.c
index f34a4f8..a120f1f 100644
--- a/tests/core/strmap.c
+++ b/tests/core/strmap.c
@@ -3,12 +3,22 @@
 
 GIT__USE_STRMAP;
 
+git_strmap *g_table;
+
+void test_core_strmap__initialize(void)
+{
+	cl_git_pass(git_strmap_alloc(&g_table));
+	cl_assert(g_table != NULL);
+}
+
+void test_core_strmap__cleanup(void)
+{
+	git_strmap_free(g_table);
+}
+
 void test_core_strmap__0(void)
 {
-	git_strmap *table = git_strmap_alloc();
-	cl_assert(table != NULL);
-	cl_assert(git_strmap_num_entries(table) == 0);
-	git_strmap_free(table);
+	cl_assert(git_strmap_num_entries(g_table) == 0);
 }
 
 static void insert_strings(git_strmap *table, int count)
@@ -37,21 +47,17 @@ void test_core_strmap__1(void)
 {
 	int i;
 	char *str;
-	git_strmap *table = git_strmap_alloc();
-	cl_assert(table != NULL);
 
-	insert_strings(table, 20);
+	insert_strings(g_table, 20);
 
-	cl_assert(git_strmap_exists(table, "aaaaaaaaa"));
-	cl_assert(git_strmap_exists(table, "ggggggggg"));
-	cl_assert(!git_strmap_exists(table, "aaaaaaaab"));
-	cl_assert(!git_strmap_exists(table, "abcdefghi"));
+	cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
+	cl_assert(git_strmap_exists(g_table, "ggggggggg"));
+	cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
+	cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
 
 	i = 0;
-	git_strmap_foreach_value(table, str, { i++; free(str); });
+	git_strmap_foreach_value(g_table, str, { i++; free(str); });
 	cl_assert(i == 20);
-
-	git_strmap_free(table);
 }
 
 void test_core_strmap__2(void)
@@ -59,44 +65,36 @@ void test_core_strmap__2(void)
 	khiter_t pos;
 	int i;
 	char *str;
-	git_strmap *table = git_strmap_alloc();
-	cl_assert(table != NULL);
 
-	insert_strings(table, 20);
+	insert_strings(g_table, 20);
 
-	cl_assert(git_strmap_exists(table, "aaaaaaaaa"));
-	cl_assert(git_strmap_exists(table, "ggggggggg"));
-	cl_assert(!git_strmap_exists(table, "aaaaaaaab"));
-	cl_assert(!git_strmap_exists(table, "abcdefghi"));
+	cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
+	cl_assert(git_strmap_exists(g_table, "ggggggggg"));
+	cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
+	cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
 
-	cl_assert(git_strmap_exists(table, "bbbbbbbbb"));
-	pos = git_strmap_lookup_index(table, "bbbbbbbbb");
-	cl_assert(git_strmap_valid_index(table, pos));
-	cl_assert_equal_s(git_strmap_value_at(table, pos), "bbbbbbbbb");
-	free(git_strmap_value_at(table, pos));
-	git_strmap_delete_at(table, pos);
+	cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
+	pos = git_strmap_lookup_index(g_table, "bbbbbbbbb");
+	cl_assert(git_strmap_valid_index(g_table, pos));
+	cl_assert_equal_s(git_strmap_value_at(g_table, pos), "bbbbbbbbb");
+	free(git_strmap_value_at(g_table, pos));
+	git_strmap_delete_at(g_table, pos);
 
-	cl_assert(!git_strmap_exists(table, "bbbbbbbbb"));
+	cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
 
 	i = 0;
-	git_strmap_foreach_value(table, str, { i++; free(str); });
+	git_strmap_foreach_value(g_table, str, { i++; free(str); });
 	cl_assert(i == 19);
-
-	git_strmap_free(table);
 }
 
 void test_core_strmap__3(void)
 {
 	int i;
 	char *str;
-	git_strmap *table = git_strmap_alloc();
-	cl_assert(table != NULL);
 
-	insert_strings(table, 10000);
+	insert_strings(g_table, 10000);
 
 	i = 0;
-	git_strmap_foreach_value(table, str, { i++; free(str); });
+	git_strmap_foreach_value(g_table, str, { i++; free(str); });
 	cl_assert(i == 10000);
-
-	git_strmap_free(table);
 }
diff --git a/tests/core/vector.c b/tests/core/vector.c
index db52c00..66f90b8 100644
--- a/tests/core/vector.c
+++ b/tests/core/vector.c
@@ -190,8 +190,9 @@ void test_core_vector__5(void)
 	git_vector_free(&x);
 }
 
-static int remove_ones(const git_vector *v, size_t idx)
+static int remove_ones(const git_vector *v, size_t idx, void *p)
 {
+	GIT_UNUSED(p);
 	return (git_vector_get(v, idx) == (void *)0x001);
 }
 
@@ -206,7 +207,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 1);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 0);
 
 	git_vector_insert(&x, (void*) 0x001);
@@ -214,7 +215,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 3);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 0);
 
 	git_vector_insert(&x, (void*) 0x002);
@@ -223,7 +224,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -238,7 +239,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -253,7 +254,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -268,7 +269,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x003);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 4);
 
 	git_vector_free(&x);
diff --git a/tests/diff/diff_helpers.c b/tests/diff/diff_helpers.c
index 33bb561..279cb20 100644
--- a/tests/diff/diff_helpers.c
+++ b/tests/diff/diff_helpers.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "diff_helpers.h"
+#include "git2/sys/diff.h"
 
 git_tree *resolve_commit_oid_to_tree(
 	git_repository *repo,
@@ -215,32 +216,16 @@ abort:
 	return GIT_EUSER;
 }
 
-static int diff_print_cb(
-	const git_diff_delta *delta,
-	const git_diff_hunk *hunk,
-	const git_diff_line *line,
-	void *payload)
-{
-	FILE *fp = payload;
-
-	GIT_UNUSED(delta); GIT_UNUSED(hunk);
-
-	if (line->origin == GIT_DIFF_LINE_CONTEXT ||
-		line->origin == GIT_DIFF_LINE_ADDITION ||
-		line->origin == GIT_DIFF_LINE_DELETION)
-		fputc(line->origin, fp);
-	fwrite(line->content, 1, line->content_len, fp);
-	return 0;
-}
-
 void diff_print(FILE *fp, git_diff *diff)
 {
-	cl_git_pass(git_diff_print(
-		diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, fp ? fp : stderr));
+	cl_git_pass(
+		git_diff_print(diff, GIT_DIFF_FORMAT_PATCH,
+			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
 }
 
 void diff_print_raw(FILE *fp, git_diff *diff)
 {
-	cl_git_pass(git_diff_print(
-		diff, GIT_DIFF_FORMAT_RAW, diff_print_cb, fp ? fp : stderr));
+	cl_git_pass(
+		git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
+			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
 }
diff --git a/tests/diff/iterator.c b/tests/diff/iterator.c
index 891d8a6..cdc64eb 100644
--- a/tests/diff/iterator.c
+++ b/tests/diff/iterator.c
@@ -355,6 +355,7 @@ static void index_iterator_test(
 	const char *sandbox,
 	const char *start,
 	const char *end,
+	git_iterator_flag_t flags,
 	int expected_count,
 	const char **expected_names,
 	const char **expected_oids)
@@ -362,11 +363,13 @@ static void index_iterator_test(
 	git_index *index;
 	git_iterator *i;
 	const git_index_entry *entry;
-	int error, count = 0;
+	int error, count = 0, caps;
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
 	cl_git_pass(git_repository_index(&index, repo));
-	cl_git_pass(git_iterator_for_index(&i, index, 0, start, end));
+	caps = git_index_caps(index);
+
+	cl_git_pass(git_iterator_for_index(&i, index, flags, start, end));
 
 	while (!(error = git_iterator_advance(&entry, i))) {
 		cl_assert(entry);
@@ -388,6 +391,8 @@ static void index_iterator_test(
 	cl_assert_equal_i(expected_count, count);
 
 	git_iterator_free(i);
+
+	cl_assert(caps == git_index_caps(index));
 	git_index_free(index);
 }
 
@@ -446,7 +451,8 @@ static const char *expected_index_oids_0[] = {
 void test_diff_iterator__index_0(void)
 {
 	index_iterator_test(
-		"attr", NULL, NULL, 23, expected_index_0, expected_index_oids_0);
+		"attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0),
+		expected_index_0, expected_index_oids_0);
 }
 
 static const char *expected_index_range[] = {
@@ -466,25 +472,26 @@ static const char *expected_index_oids_range[] = {
 void test_diff_iterator__index_range(void)
 {
 	index_iterator_test(
-		"attr", "root", "root", 4, expected_index_range, expected_index_oids_range);
+		"attr", "root", "root", 0, ARRAY_SIZE(expected_index_range),
+		expected_index_range, expected_index_oids_range);
 }
 
 void test_diff_iterator__index_range_empty_0(void)
 {
 	index_iterator_test(
-		"attr", "empty", "empty", 0, NULL, NULL);
+		"attr", "empty", "empty", 0, 0, NULL, NULL);
 }
 
 void test_diff_iterator__index_range_empty_1(void)
 {
 	index_iterator_test(
-		"attr", "z_empty_after", NULL, 0, NULL, NULL);
+		"attr", "z_empty_after", NULL, 0, 0, NULL, NULL);
 }
 
 void test_diff_iterator__index_range_empty_2(void)
 {
 	index_iterator_test(
-		"attr", NULL, ".aaa_empty_before", 0, NULL, NULL);
+		"attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL);
 }
 
 static const char *expected_index_1[] = {
@@ -522,9 +529,45 @@ static const char* expected_index_oids_1[] = {
 void test_diff_iterator__index_1(void)
 {
 	index_iterator_test(
-		"status", NULL, NULL, 13, expected_index_1, expected_index_oids_1);
+		"status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1),
+		expected_index_1, expected_index_oids_1);
 }
 
+static const char *expected_index_cs[] = {
+	"B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c",
+	"a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c",
+};
+
+static const char *expected_index_ci[] = {
+	"a", "B", "c", "D", "e", "F", "g", "H", "i", "J",
+	"k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D",
+};
+
+void test_diff_iterator__index_case_folding(void)
+{
+	git_buf path = GIT_BUF_INIT;
+	int fs_is_ci = 0;
+
+	cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg"));
+	fs_is_ci = git_path_exists(path.ptr);
+	git_buf_free(&path);
+
+	index_iterator_test(
+		"icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs),
+		fs_is_ci ? expected_index_ci : expected_index_cs, NULL);
+
+	cl_git_sandbox_cleanup();
+
+	index_iterator_test(
+		"icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE,
+		ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL);
+
+	cl_git_sandbox_cleanup();
+
+	index_iterator_test(
+		"icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE,
+		ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL);
+}
 
 /* -- WORKDIR ITERATOR TESTS -- */
 
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 6e28af1..fa5c0bb 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -544,36 +544,22 @@ void test_index_tests__corrupted_extension(void)
 	cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
 }
 
-static void assert_index_is_sorted(git_index *index)
-{
-	git_vector *entries = &index->entries;
-	size_t i;
-
-	cl_assert(git_vector_is_sorted(entries));
-
-	for (i = 1; i < git_vector_length(entries); ++i) {
-		git_index_entry *prev = git_vector_get(entries, i - 1);
-		git_index_entry *curr = git_vector_get(entries, i);
-		cl_assert(index->entries._cmp(prev, curr) <= 0);
-	}
-}
-
 void test_index_tests__reload_while_ignoring_case(void)
 {
 	git_index *index;
 	unsigned int caps;
 
 	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
-	assert_index_is_sorted(index);
+	cl_git_pass(git_vector_verify_sorted(&index->entries));
 
 	caps = git_index_caps(index);
 	cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEXCAP_IGNORE_CASE));
 	cl_git_pass(git_index_read(index, true));
-	assert_index_is_sorted(index);
+	cl_git_pass(git_vector_verify_sorted(&index->entries));
 
 	cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
 	cl_git_pass(git_index_read(index, true));
-	assert_index_is_sorted(index);
+	cl_git_pass(git_vector_verify_sorted(&index->entries));
 
 	git_index_free(index);
 }
diff --git a/tests/status/ignore.c b/tests/status/ignore.c
index d6c26a8..052a8ea 100644
--- a/tests/status/ignore.c
+++ b/tests/status/ignore.c
@@ -54,8 +54,10 @@ void test_status_ignore__0(void)
 	}
 
 	/* confirm that ignore files were cached */
-	cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/exclude"));
-	cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitignore"));
+	cl_assert(git_attr_cache__is_cached(
+		g_repo, GIT_ATTR_FILE__FROM_FILE, ".git/info/exclude"));
+	cl_assert(git_attr_cache__is_cached(
+		g_repo, GIT_ATTR_FILE__FROM_FILE, ".gitignore"));
 }
 
 
diff --git a/tests/threads/diff.c b/tests/threads/diff.c
new file mode 100644
index 0000000..79b8580
--- /dev/null
+++ b/tests/threads/diff.c
@@ -0,0 +1,182 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+
+static git_repository *_repo;
+static git_tree *_a, *_b;
+static git_atomic _counts[4];
+static int _check_counts;
+
+#define THREADS 20
+
+void test_threads_diff__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+static void setup_trees(void)
+{
+	git_index *idx;
+
+	_repo = cl_git_sandbox_reopen(); /* reopen sandbox to flush caches */
+
+	/* avoid competing to load initial index */
+	cl_git_pass(git_repository_index(&idx, _repo));
+	git_index_free(idx);
+
+	cl_git_pass(git_revparse_single(
+		(git_object **)&_a, _repo, "0017bd4ab1^{tree}"));
+	cl_git_pass(git_revparse_single(
+		(git_object **)&_b, _repo, "26a125ee1b^{tree}"));
+
+	memset(_counts, 0, sizeof(_counts));
+}
+
+static void free_trees(void)
+{
+	git_tree_free(_a); _a = NULL;
+	git_tree_free(_b); _b = NULL;
+
+	if (_check_counts) {
+		cl_assert_equal_i(288, git_atomic_get(&_counts[0]));
+		cl_assert_equal_i(112, git_atomic_get(&_counts[1]));
+		cl_assert_equal_i( 80, git_atomic_get(&_counts[2]));
+		cl_assert_equal_i( 96, git_atomic_get(&_counts[3]));
+	}
+}
+
+static void *run_index_diffs(void *arg)
+{
+	int thread = *(int *)arg;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	git_diff *diff = NULL;
+	size_t i;
+	int exp[4] = { 0, 0, 0, 0 };
+
+	switch (thread & 0x03) {
+	case 0: /* diff index to workdir */;
+		cl_git_pass(git_diff_index_to_workdir(&diff, _repo, NULL, &opts));
+		break;
+	case 1: /* diff tree 'a' to index */;
+		cl_git_pass(git_diff_tree_to_index(&diff, _repo, _a, NULL, &opts));
+		break;
+	case 2: /* diff tree 'b' to index */;
+		cl_git_pass(git_diff_tree_to_index(&diff, _repo, _b, NULL, &opts));
+		break;
+	case 3: /* diff index to workdir (explicit index) */;
+		{
+			git_index *idx;
+			cl_git_pass(git_repository_index(&idx, _repo));
+			cl_git_pass(git_diff_index_to_workdir(&diff, _repo, idx, &opts));
+			git_index_free(idx);
+			break;
+		}
+	}
+
+	/* keep some diff stats to make sure results are as expected */
+
+	i = git_diff_num_deltas(diff);
+	git_atomic_add(&_counts[0], (int32_t)i);
+	exp[0] = (int)i;
+
+	while (i > 0) {
+		switch (git_diff_get_delta(diff, --i)->status) {
+		case GIT_DELTA_MODIFIED: exp[1]++; git_atomic_inc(&_counts[1]); break;
+		case GIT_DELTA_ADDED:    exp[2]++; git_atomic_inc(&_counts[2]); break;
+		case GIT_DELTA_DELETED:  exp[3]++; git_atomic_inc(&_counts[3]); break;
+		default: break;
+		}
+	}
+
+	switch (thread & 0x03) {
+	case 0: case 3:
+		cl_assert_equal_i(8, exp[0]); cl_assert_equal_i(4, exp[1]);
+		cl_assert_equal_i(0, exp[2]); cl_assert_equal_i(4, exp[3]);
+		break;
+	case 1:
+		cl_assert_equal_i(12, exp[0]); cl_assert_equal_i(3, exp[1]);
+		cl_assert_equal_i(7, exp[2]); cl_assert_equal_i(2, exp[3]);
+		break;
+	case 2:
+		cl_assert_equal_i(8, exp[0]); cl_assert_equal_i(3, exp[1]);
+		cl_assert_equal_i(3, exp[2]); cl_assert_equal_i(2, exp[3]);
+		break;
+	}
+
+	git_diff_free(diff);
+	giterr_clear();
+
+	return arg;
+}
+
+void test_threads_diff__concurrent_diffs(void)
+{
+	_repo = cl_git_sandbox_init("status");
+	_check_counts = 1;
+
+	run_in_parallel(
+		5, 32, run_index_diffs, setup_trees, free_trees);
+}
+
+static void *run_index_diffs_with_modifier(void *arg)
+{
+	int thread = *(int *)arg;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	git_diff *diff = NULL;
+	git_index *idx = NULL;
+
+	cl_git_pass(git_repository_index(&idx, _repo));
+
+	/* have first thread altering the index as we go */
+	if (thread == 0) {
+		int i;
+
+		for (i = 0; i < 300; ++i) {
+			switch (i & 0x03) {
+			case 0: (void)git_index_add_bypath(idx, "new_file"); break;
+			case 1: (void)git_index_remove_bypath(idx, "modified_file"); break;
+			case 2: (void)git_index_remove_bypath(idx, "new_file"); break;
+			case 3: (void)git_index_add_bypath(idx, "modified_file"); break;
+			}
+			git_thread_yield();
+		}
+
+		goto done;
+	}
+
+	/* only use explicit index in this test to prevent reloading */
+
+	switch (thread & 0x03) {
+	case 0: /* diff index to workdir */;
+		cl_git_pass(git_diff_index_to_workdir(&diff, _repo, idx, &opts));
+		break;
+	case 1: /* diff tree 'a' to index */;
+		cl_git_pass(git_diff_tree_to_index(&diff, _repo, _a, idx, &opts));
+		break;
+	case 2: /* diff tree 'b' to index */;
+		cl_git_pass(git_diff_tree_to_index(&diff, _repo, _b, idx, &opts));
+		break;
+	case 3: /* diff index to workdir reversed */;
+		opts.flags |= GIT_DIFF_REVERSE;
+		cl_git_pass(git_diff_index_to_workdir(&diff, _repo, idx, &opts));
+		break;
+	}
+
+	/* results will be unpredictable with index modifier thread running */
+
+	git_diff_free(diff);
+
+done:
+	git_index_free(idx);
+	giterr_clear();
+
+	return arg;
+}
+
+void test_threads_diff__with_concurrent_index_modified(void)
+{
+	_repo = cl_git_sandbox_init("status");
+	_check_counts = 0;
+
+	run_in_parallel(
+		5, 16, run_index_diffs_with_modifier, setup_trees, free_trees);
+}
diff --git a/tests/threads/iterator.c b/tests/threads/iterator.c
new file mode 100644
index 0000000..8aeae1a
--- /dev/null
+++ b/tests/threads/iterator.c
@@ -0,0 +1,49 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+#include "iterator.h"
+
+static git_repository *_repo;
+
+void test_threads_iterator__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+static void *run_workdir_iterator(void *arg)
+{
+	int error = 0;
+	git_iterator *iter;
+	const git_index_entry *entry = NULL;
+
+	cl_git_pass(git_iterator_for_workdir(
+		&iter, _repo, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
+
+	while (!error) {
+		if (entry && entry->mode == GIT_FILEMODE_TREE) {
+			error = git_iterator_advance_into(&entry, iter);
+
+			if (error == GIT_ENOTFOUND)
+				error = git_iterator_advance(&entry, iter);
+		} else {
+			error = git_iterator_advance(&entry, iter);
+		}
+
+		if (!error)
+			(void)git_iterator_current_is_ignored(iter);
+	}
+
+	cl_assert_equal_i(GIT_ITEROVER, error);
+
+	git_iterator_free(iter);
+	giterr_clear();
+	return arg;
+}
+
+
+void test_threads_iterator__workdir(void)
+{
+	_repo = cl_git_sandbox_init("status");
+
+	run_in_parallel(
+		1, 20, run_workdir_iterator, NULL, NULL);
+}
diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c
index fbf6ac0..3b35b45 100644
--- a/tests/threads/refdb.c
+++ b/tests/threads/refdb.c
@@ -37,6 +37,7 @@ static void *iterate_refs(void *arg)
 
 	git_reference_iterator_free(i);
 
+	giterr_clear();
 	return arg;
 }
 
@@ -115,6 +116,7 @@ static void *create_refs(void *arg)
 	for (i = 0; i < 10; ++i)
 		git_reference_free(ref[i]);
 
+	giterr_clear();
 	return arg;
 }
 
@@ -141,6 +143,7 @@ static void *delete_refs(void *arg)
 		}
 	}
 
+	giterr_clear();
 	return arg;
 }
 
diff --git a/tests/threads/thread_helpers.c b/tests/threads/thread_helpers.c
new file mode 100644
index 0000000..25370dd
--- /dev/null
+++ b/tests/threads/thread_helpers.c
@@ -0,0 +1,44 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+
+void run_in_parallel(
+	int repeats,
+	int threads,
+	void *(*func)(void *),
+	void (*before_test)(void),
+	void (*after_test)(void))
+{
+	int r, t, *id = git__calloc(threads, sizeof(int));
+#ifdef GIT_THREADS
+	git_thread *th = git__calloc(threads, sizeof(git_thread));
+	cl_assert(th != NULL);
+#else
+	void *th = NULL;
+#endif
+
+	cl_assert(id != NULL);
+
+	for (r = 0; r < repeats; ++r) {
+		if (before_test) before_test();
+
+		for (t = 0; t < threads; ++t) {
+			id[t] = t;
+#ifdef GIT_THREADS
+			cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
+#else
+			cl_assert(func(&id[t]) == &id[t]);
+#endif
+		}
+
+#ifdef GIT_THREADS
+		for (t = 0; t < threads; ++t)
+			cl_git_pass(git_thread_join(th[t], NULL));
+		memset(th, 0, threads * sizeof(git_thread));
+#endif
+
+		if (after_test) after_test();
+	}
+
+	git__free(id);
+	git__free(th);
+}
diff --git a/tests/threads/thread_helpers.h b/tests/threads/thread_helpers.h
new file mode 100644
index 0000000..3c13cfb
--- /dev/null
+++ b/tests/threads/thread_helpers.h
@@ -0,0 +1,8 @@
+#include "thread-utils.h"
+
+void run_in_parallel(
+	int repeats,
+	int threads,
+	void *(*func)(void *),
+	void (*before_test)(void),
+	void (*after_test)(void));