Commit 95c148b2c772b5f97e0db60bff089d8fa944971c

Vicent Martí 2013-10-08T17:03:12

Merge pull request #1886 from libgit2/precompose-utf8 Add support for core.precomposeunicode on Mac

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
diff --git a/AUTHORS b/AUTHORS
index 587da24..f3a03ee 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -68,5 +68,6 @@ Sven Strickroth
 Tim Branyen
 Tim Clem
 Tim Harder
+Torsten Bögershausen
 Trent Mick
 Vicent Marti
diff --git a/CMakeLists.txt b/CMakeLists.txt
index df39364..c741499 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,9 +27,14 @@ OPTION( BUILD_EXAMPLES		"Build library usage example apps"		OFF )
 OPTION( TAGS				"Generate tags"							OFF )
 OPTION( PROFILE				"Generate profiling information"		OFF )
 OPTION( ENABLE_TRACE		"Enables tracing support"				OFF )
-OPTION( LIBGIT2_FILENAME	"Name of the produced binary" OFF )
+OPTION( LIBGIT2_FILENAME	"Name of the produced binary"			OFF )
 
-OPTION( ANDROID				"Build for android NDK" OFF )
+OPTION( ANDROID				"Build for android NDK"	 				OFF )
+
+OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
+IF(APPLE)
+	SET( USE_ICONV ON )
+ENDIF()
 
 IF(MSVC)
 	# This option is only available when building with MSVC. By default, libgit2
@@ -58,7 +63,13 @@ FUNCTION(TARGET_OS_LIBRARIES target)
 		TARGET_LINK_LIBRARIES(${target} socket nsl)
 	ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
 		TARGET_LINK_LIBRARIES(${target} rt)
-	ENDIF ()
+	ENDIF()
+
+	IF(USE_ICONV)
+		TARGET_LINK_LIBRARIES(${target} iconv)
+		ADD_DEFINITIONS(-DGIT_USE_ICONV)
+	ENDIF()
+
 	IF(THREADSAFE)
 		TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
 	ENDIF()
diff --git a/git.git-authors b/git.git-authors
index 3fcf27f..7d4c95b 100644
--- a/git.git-authors
+++ b/git.git-authors
@@ -68,3 +68,4 @@ ok	Sebastian Schuberth <sschuberth@gmail.com>
 ok	Shawn O. Pearce <spearce@spearce.org>
 ok	Steffen Prohaska <prohaska@zib.de>
 ok	Sven Verdoolaege <skimo@kotnet.org>
+ok	Torsten Bögershausen <tboegi@web.de>
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 4871e98..4041947 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -453,7 +453,7 @@ GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
 GIT_EXTERN(int) git_reference_is_tag(git_reference *ref);
 
 typedef enum {
-	GIT_REF_FORMAT_NORMAL = 0,
+	GIT_REF_FORMAT_NORMAL = 0u,
 
 	/**
 	 * Control whether one-level refnames are accepted
@@ -461,7 +461,7 @@ typedef enum {
 	 * components). Those are expected to be written only using
 	 * uppercase letters and underscore (FETCH_HEAD, ...)
 	 */
-	GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
+	GIT_REF_FORMAT_ALLOW_ONELEVEL = (1u << 0),
 
 	/**
 	 * Interpret the provided name as a reference pattern for a
@@ -470,14 +470,14 @@ typedef enum {
 	 * in place of a one full pathname component
 	 * (e.g., foo/<star>/bar but not foo/bar<star>).
 	 */
-	GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
+	GIT_REF_FORMAT_REFSPEC_PATTERN = (1u << 1),
 
 	/**
 	 * Interpret the name as part of a refspec in shorthand form
 	 * so the `ONELEVEL` naming rules aren't enforced and 'master'
 	 * becomes a valid name.
 	 */
-	GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1 << 2),
+	GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1u << 2),
 } git_reference_normalize_t;
 
 /**
diff --git a/include/git2/sys/repository.h b/include/git2/sys/repository.h
index ba3d65a..36f8b58 100644
--- a/include/git2/sys/repository.h
+++ b/include/git2/sys/repository.h
@@ -27,7 +27,6 @@ GIT_BEGIN_DECL
  */
 GIT_EXTERN(int) git_repository_new(git_repository **out);
 
-
 /**
  * Reset all the internal state in a repository.
  *
@@ -42,6 +41,25 @@ GIT_EXTERN(int) git_repository_new(git_repository **out);
 GIT_EXTERN(void) git_repository__cleanup(git_repository *repo);
 
 /**
+ * Update the filesystem config settings for an open repository
+ *
+ * When a repository is initialized, config values are set based on the
+ * properties of the filesystem that the repository is on, such as
+ * "core.ignorecase", "core.filemode", "core.symlinks", etc.  If the
+ * repository is moved to a new filesystem, these properties may no
+ * longer be correct and API calls may not behave as expected.  This
+ * call reruns the phase of repository initialization that sets those
+ * properties to compensate for the current filesystem of the repo.
+ *
+ * @param repo A repository object
+ * @param recurse_submodules Should submodules be updated recursively
+ * @returrn 0 on success, < 0 on error
+ */
+GIT_EXTERN(int) git_repository_reinit_filesystem(
+	git_repository *repo,
+	int recurse_submodules);
+
+/**
  * Set the configuration file for this repository
  *
  * This configuration file will be used for all configuration
diff --git a/src/checkout.c b/src/checkout.c
index 5d741d3..d3f673d 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -783,7 +783,8 @@ static int checkout_update_index(
 
 	memset(&entry, 0, sizeof(entry));
 	entry.path = (char *)file->path; /* cast to prevent warning */
-	git_index_entry__init_from_stat(&entry, st);
+	git_index_entry__init_from_stat(
+		&entry, st, !(git_index_caps(data->index) & GIT_INDEXCAP_NO_FILEMODE));
 	git_oid_cpy(&entry.oid, &file->oid);
 
 	return git_index_add(data->index, &entry);
diff --git a/src/clone.c b/src/clone.c
index f3e365c..6572439 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -391,11 +391,11 @@ int git_clone(
 	const char *local_path,
 	const git_clone_options *_options)
 {
-	int retcode = GIT_ERROR;
+	int error = 0;
 	git_repository *repo = NULL;
 	git_remote *origin;
 	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
-	int remove_directory_on_failure = 0;
+	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
 
 	assert(out && url && local_path);
 
@@ -408,33 +408,29 @@ int git_clone(
 	if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
 		giterr_set(GITERR_INVALID,
 			"'%s' exists and is not an empty directory", local_path);
-		return GIT_ERROR;
+		return GIT_EEXISTS;
 	}
 
-	/* Only remove the directory on failure if we create it */
-	remove_directory_on_failure = !git_path_exists(local_path);
+	/* Only remove the root directory on failure if we create it */
+	if (git_path_exists(local_path))
+		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
 
-	if ((retcode = git_repository_init(&repo, local_path, options.bare)) < 0)
-		return retcode;
+	if ((error = git_repository_init(&repo, local_path, options.bare)) < 0)
+		return error;
 
-	if ((retcode = create_and_configure_origin(&origin, repo, url, &options)) < 0)
-		goto cleanup;
+	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
+		error = git_clone_into(
+			repo, origin, &options.checkout_opts, options.checkout_branch);
 
-	retcode = git_clone_into(repo, origin, &options.checkout_opts, options.checkout_branch);
-	git_remote_free(origin);
+		git_remote_free(origin);
+	}
 
-	if (retcode < 0)
-		goto cleanup;
+	if (error < 0) {
+		git_repository_free(repo);
+		repo = NULL;
+		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
+	}
 
 	*out = repo;
-	return 0;
-
-cleanup:
-	git_repository_free(repo);
-	if (remove_directory_on_failure)
-		git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES);
-	else
-		git_futils_cleanupdir_r(local_path);
-
-	return retcode;
+	return error;
 }
diff --git a/src/config.h b/src/config.h
index 85db5e3..01e8465 100644
--- a/src/config.h
+++ b/src/config.h
@@ -47,7 +47,7 @@ extern int git_config_rename_section(
  * @param out the new backend
  * @param path where the config file is located
  */
-extern int git_config_file__ondisk(struct git_config_backend **out, const char *path);
+extern int git_config_file__ondisk(git_config_backend **out, const char *path);
 
 extern int git_config__normalize_name(const char *in, char **out);
 
diff --git a/src/config_cache.c b/src/config_cache.c
index 84de3a5..6808521 100644
--- a/src/config_cache.c
+++ b/src/config_cache.c
@@ -67,6 +67,7 @@ static struct map_data _cvar_maps[] = {
 	{"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
 	{"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
 	{"core.abbrev", _cvar_map_int, 1, GIT_ABBREV_DEFAULT },
+	{"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
 };
 
 int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar)
diff --git a/src/errors.c b/src/errors.c
index e2629f6..c9d9e4e 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -116,4 +116,3 @@ const git_error *giterr_last(void)
 {
 	return GIT_GLOBAL->last_error;
 }
-
diff --git a/src/fileops.c b/src/fileops.c
index 5a5041c..63aedc6 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -78,11 +78,8 @@ int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, con
 int git_futils_open_ro(const char *path)
 {
 	int fd = p_open(path, O_RDONLY);
-	if (fd < 0) {
-		if (errno == ENOENT || errno == ENOTDIR)
-			fd = GIT_ENOTFOUND;
-		giterr_set(GITERR_OS, "Failed to open '%s'", path);
-	}
+	if (fd < 0)
+		return git_path_set_error(errno, path, "open");
 	return fd;
 }
 
@@ -138,7 +135,6 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
 int git_futils_readbuffer_updated(
 	git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
 {
-	int error = 0;
 	git_file fd;
 	struct stat st;
 	bool changed = false;
@@ -148,13 +144,8 @@ int git_futils_readbuffer_updated(
 	if (updated != NULL)
 		*updated = 0;
 
-	if (p_stat(path, &st) < 0) {
-		error = errno;
-		giterr_set(GITERR_OS, "Failed to stat '%s'", path);
-		if (error == ENOENT || error == ENOTDIR)
-			return GIT_ENOTFOUND;
-		return -1;
-	}
+	if (p_stat(path, &st) < 0)
+		return git_path_set_error(errno, path, "stat");
 
 	if (S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
 		giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
@@ -398,8 +389,11 @@ typedef struct {
 	size_t baselen;
 	uint32_t flags;
 	int error;
+	int depth;
 } futils__rmdir_data;
 
+#define FUTILS_MAX_DEPTH 100
+
 static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
 {
 	if (filemsg)
@@ -438,51 +432,60 @@ static int futils__rm_first_parent(git_buf *path, const char *ceiling)
 
 static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
 {
-	struct stat st;
 	futils__rmdir_data *data = opaque;
+	int error = data->error;
+	struct stat st;
+
+	if (data->depth > FUTILS_MAX_DEPTH)
+		error = futils__error_cannot_rmdir(
+			path->ptr, "directory nesting too deep");
 
-	if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) {
+	else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
 		if (errno == ENOENT)
-			data->error = 0;
+			error = 0;
 		else if (errno == ENOTDIR) {
 			/* asked to remove a/b/c/d/e and a/b is a normal file */
 			if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
-				data->error = futils__rm_first_parent(path, data->base);
+				error = futils__rm_first_parent(path, data->base);
 			else
 				futils__error_cannot_rmdir(
 					path->ptr, "parent is not directory");
 		}
 		else
-			futils__error_cannot_rmdir(path->ptr, "cannot access");
+			error = git_path_set_error(errno, path->ptr, "rmdir");
 	}
 
 	else if (S_ISDIR(st.st_mode)) {
-		int error = git_path_direach(path, futils__rmdir_recurs_foreach, data);
+		data->depth++;
+
+		error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
 		if (error < 0)
 			return (error == GIT_EUSER) ? data->error : error;
 
-		data->error = p_rmdir(path->ptr);
+		data->depth--;
+
+		if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
+			return data->error;
 
-		if (data->error < 0) {
+		if ((error = p_rmdir(path->ptr)) < 0) {
 			if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
 				(errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
-				data->error = 0;
+				error = 0;
 			else
-				futils__error_cannot_rmdir(path->ptr, NULL);
+				error = git_path_set_error(errno, path->ptr, "rmdir");
 		}
 	}
 
 	else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
-		data->error = p_unlink(path->ptr);
-
-		if (data->error < 0)
-			futils__error_cannot_rmdir(path->ptr, "cannot be removed");
+		if (p_unlink(path->ptr) < 0)
+			error = git_path_set_error(errno, path->ptr, "remove");
 	}
 
 	else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
-		data->error = futils__error_cannot_rmdir(path->ptr, "still present");
+		error = futils__error_cannot_rmdir(path->ptr, "still present");
 
-	return data->error;
+	data->error = error;
+	return error;
 }
 
 static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
@@ -505,7 +508,7 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
 			giterr_clear();
 			error = GIT_ITEROVER;
 		} else {
-			futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
+			error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
 		}
 	}
 
@@ -517,7 +520,7 @@ int git_futils_rmdir_r(
 {
 	int error;
 	git_buf fullpath = GIT_BUF_INIT;
-	futils__rmdir_data data;
+	futils__rmdir_data data = { 0 };
 
 	/* build path and find "root" where we should start calling mkdir */
 	if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
@@ -526,7 +529,6 @@ int git_futils_rmdir_r(
 	data.base    = base ? base : "";
 	data.baselen = base ? strlen(base) : 0;
 	data.flags   = flags;
-	data.error   = 0;
 
 	error = futils__rmdir_recurs_foreach(&data, &fullpath);
 
@@ -544,41 +546,6 @@ int git_futils_rmdir_r(
 	return error;
 }
 
-int git_futils_cleanupdir_r(const char *path)
-{
-	int error;
-	git_buf fullpath = GIT_BUF_INIT;
-	futils__rmdir_data data;
-
-	if ((error = git_buf_put(&fullpath, path, strlen(path))) < 0)
-		goto clean_up;
-
-	data.base    = "";
-	data.baselen = 0;
-	data.flags   = GIT_RMDIR_REMOVE_FILES;
-	data.error   = 0;
-
-	if (!git_path_exists(path)) {
-		giterr_set(GITERR_OS, "Path does not exist: %s" , path);
-		error = GIT_ERROR;
-		goto clean_up;
-	}
-
-	if (!git_path_isdir(path)) {
-		giterr_set(GITERR_OS, "Path is not a directory: %s" , path);
-		error = GIT_ERROR;
-		goto clean_up;
-	}
-
-	error = git_path_direach(&fullpath, futils__rmdir_recurs_foreach, &data);
-	if (error == GIT_EUSER)
-		error = data.error;
-
-clean_up:
-	git_buf_free(&fullpath);
-	return error;
-}
-
 
 static int git_futils_guess_system_dirs(git_buf *out)
 {
@@ -836,11 +803,8 @@ int git_futils_cp(const char *from, const char *to, mode_t filemode)
 		return ifd;
 
 	if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
-		if (errno == ENOENT || errno == ENOTDIR)
-			ofd = GIT_ENOTFOUND;
-		giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
 		p_close(ifd);
-		return ofd;
+		return git_path_set_error(errno, to, "open for writing");
 	}
 
 	return cp_by_fd(ifd, ofd, true);
@@ -923,15 +887,14 @@ static int _cp_r_callback(void *ref, git_buf *from)
 		goto exit;
 	}
 
-	if (p_lstat(info->to.ptr, &to_st) < 0) {
-		if (errno != ENOENT && errno != ENOTDIR) {
-			giterr_set(GITERR_OS,
-				"Could not access %s while copying files", info->to.ptr);
-			error = -1;
-			goto exit;
-		}
-	} else
+	if (!(error = git_path_lstat(info->to.ptr, &to_st)))
 		exists = true;
+	else if (error != GIT_ENOTFOUND)
+		goto exit;
+	else {
+		giterr_clear();
+		error = 0;
+	}
 
 	if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
 		goto exit;
@@ -948,9 +911,12 @@ static int _cp_r_callback(void *ref, git_buf *from)
 			error = _cp_r_mkdir(info, from);
 
 		/* recurse onto target directory */
-		if (!error && (!exists || S_ISDIR(to_st.st_mode)) &&
-			((error = git_path_direach(from, _cp_r_callback, info)) == GIT_EUSER))
-			error = info->error;
+		if (!error && (!exists || S_ISDIR(to_st.st_mode))) {
+			error = git_path_direach(from, 0, _cp_r_callback, info);
+
+			if (error == GIT_EUSER)
+				error = info->error;
+		}
 
 		if (oldmode != 0)
 			info->dirmode = oldmode;
diff --git a/src/fileops.h b/src/fileops.h
index 6bd693b..1b2728e 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -115,12 +115,7 @@ extern int git_futils_mkpath2file(const char *path, const mode_t mode);
  * * GIT_RMDIR_EMPTY_PARENTS   - remove containing directories up to base
  *       if removing this item leaves them empty
  * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
- *
- * The old values translate into the new as follows:
- *
- * * GIT_DIRREMOVAL_EMPTY_HIERARCHY == GIT_RMDIR_EMPTY_HIERARCHY
- * * GIT_DIRREMOVAL_FILES_AND_DIRS  ~= GIT_RMDIR_REMOVE_FILES
- * * GIT_DIRREMOVAL_ONLY_EMPTY_DIRS == GIT_RMDIR_SKIP_NONEMPTY
+ * * GIT_RMDIR_SKIP_ROOT       - don't remove root directory itself
  */
 typedef enum {
 	GIT_RMDIR_EMPTY_HIERARCHY = 0,
@@ -128,6 +123,7 @@ typedef enum {
 	GIT_RMDIR_SKIP_NONEMPTY   = (1 << 1),
 	GIT_RMDIR_EMPTY_PARENTS   = (1 << 2),
 	GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3),
+	GIT_RMDIR_SKIP_ROOT       = (1 << 4),
 } git_futils_rmdir_flags;
 
 /**
@@ -141,14 +137,6 @@ typedef enum {
 extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags);
 
 /**
- * Remove all files and directories beneath the specified path.
- *
- * @param path Path to the top level directory to process.
- * @return 0 on success; -1 on error.
- */
-extern int git_futils_cleanupdir_r(const char *path);
-
-/**
  * Create and open a temporary file with a `_git2_` suffix.
  * Writes the filename into path_out.
  * @return On success, an open file descriptor, else an error code < 0.
diff --git a/src/index.c b/src/index.c
index c923f16..5cdd40a 100644
--- a/src/index.c
+++ b/src/index.c
@@ -580,7 +580,8 @@ const git_index_entry *git_index_get_bypath(
 	return git_index_get_byindex(index, pos);
 }
 
-void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st)
+void git_index_entry__init_from_stat(
+	git_index_entry *entry, struct stat *st, bool trust_mode)
 {
 	entry->ctime.seconds = (git_time_t)st->st_ctime;
 	entry->mtime.seconds = (git_time_t)st->st_mtime;
@@ -588,7 +589,8 @@ void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st)
 	/* entry->ctime.nanoseconds = st->st_ctimensec; */
 	entry->dev  = st->st_rdev;
 	entry->ino  = st->st_ino;
-	entry->mode = index_create_mode(st->st_mode);
+	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
+		index_create_mode(0666) : index_create_mode(st->st_mode);
 	entry->uid  = st->st_uid;
 	entry->gid  = st->st_gid;
 	entry->file_size = st->st_size;
@@ -632,7 +634,7 @@ static int index_entry_init(
 	entry = git__calloc(1, sizeof(git_index_entry));
 	GITERR_CHECK_ALLOC(entry);
 
-	git_index_entry__init_from_stat(entry, &st);
+	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
 
 	entry->oid = oid;
 	entry->path = git__strdup(rel_path);
diff --git a/src/index.h b/src/index.h
index 40577e1..4c448fa 100644
--- a/src/index.h
+++ b/src/index.h
@@ -48,7 +48,7 @@ struct git_index_conflict_iterator {
 };
 
 extern void git_index_entry__init_from_stat(
-	git_index_entry *entry, struct stat *st);
+	git_index_entry *entry, struct stat *st, bool trust_mode);
 
 extern size_t git_index__prefix_position(git_index *index, const char *path);
 
diff --git a/src/indexer.c b/src/indexer.c
index 4ce69fc..1270488 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -50,7 +50,7 @@ struct git_indexer_stream {
 
 	/* Fields for calculating the packfile trailer (hash of everything before it) */
 	char inbuf[GIT_OID_RAWSZ];
-	int inbuf_len;
+	size_t inbuf_len;
 	git_hash_ctx trailer;
 };
 
@@ -378,13 +378,13 @@ static int do_progress_callback(git_indexer_stream *idx, git_transfer_progress *
 /* Hash everything but the last 20B of input */
 static void hash_partially(git_indexer_stream *idx, const uint8_t *data, size_t size)
 {
-	int to_expell, to_keep;
+	size_t to_expell, to_keep;
 
 	if (size == 0)
 		return;
 
 	/* Easy case, dump the buffer and the data minus the last 20 bytes */
-	if (size >= 20) {
+	if (size >= GIT_OID_RAWSZ) {
 		git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len);
 		git_hash_update(&idx->trailer, data, size - GIT_OID_RAWSZ);
 
@@ -402,8 +402,8 @@ static void hash_partially(git_indexer_stream *idx, const uint8_t *data, size_t 
 	}
 
 	/* We need to partially drain the buffer and then append */
-	to_expell = abs(size - (GIT_OID_RAWSZ - idx->inbuf_len));
-	to_keep = abs(idx->inbuf_len - to_expell);
+	to_keep   = GIT_OID_RAWSZ - size;
+	to_expell = idx->inbuf_len - to_keep;
 
 	git_hash_update(&idx->trailer, idx->inbuf, to_expell);
 
diff --git a/src/iterator.c b/src/iterator.c
index bdc98d2..c0d7862 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -893,6 +893,7 @@ struct fs_iterator {
 	git_index_entry entry;
 	git_buf path;
 	size_t root_len;
+	uint32_t dirload_flags;
 	int depth;
 
 	int (*enter_dir_cb)(fs_iterator *self);
@@ -986,7 +987,7 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
 	GITERR_CHECK_ALLOC(ff);
 
 	error = git_path_dirload_with_stat(
-		fi->path.ptr, fi->root_len, iterator__ignore_case(fi),
+		fi->path.ptr, fi->root_len, fi->dirload_flags,
 		fi->base.start, fi->base.end, &ff->entries);
 
 	if (error < 0) {
@@ -1174,7 +1175,7 @@ static int fs_iterator__update_entry(fs_iterator *fi)
 		return GIT_ITEROVER;
 
 	fi->entry.path = ps->path;
-	git_index_entry__init_from_stat(&fi->entry, &ps->st);
+	git_index_entry__init_from_stat(&fi->entry, &ps->st, true);
 
 	/* need different mode here to keep directories during iteration */
 	fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
@@ -1207,6 +1208,11 @@ static int fs_iterator__initialize(
 	}
 	fi->root_len = fi->path.size;
 
+	fi->dirload_flags =
+		(iterator__ignore_case(fi) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
+		(iterator__flag(fi, PRECOMPOSE_UNICODE) ?
+			GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);
+
 	if ((error = fs_iterator__expand_dir(fi)) < 0) {
 		if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) {
 			giterr_clear();
@@ -1329,7 +1335,7 @@ int git_iterator_for_workdir_ext(
 	const char *start,
 	const char *end)
 {
-	int error;
+	int error, precompose = 0;
 	workdir_iterator *wi;
 
 	if (!repo_workdir) {
@@ -1356,6 +1362,12 @@ int git_iterator_for_workdir_ext(
 		return error;
 	}
 
+	/* try to look up precompose and set flag if appropriate */
+	if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
+		giterr_clear();
+	else if (precompose)
+		wi->fi.base.flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
+
 	return fs_iterator__initialize(out, &wi->fi, repo_workdir);
 }
 
diff --git a/src/iterator.h b/src/iterator.h
index ea88fa6..751e139 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -24,13 +24,15 @@ typedef enum {
 
 typedef enum {
 	/** ignore case for entry sort order */
-	GIT_ITERATOR_IGNORE_CASE = (1 << 0),
+	GIT_ITERATOR_IGNORE_CASE = (1u << 0),
 	/** force case sensitivity for entry sort order */
-	GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1),
+	GIT_ITERATOR_DONT_IGNORE_CASE = (1u << 1),
 	/** return tree items in addition to blob items */
-	GIT_ITERATOR_INCLUDE_TREES    = (1 << 2),
+	GIT_ITERATOR_INCLUDE_TREES    = (1u << 2),
 	/** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
-	GIT_ITERATOR_DONT_AUTOEXPAND  = (1 << 3),
+	GIT_ITERATOR_DONT_AUTOEXPAND  = (1u << 3),
+	/** convert precomposed unicode to decomposed unicode */
+	GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
 } git_iterator_flag_t;
 
 typedef struct {
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 4ff5715..07dfae5 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -544,7 +544,7 @@ static int locate_object_short_oid(
 
 	/* Explore directory to find a unique object matching short_oid */
 	error = git_path_direach(
-		object_location, fn_locate_object_short_oid, &state);
+		object_location, 0, fn_locate_object_short_oid, &state);
 
 	if (error && error != GIT_EUSER)
 		return error;
@@ -745,7 +745,7 @@ static int foreach_cb(void *_state, git_buf *path)
 {
 	struct foreach_state *state = (struct foreach_state *) _state;
 
-	return git_path_direach(path, foreach_object_dir_cb, state);
+	return git_path_direach(path, 0, foreach_object_dir_cb, state);
 }
 
 static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data)
@@ -768,7 +768,7 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb 
 	state.data = data;
 	state.dir_len = git_buf_len(&buf);
 
-	error = git_path_direach(&buf, foreach_cb, &state);
+	error = git_path_direach(&buf, 0, foreach_cb, &state);
 
 	git_buf_free(&buf);
 
diff --git a/src/odb_pack.c b/src/odb_pack.c
index cadc93a..897bddf 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -331,7 +331,7 @@ static int pack_backend__refresh(git_odb_backend *_backend)
 	git_buf_sets(&path, backend->pack_folder);
 
 	/* reload all packs */
-	error = git_path_direach(&path, packfile_load__cb, (void *)backend);
+	error = git_path_direach(&path, 0, packfile_load__cb, backend);
 
 	git_buf_free(&path);
 
diff --git a/src/path.c b/src/path.c
index 9d8e903..d45751c 100644
--- a/src/path.c
+++ b/src/path.c
@@ -483,23 +483,23 @@ bool git_path_isfile(const char *path)
 
 bool git_path_is_empty_dir(const char *path)
 {
-	git_buf pathbuf = GIT_BUF_INIT;
 	HANDLE hFind = INVALID_HANDLE_VALUE;
 	git_win32_path wbuf;
+	int wbufsz;
 	WIN32_FIND_DATAW ffd;
 	bool retval = true;
 
-	if (!git_path_isdir(path)) return false;
+	if (!git_path_isdir(path))
+		return false;
 
-	git_buf_printf(&pathbuf, "%s\\*", path);
-	git_win32_path_from_c(wbuf, git_buf_cstr(&pathbuf));
+	wbufsz = git_win32_path_from_c(wbuf, path);
+	if (!wbufsz || wbufsz + 2 > GIT_WIN_PATH_UTF16)
+		return false;
+	memcpy(&wbuf[wbufsz - 1], L"\\*", 3 * sizeof(wchar_t));
 
 	hFind = FindFirstFileW(wbuf, &ffd);
-	if (INVALID_HANDLE_VALUE == hFind) {
-		giterr_set(GITERR_OS, "Couldn't open '%s'", path);
-		git_buf_free(&pathbuf);
+	if (INVALID_HANDLE_VALUE == hFind)
 		return false;
-	}
 
 	do {
 		if (!git_path_is_dot_or_dotdotW(ffd.cFileName)) {
@@ -509,50 +509,64 @@ bool git_path_is_empty_dir(const char *path)
 	} while (FindNextFileW(hFind, &ffd) != 0);
 
 	FindClose(hFind);
-	git_buf_free(&pathbuf);
 	return retval;
 }
 
 #else
 
-bool git_path_is_empty_dir(const char *path)
+static int path_found_entry(void *payload, git_buf *path)
 {
-	DIR *dir = NULL;
-	struct dirent *e;
-	bool retval = true;
+	GIT_UNUSED(payload);
+	return !git_path_is_dot_or_dotdot(path->ptr);
+}
 
-	if (!git_path_isdir(path)) return false;
+bool git_path_is_empty_dir(const char *path)
+{
+	int error;
+	git_buf dir = GIT_BUF_INIT;
 
-	dir = opendir(path);
-	if (!dir) {
-		giterr_set(GITERR_OS, "Couldn't open '%s'", path);
+	if (!git_path_isdir(path))
 		return false;
-	}
 
-	while ((e = readdir(dir)) != NULL) {
-		if (!git_path_is_dot_or_dotdot(e->d_name)) {
-			giterr_set(GITERR_INVALID,
-						  "'%s' exists and is not an empty directory", path);
-			retval = false;
-			break;
-		}
-	}
-	closedir(dir);
+	if (!(error = git_buf_sets(&dir, path)))
+		error = git_path_direach(&dir, 0, path_found_entry, NULL);
 
-	return retval;
+	git_buf_free(&dir);
+
+	return !error;
 }
+
 #endif
 
-int git_path_lstat(const char *path, struct stat *st)
+int git_path_set_error(int errno_value, const char *path, const char *action)
 {
-	int err = 0;
-
-	if (p_lstat(path, st) < 0) {
-		err = (errno == ENOENT) ? GIT_ENOTFOUND : -1;
-		giterr_set(GITERR_OS, "Failed to stat file '%s'", path);
+	switch (errno_value) {
+	case ENOENT:
+	case ENOTDIR:
+		giterr_set(GITERR_OS, "Could not find '%s' to %s", path, action);
+		return GIT_ENOTFOUND;
+
+	case EINVAL:
+	case ENAMETOOLONG:
+		giterr_set(GITERR_OS, "Invalid path for filesystem '%s'", path);
+		return GIT_EINVALIDSPEC;
+
+	case EEXIST:
+		giterr_set(GITERR_OS, "Failed %s - '%s' already exists", action, path);
+		return GIT_EEXISTS;
+
+	default:
+		giterr_set(GITERR_OS, "Could not %s '%s'", action, path);
+		return -1;
 	}
+}
+
+int git_path_lstat(const char *path, struct stat *st)
+{
+	if (p_lstat(path, st) == 0)
+		return 0;
 
-	return err;
+	return git_path_set_error(errno, path, "stat");
 }
 
 static bool _check_dir_contents(
@@ -724,14 +738,103 @@ int git_path_cmp(
 	return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
 }
 
+bool git_path_has_non_ascii(const char *path, size_t pathlen)
+{
+	const uint8_t *scan = (const uint8_t *)path, *end;
+
+	for (end = scan + pathlen; scan < end; ++scan)
+		if (*scan & 0x80)
+			return true;
+
+	return false;
+}
+
+#ifdef GIT_USE_ICONV
+
+int git_path_iconv_init_precompose(git_path_iconv_t *ic)
+{
+	git_buf_init(&ic->buf, 0);
+	ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
+	return 0;
+}
+
+void git_path_iconv_clear(git_path_iconv_t *ic)
+{
+	if (ic) {
+		if (ic->map != (iconv_t)-1)
+			iconv_close(ic->map);
+		git_buf_free(&ic->buf);
+	}
+}
+
+int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
+{
+	char *nfd = *in, *nfc;
+	size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, rv;
+	int retry = 1;
+
+	if (!ic || ic->map == (iconv_t)-1 ||
+		!git_path_has_non_ascii(*in, *inlen))
+		return 0;
+
+	while (1) {
+		if (git_buf_grow(&ic->buf, wantlen) < 0)
+			return -1;
+
+		nfc    = ic->buf.ptr   + ic->buf.size;
+		nfclen = ic->buf.asize - ic->buf.size;
+
+		rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
+
+		ic->buf.size = (nfc - ic->buf.ptr);
+
+		if (rv != (size_t)-1)
+			break;
+
+		if (errno != E2BIG)
+			goto fail;
+
+		/* make space for 2x the remaining data to be converted
+		 * (with per retry overhead to avoid infinite loops)
+		 */
+		wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
+
+		if (retry++ > 4)
+			goto fail;
+	}
+
+	ic->buf.ptr[ic->buf.size] = '\0';
+
+	*in    = ic->buf.ptr;
+	*inlen = ic->buf.size;
+
+	return 0;
+
+fail:
+	giterr_set(GITERR_OS, "Unable to convert unicode path data");
+	return -1;
+}
+
+#endif
+
+#if defined(__sun) || defined(__GNU__)
+typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1];
+#else
+typedef struct dirent path_dirent_data;
+#endif
+
 int git_path_direach(
 	git_buf *path,
+	uint32_t flags,
 	int (*fn)(void *, git_buf *),
 	void *arg)
 {
+	int error = 0;
 	ssize_t wd_len;
 	DIR *dir;
-	struct dirent *de, *de_buf;
+	path_dirent_data de_data;
+	struct dirent *de, *de_buf = (struct dirent *)&de_data;
+	git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
 
 	if (git_path_to_dir(path) < 0)
 		return -1;
@@ -743,99 +846,98 @@ int git_path_direach(
 		return -1;
 	}
 
-#if defined(__sun) || defined(__GNU__)
-	de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
-#else
-	de_buf = git__malloc(sizeof(struct dirent));
-#endif
+	if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
+		(void)git_path_iconv_init_precompose(&ic);
 
 	while (p_readdir_r(dir, de_buf, &de) == 0 && de != NULL) {
-		int result;
+		char *de_path = de->d_name;
+		size_t de_len = strlen(de_path);
 
-		if (git_path_is_dot_or_dotdot(de->d_name))
+		if (git_path_is_dot_or_dotdot(de_path))
 			continue;
 
-		if (git_buf_puts(path, de->d_name) < 0) {
-			closedir(dir);
-			git__free(de_buf);
-			return -1;
-		}
+		if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0 ||
+			(error = git_buf_put(path, de_path, de_len)) < 0)
+			break;
 
-		result = fn(arg, path);
+		error = fn(arg, path);
 
 		git_buf_truncate(path, wd_len); /* restore path */
 
-		if (result) {
-			closedir(dir);
-			git__free(de_buf);
-			return GIT_EUSER;
+		if (error) {
+			error = GIT_EUSER;
+			break;
 		}
 	}
 
 	closedir(dir);
-	git__free(de_buf);
-	return 0;
+	git_path_iconv_clear(&ic);
+
+	return error;
 }
 
 int git_path_dirload(
 	const char *path,
 	size_t prefix_len,
 	size_t alloc_extra,
+	unsigned int flags,
 	git_vector *contents)
 {
 	int error, need_slash;
 	DIR *dir;
-	struct dirent *de, *de_buf;
 	size_t path_len;
+	path_dirent_data de_data;
+	struct dirent *de, *de_buf = (struct dirent *)&de_data;
+	git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
+
+	assert(path && contents);
 
-	assert(path != NULL && contents != NULL);
 	path_len = strlen(path);
-	assert(path_len > 0 && path_len >= prefix_len);
 
+	if (!path_len || path_len < prefix_len) {
+		giterr_set(GITERR_INVALID, "Invalid directory path '%s'", path);
+		return -1;
+	}
 	if ((dir = opendir(path)) == NULL) {
 		giterr_set(GITERR_OS, "Failed to open directory '%s'", path);
 		return -1;
 	}
 
-#if defined(__sun) || defined(__GNU__)
-	de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
-#else
-	de_buf = git__malloc(sizeof(struct dirent));
-#endif
+	if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
+		(void)git_path_iconv_init_precompose(&ic);
 
 	path += prefix_len;
 	path_len -= prefix_len;
 	need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
 
 	while ((error = p_readdir_r(dir, de_buf, &de)) == 0 && de != NULL) {
-		char *entry_path;
-		size_t entry_len;
+		char *entry_path, *de_path = de->d_name;
+		size_t alloc_size, de_len = strlen(de_path);
 
-		if (git_path_is_dot_or_dotdot(de->d_name))
+		if (git_path_is_dot_or_dotdot(de_path))
 			continue;
 
-		entry_len = strlen(de->d_name);
+		if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
+			break;
 
-		entry_path = git__malloc(
-			path_len + need_slash + entry_len + 1 + alloc_extra);
-		GITERR_CHECK_ALLOC(entry_path);
+		alloc_size = path_len + need_slash + de_len + 1 + alloc_extra;
+		if ((entry_path = git__calloc(alloc_size, 1)) == NULL) {
+			error = -1;
+			break;
+		}
 
 		if (path_len)
 			memcpy(entry_path, path, path_len);
 		if (need_slash)
 			entry_path[path_len] = '/';
-		memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len);
-		entry_path[path_len + need_slash + entry_len] = '\0';
+		memcpy(&entry_path[path_len + need_slash], de_path, de_len);
 
-		if (git_vector_insert(contents, entry_path) < 0) {
-			closedir(dir);
-			git__free(de_buf);
-			return -1;
-		}
+		if ((error = git_vector_insert(contents, entry_path)) < 0)
+			break;
 	}
 
 	closedir(dir);
-	git__free(de_buf);
+	git_path_iconv_clear(&ic);
 
 	if (error != 0)
 		giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path);
@@ -858,7 +960,7 @@ int git_path_with_stat_cmp_icase(const void *a, const void *b)
 int git_path_dirload_with_stat(
 	const char *path,
 	size_t prefix_len,
-	bool ignore_case,
+	unsigned int flags,
 	const char *start_stat,
 	const char *end_stat,
 	git_vector *contents)
@@ -875,13 +977,14 @@ int git_path_dirload_with_stat(
 		return -1;
 
 	error = git_path_dirload(
-		path, prefix_len, sizeof(git_path_with_stat) + 1, contents);
+		path, prefix_len, sizeof(git_path_with_stat) + 1, flags, contents);
 	if (error < 0) {
 		git_buf_free(&full);
 		return error;
 	}
 
-	strncomp = ignore_case ? git__strncasecmp : git__strncmp;
+	strncomp = (flags & GIT_PATH_DIR_IGNORE_CASE) != 0 ?
+		git__strncasecmp : git__strncmp;
 
 	/* stat struct at start of git_path_with_stat, so shift path text */
 	git_vector_foreach(contents, i, ps) {
diff --git a/src/path.h b/src/path.h
index b2899e9..eaf94d4 100644
--- a/src/path.h
+++ b/src/path.h
@@ -242,21 +242,28 @@ extern int git_path_resolve_relative(git_buf *path, size_t ceiling);
  */
 extern int git_path_apply_relative(git_buf *target, const char *relpath);
 
+enum {
+	GIT_PATH_DIR_IGNORE_CASE = (1u << 0),
+	GIT_PATH_DIR_PRECOMPOSE_UNICODE = (1u << 1),
+};
+
 /**
  * Walk each directory entry, except '.' and '..', calling fn(state).
  *
- * @param pathbuf buffer the function reads the initial directory
+ * @param pathbuf Buffer the function reads the initial directory
  * 		path from, and updates with each successive entry's name.
- * @param fn function to invoke with each entry. The first arg is
- *		the input state and the second arg is pathbuf. The function
- *		may modify the pathbuf, but only by appending new text.
- * @param state to pass to fn as the first arg.
+ * @param flags Combination of GIT_PATH_DIR flags.
+ * @param callback Callback for each entry. Passed the `payload` and each
+ *		successive path inside the directory as a full path.  This may
+ *		safely append text to the pathbuf if needed.
+ * @param payload Passed to callback as first argument.
  * @return 0 on success, GIT_EUSER on non-zero callback, or error code
  */
 extern int git_path_direach(
 	git_buf *pathbuf,
-	int (*fn)(void *, git_buf *),
-	void *state);
+	uint32_t flags,
+	int (*callback)(void *payload, git_buf *path),
+	void *payload);
 
 /**
  * Sort function to order two paths
@@ -276,19 +283,19 @@ extern int git_path_cmp(
  * @param pathbuf Buffer the function reads the directory from and
  *		and updates with each successive name.
  * @param ceiling Prefix of path at which to stop walking up.  If NULL,
- *      this will walk all the way up to the root.  If not a prefix of
- *      pathbuf, the callback will be invoked a single time on the
- *      original input path.
- * @param fn Function to invoke on each path.  The first arg is the
- *		input satte and the second arg is the pathbuf.  The function
- *		should not modify the pathbuf.
+ *		this will walk all the way up to the root.  If not a prefix of
+ *		pathbuf, the callback will be invoked a single time on the
+ *		original input path.
+ * @param callback Function to invoke on each path.  Passed the `payload`
+ *		and the buffer containing the current path.  The path should not
+ *		be modified in any way.
  * @param state Passed to fn as the first ath.
  */
 extern int git_path_walk_up(
 	git_buf *pathbuf,
 	const char *ceiling,
-	int (*fn)(void *state, git_buf *),
-	void *state);
+	int (*callback)(void *payload, git_buf *path),
+	void *payload);
 
 /**
  * Load all directory entries (except '.' and '..') into a vector.
@@ -304,12 +311,14 @@ extern int git_path_walk_up(
  * 		prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
  * @param alloc_extra Extra bytes to add to each string allocation in
  * 		case you want to append anything funny.
+ * @param flags Combination of GIT_PATH_DIR flags.
  * @param contents Vector to fill with directory entry names.
  */
 extern int git_path_dirload(
 	const char *path,
 	size_t prefix_len,
 	size_t alloc_extra,
+	uint32_t flags,
 	git_vector *contents);
 
 
@@ -336,7 +345,7 @@ extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
  *
  * @param path The directory to read from
  * @param prefix_len The trailing part of path to prefix to entry paths
- * @param ignore_case How to sort and compare paths with start/end limits
+ * @param flags GIT_PATH_DIR flags from above
  * @param start_stat As optimization, only stat values after this prefix
  * @param end_stat As optimization, only stat values before this prefix
  * @param contents Vector to fill with git_path_with_stat structures
@@ -344,9 +353,60 @@ extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
 extern int git_path_dirload_with_stat(
 	const char *path,
 	size_t prefix_len,
-	bool ignore_case,
+	uint32_t flags,
 	const char *start_stat,
 	const char *end_stat,
 	git_vector *contents);
 
+/* translate errno to libgit2 error code and set error message */
+extern int git_path_set_error(
+	int errno_value, const char *path, const char *action);
+
+/* check if non-ascii characters are present in filename */
+extern bool git_path_has_non_ascii(const char *path, size_t pathlen);
+
+#define GIT_PATH_REPO_ENCODING "UTF-8"
+
+#ifdef __APPLE__
+#define GIT_PATH_NATIVE_ENCODING "UTF-8-MAC"
+#else
+#define GIT_PATH_NATIVE_ENCODING "UTF-8"
+#endif
+
+#ifdef GIT_USE_ICONV
+
+#include <iconv.h>
+
+typedef struct {
+	iconv_t map;
+	git_buf buf;
+} git_path_iconv_t;
+
+#define GIT_PATH_ICONV_INIT { (iconv_t)-1, GIT_BUF_INIT }
+
+/* Init iconv data for converting decomposed UTF-8 to precomposed */
+extern int git_path_iconv_init_precompose(git_path_iconv_t *ic);
+
+/* Clear allocated iconv data */
+extern void git_path_iconv_clear(git_path_iconv_t *ic);
+
+/*
+ * Rewrite `in` buffer using iconv map if necessary, replacing `in`
+ * pointer internal iconv buffer if rewrite happened.  The `in` pointer
+ * will be left unchanged if no rewrite was needed.
+ */
+extern int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen);
+
+#else
+
+typedef struct {
+	int unused;
+} git_path_iconv_t;
+#define GIT_PATH_ICONV_INIT { 0 }
+#define git_path_iconv_init_precompose(X) 0
+#define git_path_iconv_clear(X) (void)(X)
+#define git_path_iconv(X,Y,Z) 0
+
+#endif /* GIT_USE_ICONV */
+
 #endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 0ba711e..afe1a2a 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -56,6 +56,8 @@ typedef struct refdb_fs_backend {
 
 	git_sortedcache *refcache;
 	int peeling_mode;
+	git_iterator_flag_t iterator_flags;
+	uint32_t direach_flags;
 } refdb_fs_backend;
 
 static int packref_cmp(const void *a_, const void *b_)
@@ -269,7 +271,8 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
 		return 0;
 
 	if (git_path_isdir(full_path->ptr))
-		return git_path_direach(full_path, _dirent_loose_load, backend);
+		return git_path_direach(
+			full_path, backend->direach_flags, _dirent_loose_load, backend);
 
 	file_path = full_path->ptr + strlen(backend->path);
 
@@ -295,7 +298,8 @@ static int packed_loadloose(refdb_fs_backend *backend)
 	 * This will overwrite any old packed entries with their
 	 * updated loose versions
 	 */
-	error = git_path_direach(&refs_path, _dirent_loose_load, backend);
+	error = git_path_direach(
+		&refs_path, backend->direach_flags, _dirent_loose_load, backend);
 
 	git_buf_free(&refs_path);
 
@@ -468,7 +472,7 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
 
 	if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 ||
 		(error = git_iterator_for_filesystem(
-			&fsit, git_buf_cstr(&path), 0, NULL, NULL)) < 0) {
+			&fsit, path.ptr, backend->iterator_flags, NULL, NULL)) < 0) {
 		git_buf_free(&path);
 		return error;
 	}
@@ -1071,6 +1075,7 @@ int git_refdb_backend_fs(
 	git_refdb_backend **backend_out,
 	git_repository *repository)
 {
+	int t = 0;
 	git_buf path = GIT_BUF_INIT;
 	refdb_fs_backend *backend;
 
@@ -1092,6 +1097,15 @@ int git_refdb_backend_fs(
 
 	git_buf_free(&path);
 
+	if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) {
+		backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
+		backend->direach_flags  |= GIT_PATH_DIR_IGNORE_CASE;
+	}
+	if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_PRECOMPOSE) && t) {
+		backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
+		backend->direach_flags  |= GIT_PATH_DIR_PRECOMPOSE_UNICODE;
+	}
+
 	backend->parent.exists = &refdb_fs_backend__exists;
 	backend->parent.lookup = &refdb_fs_backend__lookup;
 	backend->parent.iterator = &refdb_fs_backend__iterator;
diff --git a/src/refs.c b/src/refs.c
index c045ab9..0da02a6 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -138,6 +138,22 @@ int git_reference_name_to_id(
 	return 0;
 }
 
+static int reference_normalize_for_repo(
+	char *out,
+	size_t out_size,
+	git_repository *repo,
+	const char *name)
+{
+	int precompose;
+	unsigned int flags = GIT_REF_FORMAT_ALLOW_ONELEVEL;
+
+	if (!git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) &&
+		precompose)
+		flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;
+
+	return git_reference_normalize_name(out, out_size, name, flags);
+}
+
 int git_reference_lookup_resolved(
 	git_reference **ref_out,
 	git_repository *repo,
@@ -159,13 +175,13 @@ int git_reference_lookup_resolved(
 	else if (max_nesting < 0)
 		max_nesting = DEFAULT_NESTING_LEVEL;
 
-	strncpy(scan_name, name, GIT_REFNAME_MAX);
 	scan_type = GIT_REF_SYMBOLIC;
 
-	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
-		return -1;
+	if ((error = reference_normalize_for_repo(
+			scan_name, sizeof(scan_name), repo, name)) < 0)
+		return error;
 
-	if ((error = git_reference__normalize_name_lax(scan_name, GIT_REFNAME_MAX, name)) < 0)
+	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
 		return error;
 
 	for (nesting = max_nesting;
@@ -173,7 +189,7 @@ int git_reference_lookup_resolved(
 		 nesting--)
 	{
 		if (nesting != max_nesting) {
-			strncpy(scan_name, ref->target.symbolic, GIT_REFNAME_MAX);
+			strncpy(scan_name, ref->target.symbolic, sizeof(scan_name));
 			git_reference_free(ref);
 		}
 
@@ -711,17 +727,18 @@ static bool is_all_caps_and_underscore(const char *name, size_t len)
 	return true;
 }
 
+/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */
 int git_reference__normalize_name(
 	git_buf *buf,
 	const char *name,
 	unsigned int flags)
 {
-	// Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100
-
 	char *current;
 	int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC;
 	unsigned int process_flags;
 	bool normalize = (buf != NULL);
+	git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
+
 	assert(name);
 
 	process_flags = flags;
@@ -733,6 +750,13 @@ int git_reference__normalize_name(
 	if (normalize)
 		git_buf_clear(buf);
 
+	if ((flags & GIT_REF_FORMAT__PRECOMPOSE_UNICODE) != 0) {
+		size_t namelen = strlen(current);
+		if ((error = git_path_iconv_init_precompose(&ic)) < 0 ||
+			(error = git_path_iconv(&ic, &current, &namelen)) < 0)
+			goto cleanup;
+	}
+
 	while (true) {
 		segment_len = ensure_segment_validity(current);
 		if (segment_len < 0) {
@@ -809,6 +833,8 @@ cleanup:
 	if (error && normalize)
 		git_buf_free(buf);
 
+	git_path_iconv_clear(&ic);
+
 	return error;
 }
 
@@ -983,9 +1009,9 @@ static int peel_error(int error, git_reference *ref, const char* msg)
 }
 
 int git_reference_peel(
-		git_object **peeled,
-		git_reference *ref,
-		git_otype target_type)
+	git_object **peeled,
+	git_reference *ref,
+	git_otype target_type)
 {
 	git_reference *resolved = NULL;
 	git_object *target = NULL;
@@ -1027,24 +1053,19 @@ cleanup:
 	return error;
 }
 
-int git_reference__is_valid_name(
-	const char *refname,
-	unsigned int flags)
+int git_reference__is_valid_name(const char *refname, unsigned int flags)
 {
-	int error;
-
-	error = git_reference__normalize_name(NULL, refname, flags) == 0;
-	giterr_clear();
+	if (git_reference__normalize_name(NULL, refname, flags) < 0) {
+		giterr_clear();
+		return false;
+	}
 
-	return error;
+	return true;
 }
 
-int git_reference_is_valid_name(
-	const char *refname)
+int git_reference_is_valid_name(const char *refname)
 {
-	return git_reference__is_valid_name(
-		refname,
-		GIT_REF_FORMAT_ALLOW_ONELEVEL);
+	return git_reference__is_valid_name(refname, GIT_REF_FORMAT_ALLOW_ONELEVEL);
 }
 
 const char *git_reference_shorthand(git_reference *ref)
diff --git a/src/refs.h b/src/refs.h
index 4b91c25..80c7703 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -46,6 +46,8 @@
 #define GIT_STASH_FILE "stash"
 #define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
 
+#define GIT_REF_FORMAT__PRECOMPOSE_UNICODE	(1u << 16)
+
 #define GIT_REFNAME_MAX 1024
 
 struct git_reference {
diff --git a/src/repository.c b/src/repository.c
index 0d7c094..c5ce842 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -843,10 +843,6 @@ fail:
 static bool is_chmod_supported(const char *file_path)
 {
 	struct stat st1, st2;
-	static int _is_supported = -1;
-
-	if (_is_supported > -1)
-		return _is_supported;
 
 	if (p_stat(file_path, &st1) < 0)
 		return false;
@@ -857,27 +853,19 @@ static bool is_chmod_supported(const char *file_path)
 	if (p_stat(file_path, &st2) < 0)
 		return false;
 
-	_is_supported = (st1.st_mode != st2.st_mode);
-
-	return _is_supported;
+	return (st1.st_mode != st2.st_mode);
 }
 
 static bool is_filesystem_case_insensitive(const char *gitdir_path)
 {
 	git_buf path = GIT_BUF_INIT;
-	static int _is_insensitive = -1;
+	int is_insensitive = -1;
 
-	if (_is_insensitive > -1)
-		return _is_insensitive;
-
-	if (git_buf_joinpath(&path, gitdir_path, "CoNfIg") < 0)
-		goto cleanup;
+	if (!git_buf_joinpath(&path, gitdir_path, "CoNfIg"))
+		is_insensitive = git_path_exists(git_buf_cstr(&path));
 
-	_is_insensitive = git_path_exists(git_buf_cstr(&path));
-
-cleanup:
 	git_buf_free(&path);
-	return _is_insensitive;
+	return is_insensitive;
 }
 
 static bool are_symlinks_supported(const char *wd_path)
@@ -885,26 +873,77 @@ static bool are_symlinks_supported(const char *wd_path)
 	git_buf path = GIT_BUF_INIT;
 	int fd;
 	struct stat st;
-	static int _symlinks_supported = -1;
-
-	if (_symlinks_supported > -1)
-		return _symlinks_supported;
+	int symlinks_supported = -1;
 
 	if ((fd = git_futils_mktmp(&path, wd_path)) < 0 ||
 		p_close(fd) < 0 ||
 		p_unlink(path.ptr) < 0 ||
 		p_symlink("testing", path.ptr) < 0 ||
 		p_lstat(path.ptr, &st) < 0)
-		_symlinks_supported = false;
+		symlinks_supported = false;
 	else
-		_symlinks_supported = (S_ISLNK(st.st_mode) != 0);
+		symlinks_supported = (S_ISLNK(st.st_mode) != 0);
 
 	(void)p_unlink(path.ptr);
 	git_buf_free(&path);
 
-	return _symlinks_supported;
+	return symlinks_supported;
+}
+
+#ifdef GIT_USE_ICONV
+
+static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D.XXXXXX";
+static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
+
+/* Check if the platform is decomposing unicode data for us.  We will
+ * emulate core Git and prefer to use precomposed unicode data internally
+ * on these platforms, composing the decomposed unicode on the fly.
+ *
+ * This mainly happens on the Mac where HDFS stores filenames as
+ * decomposed unicode.  Even on VFAT and SAMBA file systems, the Mac will
+ * return decomposed unicode from readdir() even when the actual
+ * filesystem is storing precomposed unicode.
+ */
+static bool does_fs_decompose_unicode_paths(const char *wd_path)
+{
+	git_buf path = GIT_BUF_INIT;
+	int fd;
+	bool found_decomposed = false;
+	char tmp[6];
+
+	/* Create a file using a precomposed path and then try to find it
+	 * using the decomposed name.  If the lookup fails, then we will mark
+	 * that we should precompose unicode for this repository.
+	 */
+	if (git_buf_joinpath(&path, wd_path, nfc_file) < 0 ||
+		(fd = p_mkstemp(path.ptr)) < 0)
+		goto done;
+	p_close(fd);
+
+	/* record trailing digits generated by mkstemp */
+	memcpy(tmp, path.ptr + path.size - sizeof(tmp), sizeof(tmp));
+
+	/* try to look up as NFD path */
+	if (git_buf_joinpath(&path, wd_path, nfd_file) < 0)
+		goto done;
+	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
+
+	found_decomposed = git_path_exists(path.ptr);
+
+	/* remove temporary file (using original precomposed path) */
+	if (git_buf_joinpath(&path, wd_path, nfc_file) < 0)
+		goto done;
+	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
+
+	(void)p_unlink(path.ptr);
+
+done:
+	git_buf_free(&path);
+	return found_decomposed;
 }
 
+#endif
+
 static int create_empty_file(const char *path, mode_t mode)
 {
 	int fd;
@@ -922,71 +961,131 @@ static int create_empty_file(const char *path, mode_t mode)
 	return 0;
 }
 
+static int repo_local_config(
+	git_config **out,
+	git_buf *config_dir,
+	git_repository *repo,
+	const char *repo_dir)
+{
+	int error = 0;
+	git_config *parent;
+	const char *cfg_path;
+
+	if (git_buf_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
+		return -1;
+	cfg_path = git_buf_cstr(config_dir);
+
+	/* make LOCAL config if missing */
+	if (!git_path_isfile(cfg_path) &&
+		(error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0)
+		return error;
+
+	/* if no repo, just open that file directly */
+	if (!repo)
+		return git_config_open_ondisk(out, cfg_path);
+
+	/* otherwise, open parent config and get that level */
+	if ((error = git_repository_config__weakptr(&parent, repo)) < 0)
+		return error;
+
+	if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) {
+		giterr_clear();
+
+		if (!(error = git_config_add_file_ondisk(
+				parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, false)))
+			error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL);
+	}
+
+	git_config_free(parent);
+
+	return error;
+}
+
+static int repo_init_fs_configs(
+	git_config *cfg,
+	const char *cfg_path,
+	const char *repo_dir,
+	const char *work_dir,
+	bool update_ignorecase)
+{
+	int error = 0;
+
+	if (!work_dir)
+		work_dir = repo_dir;
+
+	if ((error = git_config_set_bool(
+			cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0)
+		return error;
+
+	if (!are_symlinks_supported(work_dir)) {
+		if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0)
+			return error;
+	} else if (git_config_delete_entry(cfg, "core.symlinks") < 0)
+		giterr_clear();
+
+	if (update_ignorecase) {
+		if (is_filesystem_case_insensitive(repo_dir)) {
+			if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0)
+				return error;
+		} else if (git_config_delete_entry(cfg, "core.ignorecase") < 0)
+			giterr_clear();
+	}
+
+#ifdef GIT_USE_ICONV
+	if ((error = git_config_set_bool(
+			cfg, "core.precomposeunicode",
+			does_fs_decompose_unicode_paths(work_dir))) < 0)
+		return error;
+#endif
+
+	return 0;
+}
+
 static int repo_init_config(
 	const char *repo_dir,
 	const char *work_dir,
-	git_repository_init_options *opts)
+	uint32_t flags,
+	uint32_t mode)
 {
 	int error = 0;
 	git_buf cfg_path = GIT_BUF_INIT;
 	git_config *config = NULL;
+	bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0);
+	bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0);
 
-#define SET_REPO_CONFIG(TYPE, NAME, VAL) do {\
-	if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
-		goto cleanup; } while (0)
+	if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0)
+		goto cleanup;
 
-	if (git_buf_joinpath(&cfg_path, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
-		return -1;
+	if (is_reinit && (error = check_repositoryformatversion(config)) < 0)
+		goto cleanup;
 
-	if (!git_path_isfile(git_buf_cstr(&cfg_path)) &&
-		create_empty_file(git_buf_cstr(&cfg_path), GIT_CONFIG_FILE_MODE) < 0) {
-			git_buf_free(&cfg_path);
-			return -1;
-	}
+#define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
+	if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
+		goto cleanup; } while (0)
 
-	if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) {
-		git_buf_free(&cfg_path);
-		return -1;
-	}
+	SET_REPO_CONFIG(bool, "core.bare", is_bare);
+	SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION);
 
-	if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0 &&
-		(error = check_repositoryformatversion(config)) < 0)
+	if ((error = repo_init_fs_configs(
+			config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0)
 		goto cleanup;
 
-	SET_REPO_CONFIG(
-		bool, "core.bare", (opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
-	SET_REPO_CONFIG(
-		int32, "core.repositoryformatversion", GIT_REPO_VERSION);
-	SET_REPO_CONFIG(
-		bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path)));
-
-	if (!(opts->flags & GIT_REPOSITORY_INIT_BARE)) {
+	if (!is_bare) {
 		SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
 
-		if (!are_symlinks_supported(work_dir))
-			SET_REPO_CONFIG(bool, "core.symlinks", false);
-
-		if (!(opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD)) {
+		if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD))
 			SET_REPO_CONFIG(string, "core.worktree", work_dir);
-		}
-		else if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0) {
+		else if (is_reinit) {
 			if (git_config_delete_entry(config, "core.worktree") < 0)
 				giterr_clear();
 		}
-	} else {
-		if (!are_symlinks_supported(repo_dir))
-			SET_REPO_CONFIG(bool, "core.symlinks", false);
 	}
 
-	if (!(opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) &&
-		is_filesystem_case_insensitive(repo_dir))
-		SET_REPO_CONFIG(bool, "core.ignorecase", true);
-
-	if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
+	if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
 		SET_REPO_CONFIG(int32, "core.sharedrepository", 1);
 		SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
 	}
-	else if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
+	else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
 		SET_REPO_CONFIG(int32, "core.sharedrepository", 2);
 		SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
 	}
@@ -998,6 +1097,41 @@ cleanup:
 	return error;
 }
 
+static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p)
+{
+	git_repository *smrepo = NULL;
+	GIT_UNUSED(n); GIT_UNUSED(p);
+
+	if (git_submodule_open(&smrepo, sm) < 0 ||
+		git_repository_reinit_filesystem(smrepo, true) < 0)
+		giterr_clear();
+	git_repository_free(smrepo);
+
+	return 0;
+}
+
+int git_repository_reinit_filesystem(git_repository *repo, int recurse)
+{
+	int error = 0;
+	git_buf path = GIT_BUF_INIT;
+	git_config *config = NULL;
+	const char *repo_dir = git_repository_path(repo);
+
+	if (!(error = repo_local_config(&config, &path, repo, repo_dir)))
+		error = repo_init_fs_configs(
+			config, path.ptr, repo_dir, git_repository_workdir(repo), true);
+
+	git_config_free(config);
+	git_buf_free(&path);
+
+	git_repository__cvar_cache_clear(repo);
+
+	if (!repo->is_bare && recurse)
+		(void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL);
+
+	return error;
+}
+
 static int repo_write_template(
 	const char *git_dir,
 	bool allow_overwrite,
@@ -1386,22 +1520,22 @@ int git_repository_init_ext(
 		opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
 
 		error = repo_init_config(
-			git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts);
+			repo_path.ptr, wd_path.ptr, opts->flags, opts->mode);
 
 		/* TODO: reinitialize the templates */
 	}
 	else {
 		if (!(error = repo_init_structure(
-				git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)) &&
+				repo_path.ptr, wd_path.ptr, opts)) &&
 			!(error = repo_init_config(
-				git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)))
+				repo_path.ptr, wd_path.ptr, opts->flags, opts->mode)))
 			error = repo_init_create_head(
-				git_buf_cstr(&repo_path), opts->initial_head);
+				repo_path.ptr, opts->initial_head);
 	}
 	if (error < 0)
 		goto cleanup;
 
-	error = git_repository_open(out, git_buf_cstr(&repo_path));
+	error = git_repository_open(out, repo_path.ptr);
 
 	if (!error && opts->origin_url)
 		error = repo_init_create_origin(*out, opts->origin_url);
diff --git a/src/repository.h b/src/repository.h
index 12dc50d..832df3b 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -37,6 +37,7 @@ typedef enum {
 	GIT_CVAR_IGNORESTAT,    /* core.ignorestat */
 	GIT_CVAR_TRUSTCTIME,    /* core.trustctime */
 	GIT_CVAR_ABBREV,        /* core.abbrev */
+	GIT_CVAR_PRECOMPOSE,    /* core.precomposeunicode */
 	GIT_CVAR_CACHE_MAX
 } git_cvar_cached;
 
@@ -86,6 +87,8 @@ typedef enum {
 	GIT_TRUSTCTIME_DEFAULT = GIT_CVAR_TRUE,
 	/* core.abbrev */
 	GIT_ABBREV_DEFAULT = 7,
+	/* core.precomposeunicode */
+	GIT_PRECOMPOSE_DEFAULT = GIT_CVAR_FALSE,
 
 } git_cvar_value;
 
diff --git a/src/submodule.c b/src/submodule.c
index 121383b..12ade83 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -372,7 +372,8 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index)
 
 	memset(&entry, 0, sizeof(entry));
 	entry.path = sm->path;
-	git_index_entry__init_from_stat(&entry, &st);
+	git_index_entry__init_from_stat(
+		&entry, &st, !(git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE));
 
 	/* calling git_submodule_open will have set sm->wd_oid if possible */
 	if ((sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) == 0) {
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 2f49052..18f717b 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -125,8 +125,8 @@ static int do_lstat(
 
 	errno = ENOENT;
 
-	/* We need POSIX behavior, then ENOTDIR must set when any of the folders in the
-	 * file path is a regular file,otherwise ENOENT must be set.
+	/* To match POSIX behavior, set ENOTDIR when any of the folders in the
+	 * file path is a regular file, otherwise set ENOENT.
 	 */
 	if (posix_enotdir) {
 		/* scan up path until we find an existing item */
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index 73050d0..48d6d79 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -224,13 +224,15 @@ void test_checkout_index__options_disable_filters(void)
 
 void test_checkout_index__options_dir_modes(void)
 {
-#ifndef GIT_WIN32
 	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
 	struct stat st;
 	git_oid oid;
 	git_commit *commit;
 	mode_t um;
 
+	if (!cl_is_chmod_supported())
+		return;
+
 	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
 	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
 
@@ -252,15 +254,16 @@ void test_checkout_index__options_dir_modes(void)
 	cl_assert_equal_i_fmt(st.st_mode, GIT_FILEMODE_BLOB_EXECUTABLE, "%07o");
 
 	git_commit_free(commit);
-#endif
 }
 
 void test_checkout_index__options_override_file_modes(void)
 {
-#ifndef GIT_WIN32
 	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
 	struct stat st;
 
+	if (!cl_is_chmod_supported())
+		return;
+
 	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
 	opts.file_mode = 0700;
 
@@ -268,7 +271,6 @@ void test_checkout_index__options_override_file_modes(void)
 
 	cl_git_pass(p_stat("./testrepo/new.txt", &st));
 	cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
-#endif
 }
 
 void test_checkout_index__options_open_flags(void)
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 6063bf9..50762cd 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -1,6 +1,7 @@
 #include "clar_libgit2.h"
 #include "posix.h"
 #include "path.h"
+#include "git2/sys/repository.h"
 
 void cl_git_report_failure(
 	int error, const char *file, int line, const char *fncall)
@@ -190,6 +191,9 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
 	/* Now open the sandbox repository and make it available for tests */
 	cl_git_pass(git_repository_open(&_cl_repo, sandbox));
 
+	/* Adjust configs after copying to new filesystem */
+	cl_git_pass(git_repository_reinit_filesystem(_cl_repo, 0));
+
 	return _cl_repo;
 }
 
@@ -301,7 +305,7 @@ static int remove_placeholders_recurs(void *_data, git_buf *path)
 	size_t pathlen;
 
 	if (git_path_isdir(path->ptr) == true)
-		return git_path_direach(path, remove_placeholders_recurs, data);
+		return git_path_direach(path, 0, remove_placeholders_recurs, data);
 
 	pathlen = path->size;
 
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 4bcb5be..071e3d0 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -56,41 +56,18 @@ void test_clone_nonetwork__bad_url(void)
 	cl_assert(!git_path_exists("./foo"));
 }
 
-static int dont_call_me(void *state, git_buf *path)
-{
-	GIT_UNUSED(state);
-	GIT_UNUSED(path);
-	return GIT_ERROR;
-}
-
 void test_clone_nonetwork__do_not_clean_existing_directory(void)
 {
-	git_buf path_buf = GIT_BUF_INIT;
-
-	git_buf_put(&path_buf, "./foo", 5);
-
 	/* Clone should not remove the directory if it already exists, but
 	 * Should clean up entries it creates. */
 	p_mkdir("./foo", GIT_DIR_MODE);
 	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
-	cl_assert(git_path_exists("./foo"));
-
-	/* Make sure the directory is empty. */
-	cl_git_pass(git_path_direach(&path_buf,
-		dont_call_me,
-		NULL));
+	cl_assert(git_path_is_empty_dir("./foo"));
 
 	/* Try again with a bare repository. */
 	g_options.bare = true;
 	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
-	cl_assert(git_path_exists("./foo"));
-
-	/* Make sure the directory is empty. */
-	cl_git_pass(git_path_direach(&path_buf,
-		dont_call_me,
-		NULL));
-
-	git_buf_free(&path_buf);
+	cl_assert(git_path_is_empty_dir("./foo"));
 }
 
 void test_clone_nonetwork__local(void)
diff --git a/tests-clar/core/dirent.c b/tests-clar/core/dirent.c
index 5a7859d..f172603 100644
--- a/tests-clar/core/dirent.c
+++ b/tests-clar/core/dirent.c
@@ -88,14 +88,6 @@ static int one_entry(void *state, git_buf *path)
 	return GIT_ERROR;
 }
 
-static int dont_call_me(void *state, git_buf *path)
-{
-	GIT_UNUSED(state);
-	GIT_UNUSED(path);
-	return GIT_ERROR;
-}
-
-
 
 static name_data dot_names[] = {
 	{ 0, "./a" },
@@ -115,9 +107,7 @@ void test_core_dirent__dont_traverse_dot(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &dot);
 	setup(&dot);
 
-	cl_git_pass(git_path_direach(&dot.path,
-					one_entry,
-					&dot));
+	cl_git_pass(git_path_direach(&dot.path, 0, one_entry, &dot));
 
 	check_counts(&dot);
 }
@@ -141,9 +131,7 @@ void test_core_dirent__traverse_subfolder(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &sub);
 	setup(&sub);
 
-	cl_git_pass(git_path_direach(&sub.path,
-					one_entry,
-					&sub));
+	cl_git_pass(git_path_direach(&sub.path, 0, one_entry, &sub));
 
 	check_counts(&sub);
 }
@@ -161,9 +149,7 @@ void test_core_dirent__traverse_slash_terminated_folder(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &sub_slash);
 	setup(&sub_slash);
 
-	cl_git_pass(git_path_direach(&sub_slash.path,
-					one_entry,
-					&sub_slash));
+	cl_git_pass(git_path_direach(&sub_slash.path, 0, one_entry, &sub_slash));
 
 	check_counts(&sub_slash);
 }
@@ -184,16 +170,12 @@ void test_core_dirent__dont_traverse_empty_folders(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &empty);
 	setup(&empty);
 
-	cl_git_pass(git_path_direach(&empty.path,
-					one_entry,
-					&empty));
+	cl_git_pass(git_path_direach(&empty.path, 0, one_entry, &empty));
 
 	check_counts(&empty);
 
 	/* make sure callback not called */
-	cl_git_pass(git_path_direach(&empty.path,
-					dont_call_me,
-					&empty));
+	cl_assert(git_path_is_empty_dir(empty.path.ptr));
 }
 
 static name_data odd_names[] = {
@@ -216,9 +198,7 @@ void test_core_dirent__traverse_weird_filenames(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &odd);
 	setup(&odd);
 
-	cl_git_pass(git_path_direach(&odd.path,
-					one_entry,
-					&odd));
+	cl_git_pass(git_path_direach(&odd.path, 0, one_entry, &odd));
 
 	check_counts(&odd);
 }
@@ -231,5 +211,26 @@ void test_core_dirent__length_limits(void)
 	big_filename[FILENAME_MAX] = 0;
 
 	cl_must_fail(p_creat(big_filename, 0666));
+
 	git__free(big_filename);
 }
+
+void test_core_dirent__empty_dir(void)
+{
+	cl_must_pass(p_mkdir("empty_dir", 0777));
+	cl_assert(git_path_is_empty_dir("empty_dir"));
+
+	cl_git_mkfile("empty_dir/content", "whatever\n");
+	cl_assert(!git_path_is_empty_dir("empty_dir"));
+	cl_assert(!git_path_is_empty_dir("empty_dir/content"));
+
+	cl_must_pass(p_unlink("empty_dir/content"));
+
+	cl_must_pass(p_mkdir("empty_dir/content", 0777));
+	cl_assert(!git_path_is_empty_dir("empty_dir"));
+	cl_assert(git_path_is_empty_dir("empty_dir/content"));
+
+	cl_must_pass(p_rmdir("empty_dir/content"));
+
+	cl_must_pass(p_rmdir("empty_dir"));
+}
diff --git a/tests-clar/core/iconv.c b/tests-clar/core/iconv.c
new file mode 100644
index 0000000..73bc99a
--- /dev/null
+++ b/tests-clar/core/iconv.c
@@ -0,0 +1,60 @@
+#include "clar_libgit2.h"
+#include "path.h"
+
+static git_path_iconv_t ic;
+static char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
+static char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
+
+void test_core_iconv__initialize(void)
+{
+	cl_git_pass(git_path_iconv_init_precompose(&ic));
+}
+
+void test_core_iconv__cleanup(void)
+{
+	git_path_iconv_clear(&ic);
+}
+
+void test_core_iconv__unchanged(void)
+{
+	char *data = "Ascii data", *original = data;
+	size_t datalen = strlen(data);
+
+	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+	GIT_UNUSED(datalen);
+
+	/* There are no high bits set, so this should leave data untouched */
+	cl_assert(data == original);
+}
+
+void test_core_iconv__decomposed_to_precomposed(void)
+{
+	char *data = nfd;
+	size_t datalen = strlen(nfd);
+
+	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+	GIT_UNUSED(datalen);
+
+	/* The decomposed nfd string should be transformed to the nfc form
+	 * (on platforms where iconv is enabled, of course).
+	 */
+#ifdef GIT_USE_ICONV
+	cl_assert_equal_s(nfc, data);
+#else
+	cl_assert_equal_s(nfd, data);
+#endif
+}
+
+void test_core_iconv__precomposed_is_unmodified(void)
+{
+	char *data = nfc;
+	size_t datalen = strlen(nfc);
+
+	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+	GIT_UNUSED(datalen);
+
+	/* data is already in precomposed form, so even though some bytes have
+	 * the high-bit set, the iconv transform should result in no change.
+	 */
+	cl_assert_equal_s(nfc, data);
+}
diff --git a/tests-clar/core/mkdir.c b/tests-clar/core/mkdir.c
index a969e4d..a8c5b10 100644
--- a/tests-clar/core/mkdir.c
+++ b/tests-clar/core/mkdir.c
@@ -111,14 +111,20 @@ static void cleanup_chmod_root(void *ref)
 	git_futils_rmdir_r("r", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
 }
 
-static void check_mode(mode_t expected, mode_t actual)
+#define check_mode(X,A) check_mode_at_line((X), (A), __FILE__, __LINE__)
+
+static void check_mode_at_line(
+	mode_t expected, mode_t actual, const char *file, int line)
 {
-#ifdef GIT_WIN32
-	/* chmod on Win32 doesn't support exec bit, not group/world bits */
-	cl_assert_equal_i_fmt((expected & 0600), (actual & 0777), "%07o");
-#else
-	cl_assert_equal_i_fmt(expected, (actual & 0777), "%07o");
-#endif
+	/* FAT filesystems don't support exec bit, nor group/world bits */
+	if (!cl_is_chmod_supported()) {
+		expected &= 0600;
+		actual &= 0600;
+	}
+
+	clar__assert_equal(
+		file, line, "expected_mode != actual_mode", 1,
+		"%07o", (int)expected, (int)(actual & 0777));
 }
 
 void test_core_mkdir__chmods(void)
diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c
index e584d61..cf2d5e9 100644
--- a/tests-clar/core/path.c
+++ b/tests-clar/core/path.c
@@ -1,5 +1,5 @@
 #include "clar_libgit2.h"
-#include <fileops.h>
+#include "fileops.h"
 
 static void
 check_dirname(const char *A, const char *B)
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index a5c322b..3452f23 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -21,6 +21,35 @@ git_tree *resolve_commit_oid_to_tree(
 	return tree;
 }
 
+static char diff_pick_suffix(int mode)
+{
+	if (S_ISDIR(mode))
+		return '/';
+	else if (GIT_PERMS_IS_EXEC(mode))
+		return '*';
+	else
+		return ' ';
+}
+
+static void fprintf_delta(FILE *fp, const git_diff_delta *delta, float progress)
+{
+	char code = git_diff_status_char(delta->status);
+	char old_suffix = diff_pick_suffix(delta->old_file.mode);
+	char new_suffix = diff_pick_suffix(delta->new_file.mode);
+
+	fprintf(fp, "%c\t%s", code, delta->old_file.path);
+
+	if ((delta->old_file.path != delta->new_file.path &&
+		 strcmp(delta->old_file.path, delta->new_file.path) != 0) ||
+		(delta->old_file.mode != delta->new_file.mode &&
+		 delta->old_file.mode != 0 && delta->new_file.mode != 0))
+		fprintf(fp, "%c %s%c", old_suffix, delta->new_file.path, new_suffix);
+	else if (old_suffix != ' ')
+		fprintf(fp, "%c", old_suffix);
+
+	fprintf(fp, "\t[%.2f]\n", progress);
+}
+
 int diff_file_cb(
 	const git_diff_delta *delta,
 	float progress,
@@ -29,9 +58,7 @@ int diff_file_cb(
 	diff_expects *e = payload;
 
 	if (e->debug)
-		fprintf(stderr, "%c %s (%.3f)\n",
-			git_diff_status_char(delta->status),
-			delta->old_file.path, progress);
+		fprintf_delta(stderr, delta, progress);
 
 	if (e->names)
 		cl_assert_equal_s(e->names[e->files], delta->old_file.path);
@@ -55,8 +82,14 @@ int diff_print_file_cb(
 	float progress,
 	void *payload)
 {
-	fprintf(stderr, "%c %s\n",
-		git_diff_status_char(delta->status), delta->old_file.path);
+	if (!payload) {
+		fprintf_delta(stderr, delta, progress);
+		return 0;
+	}
+
+	if (!((diff_expects *)payload)->debug)
+		fprintf_delta(stderr, delta, progress);
+
 	return diff_file_cb(delta, progress, payload);
 }
 
diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c
index 932d720..ea59084 100644
--- a/tests-clar/diff/diffiter.c
+++ b/tests-clar/diff/diffiter.c
@@ -27,25 +27,25 @@ void test_diff_diffiter__create(void)
 	git_diff_list_free(diff);
 }
 
-void test_diff_diffiter__iterate_files(void)
+void test_diff_diffiter__iterate_files_1(void)
 {
 	git_repository *repo = cl_git_sandbox_init("attr");
 	git_diff_list *diff;
 	size_t d, num_d;
-	int count = 0;
+	diff_expects exp = { 0 };
 
 	cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));
 
 	num_d = git_diff_num_deltas(diff);
-	cl_assert_equal_i(6, (int)num_d);
 
 	for (d = 0; d < num_d; ++d) {
 		const git_diff_delta *delta;
 		cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d));
 		cl_assert(delta != NULL);
-		count++;
+
+		diff_file_cb(delta, (float)d / (float)num_d, &exp);
 	}
-	cl_assert_equal_i(6, count);
+	cl_assert_equal_sz(6, exp.files);
 
 	git_diff_list_free(diff);
 }
diff --git a/tests-clar/diff/drivers.c b/tests-clar/diff/drivers.c
index e02dd5c..719d229 100644
--- a/tests-clar/diff/drivers.c
+++ b/tests-clar/diff/drivers.c
@@ -147,6 +147,13 @@ void test_diff_drivers__long_lines(void)
 	cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
 	cl_git_pass(git_diff_patch_to_str(&actual, patch));
 
+	/* if chmod not supported, overwrite mode bits since anything is possible */
+	if (!cl_is_chmod_supported()) {
+		size_t actual_len = strlen(actual);
+		if (actual_len > 72 && memcmp(&actual[66], "100644", 6) != 0)
+			memcpy(&actual[66], "100644", 6);
+	}
+
 	cl_assert_equal_s(expected, actual);
 
 	free(actual);
diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c
index 6a33fa9..171abc8 100644
--- a/tests-clar/diff/patch.c
+++ b/tests-clar/diff/patch.c
@@ -238,6 +238,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 
 	cl_git_pass(git_config_new(&cfg));
 	git_repository_set_config(g_repo, cfg);
+	git_config_free(cfg);
+
+	git_repository_reinit_filesystem(g_repo, false);
 
 	cl_git_pass(
 		git_futils_readbuffer(&old_content, "renames/songof7cities.txt"));
@@ -408,7 +411,6 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 	git_buf_free(&actual);
 	git_buf_free(&old_content);
 	git_tree_free(head);
-	git_config_free(cfg);
 }
 
 static void check_single_patch_stats(
@@ -520,6 +522,9 @@ void test_diff_patch__line_counts_with_eofnl(void)
 
 	cl_git_pass(git_config_new(&cfg));
 	git_repository_set_config(g_repo, cfg);
+	git_config_free(cfg);
+
+	git_repository_reinit_filesystem(g_repo, false);
 
 	cl_git_pass(git_futils_readbuffer(&content, "renames/songof7cities.txt"));
 
@@ -574,5 +579,4 @@ void test_diff_patch__line_counts_with_eofnl(void)
 		g_repo, 1, 1, 1, 6, expected_sizes, expected);
 
 	git_buf_free(&content);
-	git_config_free(cfg);
 }
diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c
index 6c17b41..aeef7b9 100644
--- a/tests-clar/diff/workdir.c
+++ b/tests-clar/diff/workdir.c
@@ -761,16 +761,7 @@ void test_diff_workdir__submodules(void)
 	git_diff_list *diff = NULL;
 	diff_expects exp;
 
-	g_repo = cl_git_sandbox_init("submod2");
-
-	cl_fixture_sandbox("submod2_target");
-	p_rename("submod2_target/.gitted", "submod2_target/.git");
-
-	rewrite_gitmodules(git_repository_workdir(g_repo));
-	p_rename("submod2/not-submodule/.gitted", "submod2/not-submodule/.git");
-	p_rename("submod2/not/.gitted", "submod2/not/.git");
-
-	cl_fixture_cleanup("submod2_target");
+	g_repo = setup_fixture_submod2();
 
 	a = resolve_commit_oid_to_tree(g_repo, a_commit);
 
diff --git a/tests-clar/index/addall.c b/tests-clar/index/addall.c
index f46a1e1..3b5f5f2 100644
--- a/tests-clar/index/addall.c
+++ b/tests-clar/index/addall.c
@@ -111,8 +111,9 @@ static void check_stat_data(git_index *index, const char *path, bool match)
 		cl_assert(st.st_gid  == entry->gid);
 		cl_assert_equal_i_fmt(
 			GIT_MODE_TYPE(st.st_mode), GIT_MODE_TYPE(entry->mode), "%07o");
-		cl_assert_equal_b(
-			GIT_PERMS_IS_EXEC(st.st_mode), GIT_PERMS_IS_EXEC(entry->mode));
+		if (cl_is_chmod_supported())
+			cl_assert_equal_b(
+				GIT_PERMS_IS_EXEC(st.st_mode), GIT_PERMS_IS_EXEC(entry->mode));
 	} else {
 		/* most things will still match */
 		cl_assert(st.st_size != entry->file_size);
diff --git a/tests-clar/index/filemodes.c b/tests-clar/index/filemodes.c
index 02f83ef..0139326 100644
--- a/tests-clar/index/filemodes.c
+++ b/tests-clar/index/filemodes.c
@@ -51,24 +51,29 @@ static void replace_file_with_mode(
 	git_buf_free(&content);
 }
 
-static void add_and_check_mode(
-	git_index *index, const char *filename, unsigned int expect_mode)
+#define add_and_check_mode(I,F,X) add_and_check_mode_(I,F,X,__FILE__,__LINE__)
+
+static void add_and_check_mode_(
+	git_index *index, const char *filename, unsigned int expect_mode,
+	const char *file, int line)
 {
 	size_t pos;
 	const git_index_entry *entry;
 
 	cl_git_pass(git_index_add_bypath(index, filename));
 
-	cl_assert(!git_index_find(&pos, index, filename));
+	clar__assert(!git_index_find(&pos, index, filename),
+		file, line, "Cannot find index entry", NULL, 1);
 
 	entry = git_index_get_byindex(index, pos);
-	cl_assert(entry->mode == expect_mode);
+
+	clar__assert_equal(file, line, "Expected mode does not match index",
+		1, "%07o", (unsigned int)entry->mode, (unsigned int)expect_mode);
 }
 
 void test_index_filemodes__untrusted(void)
 {
 	git_index *index;
-	bool can_filemode = cl_is_chmod_supported();
 
 	cl_repo_set_bool(g_repo, "core.filemode", false);
 
@@ -96,15 +101,10 @@ void test_index_filemodes__untrusted(void)
 		O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
 
-	/* this test won't give predictable results on a platform
-	 * that doesn't support filemodes correctly, so skip it.
-	 */
-	if (can_filemode) {
-		/* 6 - add 0755 -> expect 0755 */
-		cl_git_write2file("filemodes/new_on", "blah", 0,
-			O_WRONLY | O_CREAT | O_TRUNC, 0755);
-		add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);
-	}
+	/* 6 - add new 0755 -> expect 0644 if core.filemode == false */
+	cl_git_write2file("filemodes/new_on", "blah", 0,
+		O_WRONLY | O_CREAT | O_TRUNC, 0755);
+	add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB);
 
 	git_index_free(index);
 }
diff --git a/tests-clar/merge/merge_helpers.c b/tests-clar/merge/merge_helpers.c
index e409278..ddcb93f 100644
--- a/tests-clar/merge/merge_helpers.c
+++ b/tests-clar/merge/merge_helpers.c
@@ -291,7 +291,7 @@ int merge_test_workdir(git_repository *repo, const struct merge_index_entry expe
 	git_buf wd = GIT_BUF_INIT;
 
 	git_buf_puts(&wd, repo->workdir);
-	git_path_direach(&wd, dircount, &actual_len);
+	git_path_direach(&wd, 0, dircount, &actual_len);
 
 	if (actual_len != expected_len)
 		return 0;
diff --git a/tests-clar/online/push.c b/tests-clar/online/push.c
index 4c2bec7..957cef7 100644
--- a/tests-clar/online/push.c
+++ b/tests-clar/online/push.c
@@ -356,7 +356,7 @@ static int push_pack_progress_cb(int stage, unsigned int current, unsigned int t
 static int push_transfer_progress_cb(unsigned int current, unsigned int total, size_t bytes, void* payload)
 {
 	int *was_called = (int *) payload;
-	GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes); 
+	GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
 	*was_called = 1;
 	return 0;
 }
diff --git a/tests-clar/refs/unicode.c b/tests-clar/refs/unicode.c
index 2ec1032..b560128 100644
--- a/tests-clar/refs/unicode.c
+++ b/tests-clar/refs/unicode.c
@@ -4,17 +4,13 @@ static git_repository *repo;
 
 void test_refs_unicode__initialize(void)
 {
-	cl_fixture_sandbox("testrepo.git");
-
-	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+	repo = cl_git_sandbox_init("testrepo.git");
 }
 
 void test_refs_unicode__cleanup(void)
 {
-	git_repository_free(repo);
+	cl_git_sandbox_cleanup();
 	repo = NULL;
-
-	cl_fixture_cleanup("testrepo.git");
 }
 
 void test_refs_unicode__create_and_lookup(void)
@@ -22,23 +18,39 @@ void test_refs_unicode__create_and_lookup(void)
 	git_reference *ref0, *ref1, *ref2;
 	git_repository *repo2;
 
-	const char *REFNAME = "refs/heads/" "\305" "ngstr" "\366" "m";
+	const char *REFNAME = "refs/heads/" "\303\205" "ngstr" "\303\266" "m";
 	const char *master = "refs/heads/master";
 
 	/* Create the reference */
 	cl_git_pass(git_reference_lookup(&ref0, repo, master));
-	cl_git_pass(git_reference_create(&ref1, repo, REFNAME, git_reference_target(ref0), 0));
+	cl_git_pass(git_reference_create(
+		&ref1, repo, REFNAME, git_reference_target(ref0), 0));
 	cl_assert_equal_s(REFNAME, git_reference_name(ref1));
+	git_reference_free(ref0);
 
 	/* Lookup the reference in a different instance of the repository */
 	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
+
 	cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME));
+	cl_assert_equal_i(
+		0, git_oid_cmp(git_reference_target(ref1), git_reference_target(ref2)));
+	cl_assert_equal_s(REFNAME, git_reference_name(ref2));
+	git_reference_free(ref2);
 
-	cl_assert(git_oid_cmp(git_reference_target(ref1), git_reference_target(ref2)) == 0);
+#if GIT_USE_ICONV
+	/* Lookup reference by decomposed unicode name */
+
+#define REFNAME_DECOMPOSED "refs/heads/" "A" "\314\212" "ngstro" "\314\210" "m"
+
+	cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME_DECOMPOSED));
+	cl_assert_equal_i(
+		0, git_oid_cmp(git_reference_target(ref1), git_reference_target(ref2)));
 	cl_assert_equal_s(REFNAME, git_reference_name(ref2));
+	git_reference_free(ref2);
+#endif
+
+	/* Cleanup */
 
-	git_reference_free(ref0);
 	git_reference_free(ref1);
-	git_reference_free(ref2);
 	git_repository_free(repo2);
 }
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index 392be20..617fdf8 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -179,41 +179,32 @@ void test_repo_init__additional_templates(void)
 	git_buf_free(&path);
 }
 
-static void assert_config_entry_on_init_bytype(const char *config_key, int expected_value, bool is_bare)
+static void assert_config_entry_on_init_bytype(
+	const char *config_key, int expected_value, bool is_bare)
 {
 	git_config *config;
-	int current_value;
-	git_buf repo_path = GIT_BUF_INIT;
+	int error, current_value;
+	const char *repo_path = is_bare ?
+		"config_entry/test.bare.git" : "config_entry/test.non.bare.git";
 
 	cl_set_cleanup(&cleanup_repository, "config_entry");
 
-	cl_git_pass(git_buf_puts(&repo_path, "config_entry/test."));
-
-	if (!is_bare)
-		cl_git_pass(git_buf_puts(&repo_path, "non."));
-
-	cl_git_pass(git_buf_puts(&repo_path, "bare.git"));
-
-	cl_git_pass(git_repository_init(&_repo, git_buf_cstr(&repo_path), is_bare));
+	cl_git_pass(git_repository_init(&_repo, repo_path, is_bare));
 
-	git_buf_free(&repo_path);
-
-	git_repository_config(&config, _repo);
+	cl_git_pass(git_repository_config(&config, _repo));
+	error = git_config_get_bool(&current_value, config, config_key);
+	git_config_free(config);
 
 	if (expected_value >= 0) {
-		cl_git_pass(git_config_get_bool(&current_value, config, config_key));
-
+		cl_assert_equal_i(0, error);
 		cl_assert_equal_i(expected_value, current_value);
 	} else {
-		int error = git_config_get_bool(&current_value, config, config_key);
-
 		cl_assert_equal_i(expected_value, error);
 	}
-
-	git_config_free(config);
 }
 
-static void assert_config_entry_on_init(const char *config_key, int expected_value)
+static void assert_config_entry_on_init(
+	const char *config_key, int expected_value)
 {
 	assert_config_entry_on_init_bytype(config_key, expected_value, true);
 	git_repository_free(_repo);
@@ -223,21 +214,36 @@ static void assert_config_entry_on_init(const char *config_key, int expected_val
 
 void test_repo_init__detect_filemode(void)
 {
-#ifdef GIT_WIN32
-	assert_config_entry_on_init("core.filemode", false);
-#else
-	assert_config_entry_on_init("core.filemode", true);
-#endif
+	assert_config_entry_on_init("core.filemode", cl_is_chmod_supported());
 }
 
-#define CASE_INSENSITIVE_FILESYSTEM (defined GIT_WIN32 || defined __APPLE__)
-
 void test_repo_init__detect_ignorecase(void)
 {
-#if CASE_INSENSITIVE_FILESYSTEM
-	assert_config_entry_on_init("core.ignorecase", true);
+	struct stat st;
+	bool found_without_match;
+
+	cl_git_write2file("testCAPS", "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
+	found_without_match = (p_stat("Testcaps", &st) == 0);
+	cl_must_pass(p_unlink("testCAPS"));
+
+	assert_config_entry_on_init(
+		"core.ignorecase", found_without_match ? true : GIT_ENOTFOUND);
+}
+
+void test_repo_init__detect_precompose_unicode_required(void)
+{
+	char *composed = "ḱṷṓn", *decomposed = "ḱṷṓn";
+	struct stat st;
+	bool found_with_nfd;
+
+	cl_git_write2file(composed, "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
+	found_with_nfd = (p_stat(decomposed, &st) == 0);
+	cl_must_pass(p_unlink(composed));
+
+#ifdef GIT_USE_ICONV
+	assert_config_entry_on_init("core.precomposeunicode", found_with_nfd);
 #else
-	assert_config_entry_on_init("core.ignorecase", GIT_ENOTFOUND);
+	assert_config_entry_on_init("core.precomposeunicode", GIT_ENOTFOUND);
 #endif
 }
 
@@ -270,13 +276,7 @@ void test_repo_init__reinit_doesnot_overwrite_ignorecase(void)
 
 void test_repo_init__reinit_overwrites_filemode(void)
 {
-	int expected, current_value;
-
-#ifdef GIT_WIN32
-	expected = false;
-#else
-	expected = true;
-#endif
+	int expected = cl_is_chmod_supported(), current_value;
 
 	/* Init a new repo */
 	cl_set_cleanup(&cleanup_repository, "overwrite.git");
@@ -348,7 +348,10 @@ void test_repo_init__extended_1(void)
 
 	cl_git_pass(git_path_lstat(git_repository_path(_repo), &st));
 	cl_assert(S_ISDIR(st.st_mode));
-	cl_assert((S_ISGID & st.st_mode) == S_ISGID);
+	if (cl_is_chmod_supported())
+		cl_assert((S_ISGID & st.st_mode) == S_ISGID);
+	else
+		cl_assert((S_ISGID & st.st_mode) == 0);
 
 	cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD"));
 	cl_assert(git_reference_type(ref) == GIT_REF_SYMBOLIC);
diff --git a/tests-clar/status/renames.c b/tests-clar/status/renames.c
index de84a57..16fd026 100644
--- a/tests-clar/status/renames.c
+++ b/tests-clar/status/renames.c
@@ -69,14 +69,14 @@ static void test_status(
 		actual = git_status_byindex(status_list, i);
 		expected = &expected_list[i];
 
-		cl_assert_equal_i_fmt(expected->status, actual->status, "%04x");
-
 		oldname = actual->head_to_index ? actual->head_to_index->old_file.path :
 			actual->index_to_workdir ? actual->index_to_workdir->old_file.path : NULL;
 
 		newname = actual->index_to_workdir ? actual->index_to_workdir->new_file.path :
 			actual->head_to_index ? actual->head_to_index->new_file.path : NULL;
 
+		cl_assert_equal_i_fmt(expected->status, actual->status, "%04x");
+
 		if (oldname)
 			cl_assert(git__strcmp(oldname, expected->oldname) == 0);
 		else
@@ -507,14 +507,14 @@ void test_status_renames__both_casechange_two(void)
 		  "untimely.txt", "untimeliest.txt" }
 	};
 	struct status_entry expected_case[] = {
-		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED |
-		  GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
-		  "ikeepsix.txt", "ikeepsix.txt" },
-		{ GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_RENAMED,
-		  "sixserving.txt", "SixServing.txt" },
 		{ GIT_STATUS_INDEX_RENAMED |
 		  GIT_STATUS_WT_MODIFIED | GIT_STATUS_WT_RENAMED,
 		  "songof7cities.txt", "SONGOF7.txt" },
+		{ GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_RENAMED,
+		  "sixserving.txt", "SixServing.txt" },
+		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED |
+		  GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
+		  "ikeepsix.txt", "ikeepsix.txt" },
 		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
 		  "untimely.txt", "untimeliest.txt" }
 	};
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 135a958..3b569c7 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -119,7 +119,7 @@ void test_status_worktree__purged_worktree(void)
 
 	/* first purge the contents of the worktree */
 	cl_git_pass(git_buf_sets(&workdir, git_repository_workdir(repo)));
-	cl_git_pass(git_path_direach(&workdir, remove_file_cb, NULL));
+	cl_git_pass(git_path_direach(&workdir, 0, remove_file_cb, NULL));
 	git_buf_free(&workdir);
 
 	/* now get status */
diff --git a/tests-clar/submodule/lookup.c b/tests-clar/submodule/lookup.c
index b626cdf..5f320e7 100644
--- a/tests-clar/submodule/lookup.c
+++ b/tests-clar/submodule/lookup.c
@@ -7,20 +7,7 @@ static git_repository *g_repo = NULL;
 
 void test_submodule_lookup__initialize(void)
 {
-	g_repo = cl_git_sandbox_init("submod2");
-
-	cl_fixture_sandbox("submod2_target");
-	p_rename("submod2_target/.gitted", "submod2_target/.git");
-
-	/* must create submod2_target before rewrite so prettify will work */
-	rewrite_gitmodules(git_repository_workdir(g_repo));
-	p_rename("submod2/not-submodule/.gitted", "submod2/not-submodule/.git");
-}
-
-void test_submodule_lookup__cleanup(void)
-{
-	cl_git_sandbox_cleanup();
-	cl_fixture_cleanup("submod2_target");
+	g_repo = setup_fixture_submod2();
 }
 
 void test_submodule_lookup__simple_lookup(void)
diff --git a/tests-clar/submodule/modify.c b/tests-clar/submodule/modify.c
index c0498ce..e326287 100644
--- a/tests-clar/submodule/modify.c
+++ b/tests-clar/submodule/modify.c
@@ -11,20 +11,7 @@ static git_repository *g_repo = NULL;
 
 void test_submodule_modify__initialize(void)
 {
-	g_repo = cl_git_sandbox_init("submod2");
-
-	cl_fixture_sandbox("submod2_target");
-	p_rename("submod2_target/.gitted", "submod2_target/.git");
-
-	/* must create submod2_target before rewrite so prettify will work */
-	rewrite_gitmodules(git_repository_workdir(g_repo));
-	p_rename("submod2/not-submodule/.gitted", "submod2/not-submodule/.git");
-}
-
-void test_submodule_modify__cleanup(void)
-{
-	cl_git_sandbox_cleanup();
-	cl_fixture_cleanup("submod2_target");
+	g_repo = setup_fixture_submod2();
 }
 
 void test_submodule_modify__add(void)
diff --git a/tests-clar/submodule/status.c b/tests-clar/submodule/status.c
index f1227a5..f5111c8 100644
--- a/tests-clar/submodule/status.c
+++ b/tests-clar/submodule/status.c
@@ -388,7 +388,8 @@ void test_submodule_status__iterator(void)
 	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
 		GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
 		GIT_STATUS_OPT_INCLUDE_IGNORED |
-		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
+		GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY;
 
 	cl_git_pass(git_status_foreach_ext(
 		g_repo, &opts, confirm_submodule_status, &exp));
diff --git a/tests-clar/submodule/submodule_helpers.c b/tests-clar/submodule/submodule_helpers.c
index 3e79c77..d575067 100644
--- a/tests-clar/submodule/submodule_helpers.c
+++ b/tests-clar/submodule/submodule_helpers.c
@@ -4,6 +4,7 @@
 #include "util.h"
 #include "posix.h"
 #include "submodule_helpers.h"
+#include "git2/sys/repository.h"
 
 /* rewrite gitmodules -> .gitmodules
  * rewrite the empty or relative urls inside each module
@@ -102,6 +103,8 @@ git_repository *setup_fixture_submodules(void)
 
 	cl_set_cleanup(cleanup_fixture_submodules, "testrepo.git");
 
+	cl_git_pass(git_repository_reinit_filesystem(repo, 1));
+
 	return repo;
 }
 
@@ -118,5 +121,7 @@ git_repository *setup_fixture_submod2(void)
 
 	cl_set_cleanup(cleanup_fixture_submodules, "submod2_target");
 
+	cl_git_pass(git_repository_reinit_filesystem(repo, 1));
+
 	return repo;
 }