Commit 79194bcdc956406979cd27ac99198826860d3f20

Vicent Marti 2013-12-13T06:20:19

Merge pull request #1986 from libgit2/rb/error-handling-cleanups Clean up some error handling and change callback error behavior

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9c19a5a..48cbccb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,6 +33,7 @@ OPTION( ANDROID				"Build for android NDK"	 				OFF )
 
 OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
 OPTION( USE_SSH				"Link with libssh to enable SSH support" ON )
+OPTION( VALGRIND			"Configure build for valgrind"			OFF )
 
 IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
 	SET( USE_ICONV ON )
@@ -340,9 +341,11 @@ IF (WIN32 AND NOT CYGWIN)
 	ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501)
 	FILE(GLOB SRC_OS src/win32/*.c src/win32/*.h)
 ELSEIF (AMIGA)
-	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
-	FILE(GLOB SRC_OS src/amiga/*.c src/amiga/*.h)
+	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
 ELSE()
+	IF (VALGRIND)
+		ADD_DEFINITIONS(-DNO_MMAP)
+	ENDIF()
 	FILE(GLOB SRC_OS src/unix/*.c src/unix/*.h)
 ENDIF()
 FILE(GLOB SRC_GIT2 src/*.c src/*.h src/transports/*.c src/transports/*.h src/xdiff/*.c src/xdiff/*.h)
diff --git a/include/git2/attr.h b/include/git2/attr.h
index f256ff8..3adbb2c 100644
--- a/include/git2/attr.h
+++ b/include/git2/attr.h
@@ -199,8 +199,9 @@ typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *pa
  *             only once per attribute name, even if there are multiple
  *             rules for a given file.  The highest priority rule will be
  *             used.  Return a non-zero value from this to stop looping.
+ *             The value will be returned from `git_attr_foreach`.
  * @param payload Passed on as extra parameter to callback function.
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_attr_foreach(
 	git_repository *repo,
diff --git a/include/git2/blob.h b/include/git2/blob.h
index dcab4fb..19ad4d9 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -159,37 +159,32 @@ typedef int (*git_blob_chunk_cb)(char *content, size_t max_length, void *payload
  * Write a loose blob to the Object Database from a
  * provider of chunks of data.
  *
- * Provided the `hintpath` parameter is filled, its value
- * will help to determine what git filters should be applied
- * to the object before it can be placed to the object database.
+ * If the `hintpath` parameter is filled, it will be used to determine
+ * what git filters should be applied to the object before it is written
+ * to the object database.
  *
+ * The implementation of the callback MUST respect the following rules:
  *
- * The implementation of the callback has to respect the
- * following rules:
+ *  - `content` must be filled by the callback. The maximum number of
+ *    bytes that the buffer can accept per call is defined by the
+ *    `max_length` parameter. Allocation and freeing of the buffer will
+ *    be taken care of by libgit2.
  *
- *  - `content` will have to be filled by the consumer. The maximum number
- * of bytes that the buffer can accept per call is defined by the
- * `max_length` parameter. Allocation and freeing of the buffer will be taken
- * care of by the function.
+ *  - The `callback` must return the number of bytes that have been
+ *    written to the `content` buffer.
  *
- *  - The callback is expected to return the number of bytes
- * that `content` have been filled with.
- *
- *  - When there is no more data to stream, the callback should
- * return 0. This will prevent it from being invoked anymore.
- *
- *  - When an error occurs, the callback should return -1.
+ *  - When there is no more data to stream, `callback` should return
+ *    0. This will prevent it from being invoked anymore.
  *
+ *  - If an error occurs, the callback should return a negative value.
+ *    This value will be returned to the caller.
  *
  * @param id Return the id of the written blob
- *
- * @param repo repository where the blob will be written.
- * This repository can be bare or not.
- *
- * @param hintpath if not NULL, will help selecting the filters
- * to apply onto the content of the blob to be created.
- *
- * @return 0 or an error code
+ * @param repo Repository where the blob will be written.
+ *        This repository can be bare or not.
+ * @param hintpath If not NULL, will be used to select data filters
+ *        to apply onto the content of the blob to be created.
+ * @return 0 or error code (from either libgit2 or callback function)
  */
 GIT_EXTERN(int) git_blob_create_fromchunks(
 	git_oid *id,
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index efafdc3..0e9d338 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -174,7 +174,12 @@ typedef enum {
  * - GIT_CHECKOUT_NOTIFY_IGNORED notifies about ignored files.
  *
  * Returning a non-zero value from this callback will cancel the checkout.
- * Notification callbacks are made prior to modifying any files on disk.
+ * The non-zero return value will be propagated back and returned by the
+ * git_checkout_... call.
+ *
+ * Notification callbacks are made prior to modifying any files on disk,
+ * so canceling on any notification will still happen prior to any files
+ * being modified.
  */
 typedef enum {
 	GIT_CHECKOUT_NOTIFY_NONE      = 0,
@@ -252,9 +257,9 @@ typedef struct git_checkout_opts {
  *
  * @param repo repository to check out (must be non-bare)
  * @param opts specifies checkout options (may be NULL)
- * @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing
- * branch, GIT_ERROR otherwise (use giterr_last for information
- * about the error)
+ * @return 0 on success, GIT_EUNBORNBRANCH if HEAD points to a non
+ *         existing branch, non-zero value returned by `notify_cb`, or
+ *         other error code < 0 (use giterr_last for error details)
  */
 GIT_EXTERN(int) git_checkout_head(
 	git_repository *repo,
@@ -266,8 +271,8 @@ GIT_EXTERN(int) git_checkout_head(
  * @param repo repository into which to check out (must be non-bare)
  * @param index index to be checked out (or NULL to use repository index)
  * @param opts specifies checkout options (may be NULL)
- * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
- * about the error)
+ * @return 0 on success, non-zero return value from `notify_cb`, or error
+ *         code < 0 (use giterr_last for error details)
  */
 GIT_EXTERN(int) git_checkout_index(
 	git_repository *repo,
@@ -282,8 +287,8 @@ GIT_EXTERN(int) git_checkout_index(
  * @param treeish a commit, tag or tree which content will be used to update
  * the working directory (or NULL to use HEAD)
  * @param opts specifies checkout options (may be NULL)
- * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
- * about the error)
+ * @return 0 on success, non-zero return value from `notify_cb`, or error
+ *         code < 0 (use giterr_last for error details)
  */
 GIT_EXTERN(int) git_checkout_tree(
 	git_repository *repo,
diff --git a/include/git2/clone.h b/include/git2/clone.h
index 331cf92..59a73aa 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -26,23 +26,25 @@ GIT_BEGIN_DECL
 /**
  * Clone options structure
  *
- * Use zeros to indicate default settings.  It's easiest to use the
- * `GIT_CLONE_OPTIONS_INIT` macro:
+ * Use the GIT_CLONE_OPTIONS_INIT to get the default settings, like this:
  *
  *		git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
  *
- * - `checkout_opts` is options for the checkout step.  To disable checkout,
- *   set the `checkout_strategy` to GIT_CHECKOUT_DEFAULT.
- * - `bare` should be set to zero to create a standard repo, non-zero for
- *   a bare repo
- * - `ignore_cert_errors` should be set to 1 if errors validating the remote host's
- *   certificate should be ignored.
+ * - `checkout_opts` are option passed to the checkout step.  To disable
+ *   checkout, set the `checkout_strategy` to GIT_CHECKOUT_NONE.
+ *   Generally you will want the use GIT_CHECKOUT_SAFE_CREATE to create
+ *   all files in the working directory for the newly cloned repository.
+ * - `bare` should be set to zero (false) to create a standard repo,
+ *   or non-zero for a bare repo
+ * - `ignore_cert_errors` should be set to 1 if errors validating the
+ *   remote host's certificate should be ignored.
  *
  *   ** "origin" remote options: **
- * - `remote_name` is the name given to the "origin" remote.  The default is
- *   "origin".
- * - `checkout_branch` gives the name of the branch to checkout. NULL means
- *   use the remote's HEAD.
+ *
+ * - `remote_name` is the name to be given to the "origin" remote.  The
+ *   default is "origin".
+ * - `checkout_branch` gives the name of the branch to checkout.  NULL
+ *   means use the remote's HEAD.
  */
 
 typedef struct git_clone_options {
@@ -70,16 +72,17 @@ typedef struct git_clone_options {
  * @param out pointer that will receive the resulting repository object
  * @param url the remote repository to clone
  * @param local_path local directory to clone to
- * @param options configuration options for the clone.  If NULL, the function
- * works as though GIT_OPTIONS_INIT were passed.
- * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
- * about the error)
+ * @param options configuration options for the clone.  If NULL, the
+ *        function works as though GIT_OPTIONS_INIT were passed.
+ * @return 0 on success, any non-zero return value from a callback
+ *         function, or a negative value to indicate an error (use
+ *         `giterr_last` for a detailed error message)
  */
 GIT_EXTERN(int) git_clone(
-		git_repository **out,
-		const char *url,
-		const char *local_path,
-		const git_clone_options *options);
+	git_repository **out,
+	const char *url,
+	const char *local_path,
+	const git_clone_options *options);
 
 /**
  * Clone into a repository
@@ -91,11 +94,17 @@ GIT_EXTERN(int) git_clone(
  * @param repo the repository to use
  * @param remote the remote repository to clone from
  * @param co_opts options to use during checkout
- * @param branch the branch to checkout after the clone, pass NULL for the remote's
- * default branch
- * @return 0 on success or an error code
+ * @param branch the branch to checkout after the clone, pass NULL for the
+ *        remote's default branch
+ * @return 0 on success, any non-zero return value from a callback
+ *         function, or a negative value to indicate an error (use
+ *         `giterr_last` for a detailed error message)
  */
-GIT_EXTERN(int) git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_opts *co_opts, const char *branch);
+GIT_EXTERN(int) git_clone_into(
+	git_repository *repo,
+	git_remote *remote,
+	const git_checkout_opts *co_opts,
+	const char *branch);
 
 /** @} */
 GIT_END_DECL
diff --git a/include/git2/config.h b/include/git2/config.h
index 95da4bc..f650f1b 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -450,13 +450,13 @@ GIT_EXTERN(int) git_config_delete_multivar(git_config *cfg, const char *name, co
  *
  * The callback receives the normalized name and value of each variable
  * in the config backend, and the data pointer passed to this function.
- * As soon as one of the callback functions returns something other than 0,
- * this function stops iterating and returns `GIT_EUSER`.
+ * If the callback returns a non-zero value, the function stops iterating
+ * and returns that value to the caller.
  *
  * @param cfg where to get the variables from
  * @param callback the function to call on each variable
  * @param payload the data to pass to the callback
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_config_foreach(
 	const git_config *cfg,
@@ -612,8 +612,8 @@ GIT_EXTERN(int) git_config_parse_int64(int64_t *out, const char *value);
 GIT_EXTERN(int) git_config_backend_foreach_match(
 	git_config_backend *backend,
 	const char *regexp,
-	int (*fn)(const git_config_entry *, void *),
-	void *data);
+	git_config_foreach_cb callback,
+	void *payload);
 
 
 /** @} */
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 315cc12..76fb236 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -468,7 +468,7 @@ typedef int (*git_diff_line_cb)(
  * Flags to control the behavior of diff rename/copy detection.
  */
 typedef enum {
-	/** Obey `diff.renames`. This is overridden by any other GIT_DIFF_FIND_ALL flag. */
+	/** Obey `diff.renames`. Overridden by any other GIT_DIFF_FIND_... flag. */
 	GIT_DIFF_FIND_BY_CONFIG = 0,
 
 	/** Look for renames? (`--find-renames`) */
@@ -577,9 +577,9 @@ typedef struct {
 	unsigned int version;
 
 	/**
-	 * Combination of git_diff_find_t values (default FIND_BY_CONFIG).
-	 * Note that if you don't explicitly set this, `diff.renames` could be set
-	 * to false, resulting in `git_diff_find_similar` doing nothing. 
+	 * Combination of git_diff_find_t values (default GIT_DIFF_FIND_BY_CONFIG).
+	 * NOTE: if you don't explicitly set this, `diff.renames` could be set
+	 * to false, resulting in `git_diff_find_similar` doing nothing.
 	 */
 	uint32_t flags;
 
@@ -870,7 +870,7 @@ GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff);
  * files whose only changed is a file mode change.
  *
  * Returning a non-zero value from any of the callbacks will terminate
- * the iteration and cause this return `GIT_EUSER`.
+ * the iteration and return the value to the user.
  *
  * @param diff A git_diff generated by one of the above functions.
  * @param file_cb Callback function to make per file in the diff.
@@ -881,7 +881,7 @@ GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff);
  *                same callback will be made for context lines, added, and
  *                removed lines, and even for a deleted trailing newline.
  * @param payload Reference pointer that will be passed to your callbacks.
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_diff_foreach(
 	git_diff *diff,
@@ -918,13 +918,13 @@ typedef enum {
  * Iterate over a diff generating formatted text output.
  *
  * Returning a non-zero value from the callbacks will terminate the
- * iteration and cause this return `GIT_EUSER`.
+ * iteration and return the non-zero value to the caller.
  *
  * @param diff A git_diff generated by one of the above functions.
  * @param format A git_diff_format_t value to pick the text format.
  * @param print_cb Callback to make per line of diff text.
  * @param payload Reference pointer that will be passed to your callback.
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_diff_print(
 	git_diff *diff,
@@ -964,7 +964,7 @@ GIT_EXTERN(int) git_diff_print(
  * @param hunk_cb Callback for each hunk in diff; can be NULL
  * @param line_cb Callback for each line in diff; can be NULL
  * @param payload Payload passed to each callback function
- * @return 0 on success, GIT_EUSER on non-zero callback return, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_diff_blobs(
 	const git_blob *old_blob,
@@ -999,7 +999,7 @@ GIT_EXTERN(int) git_diff_blobs(
  * @param hunk_cb Callback for each hunk in diff; can be NULL
  * @param line_cb Callback for each line in diff; can be NULL
  * @param payload Payload passed to each callback function
- * @return 0 on success, GIT_EUSER on non-zero callback return, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_diff_blob_to_buffer(
 	const git_blob *old_blob,
diff --git a/include/git2/errors.h b/include/git2/errors.h
index f1a8ea1..973d560 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -8,7 +8,6 @@
 #define INCLUDE_git_errors_h__
 
 #include "common.h"
-#include "buffer.h"
 
 /**
  * @file git2/errors.h
@@ -20,25 +19,38 @@ GIT_BEGIN_DECL
 
 /** Generic return codes */
 typedef enum {
-	GIT_OK = 0,
-	GIT_ERROR = -1,
-	GIT_ENOTFOUND = -3,
-	GIT_EEXISTS = -4,
-	GIT_EAMBIGUOUS = -5,
-	GIT_EBUFS = -6,
-	GIT_EUSER = -7,
-	GIT_EBAREREPO = -8,
-	GIT_EUNBORNBRANCH = -9,
-	GIT_EUNMERGED = -10,
-	GIT_ENONFASTFORWARD = -11,
-	GIT_EINVALIDSPEC = -12,
-	GIT_EMERGECONFLICT = -13,
-	GIT_ELOCKED = -14,
+	GIT_OK         =  0,		/*< No error */
 
-	GIT_PASSTHROUGH = -30,
-	GIT_ITEROVER = -31,
+	GIT_ERROR      = -1,		/*< Generic error */
+	GIT_ENOTFOUND  = -3,		/*< Requested object could not be found */
+	GIT_EEXISTS    = -4,		/*< Object exists preventing operation */
+	GIT_EAMBIGUOUS = -5,		/*< More than one object matches */
+	GIT_EBUFS      = -6,		/*< Output buffer too short to hold data */
+
+	/* GIT_EUSER is a special error that is never generated by libgit2
+	 * code.  You can return it from a callback (e.g to stop an iteration)
+	 * to know that it was generated by the callback and not by libgit2.
+	 */
+	GIT_EUSER      = -7,
+
+	GIT_EBAREREPO       =  -8,	/*< Operation not allowed on bare repository */
+	GIT_EUNBORNBRANCH   =  -9,	/*< HEAD refers to branch with no commits */
+	GIT_EUNMERGED       = -10,	/*< Merge in progress prevented operation */
+	GIT_ENONFASTFORWARD = -11,	/*< Reference was not fast-forwardable */
+	GIT_EINVALIDSPEC    = -12,	/*< Name/ref spec was not in a valid format */
+	GIT_EMERGECONFLICT  = -13,	/*< Merge conflicts prevented operation */
+	GIT_ELOCKED         = -14,	/*< Lock file prevented operation */
+
+	GIT_PASSTHROUGH     = -30,	/*< Internal only */
+	GIT_ITEROVER        = -31,	/*< Signals end of iteration with iterator */
 } git_error_code;
 
+/**
+ * Structure to store extra details of the last error that occurred.
+ *
+ * This is kept on a per-thread basis if GIT_THREADS was defined when the
+ * library was build, otherwise one is kept globally for the library
+ */
 typedef struct {
 	char *message;
 	int klass;
@@ -72,6 +84,7 @@ typedef enum {
 	GITERR_SSH,
 	GITERR_FILTER,
 	GITERR_REVERT,
+	GITERR_CALLBACK,
 } git_error_t;
 
 /**
@@ -91,7 +104,7 @@ GIT_EXTERN(void) giterr_clear(void);
  * Get the last error data and clear it.
  *
  * This copies the last error into the given `git_error` struct
- * and returns 0 if the copy was successful, leaving the error 
+ * and returns 0 if the copy was successful, leaving the error
  * cleared as if `giterr_clear` had been called.
  *
  * If there was no existing error in the library, -1 will be returned
diff --git a/include/git2/index.h b/include/git2/index.h
index a60db37..ffefad1 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -493,7 +493,7 @@ GIT_EXTERN(int) git_index_remove_bypath(git_index *index, const char *path);
  * item in the working directory immediately *before* it is added to /
  * updated in the index.  Returning zero will add the item to the index,
  * greater than zero will skip the item, and less than zero will abort the
- * scan and cause GIT_EUSER to be returned.
+ * scan and return that value to the caller.
  *
  * @param index an existing index object
  * @param pathspec array of path patterns
@@ -502,7 +502,7 @@ GIT_EXTERN(int) git_index_remove_bypath(git_index *index, const char *path);
  *                 gets index of matching pathspec entry); can be NULL;
  *                 return 0 to add, >0 to skip, <0 to abort scan.
  * @param payload payload passed through to callback function
- * @return 0 or an error code
+ * @return 0 on success, negative callback return value, or error code
  */
 GIT_EXTERN(int) git_index_add_all(
 	git_index *index,
@@ -524,7 +524,7 @@ GIT_EXTERN(int) git_index_add_all(
  *                 gets index of matching pathspec entry); can be NULL;
  *                 return 0 to add, >0 to skip, <0 to abort scan.
  * @param payload payload passed through to callback function
- * @return 0 or an error code
+ * @return 0 on success, negative callback return value, or error code
  */
 GIT_EXTERN(int) git_index_remove_all(
 	git_index *index,
@@ -553,7 +553,7 @@ GIT_EXTERN(int) git_index_remove_all(
  *                 gets index of matching pathspec entry); can be NULL;
  *                 return 0 to add, >0 to skip, <0 to abort scan.
  * @param payload payload passed through to callback function
- * @return 0 or an error code
+ * @return 0 on success, negative callback return value, or error code
  */
 GIT_EXTERN(int) git_index_update_all(
 	git_index *index,
diff --git a/include/git2/notes.h b/include/git2/notes.h
index 7636163..0cb6ce5 100644
--- a/include/git2/notes.h
+++ b/include/git2/notes.h
@@ -189,7 +189,7 @@ GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo);
  *
  * @param payload Extra parameter to callback function.
  *
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_note_foreach(
 	git_repository *repo,
diff --git a/include/git2/odb.h b/include/git2/odb.h
index ad56384..82df4d3 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -189,7 +189,7 @@ GIT_EXTERN(int) git_odb_refresh(struct git_odb *db);
  * @param db database to use
  * @param cb the callback to call for each object
  * @param payload data to pass to the callback
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload);
 
diff --git a/include/git2/pack.h b/include/git2/pack.h
index 88a2716..11bb559 100644
--- a/include/git2/pack.h
+++ b/include/git2/pack.h
@@ -52,7 +52,7 @@ typedef enum {
 	GIT_PACKBUILDER_ADDING_OBJECTS = 0,
 	GIT_PACKBUILDER_DELTAFICATION = 1,
 } git_packbuilder_stage_t;
-	
+
 /**
  * Initialize a new packbuilder
  *
@@ -143,6 +143,7 @@ GIT_EXTERN(int) git_packbuilder_write(
 GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
 
 typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
+
 /**
  * Create the new pack and pass each object to the callback
  *
diff --git a/include/git2/patch.h b/include/git2/patch.h
index 6a6ad92..e09f625 100644
--- a/include/git2/patch.h
+++ b/include/git2/patch.h
@@ -218,13 +218,13 @@ GIT_EXTERN(size_t) git_patch_size(
  * Serialize the patch to text via callback.
  *
  * Returning a non-zero value from the callback will terminate the iteration
- * and cause this return `GIT_EUSER`.
+ * and return that value to the caller.
  *
  * @param patch A git_patch representing changes to one file
  * @param print_cb Callback function to output lines of the patch.  Will be
  *                 called for file headers, hunk headers, and diff lines.
  * @param payload Reference pointer that will be passed to your callbacks.
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_patch_print(
 	git_patch *patch,
diff --git a/include/git2/push.h b/include/git2/push.h
index 77ef740..12f0e7f 100644
--- a/include/git2/push.h
+++ b/include/git2/push.h
@@ -132,17 +132,19 @@ GIT_EXTERN(int) git_push_finish(git_push *push);
 GIT_EXTERN(int) git_push_unpack_ok(git_push *push);
 
 /**
- * Call callback `cb' on each status
+ * Invoke callback `cb' on each status entry
  *
  * For each of the updated references, we receive a status report in the
  * form of `ok refs/heads/master` or `ng refs/heads/master <msg>`.
  * `msg != NULL` means the reference has not been updated for the given
  * reason.
  *
+ * Return a non-zero value from the callback to stop the loop.
+ *
  * @param push The push object
  * @param cb The callback to call on each object
  *
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_push_status_foreach(git_push *push,
 			int (*cb)(const char *ref, const char *msg, void *data),
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 4041947..e2bfa96 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -310,20 +310,33 @@ typedef int (*git_reference_foreach_name_cb)(const char *name, void *payload);
  * Perform a callback on each reference in the repository.
  *
  * The `callback` function will be called for each reference in the
- * repository, receiving the name of the reference and the `payload` value
+ * repository, receiving the reference object and the `payload` value
  * passed to this method.  Returning a non-zero value from the callback
  * will terminate the iteration.
  *
  * @param repo Repository where to find the refs
  * @param callback Function which will be called for every listed ref
  * @param payload Additional data to pass to the callback
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_reference_foreach(
 	git_repository *repo,
 	git_reference_foreach_cb callback,
 	void *payload);
 
+/**
+ * Perform a callback on the fully-qualified name of each reference.
+ *
+ * The `callback` function will be called for each reference in the
+ * repository, receiving the name of the reference and the `payload` value
+ * passed to this method.  Returning a non-zero value from the callback
+ * will terminate the iteration.
+ *
+ * @param repo Repository where to find the refs
+ * @param callback Function which will be called for every listed ref name
+ * @param payload Additional data to pass to the callback
+ * @return 0 on success, non-zero callback return value, or error code
+ */
 GIT_EXTERN(int) git_reference_foreach_name(
 	git_repository *repo,
 	git_reference_foreach_name_cb callback,
diff --git a/include/git2/repository.h b/include/git2/repository.h
index c6bd4dc..9f71d29 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -503,14 +503,18 @@ typedef int (*git_repository_fetchhead_foreach_cb)(const char *ref_name,
 	void *payload);
 
 /**
- * Call callback 'callback' for each entry in the given FETCH_HEAD file.
+ * Invoke 'callback' for each entry in the given FETCH_HEAD file.
+ *
+ * Return a non-zero value from the callback to stop the loop.
  *
  * @param repo A repository object
  * @param callback Callback function
  * @param payload Pointer to callback data (optional)
- * @return 0 on success, GIT_ENOTFOUND, GIT_EUSER or error
+ * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if
+ *         there is no FETCH_HEAD file, or other error code.
  */
-GIT_EXTERN(int) git_repository_fetchhead_foreach(git_repository *repo,
+GIT_EXTERN(int) git_repository_fetchhead_foreach(
+	git_repository *repo,
 	git_repository_fetchhead_foreach_cb callback,
 	void *payload);
 
@@ -518,15 +522,19 @@ typedef int (*git_repository_mergehead_foreach_cb)(const git_oid *oid,
 	void *payload);
 
 /**
- * If a merge is in progress, call callback 'cb' for each commit ID in the
+ * If a merge is in progress, invoke 'callback' for each commit ID in the
  * MERGE_HEAD file.
  *
+ * Return a non-zero value from the callback to stop the loop.
+ *
  * @param repo A repository object
  * @param callback Callback function
  * @param payload Pointer to callback data (optional)
- * @return 0 on success, GIT_ENOTFOUND, GIT_EUSER or error
+ * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if
+ *         there is no MERGE_HEAD file, or other error code.
  */
-GIT_EXTERN(int) git_repository_mergehead_foreach(git_repository *repo,
+GIT_EXTERN(int) git_repository_mergehead_foreach(
+	git_repository *repo,
 	git_repository_mergehead_foreach_cb callback,
 	void *payload);
 
diff --git a/include/git2/stash.h b/include/git2/stash.h
index b48d33f..e2fe2cf 100644
--- a/include/git2/stash.h
+++ b/include/git2/stash.h
@@ -62,19 +62,15 @@ GIT_EXTERN(int) git_stash_save(
 	unsigned int flags);
 
 /**
- * When iterating over all the stashed states, callback that will be
- * issued per entry.
+ * This is a callback function you can provide to iterate over all the
+ * stashed states that will be invoked per entry.
  *
  * @param index The position within the stash list. 0 points to the
- * most recent stashed state.
- *
+ *              most recent stashed state.
  * @param message The stash message.
- *
  * @param stash_id The commit oid of the stashed state.
- *
  * @param payload Extra parameter to callback function.
- *
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 to continue iterating or non-zero to stop
  */
 typedef int (*git_stash_cb)(
 	size_t index,
@@ -89,12 +85,12 @@ typedef int (*git_stash_cb)(
  *
  * @param repo Repository where to find the stash.
  *
- * @param callback Callback to invoke per found stashed state. The most recent
- * stash state will be enumerated first.
+ * @param callback Callback to invoke per found stashed state. The most
+ *                 recent stash state will be enumerated first.
  *
  * @param payload Extra parameter to callback function.
  *
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_stash_foreach(
 	git_repository *repo,
diff --git a/include/git2/status.h b/include/git2/status.h
index 4ec3432..aa68c0d 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -203,12 +203,12 @@ typedef struct {
  * into this function.
  *
  * If the callback returns a non-zero value, this function will stop looping
- * and return GIT_EUSER.
+ * and return that value to caller.
  *
  * @param repo A repository object
  * @param callback The function to call on each file
  * @param payload Pointer to pass through to callback function
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_status_foreach(
 	git_repository *repo,
@@ -227,7 +227,7 @@ GIT_EXTERN(int) git_status_foreach(
  * @param opts Status options structure
  * @param callback The function to call on each file
  * @param payload Pointer to pass through to callback function
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success, non-zero callback return value, or error code
  */
 GIT_EXTERN(int) git_status_foreach_ext(
 	git_repository *repo,
diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index 94ad3ae..8fe21c9 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -149,6 +149,7 @@ typedef int (*git_filter_init_fn)(git_filter *self);
  * Specified as `filter.shutdown`, this is an optional callback invoked
  * when the filter is unregistered or when libgit2 is shutting down.  It
  * will be called once at most and should release resources as needed.
+ * This may be called even if the `initialize` callback was not made.
  *
  * Typically this function will free the `git_filter` object itself.
  */
diff --git a/include/git2/tree.h b/include/git2/tree.h
index d94b446..4223656 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -332,11 +332,18 @@ GIT_EXTERN(int) git_treebuilder_insert(
 GIT_EXTERN(int) git_treebuilder_remove(
 	git_treebuilder *bld, const char *filename);
 
+/**
+ * Callback for git_treebuilder_filter
+ *
+ * The return value is treated as a boolean, with zero indicating that the
+ * entry should be left alone and any non-zero value meaning that the
+ * entry should be removed from the treebuilder list (i.e. filtered out).
+ */
 typedef int (*git_treebuilder_filter_cb)(
 	const git_tree_entry *entry, void *payload);
 
 /**
- * Filter the entries in the tree
+ * Selectively remove entries in the tree
  *
  * The `filter` callback will be called for each entry in the tree with a
  * pointer to the entry and the provided `payload`; if the callback returns
@@ -344,7 +351,7 @@ typedef int (*git_treebuilder_filter_cb)(
  *
  * @param bld Tree builder
  * @param filter Callback to filter entries
- * @param payload Extra data to pass to filter
+ * @param payload Extra data to pass to filter callback
  */
 GIT_EXTERN(void) git_treebuilder_filter(
 	git_treebuilder *bld,
diff --git a/src/amiga/map.c b/src/amiga/map.c
deleted file mode 100644
index 0ba7995..0000000
--- a/src/amiga/map.c
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#include <git2/common.h>
-
-#ifndef GIT_WIN32
-
-#include "posix.h"
-#include "map.h"
-#include <errno.h>
-
-int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
-{
-	GIT_MMAP_VALIDATE(out, len, prot, flags);
-
-	out->data = NULL;
-	out->len = 0;
-
-	if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) {
-		giterr_set(GITERR_OS, "Trying to map shared-writeable");
-		return -1;
-	}
-
-	out->data = malloc(len);
-	GITERR_CHECK_ALLOC(out->data);
-
-	if ((p_lseek(fd, offset, SEEK_SET) < 0) || ((size_t)p_read(fd, out->data, len) != len)) {
-		giterr_set(GITERR_OS, "mmap emulation failed");
-		return -1;
-	}
-
-	out->len = len;
-	return 0;
-}
-
-int p_munmap(git_map *map)
-{
-	assert(map != NULL);
-	free(map->data);
-
-	return 0;
-}
-
-#endif
-
diff --git a/src/attr.c b/src/attr.c
index 98a328a..e6e274e 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -193,8 +193,7 @@ int git_attr_foreach(
 
 				error = callback(assign->name, assign->value, payload);
 				if (error) {
-					giterr_clear();
-					error = GIT_EUSER;
+					giterr_set_after_callback(error);
 					goto cleanup;
 				}
 			}
@@ -536,7 +535,7 @@ static int collect_attr_files(
 	int error;
 	git_buf dir = GIT_BUF_INIT;
 	const char *workdir = git_repository_workdir(repo);
-	attr_walk_up_info info;
+	attr_walk_up_info info = { NULL };
 
 	if (git_attr_cache__init(repo) < 0 ||
 		git_vector_init(files, 4, NULL) < 0)
@@ -603,11 +602,15 @@ static int attr_cache__lookup_path(
 {
 	git_buf buf = GIT_BUF_INIT;
 	int error;
-	const char *cfgval = NULL;
+	const git_config_entry *entry = NULL;
 
 	*out = NULL;
 
-	if (!(error = git_config_get_string(&cfgval, cfg, key))) {
+	if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
+		return error;
+
+	if (entry) {
+		const char *cfgval = entry->value;
 
 		/* expand leading ~/ as needed */
 		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
@@ -616,13 +619,9 @@ static int attr_cache__lookup_path(
 		else if (cfgval)
 			*out = git__strdup(cfgval);
 
-	} else if (error == GIT_ENOTFOUND) {
-		giterr_clear();
-		error = 0;
-
-		if (!git_futils_find_xdg_file(&buf, fallback))
-			*out = git_buf_detach(&buf);
 	}
+	else if (!git_futils_find_xdg_file(&buf, fallback))
+		*out = git_buf_detach(&buf);
 
 	git_buf_free(&buf);
 
diff --git a/src/blame.c b/src/blame.c
index ea9f77a..a135741 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -108,17 +108,23 @@ git_blame* git_blame__alloc(
 	git_blame_options opts,
 	const char *path)
 {
-	git_blame *gbr = (git_blame*)git__calloc(1, sizeof(git_blame));
-	if (!gbr) {
-		giterr_set_oom();
+	git_blame *gbr = git__calloc(1, sizeof(git_blame));
+	if (!gbr)
 		return NULL;
-	}
-	git_vector_init(&gbr->hunks, 8, hunk_cmp);
-	git_vector_init(&gbr->paths, 8, paths_cmp);
+
 	gbr->repository = repo;
 	gbr->options = opts;
-	gbr->path = git__strdup(path);
-	git_vector_insert(&gbr->paths, git__strdup(path));
+
+	if (git_vector_init(&gbr->hunks, 8, hunk_cmp) < 0 ||
+		git_vector_init(&gbr->paths, 8, paths_cmp) < 0 ||
+		(gbr->path = git__strdup(path)) == NULL ||
+		git_vector_insert(&gbr->paths, git__strdup(path)) < 0)
+	{
+		git_blame_free(gbr);
+		git__free(gbr);
+		return NULL;
+	}
+
 	return gbr;
 }
 
@@ -126,7 +132,6 @@ void git_blame_free(git_blame *blame)
 {
 	size_t i;
 	git_blame_hunk *hunk;
-	char *path;
 
 	if (!blame) return;
 
@@ -134,13 +139,11 @@ void git_blame_free(git_blame *blame)
 		free_hunk(hunk);
 	git_vector_free(&blame->hunks);
 
-	git_vector_foreach(&blame->paths, i, path)
-		git__free(path);
-	git_vector_free(&blame->paths);
+	git_vector_free_deep(&blame->paths);
 
 	git_array_clear(blame->line_index);
 
-	git__free((void*)blame->path);
+	git__free(blame->path);
 	git_blob_free(blame->final_blob);
 	git__free(blame);
 }
diff --git a/src/blame.h b/src/blame.h
index 637e439..7e23de8 100644
--- a/src/blame.h
+++ b/src/blame.h
@@ -64,7 +64,7 @@ typedef struct git_blame__entry {
 } git_blame__entry;
 
 struct git_blame {
-	const char *path;
+	char *path;
 	git_repository *repository;
 	git_blame_options options;
 
diff --git a/src/blob.c b/src/blob.c
index 2c6d528..ab344ae 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -272,37 +272,44 @@ int git_blob_create_fromchunks(
 	int (*source_cb)(char *content, size_t max_length, void *payload),
 	void *payload)
 {
-	int error = -1, read_bytes;
+	int error;
 	char *content = NULL;
 	git_filebuf file = GIT_FILEBUF_INIT;
 	git_buf path = GIT_BUF_INIT;
 
-	if (git_buf_joinpath(
-			&path, git_repository_path(repo), GIT_OBJECTS_DIR "streamed") < 0)
+	assert(oid && repo && source_cb);
+
+	if ((error = git_buf_joinpath(
+			&path, git_repository_path(repo), GIT_OBJECTS_DIR "streamed")) < 0)
 		goto cleanup;
 
 	content = git__malloc(BUFFER_SIZE);
 	GITERR_CHECK_ALLOC(content);
 
-	if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666) < 0)
+	if ((error = git_filebuf_open(
+			&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666)) < 0)
 		goto cleanup;
 
 	while (1) {
-		read_bytes = source_cb(content, BUFFER_SIZE, payload);
-
-		assert(read_bytes <= BUFFER_SIZE);
+		int read_bytes = source_cb(content, BUFFER_SIZE, payload);
 
-		if (read_bytes <= 0)
+		if (!read_bytes)
 			break;
 
-		if (git_filebuf_write(&file, content, read_bytes) < 0)
+		if (read_bytes > BUFFER_SIZE) {
+			giterr_set(GITERR_OBJECT, "Invalid chunk size while creating blob");
+			error = GIT_EBUFS;
+		} else if (read_bytes < 0) {
+			error = giterr_set_after_callback(read_bytes);
+		} else {
+			error = git_filebuf_write(&file, content, read_bytes);
+		}
+
+		if (error < 0)
 			goto cleanup;
 	}
 
-	if (read_bytes < 0)
-		goto cleanup;
-
-	if (git_filebuf_flush(&file) < 0)
+	if ((error = git_filebuf_flush(&file)) < 0)
 		goto cleanup;
 
 	error = git_blob__create_from_paths(
@@ -312,6 +319,7 @@ cleanup:
 	git_buf_free(&path);
 	git_filebuf_cleanup(&file);
 	git__free(content);
+
 	return error;
 }
 
diff --git a/src/branch.c b/src/branch.c
index 95b3fd9..ef71c2c 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -90,29 +90,28 @@ int git_branch_delete(git_reference *branch)
 
 	assert(branch);
 
-	if (!git_reference_is_branch(branch) &&
-		!git_reference_is_remote(branch)) {
-		giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.", git_reference_name(branch));
-		return -1;
+	if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) {
+		giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.",
+			git_reference_name(branch));
+		return GIT_ENOTFOUND;
 	}
 
 	if ((is_head = git_branch_is_head(branch)) < 0)
 		return is_head;
 
 	if (is_head) {
-		giterr_set(GITERR_REFERENCE,
-				"Cannot delete branch '%s' as it is the current HEAD of the repository.", git_reference_name(branch));
+		giterr_set(GITERR_REFERENCE, "Cannot delete branch '%s' as it is "
+			"the current HEAD of the repository.", git_reference_name(branch));
 		return -1;
 	}
 
-	if (git_buf_printf(&config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
+	if (git_buf_join(&config_section, '.', "branch",
+			git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
 		goto on_error;
 
 	if (git_config_rename_section(
-		git_reference_owner(branch), 
-		git_buf_cstr(&config_section),
-		NULL) < 0)
-			goto on_error;
+		git_reference_owner(branch), git_buf_cstr(&config_section), NULL) < 0)
+		goto on_error;
 
 	if (git_reference_delete(branch) < 0)
 		goto on_error;
@@ -206,17 +205,21 @@ int git_branch_move(
 	if (error < 0)
 		goto done;
 
-	git_buf_printf(&old_config_section,
-		"branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
-
-	git_buf_printf(&new_config_section, "branch.%s", new_branch_name);
+	/* first update ref then config so failure won't trash config */
 
-	if ((error = git_config_rename_section(git_reference_owner(branch),
-		git_buf_cstr(&old_config_section),
-		git_buf_cstr(&new_config_section))) < 0)
+	error = git_reference_rename(
+		out, branch, git_buf_cstr(&new_reference_name), force);
+	if (error < 0)
 		goto done;
 
-	error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force);
+	git_buf_join(&old_config_section, '.', "branch",
+		git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
+	git_buf_join(&new_config_section, '.', "branch", new_branch_name);
+
+	error = git_config_rename_section(
+		git_reference_owner(branch),
+		git_buf_cstr(&old_config_section),
+		git_buf_cstr(&new_config_section));
 
 done:
 	git_buf_free(&new_reference_name);
diff --git a/src/checkout.c b/src/checkout.c
index 6d7e3cf..0f30d16 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -83,10 +83,8 @@ static int checkout_notify(
 	const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL;
 	const char *path = NULL;
 
-	if (!data->opts.notify_cb)
-		return 0;
-
-	if ((why & data->opts.notify_flags) == 0)
+	if (!data->opts.notify_cb ||
+		(why & data->opts.notify_flags) == 0)
 		return 0;
 
 	if (wditem) {
@@ -125,8 +123,13 @@ static int checkout_notify(
 		path = delta->old_file.path;
 	}
 
-	return data->opts.notify_cb(
-		why, path, baseline, target, workdir, data->opts.notify_payload);
+	{
+		int error = data->opts.notify_cb(
+			why, path, baseline, target, workdir, data->opts.notify_payload);
+
+		return giterr_set_after_callback_function(
+			error, "git_checkout notification");
+	}
 }
 
 static bool checkout_is_workdir_modified(
@@ -186,69 +189,66 @@ static bool checkout_is_workdir_modified(
 	((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
 
 static int checkout_action_common(
+	int *action,
 	checkout_data *data,
-	int action,
 	const git_diff_delta *delta,
 	const git_index_entry *wd)
 {
 	git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
 
-	if (action <= 0)
-		return action;
-
 	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
-		action = (action & ~CHECKOUT_ACTION__REMOVE);
+		*action = (*action & ~CHECKOUT_ACTION__REMOVE);
 
-	if ((action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
+	if ((*action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
 		if (S_ISGITLINK(delta->new_file.mode))
-			action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
+			*action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
 				CHECKOUT_ACTION__UPDATE_SUBMODULE;
 
 		/* to "update" a symlink, we must remove the old one first */
 		if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL)
-			action |= CHECKOUT_ACTION__REMOVE;
+			*action |= CHECKOUT_ACTION__REMOVE;
 
 		notify = GIT_CHECKOUT_NOTIFY_UPDATED;
 	}
 
-	if ((action & CHECKOUT_ACTION__CONFLICT) != 0)
+	if ((*action & CHECKOUT_ACTION__CONFLICT) != 0)
 		notify = GIT_CHECKOUT_NOTIFY_CONFLICT;
 
-	if (notify != GIT_CHECKOUT_NOTIFY_NONE &&
-		checkout_notify(data, notify, delta, wd) != 0)
-		return GIT_EUSER;
-
-	return action;
+	return checkout_notify(data, notify, delta, wd);
 }
 
 static int checkout_action_no_wd(
+	int *action,
 	checkout_data *data,
 	const git_diff_delta *delta)
 {
-	int action = CHECKOUT_ACTION__NONE;
+	int error = 0;
+
+	*action = CHECKOUT_ACTION__NONE;
 
 	switch (delta->status) {
 	case GIT_DELTA_UNMODIFIED: /* case 12 */
-		if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL))
-			return GIT_EUSER;
-		action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
+		error = checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL);
+		if (error)
+			return error;
+		*action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
 		break;
 	case GIT_DELTA_ADDED:    /* case 2 or 28 (and 5 but not really) */
-		action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+		*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
 		break;
 	case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
-		action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, CONFLICT);
+		*action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, CONFLICT);
 		break;
 	case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
 		if (delta->new_file.mode == GIT_FILEMODE_TREE)
-			action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
 		break;
 	case GIT_DELTA_DELETED: /* case 8 or 25 */
 	default: /* impossible */
 		break;
 	}
 
-	return checkout_action_common(data, action, delta, NULL);
+	return checkout_action_common(action, data, delta, NULL);
 }
 
 static int checkout_action_wd_only(
@@ -257,6 +257,7 @@ static int checkout_action_wd_only(
 	const git_index_entry *wd,
 	git_vector *pathspec)
 {
+	int error = 0;
 	bool remove = false;
 	git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
 
@@ -269,13 +270,13 @@ static int checkout_action_wd_only(
 	/* check if item is tracked in the index but not in the checkout diff */
 	if (data->index != NULL) {
 		if (wd->mode != GIT_FILEMODE_TREE) {
-			int error;
-
-			if ((error = git_index_find(NULL, data->index, wd->path)) == 0) {
+			if (!(error = git_index_find(NULL, data->index, wd->path))) {
 				notify = GIT_CHECKOUT_NOTIFY_DIRTY;
 				remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
 			} else if (error != GIT_ENOTFOUND)
 				return error;
+			else
+				giterr_clear();
 		} else {
 			/* for tree entries, we have to see if there are any index
 			 * entries that are contained inside that tree
@@ -301,18 +302,16 @@ static int checkout_action_wd_only(
 		remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
 	}
 
-	if (checkout_notify(data, notify, NULL, wd))
-		return GIT_EUSER;
+	error = checkout_notify(data, notify, NULL, wd);
 
-	if (remove) {
+	if (!error && remove) {
 		char *path = git_pool_strdup(&data->pool, wd->path);
 		GITERR_CHECK_ALLOC(path);
 
-		if (git_vector_insert(&data->removes, path) < 0)
-			return -1;
+		error = git_vector_insert(&data->removes, path);
 	}
 
-	return 0;
+	return error;
 }
 
 static bool submodule_is_config_only(
@@ -331,35 +330,35 @@ static bool submodule_is_config_only(
 }
 
 static int checkout_action_with_wd(
+	int *action,
 	checkout_data *data,
 	const git_diff_delta *delta,
 	const git_index_entry *wd)
 {
-	int action = CHECKOUT_ACTION__NONE;
+	*action = CHECKOUT_ACTION__NONE;
 
 	switch (delta->status) {
 	case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */
 		if (checkout_is_workdir_modified(data, &delta->old_file, wd)) {
-			if (checkout_notify(
-					data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
-				return GIT_EUSER;
-			action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
+			GITERR_CHECK_ERROR(
+				checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
+			*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
 		}
 		break;
 	case GIT_DELTA_ADDED: /* case 3, 4 or 6 */
-		action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+		*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
 		break;
 	case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */
 		if (checkout_is_workdir_modified(data, &delta->old_file, wd))
-			action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
 		else
-			action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
+			*action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
 		break;
 	case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */
 		if (checkout_is_workdir_modified(data, &delta->old_file, wd))
-			action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+			*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
 		else
-			action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
 		break;
 	case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */
 		if (delta->old_file.mode == GIT_FILEMODE_TREE) {
@@ -367,92 +366,93 @@ static int checkout_action_with_wd(
 				/* either deleting items in old tree will delete the wd dir,
 				 * or we'll get a conflict when we attempt blob update...
 				 */
-				action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+				*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
 			else if (wd->mode == GIT_FILEMODE_COMMIT) {
 				/* workdir is possibly a "phantom" submodule - treat as a
 				 * tree if the only submodule info came from the config
 				 */
 				if (submodule_is_config_only(data, wd->path))
-					action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+					*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
 				else
-					action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+					*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 			} else
-				action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+				*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
 		}
 		else if (checkout_is_workdir_modified(data, &delta->old_file, wd))
-			action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 		else
-			action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
+			*action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
 
 		/* don't update if the typechange is to a tree */
 		if (delta->new_file.mode == GIT_FILEMODE_TREE)
-			action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB);
+			*action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB);
 		break;
 	default: /* impossible */
 		break;
 	}
 
-	return checkout_action_common(data, action, delta, wd);
+	return checkout_action_common(action, data, delta, wd);
 }
 
 static int checkout_action_with_wd_blocker(
+	int *action,
 	checkout_data *data,
 	const git_diff_delta *delta,
 	const git_index_entry *wd)
 {
-	int action = CHECKOUT_ACTION__NONE;
+	*action = CHECKOUT_ACTION__NONE;
 
 	switch (delta->status) {
 	case GIT_DELTA_UNMODIFIED:
 		/* should show delta as dirty / deleted */
-		if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
-			return GIT_EUSER;
-		action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
+		GITERR_CHECK_ERROR(
+			checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
+		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
 		break;
 	case GIT_DELTA_ADDED:
 	case GIT_DELTA_MODIFIED:
-		action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 		break;
 	case GIT_DELTA_DELETED:
-		action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
 		break;
 	case GIT_DELTA_TYPECHANGE:
 		/* not 100% certain about this... */
-		action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 		break;
 	default: /* impossible */
 		break;
 	}
 
-	return checkout_action_common(data, action, delta, wd);
+	return checkout_action_common(action, data, delta, wd);
 }
 
 static int checkout_action_with_wd_dir(
+	int *action,
 	checkout_data *data,
 	const git_diff_delta *delta,
 	const git_index_entry *wd)
 {
-	int action = CHECKOUT_ACTION__NONE;
+	*action = CHECKOUT_ACTION__NONE;
 
 	switch (delta->status) {
 	case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */
-		if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL) ||
-			checkout_notify(
-				data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
-			return GIT_EUSER;
+		GITERR_CHECK_ERROR(
+			checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL));
+		GITERR_CHECK_ERROR(
+			checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
 		break;
 	case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
 	case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
 		if (delta->old_file.mode == GIT_FILEMODE_COMMIT)
 			/* expected submodule (and maybe found one) */;
 		else if (delta->new_file.mode != GIT_FILEMODE_TREE)
-			action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 		break;
 	case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */
-		if (delta->old_file.mode != GIT_FILEMODE_TREE &&
-			checkout_notify(
-				data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
-			return GIT_EUSER;
+		if (delta->old_file.mode != GIT_FILEMODE_TREE)
+			GITERR_CHECK_ERROR(
+				checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
 		break;
 	case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
 		if (delta->old_file.mode == GIT_FILEMODE_TREE) {
@@ -462,39 +462,41 @@ static int checkout_action_with_wd_dir(
 			 * directory if is it left empty, so we can defer removing the
 			 * dir and it will succeed if no children are left.
 			 */
-			action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
-			if (action != CHECKOUT_ACTION__NONE)
-				action |= CHECKOUT_ACTION__DEFER_REMOVE;
+			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+			if (*action != CHECKOUT_ACTION__NONE)
+				*action |= CHECKOUT_ACTION__DEFER_REMOVE;
 		}
 		else if (delta->new_file.mode != GIT_FILEMODE_TREE)
 			/* For typechange to dir, dir is already created so no action */
-			action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
 		break;
 	default: /* impossible */
 		break;
 	}
 
-	return checkout_action_common(data, action, delta, wd);
+	return checkout_action_common(action, data, delta, wd);
 }
 
 static int checkout_action(
+	int *action,
 	checkout_data *data,
 	git_diff_delta *delta,
 	git_iterator *workdir,
-	const git_index_entry **wditem_ptr,
+	const git_index_entry **wditem,
 	git_vector *pathspec)
 {
-	const git_index_entry *wd = *wditem_ptr;
-	int cmp = -1, act;
+	int cmp = -1, error;
 	int (*strcomp)(const char *, const char *) = data->diff->strcomp;
 	int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
-	int error;
+	int (*advance)(const git_index_entry **, git_iterator *) = NULL;
 
 	/* move workdir iterator to follow along with deltas */
 
 	while (1) {
+		const git_index_entry *wd = *wditem;
+
 		if (!wd)
-			return checkout_action_no_wd(data, delta);
+			return checkout_action_no_wd(action, data, delta);
 
 		cmp = strcomp(wd->path, delta->old_file.path);
 
@@ -512,79 +514,77 @@ static int checkout_action(
 			if (cmp == 0) {
 				if (wd->mode == GIT_FILEMODE_TREE) {
 					/* case 2 - entry prefixed by workdir tree */
-					error = git_iterator_advance_into_or_over(&wd, workdir);
-					if (error && error != GIT_ITEROVER)
-						goto fail;
-					*wditem_ptr = wd;
+					error = git_iterator_advance_into_or_over(wditem, workdir);
+					if (error < 0 && error != GIT_ITEROVER)
+						goto done;
 					continue;
 				}
 
 				/* case 3 maybe - wd contains non-dir where dir expected */
 				if (delta->old_file.path[strlen(wd->path)] == '/') {
-					act = checkout_action_with_wd_blocker(data, delta, wd);
-					*wditem_ptr =
-						git_iterator_advance(&wd, workdir) ? NULL : wd;
-					return act;
+					error = checkout_action_with_wd_blocker(
+						action, data, delta, wd);
+					advance = git_iterator_advance;
+					goto done;
 				}
 			}
 
 			/* case 1 - handle wd item (if it matches pathspec) */
-			if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0)
-				goto fail;
-			if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
+			error = checkout_action_wd_only(data, workdir, wd, pathspec);
+			if (error)
+				goto done;
+			if ((error = git_iterator_advance(wditem, workdir)) < 0 &&
 				error != GIT_ITEROVER)
-				goto fail;
-
-			*wditem_ptr = wd;
+				goto done;
 			continue;
 		}
 
 		if (cmp == 0) {
 			/* case 4 */
-			act = checkout_action_with_wd(data, delta, wd);
-			*wditem_ptr = git_iterator_advance(&wd, workdir) ? NULL : wd;
-			return act;
+			error = checkout_action_with_wd(action, data, delta, wd);
+			advance = git_iterator_advance;
+			goto done;
 		}
 
 		cmp = pfxcomp(wd->path, delta->old_file.path);
 
 		if (cmp == 0) { /* case 5 */
 			if (wd->path[strlen(delta->old_file.path)] != '/')
-				return checkout_action_no_wd(data, delta);
+				return checkout_action_no_wd(action, data, delta);
 
 			if (delta->status == GIT_DELTA_TYPECHANGE) {
 				if (delta->old_file.mode == GIT_FILEMODE_TREE) {
-					act = checkout_action_with_wd(data, delta, wd);
-					if ((error = git_iterator_advance_into(&wd, workdir)) < 0 &&
-						error != GIT_ENOTFOUND)
-						goto fail;
-					*wditem_ptr = wd;
-					return act;
+					error = checkout_action_with_wd(action, data, delta, wd);
+					advance = git_iterator_advance_into;
+					goto done;
 				}
 
 				if (delta->new_file.mode == GIT_FILEMODE_TREE ||
 					delta->new_file.mode == GIT_FILEMODE_COMMIT ||
 					delta->old_file.mode == GIT_FILEMODE_COMMIT)
 				{
-					act = checkout_action_with_wd(data, delta, wd);
-					if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
-						error != GIT_ITEROVER)
-						goto fail;
-					*wditem_ptr = wd;
-					return act;
+					error = checkout_action_with_wd(action, data, delta, wd);
+					advance = git_iterator_advance;
+					goto done;
 				}
 			}
 
-			return checkout_action_with_wd_dir(data, delta, wd);
+			return checkout_action_with_wd_dir(action, data, delta, wd);
 		}
 
 		/* case 6 - wd is after delta */
-		return checkout_action_no_wd(data, delta);
+		return checkout_action_no_wd(action, data, delta);
 	}
 
-fail:
-	*wditem_ptr = NULL;
-	return -1;
+done:
+	if (!error && advance != NULL &&
+		(error = advance(wditem, workdir)) < 0) {
+		*wditem = NULL;
+		if (error == GIT_ITEROVER)
+			error = 0;
+	}
+
+	return error;
 }
 
 static int checkout_remaining_wd_items(
@@ -846,7 +846,7 @@ static int checkout_conflicts_coalesce_renames(
 
 	/* Juggle entries based on renames */
 	names = git_index_name_entrycount(data->index);
-	
+
 	for (i = 0; i < names; i++) {
 		name_entry = git_index_name_get_byindex(data->index, i);
 
@@ -965,7 +965,7 @@ static int checkout_get_actions(
 	checkout_data *data,
 	git_iterator *workdir)
 {
-	int error = 0;
+	int error = 0, act;
 	const git_index_entry *wditem;
 	git_vector pathspec = GIT_VECTOR_INIT, *deltas;
 	git_pool pathpool = GIT_POOL_INIT_STRINGPOOL;
@@ -992,12 +992,9 @@ static int checkout_get_actions(
 	}
 
 	git_vector_foreach(deltas, i, delta) {
-		int act = checkout_action(data, delta, workdir, &wditem, &pathspec);
-
-		if (act < 0) {
-			error = act;
+		error = checkout_action(&act, data, delta, workdir, &wditem, &pathspec);
+		if (error != 0)
 			goto fail;
-		}
 
 		actions[i] = act;
 
@@ -1012,7 +1009,7 @@ static int checkout_get_actions(
 	}
 
 	error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
-	if (error < 0)
+	if (error)
 		goto fail;
 
 	counts[CHECKOUT_ACTION__REMOVE] += data->removes.length;
@@ -1760,9 +1757,6 @@ static int checkout_create_conflicts(checkout_data *data)
 
 static void checkout_data_clear(checkout_data *data)
 {
-	checkout_conflictdata *conflict;
-	size_t i;
-
 	if (data->opts_free_baseline) {
 		git_tree_free(data->opts.baseline);
 		data->opts.baseline = NULL;
@@ -1771,10 +1765,7 @@ static void checkout_data_clear(checkout_data *data)
 	git_vector_free(&data->removes);
 	git_pool_clear(&data->pool);
 
-	git_vector_foreach(&data->conflicts, i, conflict)
-		git__free(conflict);
-
-	git_vector_free(&data->conflicts);
+	git_vector_free_deep(&data->conflicts);
 
 	git__free(data->pfx);
 	data->pfx = NULL;
@@ -1966,7 +1957,7 @@ int git_checkout_iterator(
 	 * actions to be taken, plus look for conflicts and send notifications,
 	 * then loop through conflicts.
 	 */
-	if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) < 0)
+	if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) != 0)
 		goto cleanup;
 
 	data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
@@ -1998,9 +1989,6 @@ int git_checkout_iterator(
 	assert(data.completed_steps == data.total_steps);
 
 cleanup:
-	if (error == GIT_EUSER)
-		giterr_clear();
-
 	if (!error && data.index != NULL &&
 		(data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
 		error = git_index_write(data.index);
diff --git a/src/clone.c b/src/clone.c
index 23aacd4..828c47f 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -115,43 +115,38 @@ static int reference_matches_remote_head(
 {
 	struct head_info *head_info = (struct head_info *)payload;
 	git_oid oid;
+	int error;
 
 	/* TODO: Should we guard against references
 	 * which name doesn't start with refs/heads/ ?
 	 */
 
-	/* Stop looking if we've already found a match */
-	if (head_info->found)
+	error = git_reference_name_to_id(&oid, head_info->repo, reference_name);
+	if (error == GIT_ENOTFOUND) {
+		/* If the reference doesn't exists, it obviously cannot match the
+		 * expected oid. */
+		giterr_clear();
 		return 0;
-
-	if (git_reference_name_to_id(
-		&oid,
-		head_info->repo,
-		reference_name) < 0) {
-			/* If the reference doesn't exists, it obviously cannot match the expected oid. */
-			giterr_clear();
-			return 0;
 	}
 
-	if (git_oid__cmp(&head_info->remote_head_oid, &oid) == 0) {
+	if (!error && !git_oid__cmp(&head_info->remote_head_oid, &oid)) {
 		/* Determine the local reference name from the remote tracking one */
-		if (git_refspec_transform_l(
-			&head_info->branchname,
-			head_info->refspec,
-			reference_name) < 0)
-				return -1;
-
-		if (git_buf_len(&head_info->branchname) > 0) {
-			if (git_buf_sets(
-				&head_info->branchname,
-				git_buf_cstr(&head_info->branchname) + strlen(GIT_REFS_HEADS_DIR)) < 0)
-					return -1;
+		error = git_refspec_transform_l(
+			&head_info->branchname, head_info->refspec, reference_name);
 
-			head_info->found = 1;
+		if (!error &&
+			git_buf_len(&head_info->branchname) > 0 &&
+			!(error = git_buf_sets(
+				&head_info->branchname,
+				git_buf_cstr(&head_info->branchname) +
+				strlen(GIT_REFS_HEADS_DIR))))
+		{
+			head_info->found = true;
+			error = GIT_ITEROVER;
 		}
 	}
 
-	return 0;
+	return error;
 }
 
 static int update_head_to_new_branch(
@@ -160,16 +155,11 @@ static int update_head_to_new_branch(
 	const char *name)
 {
 	git_reference *tracking_branch = NULL;
-	int error;
+	int error = create_tracking_branch(&tracking_branch, repo, target, name);
 
-	if ((error = create_tracking_branch(
-		&tracking_branch,
-		repo,
-		target,
-		name)) < 0)
-			return error;
-
-	error = git_repository_set_head(repo, git_reference_name(tracking_branch));
+	if (!error)
+		error = git_repository_set_head(
+			repo, git_reference_name(tracking_branch));
 
 	git_reference_free(tracking_branch);
 
@@ -178,34 +168,30 @@ static int update_head_to_new_branch(
 
 static int update_head_to_remote(git_repository *repo, git_remote *remote)
 {
-	int retcode = -1;
+	int error = 0;
 	size_t refs_len;
 	git_refspec dummy_spec;
 	const git_remote_head *remote_head, **refs;
 	struct head_info head_info;
 	git_buf remote_master_name = GIT_BUF_INIT;
 
-	if (git_remote_ls(&refs, &refs_len, remote) < 0)
-		return -1;
+	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
+		return error;
 
 	/* Did we just clone an empty repository? */
-	if (refs_len == 0) {
+	if (refs_len == 0)
 		return setup_tracking_config(
-			repo,
-			"master",
-			GIT_REMOTE_ORIGIN,
-			GIT_REFS_HEADS_MASTER_FILE);
-	}
+			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
 
 	/* Get the remote's HEAD. This is always the first ref in the list. */
 	remote_head = refs[0];
 	assert(remote_head);
 
+	memset(&head_info, 0, sizeof(head_info));
 	git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
-	git_buf_init(&head_info.branchname, 16);
 	head_info.repo = repo;
-	head_info.refspec = git_remote__matching_refspec(remote, GIT_REFS_HEADS_MASTER_FILE);
-	head_info.found = 0;
+	head_info.refspec =
+		git_remote__matching_refspec(remote, GIT_REFS_HEADS_MASTER_FILE);
 
 	if (head_info.refspec == NULL) {
 		memset(&dummy_spec, 0, sizeof(git_refspec));
@@ -213,50 +199,46 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 	}
 
 	/* Determine the remote tracking reference name from the local master */
-	if (git_refspec_transform_r(
+	if ((error = git_refspec_transform_r(
 		&remote_master_name,
 		head_info.refspec,
-		GIT_REFS_HEADS_MASTER_FILE) < 0)
-			return -1;
+		GIT_REFS_HEADS_MASTER_FILE)) < 0)
+		return error;
 
 	/* Check to see if the remote HEAD points to the remote master */
-	if (reference_matches_remote_head(git_buf_cstr(&remote_master_name), &head_info) < 0)
+	error = reference_matches_remote_head(
+		git_buf_cstr(&remote_master_name), &head_info);
+	if (error < 0 && error != GIT_ITEROVER)
 		goto cleanup;
 
 	if (head_info.found) {
-		retcode = update_head_to_new_branch(
+		error = update_head_to_new_branch(
 			repo,
 			&head_info.remote_head_oid,
 			git_buf_cstr(&head_info.branchname));
-
 		goto cleanup;
 	}
 
 	/* Not master. Check all the other refs. */
-	if (git_reference_foreach_name(
-		repo,
-		reference_matches_remote_head,
-		&head_info) < 0)
-			goto cleanup;
+	error = git_reference_foreach_name(
+		repo, reference_matches_remote_head, &head_info);
+	if (error < 0 && error != GIT_ITEROVER)
+		goto cleanup;
 
 	if (head_info.found) {
-		retcode = update_head_to_new_branch(
+		error = update_head_to_new_branch(
 			repo,
 			&head_info.remote_head_oid,
 			git_buf_cstr(&head_info.branchname));
-
-		goto cleanup;
 	} else {
-		retcode = git_repository_set_head_detached(
-			repo,
-			&head_info.remote_head_oid);
-		goto cleanup;
+		error = git_repository_set_head_detached(
+			repo, &head_info.remote_head_oid);
 	}
 
 cleanup:
 	git_buf_free(&remote_master_name);
 	git_buf_free(&head_info.branchname);
-	return retcode;
+	return error;
 }
 
 static int update_head_to_branch(
@@ -359,7 +341,7 @@ int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_
 	old_fetchhead = git_remote_update_fetchhead(remote);
 	git_remote_set_update_fetchhead(remote, 0);
 
-	if ((error = git_remote_fetch(remote)) < 0)
+	if ((error = git_remote_fetch(remote)) != 0)
 		goto cleanup;
 
 	if (branch)
@@ -373,10 +355,12 @@ int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_
 
 cleanup:
 	git_remote_set_update_fetchhead(remote, old_fetchhead);
+
 	/* Go back to the original refspecs */
-	if (git_remote_set_fetch_refspecs(remote, &refspecs) < 0) {
-		git_strarray_free(&refspecs);
-		return -1;
+	{
+		int error_alt = git_remote_set_fetch_refspecs(remote, &refspecs);
+		if (!error)
+			error = error_alt;
 	}
 
 	git_strarray_free(&refspecs);
@@ -424,9 +408,10 @@ int git_clone(
 		git_remote_free(origin);
 	}
 
-	if (error < 0) {
+	if (error != 0) {
 		git_repository_free(repo);
 		repo = NULL;
+
 		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
 	}
 
diff --git a/src/common.h b/src/common.h
index a188878..e315b59 100644
--- a/src/common.h
+++ b/src/common.h
@@ -62,7 +62,7 @@
  * Check a return value and propogate result if non-zero.
  */
 #define GITERR_CHECK_ERROR(code) \
-	do { int _err = (code); if (_err < 0) return _err; } while (0)
+	do { int _err = (code); if (_err) return _err; } while (0)
 
 /**
  * Set the error message for this thread, formatting as needed.
@@ -76,28 +76,61 @@ void giterr_set(int error_class, const char *string, ...);
 int giterr_set_regex(const regex_t *regex, int error_code);
 
 /**
- * Gets the system error code for this thread.
+ * Set error message for user callback if needed.
+ *
+ * If the error code in non-zero and no error message is set, this
+ * sets a generic error message.
+ *
+ * @return This always returns the `error_code` parameter.
  */
-GIT_INLINE(int) giterr_system_last(void)
+GIT_INLINE(int) giterr_set_after_callback_function(
+	int error_code, const char *action)
 {
+	if (error_code) {
+		const git_error *e = giterr_last();
+		if (!e || !e->message)
+			giterr_set(e ? e->klass : GITERR_CALLBACK,
+				"%s callback returned %d", action, error_code);
+	}
+	return error_code;
+}
+
 #ifdef GIT_WIN32
-	return GetLastError();
+#define giterr_set_after_callback(code) \
+	giterr_set_after_callback_function((code), __FUNCTION__)
 #else
-	return errno;
+#define giterr_set_after_callback(code) \
+	giterr_set_after_callback_function((code), __func__)
 #endif
-}
+
+/**
+ * Gets the system error code for this thread.
+ */
+int giterr_system_last(void);
 
 /**
  * Sets the system error code for this thread.
  */
-GIT_INLINE(void) giterr_system_set(int code)
-{
-#ifdef GIT_WIN32
-	SetLastError(code);
-#else
-	errno = code;
-#endif
-}
+void giterr_system_set(int code);
+
+/**
+ * Structure to preserve libgit2 error state
+ */
+typedef struct {
+	int       error_code;
+	git_error error_msg;
+} git_error_state;
+
+/**
+ * Capture current error state to restore later, returning error code.
+ * If `error_code` is zero, this does nothing and returns zero.
+ */
+int giterr_capture(git_error_state *state, int error_code);
+
+/**
+ * Restore error state to a previous value, returning saved error code.
+ */
+int giterr_restore(git_error_state *state);
 
 /**
  * Check a versioned structure for validity
diff --git a/src/config.c b/src/config.c
index 0d94713..056a6ae 100644
--- a/src/config.c
+++ b/src/config.c
@@ -480,47 +480,45 @@ int git_config_foreach(
 int git_config_backend_foreach_match(
 	git_config_backend *backend,
 	const char *regexp,
-	int (*fn)(const git_config_entry *, void *),
-	void *data)
+	git_config_foreach_cb cb,
+	void *payload)
 {
 	git_config_entry *entry;
 	git_config_iterator* iter;
 	regex_t regex;
-	int result = 0;
+	int error = 0;
 
 	if (regexp != NULL) {
-		if ((result = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
-			giterr_set_regex(&regex, result);
+		if ((error = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
+			giterr_set_regex(&regex, error);
 			regfree(&regex);
 			return -1;
 		}
 	}
 
-	if ((result = backend->iterator(&iter, backend)) < 0) {
+	if ((error = backend->iterator(&iter, backend)) < 0) {
 		iter = NULL;
 		return -1;
 	}
 
-	while(!(iter->next(&entry, iter) < 0)) {
+	while (!(iter->next(&entry, iter) < 0)) {
 		/* skip non-matching keys if regexp was provided */
 		if (regexp && regexec(&regex, entry->name, 0, NULL, 0) != 0)
 			continue;
 
 		/* abort iterator on non-zero return value */
-		if (fn(entry, data)) {
-			giterr_clear();
-			result = GIT_EUSER;
-			goto cleanup;
+		if ((error = cb(entry, payload)) != 0) {
+			giterr_set_after_callback(error);
+			break;
 		}
 	}
 
-cleanup:
 	if (regexp != NULL)
 		regfree(&regex);
 
 	iter->free(iter);
 
-	return result;
+	return error;
 }
 
 int git_config_foreach_match(
@@ -536,10 +534,9 @@ int git_config_foreach_match(
 	if ((error = git_config_iterator_glob_new(&iter, cfg, regexp)) < 0)
 		return error;
 
-	while ((error = git_config_next(&entry, iter)) == 0) {
-		if(cb(entry, payload)) {
-			giterr_clear();
-			error = GIT_EUSER;
+	while (!(error = git_config_next(&entry, iter))) {
+		if ((error = cb(entry, payload)) != 0) {
+			giterr_set_after_callback(error);
 			break;
 		}
 	}
@@ -620,6 +617,78 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
 /***********
  * Getters
  ***********/
+
+static int config_error_notfound(const char *name)
+{
+	giterr_set(GITERR_CONFIG, "Config value '%s' was not found", name);
+	return GIT_ENOTFOUND;
+}
+
+enum {
+	GET_ALL_ERRORS = 0,
+	GET_NO_MISSING = 1,
+	GET_NO_ERRORS  = 2
+};
+
+static int get_entry(
+	const git_config_entry **out,
+	const git_config *cfg,
+	const char *name,
+	bool normalize_name,
+	int want_errors)
+{
+	int res = GIT_ENOTFOUND;
+	const char *key = name;
+	char *normalized = NULL;
+	size_t i;
+	file_internal *internal;
+
+	*out = NULL;
+
+	if (normalize_name) {
+		if ((res = git_config__normalize_name(name, &normalized)) < 0)
+			goto cleanup;
+		key = normalized;
+	}
+
+	git_vector_foreach(&cfg->files, i, internal) {
+		if (!internal || !internal->file)
+			continue;
+
+		res = internal->file->get(internal->file, key, out);
+		if (res != GIT_ENOTFOUND)
+			break;
+	}
+
+	git__free(normalized);
+
+cleanup:
+	if (res == GIT_ENOTFOUND)
+		res = (want_errors > GET_ALL_ERRORS) ? 0 : config_error_notfound(name);
+	else if (res && (want_errors == GET_NO_ERRORS)) {
+		giterr_clear();
+		res = 0;
+	}
+
+	return res;
+}
+
+int git_config_get_entry(
+	const git_config_entry **out, const git_config *cfg, const char *name)
+{
+	return get_entry(out, cfg, name, true, GET_ALL_ERRORS);
+}
+
+int git_config__lookup_entry(
+	const git_config_entry **out,
+	const git_config *cfg,
+	const char *key,
+	bool no_errors)
+{
+	return get_entry(
+		out, cfg, key, false, no_errors ? GET_NO_ERRORS : GET_NO_MISSING);
+}
+
 int git_config_get_mapped(
 	int *out,
 	const git_config *cfg,
@@ -627,116 +696,91 @@ int git_config_get_mapped(
 	const git_cvar_map *maps,
 	size_t map_n)
 {
-	const char *value;
+	const git_config_entry *entry;
 	int ret;
 
-	if ((ret = git_config_get_string(&value, cfg, name)) < 0)
+	if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
 		return ret;
 
-	return git_config_lookup_map_value(out, maps, map_n, value);
+	return git_config_lookup_map_value(out, maps, map_n, entry->value);
 }
 
 int git_config_get_int64(int64_t *out, const git_config *cfg, const char *name)
 {
-	const char *value;
+	const git_config_entry *entry;
 	int ret;
 
-	if ((ret = git_config_get_string(&value, cfg, name)) < 0)
+	if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
 		return ret;
 
-	return git_config_parse_int64(out, value);
+	return git_config_parse_int64(out, entry->value);
 }
 
 int git_config_get_int32(int32_t *out, const git_config *cfg, const char *name)
 {
-	const char *value;
+	const git_config_entry *entry;
 	int ret;
 
-	if ((ret = git_config_get_string(&value, cfg, name)) < 0)
+	if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
 		return ret;
 
-	return git_config_parse_int32(out, value);
+	return git_config_parse_int32(out, entry->value);
 }
 
-static int get_string_at_file(const char **out, const git_config_backend *file, const char *name)
+int git_config_get_bool(int *out, const git_config *cfg, const char *name)
 {
 	const git_config_entry *entry;
-	int res;
+	int ret;
 
-	res = file->get(file, name, &entry);
-	if (!res)
-		*out = entry->value;
+	if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
+		return ret;
 
-	return res;
+	return git_config_parse_bool(out, entry->value);
 }
 
-static int config_error_notfound(const char *name)
+int git_config_get_string(
+	const char **out, const git_config *cfg, const char *name)
 {
-	giterr_set(GITERR_CONFIG, "Config value '%s' was not found", name);
-	return GIT_ENOTFOUND;
+	const git_config_entry *entry;
+	int ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
+	*out = !ret ? (entry->value ? entry->value : "") : NULL;
+	return ret;
 }
 
-static int get_string(const char **out, const git_config *cfg, const char *name)
+const char *git_config__get_string_force(
+	const git_config *cfg, const char *key, const char *fallback_value)
 {
-	file_internal *internal;
-	unsigned int i;
-	int res;
-
-	git_vector_foreach(&cfg->files, i, internal) {
-		if (!internal || !internal->file)
-			continue;
-
-		res = get_string_at_file(out, internal->file, name);
-		if (res != GIT_ENOTFOUND)
-			return res;
-	}
-
-	return config_error_notfound(name);
+	const git_config_entry *entry;
+	get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
+	return (entry && entry->value) ? entry->value : fallback_value;
 }
 
-int git_config_get_bool(int *out, const git_config *cfg, const char *name)
+int git_config__get_bool_force(
+	const git_config *cfg, const char *key, int fallback_value)
 {
-	const char *value = NULL;
-	int ret;
-
-	if ((ret = get_string(&value, cfg, name)) < 0)
-		return ret;
-
-	return git_config_parse_bool(out, value);
-}
+	int val = fallback_value;
+	const git_config_entry *entry;
 
-int git_config_get_string(const char **out, const git_config *cfg, const char *name)
-{
-	int ret;
-	const char *str = NULL;
+	get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
 
-	if ((ret = get_string(&str, cfg, name)) < 0)
-		return ret;
+	if (entry && git_config_parse_bool(&val, entry->value) < 0)
+		giterr_clear();
 
-	*out = str == NULL ? "" : str;
-	return 0;
+	return val;
 }
 
-int git_config_get_entry(const git_config_entry **out, const git_config *cfg, const char *name)
+int git_config__get_int_force(
+	const git_config *cfg, const char *key, int fallback_value)
 {
-	file_internal *internal;
-	unsigned int i;
-	git_config_backend *file;
-	int ret;
-
-	*out = NULL;
+	int32_t val = (int32_t)fallback_value;
+	const git_config_entry *entry;
 
-	git_vector_foreach(&cfg->files, i, internal) {
-		if (!internal || !internal->file)
-			continue;
-		file = internal->file;
+	get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
 
-		ret = file->get(file, name, out);
-		if (ret != GIT_ENOTFOUND)
-			return ret;
-	}
+	if (entry && git_config_parse_int32(&val, entry->value) < 0)
+		giterr_clear();
 
-	return config_error_notfound(name);
+	return (int)val;
 }
 
 int git_config_get_multivar_foreach(
@@ -753,9 +797,10 @@ int git_config_get_multivar_foreach(
 	found = 0;
 	while ((err = iter->next(&entry, iter)) == 0) {
 		found = 1;
-		if(cb(entry, payload)) {
-			iter->free(iter);
-			return GIT_EUSER;
+
+		if ((err = cb(entry, payload)) != 0) {
+			giterr_set_after_callback(err);
+			break;
 		}
 	}
 
@@ -1070,7 +1115,7 @@ int git_config_parse_int64(int64_t *out, const char *value)
 	const char *num_end;
 	int64_t num;
 
-	if (git__strtol64(&num, value, &num_end, 0) < 0)
+	if (!value || git__strtol64(&num, value, &num_end, 0) < 0)
 		goto fail_parse;
 
 	switch (*num_end) {
@@ -1167,7 +1212,6 @@ struct rename_data {
 	git_config *config;
 	git_buf *name;
 	size_t old_len;
-	int actual_error;
 };
 
 static int rename_config_entries_cb(
@@ -1190,8 +1234,6 @@ static int rename_config_entries_cb(
 	if (!error)
 		error = git_config_delete_entry(data->config, entry->name);
 
-	data->actual_error = error; /* preserve actual error code */
-
 	return error;
 }
 
@@ -1216,7 +1258,6 @@ int git_config_rename_section(
 	data.config  = config;
 	data.name    = &replace;
 	data.old_len = strlen(old_section_name) + 1;
-	data.actual_error = 0;
 
 	if ((error = git_buf_join(&replace, '.', new_section_name, "")) < 0)
 		goto cleanup;
@@ -1233,9 +1274,6 @@ int git_config_rename_section(
 	error = git_config_foreach_match(
 		config, git_buf_cstr(&pattern), rename_config_entries_cb, &data);
 
-	if (error == GIT_EUSER)
-		error = data.actual_error;
-
 cleanup:
 	git_buf_free(&pattern);
 	git_buf_free(&replace);
diff --git a/src/config.h b/src/config.h
index 01e8465..3cd888c 100644
--- a/src/config.h
+++ b/src/config.h
@@ -51,5 +51,26 @@ extern int git_config_file__ondisk(git_config_backend **out, const char *path);
 
 extern int git_config__normalize_name(const char *in, char **out);
 
+/* internal only: does not normalize key and sets out to NULL if not found */
+extern int git_config__lookup_entry(
+	const git_config_entry **out,
+	const git_config *cfg,
+	const char *key,
+	bool no_errors);
+
+/*
+ * Lookup functions that cannot fail.  These functions look up a config
+ * value and return a fallback value if the value is missing or if any
+ * failures occur while trying to access the value.
+ */
+
+extern const char *git_config__get_string_force(
+	const git_config *cfg, const char *key, const char *fallback_value);
+
+extern int git_config__get_bool_force(
+	const git_config *cfg, const char *key, int fallback_value);
+
+extern int git_config__get_int_force(
+	const git_config *cfg, const char *key, int fallback_value);
 
 #endif
diff --git a/src/config_cache.c b/src/config_cache.c
index 6808521..ec75d15 100644
--- a/src/config_cache.c
+++ b/src/config_cache.c
@@ -78,22 +78,22 @@ int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar)
 		struct map_data *data = &_cvar_maps[(int)cvar];
 		git_config *config;
 		int error;
+		const git_config_entry *entry;
 
-		error = git_repository_config__weakptr(&config, repo);
-		if (error < 0)
+		if ((error = git_repository_config__weakptr(&config, repo)) < 0)
 			return error;
 
-		if (data->maps)
-			error = git_config_get_mapped(
-				out, config, data->cvar_name, data->maps, data->map_count);
-		else
-			error = git_config_get_bool(out, config, data->cvar_name);
+		git_config__lookup_entry(&entry, config, data->cvar_name, false);
 
-		if (error == GIT_ENOTFOUND) {
-			giterr_clear();
+		if (!entry)
 			*out = data->default_value;
-		}
-		else if (error < 0)
+		else if (data->maps)
+			error = git_config_lookup_map_value(
+				out, data->maps, data->map_count, entry->value);
+		else
+			error = git_config_parse_bool(out, entry->value);
+
+		if (error < 0)
 			return error;
 
 		repo->cvar_cache[(int)cvar] = *out;
diff --git a/src/config_file.c b/src/config_file.c
index 15c8de4..0971aa7 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -404,20 +404,12 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
 /*
  * Internal function that actually gets the value in string form
  */
-static int config_get(const git_config_backend *cfg, const char *name, const git_config_entry **out)
+static int config_get(const git_config_backend *cfg, const char *key, const git_config_entry **out)
 {
 	diskfile_backend *b = (diskfile_backend *)cfg;
-	char *key;
-	khiter_t pos;
-	int error;
+	khiter_t pos = git_strmap_lookup_index(b->values, key);
 	cvar_t *var;
 
-	if ((error = git_config__normalize_name(name, &key)) < 0)
-		return error;
-
-	pos = git_strmap_lookup_index(b->values, key);
-	git__free(key);
-
 	/* no error message; the config system will write one */
 	if (!git_strmap_valid_index(b->values, pos))
 		return GIT_ENOTFOUND;
@@ -427,7 +419,6 @@ static int config_get(const git_config_backend *cfg, const char *name, const git
 		var = var->next;
 
 	*out = var->entry;
-
 	return 0;
 }
 
diff --git a/src/diff.c b/src/diff.c
index 4c33a02..7f2e58c 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -49,16 +49,29 @@ static git_diff_delta *diff_delta__alloc(
 	return delta;
 }
 
-static int diff_notify(
-	const git_diff *diff,
-	const git_diff_delta *delta,
-	const char *matched_pathspec)
+static int diff_insert_delta(
+	git_diff *diff, git_diff_delta *delta, const char *matched_pathspec)
 {
-	if (!diff->opts.notify_cb)
-		return 0;
+	int error = 0;
+
+	if (diff->opts.notify_cb) {
+		error = diff->opts.notify_cb(
+			diff, delta, matched_pathspec, diff->opts.notify_payload);
+
+		if (error) {
+			git__free(delta);
+
+			if (error > 0)	/* positive value means to skip this delta */
+				return 0;
+			else			/* negative value means to cancel diff */
+				return giterr_set_after_callback_function(error, "git_diff");
+		}
+	}
+
+	if ((error = git_vector_insert(&diff->deltas, delta)) < 0)
+		git__free(delta);
 
-	return diff->opts.notify_cb(
-		diff, delta, matched_pathspec, diff->opts.notify_payload);
+	return error;
 }
 
 static int diff_delta__from_one(
@@ -68,7 +81,6 @@ static int diff_delta__from_one(
 {
 	git_diff_delta *delta;
 	const char *matched_pathspec;
-	int notify_res;
 
 	if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
 		return 0;
@@ -111,21 +123,12 @@ static int diff_delta__from_one(
 		!git_oid_iszero(&delta->new_file.oid))
 		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
-	notify_res = diff_notify(diff, delta, matched_pathspec);
-
-	if (notify_res)
-		git__free(delta);
-	else if (git_vector_insert(&diff->deltas, delta) < 0) {
-		git__free(delta);
-		return -1;
-	}
-
-	return notify_res < 0 ? GIT_EUSER : 0;
+	return diff_insert_delta(diff, delta, matched_pathspec);
 }
 
 static int diff_delta__from_two(
 	git_diff *diff,
-	git_delta_t   status,
+	git_delta_t status,
 	const git_index_entry *old_entry,
 	uint32_t old_mode,
 	const git_index_entry *new_entry,
@@ -134,7 +137,6 @@ static int diff_delta__from_two(
 	const char *matched_pathspec)
 {
 	git_diff_delta *delta;
-	int notify_res;
 	const char *canonical_path = old_entry->path;
 
 	if (status == GIT_DELTA_UNMODIFIED &&
@@ -173,16 +175,7 @@ static int diff_delta__from_two(
 	if (new_oid || !git_oid_iszero(&new_entry->oid))
 		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
-	notify_res = diff_notify(diff, delta, matched_pathspec);
-
-	if (notify_res)
-		git__free(delta);
-	else if (git_vector_insert(&diff->deltas, delta) < 0) {
-		git__free(delta);
-		return -1;
-	}
-
-	return notify_res < 0 ? GIT_EUSER : 0;
+	return diff_insert_delta(diff, delta, matched_pathspec);
 }
 
 static git_diff_delta *diff_delta__last_for_item(
@@ -304,26 +297,6 @@ bool git_diff_delta__should_skip(
 }
 
 
-static int config_bool(git_config *cfg, const char *name, int defvalue)
-{
-	int val = defvalue;
-
-	if (git_config_get_bool(&val, cfg, name) < 0)
-		giterr_clear();
-
-	return val;
-}
-
-static int config_int(git_config *cfg, const char *name, int defvalue)
-{
-	int val = defvalue;
-
-	if (git_config_get_int32(&val, cfg, name) < 0)
-		giterr_clear();
-
-	return val;
-}
-
 static const char *diff_mnemonic_prefix(
 	git_iterator_type_t type, bool left_side)
 {
@@ -422,8 +395,8 @@ static int diff_list_apply_options(
 		diff->opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
 
 	/* load config values that affect diff behavior */
-	if (git_repository_config__weakptr(&cfg, repo) < 0)
-		return -1;
+	if ((val = git_repository_config__weakptr(&cfg, repo)) < 0)
+		return val;
 
 	if (!git_repository__cvar(&val, repo, GIT_CVAR_SYMLINKS) && val)
 		diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_HAS_SYMLINKS;
@@ -445,7 +418,7 @@ static int diff_list_apply_options(
 
 	/* If not given explicit `opts`, check `diff.xyz` configs */
 	if (!opts) {
-		int context = config_int(cfg, "diff.context", 3);
+		int context = git_config__get_int_force(cfg, "diff.context", 3);
 		diff->opts.context_lines = context >= 0 ? (uint16_t)context : 3;
 
 		/* add other defaults here */
@@ -460,12 +433,11 @@ static int diff_list_apply_options(
 
 	/* if ignore_submodules not explicitly set, check diff config */
 	if (diff->opts.ignore_submodules <= 0) {
-		const char *str;
+		const git_config_entry *entry;
+		git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
 
-		if (git_config_get_string(&str , cfg, "diff.ignoreSubmodules") < 0)
-			giterr_clear();
-		else if (str != NULL &&
-			git_submodule_parse_ignore(&diff->opts.ignore_submodules, str) < 0)
+		if (entry && git_submodule_parse_ignore(
+				&diff->opts.ignore_submodules, entry->value) < 0)
 			giterr_clear();
 	}
 
@@ -474,9 +446,9 @@ static int diff_list_apply_options(
 		const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
 		const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
 
-		if (config_bool(cfg, "diff.noprefix", 0)) {
+		if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
 			use_old = use_new = "";
-		} else if (config_bool(cfg, "diff.mnemonicprefix", 0)) {
+		else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
 			use_old = diff_mnemonic_prefix(diff->old_src, true);
 			use_new = diff_mnemonic_prefix(diff->new_src, false);
 		}
@@ -504,14 +476,7 @@ static int diff_list_apply_options(
 
 static void diff_list_free(git_diff *diff)
 {
-	git_diff_delta *delta;
-	unsigned int i;
-
-	git_vector_foreach(&diff->deltas, i, delta) {
-		git__free(delta);
-		diff->deltas.contents[i] = NULL;
-	}
-	git_vector_free(&diff->deltas);
+	git_vector_free_deep(&diff->deltas);
 
 	git_pathspec__vfree(&diff->pathspec);
 	git_pool_clear(&diff->pool);
@@ -682,6 +647,7 @@ static int maybe_modified(
 	unsigned int nmode = nitem->mode;
 	bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
 	const char *matched_pathspec;
+	int error = 0;
 
 	if (!git_pathspec__match(
 			&diff->pathspec, oitem->path,
@@ -716,10 +682,9 @@ static int maybe_modified(
 		if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE))
 			status = GIT_DELTA_TYPECHANGE;
 		else {
-			if (diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem) < 0 ||
-				diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem) < 0)
-				return -1;
-			return 0;
+			if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem)))
+				error = diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem);
+			return error;
 		}
 	}
 
@@ -741,8 +706,8 @@ static int maybe_modified(
 		/* TODO: add check against index file st_mtime to avoid racy-git */
 
 		if (S_ISGITLINK(nmode)) {
-			if (maybe_modified_submodule(&status, &noid, diff, info) < 0)
-				return -1;
+			if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
+				return error;
 		}
 
 		/* if the stat data looks different, then mark modified - this just
@@ -769,9 +734,9 @@ static int maybe_modified(
 	 */
 	if (status == GIT_DELTA_MODIFIED && git_oid_iszero(&nitem->oid)) {
 		if (git_oid_iszero(&noid)) {
-			if (git_diff__oid_for_file(diff->repo,
-					nitem->path, nitem->mode, nitem->file_size, &noid) < 0)
-				return -1;
+			if ((error = git_diff__oid_for_file(diff->repo,
+					nitem->path, nitem->mode, nitem->file_size, &noid)) < 0)
+				return error;
 		}
 
 		/* if oid matches, then mark unmodified (except submodules, where
@@ -926,7 +891,7 @@ static int handle_unmatched_new_item(
 			git_diff_delta *last;
 
 			/* attempt to insert record for this directory */
-			if ((error = diff_delta__from_one(diff, delta_type, nitem)) < 0)
+			if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
 				return error;
 
 			/* if delta wasn't created (because of rules), just skip ahead */
@@ -1005,7 +970,7 @@ static int handle_unmatched_new_item(
 	}
 
 	/* Actually create the record for this item if necessary */
-	if ((error = diff_delta__from_one(diff, delta_type, nitem)) < 0)
+	if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
 		return error;
 
 	/* If user requested TYPECHANGE records, then check for that instead of
@@ -1030,7 +995,7 @@ static int handle_unmatched_old_item(
 	git_diff *diff, diff_in_progress *info)
 {
 	int error = diff_delta__from_one(diff, GIT_DELTA_DELETED, info->oitem);
-	if (error < 0)
+	if (error != 0)
 		return error;
 
 	/* if we are generating TYPECHANGE records then check for that
@@ -1364,7 +1329,7 @@ int git_diff__paired_foreach(
 	int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
 	void *payload)
 {
-	int cmp;
+	int cmp, error = 0;
 	git_diff_delta *h2i, *i2w;
 	size_t i, j, i_max, j_max;
 	int (*strcomp)(const char *, const char *) = git__strcmp;
@@ -1420,18 +1385,17 @@ int git_diff__paired_foreach(
 			strcomp(h2i->new_file.path, i2w->old_file.path);
 
 		if (cmp < 0) {
-			if (cb(h2i, NULL, payload))
-				return GIT_EUSER;
-			i++;
+			i++; i2w = NULL;
 		} else if (cmp > 0) {
-			if (cb(NULL, i2w, payload))
-				return GIT_EUSER;
-			j++;
+			j++; h2i = NULL;
 		} else {
-			if (cb(h2i, i2w, payload))
-				return GIT_EUSER;
 			i++; j++;
 		}
+
+		if ((error = cb(h2i, i2w, payload)) != 0) {
+			giterr_set_after_callback(error);
+			break;
+		}
 	}
 
 	/* restore case-insensitive delta sort */
@@ -1447,5 +1411,5 @@ int git_diff__paired_foreach(
 		git_vector_sort(&idx2wd->deltas);
 	}
 
-	return 0;
+	return error;
 }
diff --git a/src/diff_driver.c b/src/diff_driver.c
index bd5a8fb..167c0cc 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -14,6 +14,7 @@
 #include "strmap.h"
 #include "map.h"
 #include "buf_text.h"
+#include "config.h"
 #include "repository.h"
 
 GIT__USE_STRMAP;
@@ -130,14 +131,14 @@ static git_diff_driver_registry *git_repository_driver_registry(
 static int git_diff_driver_load(
 	git_diff_driver **out, git_repository *repo, const char *driver_name)
 {
-	int error = 0, bval;
+	int error = 0;
 	git_diff_driver_registry *reg;
 	git_diff_driver *drv;
 	size_t namelen = strlen(driver_name);
 	khiter_t pos;
 	git_config *cfg;
 	git_buf name = GIT_BUF_INIT;
-	const char *val;
+	const git_config_entry *ce;
 	bool found_driver = false;
 
 	reg = git_repository_driver_registry(repo);
@@ -164,23 +165,21 @@ static int git_diff_driver_load(
 
 	if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
 		goto done;
-	if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
-		if (error != GIT_ENOTFOUND)
-			goto done;
-		/* diff.<driver>.binary unspecified, so just continue */
-		giterr_clear();
-	} else if (git_config_parse_bool(&bval, val) < 0) {
-		/* TODO: warn that diff.<driver>.binary has invalid value */
-		giterr_clear();
-	} else if (bval) {
+
+	switch (git_config__get_bool_force(cfg, name.ptr, -1)) {
+	case true:
 		/* if diff.<driver>.binary is true, just return the binary driver */
 		*out = &global_drivers[DIFF_DRIVER_BINARY];
 		goto done;
-	} else {
+	case false:
 		/* if diff.<driver>.binary is false, force binary checks off */
 		/* but still may have custom function context patterns, etc. */
 		drv->binary_flags = GIT_DIFF_FORCE_TEXT;
 		found_driver = true;
+		break;
+	default:
+		/* diff.<driver>.binary unspecified, so just continue */
+		break;
 	}
 
 	/* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
@@ -211,16 +210,16 @@ static int git_diff_driver_load(
 
 	git_buf_truncate(&name, namelen + strlen("diff.."));
 	git_buf_put(&name, "wordregex", strlen("wordregex"));
-	if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
-		if (error != GIT_ENOTFOUND)
-			goto done;
-		giterr_clear(); /* no diff.<driver>.wordregex, so just continue */
-	} else if ((error = regcomp(&drv->word_pattern, val, REG_EXTENDED)) != 0) {
-		/* TODO: warning about bad regex instead of failure */
-		error = giterr_set_regex(&drv->word_pattern, error);
+	if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0)
 		goto done;
-	} else {
+	if (!ce || !ce->value)
+		/* no diff.<driver>.wordregex, so just continue */;
+	else if (!(error = regcomp(&drv->word_pattern, ce->value, REG_EXTENDED)))
 		found_driver = true;
+	else {
+		/* TODO: warn about bad regex instead of failure */
+		error = giterr_set_regex(&drv->word_pattern, error);
+		goto done;
 	}
 
 	/* TODO: look up diff.<driver>.algorithm to turn on minimal / patience
diff --git a/src/diff_patch.c b/src/diff_patch.c
index cc49d68..9c2eb88 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -193,21 +193,18 @@ cleanup:
 	return error;
 }
 
-static int diff_patch_file_callback(
+static int diff_patch_invoke_file_callback(
 	git_patch *patch, git_diff_output *output)
 {
-	float progress;
+	float progress = patch->diff ?
+		((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
 
 	if (!output->file_cb)
 		return 0;
 
-	progress = patch->diff ?
-		((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
-
-	if (output->file_cb(patch->delta, progress, output->payload) != 0)
-		output->error = GIT_EUSER;
-
-	return output->error;
+	return giterr_set_after_callback_function(
+		output->file_cb(patch->delta, progress, output->payload),
+		"git_patch");
 }
 
 static int diff_patch_generate(git_patch *patch, git_diff_output *output)
@@ -229,7 +226,7 @@ static int diff_patch_generate(git_patch *patch, git_diff_output *output)
 		return 0;
 
 	if (output->diff_cb != NULL &&
-		!(error = output->diff_cb(output, patch)))
+		(error = output->diff_cb(output, patch)) < 0)
 		patch->flags |= GIT_DIFF_PATCH_DIFFED;
 
 	return error;
@@ -272,9 +269,10 @@ int git_diff_foreach(
 	size_t idx;
 	git_patch patch;
 
-	if (diff_required(diff, "git_diff_foreach") < 0)
-		return -1;
+	if ((error = diff_required(diff, "git_diff_foreach")) < 0)
+		return error;
 
+	memset(&xo, 0, sizeof(xo));
 	diff_output_init(
 		&xo.output, &diff->opts, file_cb, hunk_cb, data_cb, payload);
 	git_xdiff_init(&xo, &diff->opts);
@@ -285,22 +283,18 @@ int git_diff_foreach(
 		if (git_diff_delta__should_skip(&diff->opts, patch.delta))
 			continue;
 
-		if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) {
+		if ((error = diff_patch_init_from_diff(&patch, diff, idx)) < 0)
+			break;
 
-			error = diff_patch_file_callback(&patch, &xo.output);
+		if (!(error = diff_patch_invoke_file_callback(&patch, &xo.output)))
+			error = diff_patch_generate(&patch, &xo.output);
 
-			if (!error)
-				error = diff_patch_generate(&patch, &xo.output);
+		git_patch_free(&patch);
 
-			git_patch_free(&patch);
-		}
-
-		if (error < 0)
+		if (error)
 			break;
 	}
 
-	if (error == GIT_EUSER)
-		giterr_clear(); /* don't leave error message set invalidly */
 	return error;
 }
 
@@ -332,14 +326,11 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
 		!(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
 		return error;
 
-	error = diff_patch_file_callback(patch, (git_diff_output *)xo);
+	error = diff_patch_invoke_file_callback(patch, (git_diff_output *)xo);
 
 	if (!error)
 		error = diff_patch_generate(patch, (git_diff_output *)xo);
 
-	if (error == GIT_EUSER)
-		giterr_clear(); /* don't leave error message set invalidly */
-
 	return error;
 }
 
@@ -424,9 +415,7 @@ int git_diff_blobs(
 	diff_patch_with_delta pd;
 	git_xdiff_output xo;
 
-	memset(&pd, 0, sizeof(pd));
 	memset(&xo, 0, sizeof(xo));
-
 	diff_output_init(
 		&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
 	git_xdiff_init(&xo, opts);
@@ -436,6 +425,7 @@ int git_diff_blobs(
 	else if (!new_path && old_path)
 		new_path = old_path;
 
+	memset(&pd, 0, sizeof(pd));
 	error = diff_patch_from_blobs(
 		&pd, &xo, old_blob, old_path, new_blob, new_path, opts);
 
@@ -463,7 +453,6 @@ int git_patch_from_blobs(
 		return -1;
 
 	memset(&xo, 0, sizeof(xo));
-
 	diff_output_to_patch(&xo.output, &pd->patch);
 	git_xdiff_init(&xo, opts);
 
@@ -536,9 +525,7 @@ int git_diff_blob_to_buffer(
 	diff_patch_with_delta pd;
 	git_xdiff_output xo;
 
-	memset(&pd, 0, sizeof(pd));
 	memset(&xo, 0, sizeof(xo));
-
 	diff_output_init(
 		&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
 	git_xdiff_init(&xo, opts);
@@ -548,6 +535,7 @@ int git_diff_blob_to_buffer(
 	else if (!buf_path && old_path)
 		buf_path = old_path;
 
+	memset(&pd, 0, sizeof(pd));
 	error = diff_patch_from_blob_and_buffer(
 		&pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
 
@@ -576,7 +564,6 @@ int git_patch_from_blob_and_buffer(
 		return -1;
 
 	memset(&xo, 0, sizeof(xo));
-
 	diff_output_to_patch(&xo.output, &pd->patch);
 	git_xdiff_init(&xo, opts);
 
@@ -622,17 +609,18 @@ int git_patch_from_diff(
 	if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
 		return error;
 
+	memset(&xo, 0, sizeof(xo));
 	diff_output_to_patch(&xo.output, patch);
 	git_xdiff_init(&xo, &diff->opts);
 
-	error = diff_patch_file_callback(patch, &xo.output);
+	error = diff_patch_invoke_file_callback(patch, &xo.output);
 
 	if (!error)
 		error = diff_patch_generate(patch, &xo.output);
 
 	if (!error) {
-		/* if cumulative diff size is < 0.5 total size, flatten the patch */
-		/* unload the file content */
+		/* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
+		/* TODO: and unload the file content */
 	}
 
 	if (error || !patch_ptr)
@@ -640,8 +628,6 @@ int git_patch_from_diff(
 	else
 		*patch_ptr = patch;
 
-	if (error == GIT_EUSER)
-		giterr_clear(); /* don't leave error message set invalidly */
 	return error;
 }
 
@@ -905,7 +891,7 @@ static int diff_patch_line_cb(
 	GIT_UNUSED(hunk_);
 
 	hunk = git_array_last(patch->hunks);
-	GITERR_CHECK_ALLOC(hunk);
+	assert(hunk); /* programmer error if no hunk is available */
 
 	line = git_array_alloc(patch->lines);
 	GITERR_CHECK_ALLOC(line);
diff --git a/src/diff_print.c b/src/diff_print.c
index b04b115..7a70e2b 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -89,12 +89,6 @@ char git_diff_status_char(git_delta_t status)
 	return code;
 }
 
-static int callback_error(void)
-{
-	giterr_clear();
-	return GIT_EUSER;
-}
-
 static int diff_print_one_name_only(
 	const git_diff_delta *delta, float progress, void *data)
 {
@@ -108,19 +102,16 @@ static int diff_print_one_name_only(
 		return 0;
 
 	git_buf_clear(out);
-
-	if (git_buf_puts(out, delta->new_file.path) < 0 ||
-		git_buf_putc(out, '\n'))
+	git_buf_puts(out, delta->new_file.path);
+	git_buf_putc(out, '\n');
+	if (git_buf_oom(out))
 		return -1;
 
 	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
 	pi->line.content     = git_buf_cstr(out);
 	pi->line.content_len = git_buf_len(out);
 
-	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(delta, NULL, &pi->line, pi->payload);
 }
 
 static int diff_print_one_name_status(
@@ -154,7 +145,6 @@ static int diff_print_one_name_status(
 		git_buf_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix);
 	else
 		git_buf_printf(out, "%c\t%s\n", code, delta->old_file.path);
-
 	if (git_buf_oom(out))
 		return -1;
 
@@ -162,10 +152,7 @@ static int diff_print_one_name_status(
 	pi->line.content     = git_buf_cstr(out);
 	pi->line.content_len = git_buf_len(out);
 
-	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(delta, NULL, &pi->line, pi->payload);
 }
 
 static int diff_print_one_raw(
@@ -208,10 +195,7 @@ static int diff_print_one_raw(
 	pi->line.content     = git_buf_cstr(out);
 	pi->line.content_len = git_buf_len(out);
 
-	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(delta, NULL, &pi->line, pi->payload);
 }
 
 static int diff_print_oid_range(
@@ -238,10 +222,7 @@ static int diff_print_oid_range(
 		git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
 	}
 
-	if (git_buf_oom(out))
-		return -1;
-
-	return 0;
+	return git_buf_oom(out) ? -1 : 0;
 }
 
 static int diff_delta_format_with_paths(
@@ -285,8 +266,7 @@ int git_diff_delta__format_file_header(
 	git_buf_printf(out, "diff --git %s%s %s%s\n",
 		oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
 
-	if (diff_print_oid_range(out, delta, oid_strlen) < 0)
-		return -1;
+	GITERR_CHECK_ERROR(diff_print_oid_range(out, delta, oid_strlen));
 
 	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
 		diff_delta_format_with_paths(
@@ -298,6 +278,7 @@ int git_diff_delta__format_file_header(
 static int diff_print_patch_file(
 	const git_diff_delta *delta, float progress, void *data)
 {
+	int error;
 	diff_print_info *pi = data;
 	const char *oldpfx =
 		pi->diff ? pi->diff->opts.old_prefix : DIFF_OLD_PREFIX_DEFAULT;
@@ -313,36 +294,33 @@ static int diff_print_patch_file(
 		 (pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0))
 		return 0;
 
-	if (git_diff_delta__format_file_header(
-			pi->buf, delta, oldpfx, newpfx, pi->oid_strlen) < 0)
-		return -1;
+	if ((error = git_diff_delta__format_file_header(
+			pi->buf, delta, oldpfx, newpfx, pi->oid_strlen)) < 0)
+		return error;
 
 	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
 	pi->line.content     = git_buf_cstr(pi->buf);
 	pi->line.content_len = git_buf_len(pi->buf);
 
-	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
-		return callback_error();
+	if ((error = pi->print_cb(delta, NULL, &pi->line, pi->payload)) != 0)
+		return error;
 
 	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
 		return 0;
 
 	git_buf_clear(pi->buf);
 
-	if (diff_delta_format_with_paths(
+	if ((error = diff_delta_format_with_paths(
 			pi->buf, delta, oldpfx, newpfx,
-			"Binary files %s%s and %s%s differ\n") < 0)
-		return -1;
+			"Binary files %s%s and %s%s differ\n")) < 0)
+		return error;
 
 	pi->line.origin      = GIT_DIFF_LINE_BINARY;
 	pi->line.content     = git_buf_cstr(pi->buf);
 	pi->line.content_len = git_buf_len(pi->buf);
 	pi->line.num_lines   = 1;
 
-	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(delta, NULL, &pi->line, pi->payload);
 }
 
 static int diff_print_patch_hunk(
@@ -359,10 +337,7 @@ static int diff_print_patch_hunk(
 	pi->line.content     = h->header;
 	pi->line.content_len = h->header_len;
 
-	if (pi->print_cb(d, h, &pi->line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(d, h, &pi->line, pi->payload);
 }
 
 static int diff_print_patch_line(
@@ -376,10 +351,7 @@ static int diff_print_patch_line(
 	if (S_ISDIR(delta->new_file.mode))
 		return 0;
 
-	if (pi->print_cb(delta, hunk, line, pi->payload))
-		return callback_error();
-
-	return 0;
+	return pi->print_cb(delta, hunk, line, pi->payload);
 }
 
 /* print a git_diff to an output callback */
@@ -421,9 +393,14 @@ int git_diff_print(
 
 	if (!(error = diff_print_info_init(
 			&pi, &buf, diff, format, print_cb, payload)))
+	{
 		error = git_diff_foreach(
 			diff, print_file, print_hunk, print_line, &pi);
 
+		if (error) /* make sure error message is set */
+			giterr_set_after_callback_function(error, "git_diff_print");
+	}
+
 	git_buf_free(&buf);
 
 	return error;
@@ -444,10 +421,15 @@ int git_patch_print(
 	if (!(error = diff_print_info_init(
 			&pi, &temp, git_patch__diff(patch),
 			GIT_DIFF_FORMAT_PATCH, print_cb, payload)))
+	{
 		error = git_patch__invoke_callbacks(
 			patch, diff_print_patch_file, diff_print_patch_hunk,
 			diff_print_patch_line, &pi);
 
+		if (error) /* make sure error message is set */
+			giterr_set_after_callback_function(error, "git_patch_print");
+	}
+
 	git_buf_free(&temp);
 
 	return error;
@@ -478,15 +460,12 @@ int git_patch_to_str(
 	int error;
 	git_buf output = GIT_BUF_INIT;
 
-	error = git_patch_print(patch, diff_print_to_buffer_cb, &output);
-
-	/* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
-	 * meaning a memory allocation failure, so just map to -1...
-	 */
-	if (error == GIT_EUSER)
-		error = -1;
-
-	*string = git_buf_detach(&output);
+	if (!(error = git_patch_print(patch, diff_print_to_buffer_cb, &output)))
+		*string = git_buf_detach(&output);
+	else {
+		git_buf_free(&output);
+		*string = NULL;
+	}
 
 	return error;
 }
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 702e43b..263a64d 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -13,6 +13,7 @@
 #include "hashsig.h"
 #include "path.h"
 #include "fileops.h"
+#include "config.h"
 
 static git_diff_delta *diff_delta__dup(
 	const git_diff_delta *d, git_pool *pool)
@@ -208,9 +209,7 @@ int git_diff_merge(git_diff *onto, const git_diff *from)
 			git_pool_strdup_safe(&onto->pool, onto->opts.new_prefix);
 	}
 
-	git_vector_foreach(&onto_new, i, delta)
-		git__free(delta);
-	git_vector_free(&onto_new);
+	git_vector_free_deep(&onto_new);
 	git_pool_clear(&onto_pool);
 
 	return error;
@@ -281,28 +280,22 @@ static int normalize_find_opts(
 		git_repository_config__weakptr(&cfg, diff->repo) < 0)
 		return -1;
 
-	if (given) {
+	if (given)
 		memcpy(opts, given, sizeof(*opts));
-	} else {
-		GIT_INIT_STRUCTURE(opts, GIT_DIFF_FIND_OPTIONS_VERSION);
-	}
 
 	if (!given ||
 		 (given->flags & GIT_DIFF_FIND_ALL) == GIT_DIFF_FIND_BY_CONFIG)
 	{
-		const char *val = NULL;
-
-		if (git_config_get_string(&val, cfg, "diff.renames") < 0)
-			giterr_clear();
-		else if (val) {
-			int boolval;
-			if (!git__parse_bool(&boolval, val) && !boolval) {
-				/* do nothing */
-			} else if (!strcasecmp(val, "copies") || !strcasecmp(val, "copy"))
-				opts->flags |= (GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES);
-			else
-				opts->flags |= GIT_DIFF_FIND_RENAMES;
-		}
+		const char *rule =
+			git_config__get_string_force(cfg, "diff.renames", "true");
+		int boolval;
+
+		if (!git__parse_bool(&boolval, rule) && !boolval)
+			/* don't set FIND_RENAMES if bool value is false */;
+		else if (!strcasecmp(rule, "copies") || !strcasecmp(rule, "copy"))
+			opts->flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
+		else
+			opts->flags |= GIT_DIFF_FIND_RENAMES;
 	}
 
 	/* some flags imply others */
@@ -343,14 +336,11 @@ static int normalize_find_opts(
 #undef USE_DEFAULT
 
 	if (!opts->rename_limit) {
-		int32_t limit = 0;
-
-		opts->rename_limit = DEFAULT_RENAME_LIMIT;
+		opts->rename_limit = git_config__get_int_force(
+			cfg, "diff.renamelimit", DEFAULT_RENAME_LIMIT);
 
-		if (git_config_get_int32(&limit, cfg, "diff.renameLimit") < 0)
-			giterr_clear();
-		else if (limit > 0)
-			opts->rename_limit = limit;
+		if (opts->rename_limit <= 0)
+			opts->rename_limit = DEFAULT_RENAME_LIMIT;
 	}
 
 	/* assign the internal metric with whitespace flag as payload */
@@ -450,9 +440,7 @@ static int apply_splits_and_deletes(
 	return 0;
 
 on_error:
-	git_vector_foreach(&onto, i, delta)
-		git__free(delta);
-	git_vector_free(&onto);
+	git_vector_free_deep(&onto);
 
 	return -1;
 }
@@ -824,11 +812,11 @@ int git_diff_find_similar(
 	int error = 0, result;
 	uint16_t similarity;
 	git_diff_delta *src, *tgt;
-	git_diff_find_options opts;
+	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
 	size_t num_deltas, num_srcs = 0, num_tgts = 0;
 	size_t tried_srcs = 0, tried_tgts = 0;
 	size_t num_rewrites = 0, num_updates = 0, num_bumped = 0;
-	void **sigcache; /* cache of similarity metric file signatures */
+	void **sigcache = NULL; /* cache of similarity metric file signatures */
 	diff_find_match *tgt2src = NULL;
 	diff_find_match *src2tgt = NULL;
 	diff_find_match *tgt2src_copy = NULL;
@@ -838,15 +826,15 @@ int git_diff_find_similar(
 	if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
 		return error;
 
-	/* No flags set; nothing to do */
-	if ((opts.flags & GIT_DIFF_FIND_ALL) == 0)
-		return 0;
-
 	num_deltas = diff->deltas.length;
 
 	/* TODO: maybe abort if deltas.length > rename_limit ??? */
 	if (!git__is_uint32(num_deltas))
-		return 0;
+		goto cleanup;
+
+	/* No flags set; nothing to do */
+	if ((opts.flags & GIT_DIFF_FIND_ALL) == 0)
+		goto cleanup;
 
 	sigcache = git__calloc(num_deltas * 2, sizeof(void *));
 	GITERR_CHECK_ALLOC(sigcache);
@@ -1121,11 +1109,13 @@ cleanup:
 	git__free(src2tgt);
 	git__free(tgt2src_copy);
 
-	for (t = 0; t < num_deltas * 2; ++t) {
-		if (sigcache[t] != NULL)
-			opts.metric->free_signature(sigcache[t], opts.metric->payload);
+	if (sigcache) {
+		for (t = 0; t < num_deltas * 2; ++t) {
+			if (sigcache[t] != NULL)
+				opts.metric->free_signature(sigcache[t], opts.metric->payload);
+		}
+		git__free(sigcache);
 	}
-	git__free(sigcache);
 
 	if (!given_opts || !given_opts->metric)
 		git__free(opts.metric);
diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c
index e0bc11f..e5984f1 100644
--- a/src/diff_xdiff.c
+++ b/src/diff_xdiff.c
@@ -28,25 +28,29 @@ static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header)
 {
 	/* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
 	if (*header != '@')
-		return -1;
+		goto fail;
 	if (git_xdiff_scan_int(&header, &hunk->old_start) < 0)
-		return -1;
+		goto fail;
 	if (*header == ',') {
 		if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0)
-			return -1;
+			goto fail;
 	} else
 		hunk->old_lines = 1;
 	if (git_xdiff_scan_int(&header, &hunk->new_start) < 0)
-		return -1;
+		goto fail;
 	if (*header == ',') {
 		if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0)
-			return -1;
+			goto fail;
 	} else
 		hunk->new_lines = 1;
 	if (hunk->old_start < 0 || hunk->new_start < 0)
-		return -1;
+		goto fail;
 
 	return 0;
+
+fail:
+	giterr_set(GITERR_INVALID, "Malformed hunk header from xdiff");
+	return -1;
 }
 
 typedef struct {
@@ -122,8 +126,9 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 		info->hunk.header[info->hunk.header_len] = '\0';
 
 		if (output->hunk_cb != NULL &&
-			output->hunk_cb(delta, &info->hunk, output->payload))
-			output->error = GIT_EUSER;
+			(output->error = output->hunk_cb(
+				delta, &info->hunk, output->payload)))
+			return output->error;
 
 		info->old_lineno = info->hunk.old_start;
 		info->new_lineno = info->hunk.new_start;
@@ -146,10 +151,9 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 		output->error = diff_update_lines(
 			info, &line, bufs[1].ptr, bufs[1].size);
 
-		if (!output->error &&
-			output->data_cb != NULL &&
-			output->data_cb(delta, &info->hunk, &line, output->payload))
-			output->error = GIT_EUSER;
+		if (!output->error && output->data_cb != NULL)
+			output->error = output->data_cb(
+				delta, &info->hunk, &line, output->payload);
 	}
 
 	if (len == 3 && !output->error) {
@@ -168,10 +172,9 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 		output->error = diff_update_lines(
 			info, &line, bufs[2].ptr, bufs[2].size);
 
-		if (!output->error &&
-			output->data_cb != NULL &&
-			output->data_cb(delta, &info->hunk, &line, output->payload))
-			output->error = GIT_EUSER;
+		if (!output->error && output->data_cb != NULL)
+			output->error = output->data_cb(
+				delta, &info->hunk, &line, output->payload);
 	}
 
 	return output->error;
@@ -219,11 +222,9 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
 
 	xo->output.diff_cb = git_xdiff;
 
-	memset(&xo->config, 0, sizeof(xo->config));
 	xo->config.ctxlen = opts ? opts->context_lines : 3;
 	xo->config.interhunkctxlen = opts ? opts->interhunk_lines : 0;
 
-	memset(&xo->params, 0, sizeof(xo->params));
 	if (flags & GIT_DIFF_IGNORE_WHITESPACE)
 		xo->params.flags |= XDF_WHITESPACE_FLAGS;
 	if (flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
@@ -236,6 +237,5 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
 	if (flags & GIT_DIFF_MINIMAL)
 		xo->params.flags |= XDF_NEED_MINIMAL;
 
-	memset(&xo->callback, 0, sizeof(xo->callback));
 	xo->callback.outf = git_xdiff_cb;
 }
diff --git a/src/errors.c b/src/errors.c
index d04da4c..a0b0859 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -23,7 +23,8 @@ static void set_error(int error_class, char *string)
 {
 	git_error *error = &GIT_GLOBAL->error_t;
 
-	git__free(error->message);
+	if (error->message != string)
+		git__free(error->message);
 
 	error->message = string;
 	error->klass = error_class;
@@ -103,8 +104,10 @@ int giterr_set_regex(const regex_t *regex, int error_code)
 
 void giterr_clear(void)
 {
-	set_error(0, NULL);
-	GIT_GLOBAL->last_error = NULL;
+	if (GIT_GLOBAL->last_error != NULL) {
+		set_error(0, NULL);
+		GIT_GLOBAL->last_error = NULL;
+	}
 
 	errno = 0;
 #ifdef GIT_WIN32
@@ -134,3 +137,39 @@ const git_error *giterr_last(void)
 {
 	return GIT_GLOBAL->last_error;
 }
+
+int giterr_capture(git_error_state *state, int error_code)
+{
+	state->error_code = error_code;
+	if (error_code)
+		giterr_detach(&state->error_msg);
+	return error_code;
+}
+
+int giterr_restore(git_error_state *state)
+{
+	if (state && state->error_code && state->error_msg.message)
+		set_error(state->error_msg.klass, state->error_msg.message);
+	else
+		giterr_clear();
+
+	return state ? state->error_code : 0;
+}
+
+int giterr_system_last(void)
+{
+#ifdef GIT_WIN32
+	return GetLastError();
+#else
+	return errno;
+#endif
+}
+
+void giterr_system_set(int code)
+{
+#ifdef GIT_WIN32
+	SetLastError(code);
+#else
+	errno = code;
+#endif
+}
diff --git a/src/fetch.c b/src/fetch.c
index 2765918..5bf2b93 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -104,7 +104,7 @@ cleanup:
 int git_fetch_negotiate(git_remote *remote)
 {
 	git_transport *t = remote->transport;
-	
+
 	if (filter_wants(remote) < 0) {
 		giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
 		return -1;
@@ -128,9 +128,9 @@ int git_fetch_download_pack(git_remote *remote)
 {
 	git_transport *t = remote->transport;
 
-	if(!remote->need_pack)
+	if (!remote->need_pack)
 		return 0;
 
 	return t->download_pack(t, remote->repo, &remote->stats,
-				remote->callbacks.transfer_progress, remote->callbacks.payload);
+			remote->callbacks.transfer_progress, remote->callbacks.payload);
 }
diff --git a/src/fetchhead.c b/src/fetchhead.c
index 67089d1..4435454 100644
--- a/src/fetchhead.c
+++ b/src/fetchhead.c
@@ -260,8 +260,8 @@ int git_repository_fetchhead_foreach(git_repository *repo,
 	while ((line = git__strsep(&buffer, "\n")) != NULL) {
 		++line_num;
 
-		if ((error = fetchhead_ref_parse(&oid, &is_merge, &name, &remote_url,
-			line, line_num)) < 0)
+		if ((error = fetchhead_ref_parse(
+				&oid, &is_merge, &name, &remote_url, line, line_num)) < 0)
 			goto done;
 
 		if (git_buf_len(&name) > 0)
@@ -269,8 +269,9 @@ int git_repository_fetchhead_foreach(git_repository *repo,
 		else
 			ref_name = NULL;
 
-		if ((cb(ref_name, remote_url, &oid, is_merge, payload)) != 0) {
-			error = GIT_EUSER;
+		error = cb(ref_name, remote_url, &oid, is_merge, payload);
+		if (error) {
+			giterr_set_after_callback(error);
 			goto done;
 		}
 	}
diff --git a/src/fileops.c b/src/fileops.c
index 5763b37..a60689f 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -403,7 +403,6 @@ typedef struct {
 	const char *base;
 	size_t baselen;
 	uint32_t flags;
-	int error;
 	int depth;
 } futils__rmdir_data;
 
@@ -447,8 +446,8 @@ static int futils__rm_first_parent(git_buf *path, const char *ceiling)
 
 static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
 {
+	int error = 0;
 	futils__rmdir_data *data = opaque;
-	int error = data->error;
 	struct stat st;
 
 	if (data->depth > FUTILS_MAX_DEPTH)
@@ -474,13 +473,14 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
 		data->depth++;
 
 		error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
-		if (error < 0)
-			return (error == GIT_EUSER) ? data->error : error;
 
 		data->depth--;
 
+		if (error < 0)
+			return error;
+
 		if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
-			return data->error;
+			return error;
 
 		if ((error = p_rmdir(path->ptr)) < 0) {
 			if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
@@ -499,28 +499,23 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
 	else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
 		error = futils__error_cannot_rmdir(path->ptr, "still present");
 
-	data->error = error;
 	return error;
 }
 
 static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
 {
 	futils__rmdir_data *data = opaque;
-	int error;
+	int error = 0;
 
 	if (git_buf_len(path) <= data->baselen)
-		return GIT_ITEROVER;
-
-	error = p_rmdir(git_buf_cstr(path));
+		error = GIT_ITEROVER;
 
-	if (error) {
+	else if (p_rmdir(git_buf_cstr(path)) < 0) {
 		int en = errno;
 
 		if (en == ENOENT || en == ENOTDIR) {
-			giterr_clear();
-			error = 0;
+			/* do nothing */
 		} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
-			giterr_clear();
 			error = GIT_ITEROVER;
 		} else {
 			error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
@@ -535,12 +530,13 @@ int git_futils_rmdir_r(
 {
 	int error;
 	git_buf fullpath = GIT_BUF_INIT;
-	futils__rmdir_data data = { 0 };
+	futils__rmdir_data data;
 
 	/* build path and find "root" where we should start calling mkdir */
 	if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
 		return -1;
 
+	memset(&data, 0, sizeof(data));
 	data.base    = base ? base : "";
 	data.baselen = base ? strlen(base) : 0;
 	data.flags   = flags;
@@ -548,12 +544,13 @@ int git_futils_rmdir_r(
 	error = futils__rmdir_recurs_foreach(&data, &fullpath);
 
 	/* remove now-empty parents if requested */
-	if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
+	if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
 		error = git_path_walk_up(
 			&fullpath, base, futils__rmdir_empty_parent, &data);
 
-		if (error == GIT_ITEROVER)
-			error = 0;
+	if (error == GIT_ITEROVER) {
+		giterr_clear();
+		error = 0;
 	}
 
 	git_buf_free(&fullpath);
@@ -618,6 +615,8 @@ static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = {
 	git_futils_guess_template_dirs,
 };
 
+static int git_futils__dirs_shutdown_set = 0;
+
 void git_futils_dirs_global_shutdown(void)
 {
 	int i;
@@ -634,8 +633,6 @@ int git_futils_dirs_global_init(void)
 	for (i = 0; !error && i < GIT_FUTILS_DIR__MAX; i++)
 		error = git_futils_dirs_get(&path, i);
 
-	git__on_shutdown(git_futils_dirs_global_shutdown);
-
 	return error;
 }
 
@@ -655,9 +652,16 @@ int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which)
 
 	GITERR_CHECK_ERROR(git_futils_check_selector(which));
 
-	if (!git_buf_len(&git_futils__dirs[which]))
+	if (!git_buf_len(&git_futils__dirs[which])) {
+		/* prepare shutdown if we're going to need it */
+		if (!git_futils__dirs_shutdown_set) {
+			git__on_shutdown(git_futils_dirs_global_shutdown);
+			git_futils__dirs_shutdown_set = 1;
+		}
+
 		GITERR_CHECK_ERROR(
 			git_futils__dir_guess[which](&git_futils__dirs[which]));
+	}
 
 	*out = &git_futils__dirs[which];
 	return 0;
@@ -858,7 +862,6 @@ typedef struct {
 	uint32_t flags;
 	uint32_t mkdir_flags;
 	mode_t dirmode;
-	int error;
 } cp_r_info;
 
 #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
@@ -896,23 +899,21 @@ static int _cp_r_callback(void *ref, git_buf *from)
 		from->ptr[git_path_basename_offset(from)] == '.')
 		return 0;
 
-	if (git_buf_joinpath(
-			&info->to, info->to_root, from->ptr + info->from_prefix) < 0) {
-		error = -1;
-		goto exit;
-	}
+	if ((error = git_buf_joinpath(
+			&info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
+		return error;
 
 	if (!(error = git_path_lstat(info->to.ptr, &to_st)))
 		exists = true;
 	else if (error != GIT_ENOTFOUND)
-		goto exit;
+		return error;
 	else {
 		giterr_clear();
 		error = 0;
 	}
 
 	if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
-		goto exit;
+		return error;
 
 	if (S_ISDIR(from_st.st_mode)) {
 		mode_t oldmode = info->dirmode;
@@ -926,17 +927,13 @@ 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))) {
+		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;
 
-		goto exit;
+		return error;
 	}
 
 	if (exists) {
@@ -946,8 +943,7 @@ static int _cp_r_callback(void *ref, git_buf *from)
 		if (p_unlink(info->to.ptr) < 0) {
 			giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
 				info->to.ptr);
-			error = -1;
-			goto exit;
+			return GIT_EEXISTS;
 		}
 	}
 
@@ -960,7 +956,7 @@ static int _cp_r_callback(void *ref, git_buf *from)
 	/* Make container directory on demand if needed */
 	if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
 		(error = _cp_r_mkdir(info, from)) < 0)
-		goto exit;
+		return error;
 
 	/* make symlink or regular file */
 	if (S_ISLNK(from_st.st_mode))
@@ -974,8 +970,6 @@ static int _cp_r_callback(void *ref, git_buf *from)
 		error = git_futils_cp(from->ptr, info->to.ptr, usemode);
 	}
 
-exit:
-	info->error = error;
 	return error;
 }
 
@@ -992,11 +986,11 @@ int git_futils_cp_r(
 	if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
 		return -1;
 
+	memset(&info, 0, sizeof(info));
 	info.to_root = to;
 	info.flags   = flags;
 	info.dirmode = dirmode;
 	info.from_prefix = path.size;
-	info.error = 0;
 	git_buf_init(&info.to, 0);
 
 	/* precalculate mkdir flags */
@@ -1018,9 +1012,6 @@ int git_futils_cp_r(
 	git_buf_free(&path);
 	git_buf_free(&info.to);
 
-	if (error == GIT_EUSER)
-		error = info.error;
-
 	return error;
 }
 
diff --git a/src/filter.c b/src/filter.c
index 9f866fe..ff81eb1 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -69,7 +69,7 @@ static void filter_registry_shutdown(void)
 		return;
 
 	git_vector_foreach(&reg->filters, pos, fdef) {
-		if (fdef->initialized && fdef->filter && fdef->filter->shutdown) {
+		if (fdef->filter && fdef->filter->shutdown) {
 			fdef->filter->shutdown(fdef->filter);
 			fdef->initialized = false;
 		}
diff --git a/src/ignore.c b/src/ignore.c
index 27d7c7e..c79fe48 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -74,10 +74,12 @@ static int parse_ignore_file(
 #define push_ignore_file(R,IGN,S,B,F) \
 	git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,parse_ignore_file,(IGN),(S))
 
-static int push_one_ignore(void *ref, git_buf *path)
+static int push_one_ignore(void *payload, git_buf *path)
 {
-	git_ignores *ign = (git_ignores *)ref;
-	return push_ignore_file(ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
+	git_ignores *ign = payload;
+
+	return push_ignore_file(
+		ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
 }
 
 static int get_internal_ignores(git_attr_file **ign, git_repository *repo)
diff --git a/src/index.c b/src/index.c
index 09e7b23..bb81f66 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2036,11 +2036,12 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
 
 	error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data);
 
-	git_vector_sort(&entries);
-
-	git_index_clear(index);
+	if (!error) {
+		git_vector_sort(&entries);
+		git_index_clear(index);
+		git_vector_swap(&entries, &index->entries);
+	}
 
-	git_vector_swap(&entries, &index->entries);
 	git_vector_free(&entries);
 
 	return error;
@@ -2116,8 +2117,7 @@ int git_index_add_all(
 			if (error > 0) /* return > 0 means skip this one */
 				continue;
 			if (error < 0) { /* return < 0 means abort */
-				giterr_clear();
-				error = GIT_EUSER;
+				giterr_set_after_callback(error);
 				break;
 			}
 		}
@@ -2204,11 +2204,8 @@ static int index_apply_to_all(
 				error = 0;
 				continue;
 			}
-			if (error < 0) { /* return < 0 means abort */
-				giterr_clear();
-				error = GIT_EUSER;
+			if (error < 0)   /* return < 0 means abort */
 				break;
-			}
 		}
 
 		/* index manipulation may alter entry, so don't depend on it */
@@ -2253,8 +2250,13 @@ int git_index_remove_all(
 	git_index_matched_path_cb cb,
 	void *payload)
 {
-	return index_apply_to_all(
+	int error = index_apply_to_all(
 		index, INDEX_ACTION_REMOVE, pathspec, cb, payload);
+
+	if (error) /* make sure error is set if callback stopped iteration */
+		giterr_set_after_callback(error);
+
+	return error;
 }
 
 int git_index_update_all(
@@ -2263,6 +2265,11 @@ int git_index_update_all(
 	git_index_matched_path_cb cb,
 	void *payload)
 {
-	return index_apply_to_all(
+	int error = index_apply_to_all(
 		index, INDEX_ACTION_UPDATE, pathspec, cb, payload);
+
+	if (error) /* make sure error is set if callback stopped iteration */
+		giterr_set_after_callback(error);
+
+	return error;
 }
diff --git a/src/indexer.c b/src/indexer.c
index 852a041..6132571 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -386,12 +386,10 @@ on_error:
 
 static int do_progress_callback(git_indexer *idx, git_transfer_progress *stats)
 {
-	if (idx->progress_cb &&
-		idx->progress_cb(stats, idx->progress_payload)) {
-			giterr_clear();
-			return GIT_EUSER;
-	}
-
+	if (idx->progress_cb)
+		return giterr_set_after_callback_function(
+			idx->progress_cb(stats, idx->progress_payload),
+			"indexer progress");
 	return 0;
 }
 
@@ -443,8 +441,8 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 
 	processed = stats->indexed_objects;
 
-	if (git_filebuf_write(&idx->pack_file, data, size) < 0)
-		return -1;
+	if ((error = git_filebuf_write(&idx->pack_file, data, size)) < 0)
+		return error;
 
 	hash_partially(idx, data, (int)size);
 
@@ -452,12 +450,12 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 	if (idx->opened_pack) {
 		idx->pack->mwf.size += size;
 	} else {
-		if (open_pack(&idx->pack, idx->pack_file.path_lock) < 0)
-			return -1;
+		if ((error = open_pack(&idx->pack, idx->pack_file.path_lock)) < 0)
+			return error;
 		idx->opened_pack = 1;
 		mwf = &idx->pack->mwf;
-		if (git_mwindow_file_register(&idx->pack->mwf) < 0)
-			return -1;
+		if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
+			return error;
 	}
 
 	if (!idx->parsed_header) {
@@ -466,8 +464,8 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 		if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header))
 			return 0;
 
-		if (parse_header(&idx->hdr, idx->pack) < 0)
-			return -1;
+		if ((error = parse_header(&idx->hdr, idx->pack)) < 0)
+			return error;
 
 		idx->parsed_header = 1;
 		idx->nr_objects = ntohl(hdr->hdr_entries);
@@ -497,7 +495,7 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 		processed = stats->indexed_objects = 0;
 		stats->total_objects = total_objects;
 
-		if ((error = do_progress_callback(idx, stats)) < 0)
+		if ((error = do_progress_callback(idx, stats)) != 0)
 			return error;
 	}
 
@@ -505,6 +503,7 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 
 	/* As the file grows any windows we try to use will be out of date */
 	git_mwindow_free_all(mwf);
+
 	while (processed < idx->nr_objects) {
 		git_packfile_stream *stream = &idx->stream;
 		git_off_t entry_start = idx->off;
@@ -522,7 +521,7 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 				return 0;
 			}
 			if (error < 0)
-				return -1;
+				goto on_error;
 
 			git_mwindow_close(&w);
 			idx->entry_start = entry_start;
@@ -535,7 +534,7 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 					return 0;
 				}
 				if (error < 0)
-					return -1;
+					goto on_error;
 
 				idx->have_delta = 1;
 			} else {
@@ -544,9 +543,10 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 			}
 
 			idx->have_stream = 1;
-			if (git_packfile_stream_open(stream, idx->pack, idx->off) < 0)
-				goto on_error;
 
+			error = git_packfile_stream_open(stream, idx->pack, idx->off);
+			if (error < 0)
+				goto on_error;
 		}
 
 		if (idx->have_delta) {
@@ -580,7 +580,7 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 		}
 		stats->received_objects++;
 
-		if ((error = do_progress_callback(idx, stats)) < 0)
+		if ((error = do_progress_callback(idx, stats)) != 0)
 			goto on_error;
 	}
 
@@ -860,7 +860,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 
 	/* Test for this before resolve_deltas(), as it plays with idx->off */
 	if (idx->off < idx->pack->mwf.size - 20) {
-		giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
+		giterr_set(GITERR_INDEXER, "Unexpected data at the end of the pack");
 		return -1;
 	}
 
@@ -1007,30 +1007,20 @@ on_error:
 
 void git_indexer_free(git_indexer *idx)
 {
-	khiter_t k;
-	unsigned int i;
-	struct entry *e;
-	struct delta_info *delta;
-
 	if (idx == NULL)
 		return;
 
-	git_vector_foreach(&idx->objects, i, e)
-		git__free(e);
-	git_vector_free(&idx->objects);
+	git_vector_free_deep(&idx->objects);
 
 	if (idx->pack) {
-		for (k = kh_begin(idx->pack->idx_cache); k != kh_end(idx->pack->idx_cache); k++) {
-			if (kh_exist(idx->pack->idx_cache, k))
-				git__free(kh_value(idx->pack->idx_cache, k));
-		}
+		struct git_pack_entry *pentry;
+		kh_foreach_value(
+			idx->pack->idx_cache, pentry, { git__free(pentry); });
 
 		git_oidmap_free(idx->pack->idx_cache);
 	}
 
-	git_vector_foreach(&idx->deltas, i, delta)
-		git__free(delta);
-	git_vector_free(&idx->deltas);
+	git_vector_free_deep(&idx->deltas);
 	git_packfile_free(idx->pack);
 	git_filebuf_cleanup(&idx->pack_file);
 	git__free(idx);
diff --git a/src/iterator.c b/src/iterator.c
index 8646399..0e7d0db 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -920,12 +920,7 @@ static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
 
 static void fs_iterator__free_frame(fs_iterator_frame *ff)
 {
-	size_t i;
-	git_path_with_stat *path;
-
-	git_vector_foreach(&ff->entries, i, path)
-		git__free(path);
-	git_vector_free(&ff->entries);
+	git_vector_free_deep(&ff->entries);
 	git__free(ff);
 }
 
@@ -991,9 +986,8 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
 		fi->base.start, fi->base.end, &ff->entries);
 
 	if (error < 0) {
-		git_error last_error = {0};
-
-		giterr_detach(&last_error);
+		git_error_state last_error = { 0 };
+		giterr_capture(&last_error, error);
 
 		/* these callbacks may clear the error message */
 		fs_iterator__free_frame(ff);
@@ -1001,12 +995,7 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
 		/* next time return value we skipped to */
 		fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
 
-		if (last_error.message) {
-			giterr_set_str(last_error.klass, last_error.message);
-			free(last_error.message);
-		}
-
-		return error;
+		return giterr_restore(&last_error);
 	}
 
 	if (ff->entries.length == 0) {
diff --git a/src/merge.c b/src/merge.c
index 00415cb..c0be37d 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -26,6 +26,7 @@
 #include "oid.h"
 #include "index.h"
 #include "filebuf.h"
+#include "config.h"
 
 #include "git2/types.h"
 #include "git2/repository.h"
@@ -253,7 +254,8 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
 	return 0;
 }
 
-int git_repository_mergehead_foreach(git_repository *repo,
+int git_repository_mergehead_foreach(
+	git_repository *repo,
 	git_repository_mergehead_foreach_cb cb,
 	void *payload)
 {
@@ -285,8 +287,8 @@ int git_repository_mergehead_foreach(git_repository *repo,
 		if ((error = git_oid_fromstr(&oid, line)) < 0)
 			goto cleanup;
 
-		if (cb(&oid, payload) != 0) {
-			error = GIT_EUSER;
+		if ((error = cb(&oid, payload)) != 0) {
+			giterr_set_after_callback(error);
 			goto cleanup;
 		}
 
@@ -1396,19 +1398,13 @@ static int merge_tree_normalize_opts(
 	}
 
 	if (!opts->target_limit) {
-		int32_t limit = 0;
-
-		opts->target_limit = GIT_MERGE_TREE_TARGET_LIMIT;
+		int limit = git_config__get_int_force(cfg, "merge.renamelimit", 0);
 
-		if (git_config_get_int32(&limit, cfg, "merge.renameLimit") < 0) {
-			giterr_clear();
-
-			if (git_config_get_int32(&limit, cfg, "diff.renameLimit") < 0)
-				giterr_clear();
-		}
+		if (!limit)
+			limit = git_config__get_int_force(cfg, "diff.renamelimit", 0);
 
-		if (limit > 0)
-			opts->target_limit = limit;
+		opts->target_limit = (limit <= 0) ?
+			GIT_MERGE_TREE_TARGET_LIMIT : (unsigned int)limit;
 	}
 
 	/* assign the internal metric with whitespace flag as payload */
@@ -2389,11 +2385,7 @@ done:
         git_index_set_caps(index_repo, index_repo_caps);
 
 	git_index_free(index_repo);
-
-	git_vector_foreach(&paths, i, path)
-		git__free(path);
-
-	git_vector_free(&paths);
+	git_vector_free_deep(&paths);
 
 	return error;
 }
diff --git a/src/notes.c b/src/notes.c
index beace1b..7959049 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -378,20 +378,11 @@ cleanup:
 
 static int note_get_default_ref(const char **out, git_repository *repo)
 {
-	int ret;
 	git_config *cfg;
+	int ret = git_repository_config__weakptr(&cfg, repo);
 
-	*out = NULL;
-
-	if (git_repository_config__weakptr(&cfg, repo) < 0)
-		return -1;
-
-	ret = git_config_get_string(out, cfg, "core.notesRef");
-	if (ret == GIT_ENOTFOUND) {
-		giterr_clear();
-		*out = GIT_NOTES_DEFAULT_REF;
-		return 0;
-	}
+	*out = (ret != 0) ? NULL : git_config__get_string_force(
+		cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
 
 	return ret;
 }
@@ -592,8 +583,8 @@ int git_note_foreach(
 		return error;
 
 	while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
-		if (note_cb(&note_id, &annotated_id, payload)) {
-			error = GIT_EUSER;
+		if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
+			giterr_set_after_callback(error);
 			break;
 		}
 	}
diff --git a/src/odb_loose.c b/src/odb_loose.c
index ced272b..fd4ffff 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -547,8 +547,7 @@ static int locate_object_short_oid(
 	/* Explore directory to find a unique object matching short_oid */
 	error = git_path_direach(
 		object_location, 0, fn_locate_object_short_oid, &state);
-
-	if (error && error != GIT_EUSER)
+	if (error < 0 && error != GIT_EAMBIGUOUS)
 		return error;
 
 	if (!state.found)
@@ -696,7 +695,6 @@ struct foreach_state {
 	size_t dir_len;
 	git_odb_foreach_cb cb;
 	void *data;
-	int cb_error;
 };
 
 GIT_INLINE(int) filename_to_oid(git_oid *oid, const char *ptr)
@@ -735,12 +733,8 @@ static int foreach_object_dir_cb(void *_state, git_buf *path)
 	if (filename_to_oid(&oid, path->ptr + state->dir_len) < 0)
 		return 0;
 
-	if (state->cb(&oid, state->data)) {
-		state->cb_error = GIT_EUSER;
-		return -1;
-	}
-
-	return 0;
+	return giterr_set_after_callback_function(
+		state->cb(&oid, state->data), "git_odb_foreach");
 }
 
 static int foreach_cb(void *_state, git_buf *path)
@@ -764,6 +758,8 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb 
 
 	git_buf_sets(&buf, objects_dir);
 	git_path_to_dir(&buf);
+	if (git_buf_oom(&buf))
+		return -1;
 
 	memset(&state, 0, sizeof(state));
 	state.cb = cb;
@@ -774,7 +770,7 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb 
 
 	git_buf_free(&buf);
 
-	return state.cb_error ? state.cb_error : error;
+	return error;
 }
 
 static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *oid)
diff --git a/src/odb_pack.c b/src/odb_pack.c
index fd2ca0f..903b00d 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -190,31 +190,39 @@ static int packfile_sort__cb(const void *a_, const void *b_)
 }
 
 
-
-static int packfile_load__cb(void *_data, git_buf *path)
+static int packfile_load__cb(void *data, git_buf *path)
 {
-	struct pack_backend *backend = (struct pack_backend *)_data;
+	struct pack_backend *backend = data;
 	struct git_pack_file *pack;
+	const char *path_str = git_buf_cstr(path);
+	size_t i, cmp_len = git_buf_len(path);
 	int error;
-	size_t i;
 
-	if (git__suffixcmp(path->ptr, ".idx") != 0)
+	if (cmp_len <= strlen(".idx") || git__suffixcmp(path_str, ".idx") != 0)
 		return 0; /* not an index */
 
+	cmp_len -= strlen(".idx");
+
 	for (i = 0; i < backend->packs.length; ++i) {
 		struct git_pack_file *p = git_vector_get(&backend->packs, i);
-		if (memcmp(p->pack_name, git_buf_cstr(path), git_buf_len(path) - strlen(".idx")) == 0)
+
+		if (memcmp(p->pack_name, path_str, cmp_len) == 0)
 			return 0;
 	}
 
 	error = git_packfile_alloc(&pack, path->ptr);
-	if (error == GIT_ENOTFOUND)
-		/* ignore missing .pack file as git does */
+
+	/* ignore missing .pack file as git does */
+	if (error == GIT_ENOTFOUND) {
+		giterr_clear();
 		return 0;
-	else if (error < 0)
-		return error;
+	}
+
+	if (!error)
+		error = git_vector_insert(&backend->packs, pack);
+
+	return error;
 
-	return git_vector_insert(&backend->packs, pack);
 }
 
 static int pack_entry_find_inner(
@@ -314,13 +322,12 @@ static int pack_entry_find_prefix(
  * Implement the git_odb_backend API calls
  *
  ***********************************************************/
-static int pack_backend__refresh(git_odb_backend *_backend)
+static int pack_backend__refresh(git_odb_backend *backend_)
 {
-	struct pack_backend *backend = (struct pack_backend *)_backend;
-
 	int error;
 	struct stat st;
 	git_buf path = GIT_BUF_INIT;
+	struct pack_backend *backend = (struct pack_backend *)backend_;
 
 	if (backend->pack_folder == NULL)
 		return 0;
@@ -334,12 +341,9 @@ static int pack_backend__refresh(git_odb_backend *_backend)
 	error = git_path_direach(&path, 0, packfile_load__cb, backend);
 
 	git_buf_free(&path);
-
-	if (error < 0)
-		return -1;
-
 	git_vector_sort(&backend->packs);
-	return 0;
+
+	return error;
 }
 
 static int pack_backend__read_header_internal(
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 2d62507..335944c 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -205,14 +205,18 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
 	po = pb->object_list + pb->nr_objects;
 	memset(po, 0x0, sizeof(*po));
 
-	if (git_odb_read_header(&po->size, &po->type, pb->odb, oid) < 0)
-		return -1;
+	if ((ret = git_odb_read_header(&po->size, &po->type, pb->odb, oid)) < 0)
+		return ret;
 
 	pb->nr_objects++;
 	git_oid_cpy(&po->id, oid);
 	po->hash = name_hash(name);
 
 	pos = kh_put(oid, pb->object_ix, &po->id, &ret);
+	if (ret < 0) {
+		giterr_set_oom();
+		return ret;
+	}
 	assert(ret != 0);
 	kh_value(pb->object_ix, pos) = po;
 
@@ -220,12 +224,17 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
 
 	if (pb->progress_cb) {
 		double current_time = git__timer();
-		if ((current_time - pb->last_progress_report_time) >= MIN_PROGRESS_UPDATE_INTERVAL) {
+		double elapsed = current_time - pb->last_progress_report_time;
+
+		if (elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) {
 			pb->last_progress_report_time = current_time;
-			if (pb->progress_cb(GIT_PACKBUILDER_ADDING_OBJECTS, pb->nr_objects, 0, pb->progress_cb_payload)) {
-				giterr_clear();
-				return GIT_EUSER;
-			}
+
+			ret = pb->progress_cb(
+				GIT_PACKBUILDER_ADDING_OBJECTS,
+				pb->nr_objects, 0, pb->progress_cb_payload);
+
+			if (ret)
+				return giterr_set_after_callback(ret);
 		}
 	}
 
@@ -323,8 +332,10 @@ static int write_object(git_buf *buf, git_packbuilder *pb, git_pobject *po)
 		git_hash_update(&pb->ctx, data, size) < 0)
 		goto on_error;
 
-	if (po->delta_data)
+	if (po->delta_data) {
 		git__free(po->delta_data);
+		po->delta_data = NULL;
+	}
 
 	git_odb_object_free(obj);
 	git_buf_free(&zbuf);
@@ -603,6 +614,15 @@ static int write_pack(git_packbuilder *pb,
 	error = cb(entry_oid.id, GIT_OID_RAWSZ, data);
 
 done:
+	/* if callback cancelled writing, we must still free delta_data */
+	for ( ; i < pb->nr_objects; ++i) {
+		po = write_order[i];
+		if (po->delta_data) {
+			git__free(po->delta_data);
+			po->delta_data = NULL;
+		}
+	}
+
 	git__free(write_order);
 	git_buf_free(&buf);
 	return error;
@@ -1284,21 +1304,22 @@ const git_oid *git_packbuilder_hash(git_packbuilder *pb)
 	return &pb->pack_oid;
 }
 
-static int cb_tree_walk(const char *root, const git_tree_entry *entry, void *payload)
+static int cb_tree_walk(
+	const char *root, const git_tree_entry *entry, void *payload)
 {
+	int error;
 	struct tree_walk_context *ctx = payload;
 
 	/* A commit inside a tree represents a submodule commit and should be skipped. */
 	if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT)
 		return 0;
 
-	if (git_buf_sets(&ctx->buf, root) < 0 ||
-		git_buf_puts(&ctx->buf, git_tree_entry_name(entry)) < 0)
-		return -1;
+	if (!(error = git_buf_sets(&ctx->buf, root)) &&
+		!(error = git_buf_puts(&ctx->buf, git_tree_entry_name(entry))))
+		error = git_packbuilder_insert(
+			ctx->pb, git_tree_entry_id(entry), git_buf_cstr(&ctx->buf));
 
-	return git_packbuilder_insert(ctx->pb,
-		git_tree_entry_id(entry),
-		git_buf_cstr(&ctx->buf));
+	return error;
 }
 
 int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid)
@@ -1318,22 +1339,17 @@ int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid)
 
 int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid)
 {
-	git_tree *tree;
+	int error;
+	git_tree *tree = NULL;
 	struct tree_walk_context context = { pb, GIT_BUF_INIT };
 
-	if (git_tree_lookup(&tree, pb->repo, oid) < 0 ||
-	    git_packbuilder_insert(pb, oid, NULL) < 0)
-		return -1;
-
-	if (git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context) < 0) {
-		git_tree_free(tree);
-		git_buf_free(&context.buf);
-		return -1;
-	}
+	if (!(error = git_tree_lookup(&tree, pb->repo, oid)) &&
+	    !(error = git_packbuilder_insert(pb, oid, NULL)))
+		error = git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context);
 
 	git_tree_free(tree);
 	git_buf_free(&context.buf);
-	return 0;
+	return error;
 }
 
 uint32_t git_packbuilder_object_count(git_packbuilder *pb)
diff --git a/src/pack.c b/src/pack.c
index 644b2d4..23fcf35 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -1042,10 +1042,9 @@ int git_pack_foreach_entry(
 {
 	const unsigned char *index = p->index_map.data, *current;
 	uint32_t i;
+	int error = 0;
 
 	if (index == NULL) {
-		int error;
-
 		if ((error = pack_index_open(p)) < 0)
 			return error;
 
@@ -1062,7 +1061,6 @@ int git_pack_foreach_entry(
 
 	if (p->oids == NULL) {
 		git_vector offsets, oids;
-		int error;
 
 		if ((error = git_vector_init(&oids, p->num_objects, NULL)))
 			return error;
@@ -1084,15 +1082,16 @@ int git_pack_foreach_entry(
 			git_vector_foreach(&offsets, i, current)
 				git_vector_insert(&oids, (void*)&current[4]);
 		}
+
 		git_vector_free(&offsets);
-		p->oids = (git_oid **)oids.contents;
+		p->oids = (git_oid **)git_vector_detach(NULL, NULL, &oids);
 	}
 
 	for (i = 0; i < p->num_objects; i++)
-		if (cb(p->oids[i], data))
-			return GIT_EUSER;
+		if ((error = cb(p->oids[i], data)) != 0)
+			return giterr_set_after_callback(error);
 
-	return 0;
+	return error;
 }
 
 static int pack_entry_find_offset(
diff --git a/src/path.c b/src/path.c
index 750dd3e..feb2739 100644
--- a/src/path.c
+++ b/src/path.c
@@ -436,8 +436,12 @@ int git_path_walk_up(
 	while (scan >= stop) {
 		error = cb(data, &iter);
 		iter.ptr[scan] = oldc;
-		if (error < 0)
+
+		if (error) {
+			giterr_set_after_callback(error);
 			break;
+		}
+
 		scan = git_buf_rfind_next(&iter, '/');
 		if (scan >= 0) {
 			scan++;
@@ -528,7 +532,9 @@ bool git_path_is_empty_dir(const char *path)
 	if (!git_path_isdir(path))
 		return false;
 
-	if (!(error = git_buf_sets(&dir, path)))
+	if ((error = git_buf_sets(&dir, path)) != 0)
+		giterr_clear();
+	else
 		error = git_path_direach(&dir, 0, path_found_entry, NULL);
 
 	git_buf_free(&dir);
@@ -778,7 +784,7 @@ int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
 		return 0;
 
 	while (1) {
-		if (git_buf_grow(&ic->buf, wantlen) < 0)
+		if (git_buf_grow(&ic->buf, wantlen + 1) < 0)
 			return -1;
 
 		nfc    = ic->buf.ptr   + ic->buf.size;
@@ -867,7 +873,7 @@ int git_path_direach(
 		if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
 			break;
 #endif
-				
+
 		if ((error = git_buf_put(path, de_path, de_len)) < 0)
 			break;
 
@@ -875,8 +881,8 @@ int git_path_direach(
 
 		git_buf_truncate(path, wd_len); /* restore path */
 
-		if (error) {
-			error = GIT_EUSER;
+		if (error != 0) {
+			giterr_set_after_callback(error);
 			break;
 		}
 	}
diff --git a/src/path.h b/src/path.h
index 3daafd2..f26175d 100644
--- a/src/path.h
+++ b/src/path.h
@@ -255,9 +255,10 @@ enum {
  * @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.
+ *		safely append text to the pathbuf if needed.  Return non-zero to
+ *		cancel iteration (and return value will be propagated back).
  * @param payload Passed to callback as first argument.
- * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ * @return 0 on success or error code from OS error or from callback
  */
 extern int git_path_direach(
 	git_buf *pathbuf,
@@ -288,7 +289,7 @@ extern int git_path_cmp(
  *		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.
+ *		be modified in any way. Return non-zero to stop iteration.
  * @param state Passed to fn as the first ath.
  */
 extern int git_path_walk_up(
diff --git a/src/pathspec.c b/src/pathspec.c
index 1e7e65e..bad8dac 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -102,15 +102,7 @@ int git_pathspec__vinit(
 /* free data from the pathspec vector */
 void git_pathspec__vfree(git_vector *vspec)
 {
-	git_attr_fnmatch *match;
-	unsigned int i;
-
-	git_vector_foreach(vspec, i, match) {
-		git__free(match);
-		vspec->contents[i] = NULL;
-	}
-
-	git_vector_free(vspec);
+	git_vector_free_deep(vspec);
 }
 
 struct pathspec_match_context {
diff --git a/src/pool.c b/src/pool.c
index d484769..146f118 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -190,19 +190,18 @@ void *git_pool_malloc(git_pool *pool, uint32_t items)
 
 char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
 {
-	void *ptr = NULL;
+	char *ptr = NULL;
 
 	assert(pool && str && pool->item_size == sizeof(char));
 
-	if (n + 1 == 0) {
-		giterr_set_oom();
+	if ((uint32_t)(n + 1) < n)
 		return NULL;
-	}
 
 	if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
 		memcpy(ptr, str, n);
-		*(((char *)ptr) + n) = '\0';
+		ptr[n] = '\0';
 	}
+
 	pool->has_string_alloc = 1;
 
 	return ptr;
diff --git a/src/posix.c b/src/posix.c
index b75109b..525785f 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -203,4 +203,40 @@ int p_write(git_file fd, const void *buf, size_t cnt)
 	return 0;
 }
 
+#ifdef NO_MMAP
 
+#include "map.h"
+
+int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
+{
+	GIT_MMAP_VALIDATE(out, len, prot, flags);
+
+	out->data = NULL;
+	out->len = 0;
+
+	if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) {
+		giterr_set(GITERR_OS, "Trying to map shared-writeable");
+		return -1;
+	}
+
+	out->data = malloc(len);
+	GITERR_CHECK_ALLOC(out->data);
+
+	if ((p_lseek(fd, offset, SEEK_SET) < 0) || ((size_t)p_read(fd, out->data, len) != len)) {
+		giterr_set(GITERR_OS, "mmap emulation failed");
+		return -1;
+	}
+
+	out->len = len;
+	return 0;
+}
+
+int p_munmap(git_map *map)
+{
+	assert(map != NULL);
+	free(map->data);
+
+	return 0;
+}
+
+#endif
diff --git a/src/push.c b/src/push.c
index 3c9d5bb..dd77864 100644
--- a/src/push.c
+++ b/src/push.c
@@ -541,10 +541,7 @@ static int queue_objects(git_push *push)
 	error = 0;
 
 on_error:
-	git_vector_foreach(&commits, i, oid)
-		git__free(oid);
-
-	git_vector_free(&commits);
+	git_vector_free_deep(&commits);
 	return error;
 }
 
@@ -662,8 +659,9 @@ int git_push_status_foreach(git_push *push,
 	unsigned int i;
 
 	git_vector_foreach(&push->status, i, status) {
-		if (cb(status->ref, status->msg, data) < 0)
-			return GIT_EUSER;
+		int error = cb(status->ref, status->msg, data);
+		if (error)
+			return giterr_set_after_callback(error);
 	}
 
 	return 0;
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 62d5c10..df7cb9d 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -264,9 +264,9 @@ done:
 	return error;
 }
 
-static int _dirent_loose_load(void *data, git_buf *full_path)
+static int _dirent_loose_load(void *payload, git_buf *full_path)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)data;
+	refdb_fs_backend *backend = payload;
 	const char *file_path;
 
 	if (git__suffixcmp(full_path->ptr, ".lock") == 0)
@@ -305,7 +305,7 @@ static int packed_loadloose(refdb_fs_backend *backend)
 
 	git_buf_free(&refs_path);
 
-	return (error == GIT_EUSER) ? -1 : error;
+	return error;
 }
 
 static int refdb_fs_backend__exists(
diff --git a/src/refs.c b/src/refs.c
index 472a798..4f3a557 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -471,7 +471,7 @@ int git_reference_rename(
 	if ((error = git_refdb_rename(out, ref->db, ref->name, new_name, force)) < 0)
 		return error;
 
-	/* Update HEAD it was poiting to the reference being renamed. */
+	/* Update HEAD it was pointing to the reference being renamed */
 	if (should_head_be_updated &&
 		(error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
 		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
@@ -513,20 +513,19 @@ int git_reference_foreach(
 	git_reference *ref;
 	int error;
 
-	if (git_reference_iterator_new(&iter, repo) < 0)
-		return -1;
+	if ((error = git_reference_iterator_new(&iter, repo)) < 0)
+		return error;
 
-	while ((error = git_reference_next(&ref, iter)) == 0) {
-		if (callback(ref, payload)) {
-			error = GIT_EUSER;
-			goto out;
+	while (!(error = git_reference_next(&ref, iter))) {
+		if ((error = callback(ref, payload)) != 0) {
+			giterr_set_after_callback(error);
+			break;
 		}
 	}
 
 	if (error == GIT_ITEROVER)
 		error = 0;
 
-out:
 	git_reference_iterator_free(iter);
 	return error;
 }
@@ -540,20 +539,19 @@ int git_reference_foreach_name(
 	const char *refname;
 	int error;
 
-	if (git_reference_iterator_new(&iter, repo) < 0)
-		return -1;
+	if ((error = git_reference_iterator_new(&iter, repo)) < 0)
+		return error;
 
-	while ((error = git_reference_next_name(&refname, iter)) == 0) {
-		if (callback(refname, payload)) {
-			error = GIT_EUSER;
-			goto out;
+	while (!(error = git_reference_next_name(&refname, iter))) {
+		if ((error = callback(refname, payload)) != 0) {
+			giterr_set_after_callback(error);
+			break;
 		}
 	}
 
 	if (error == GIT_ITEROVER)
 		error = 0;
 
-out:
 	git_reference_iterator_free(iter);
 	return error;
 }
@@ -568,20 +566,19 @@ int git_reference_foreach_glob(
 	const char *refname;
 	int error;
 
-	if (git_reference_iterator_glob_new(&iter, repo, glob) < 0)
-		return -1;
+	if ((error = git_reference_iterator_glob_new(&iter, repo, glob)) < 0)
+		return error;
 
-	while ((error = git_reference_next_name(&refname, iter)) == 0) {
-		if (callback(refname, payload)) {
-			error = GIT_EUSER;
-			goto out;
+	while (!(error = git_reference_next_name(&refname, iter))) {
+		if ((error = callback(refname, payload)) != 0) {
+			giterr_set_after_callback(error);
+			break;
 		}
 	}
 
 	if (error == GIT_ITEROVER)
 		error = 0;
 
-out:
 	git_reference_iterator_free(iter);
 	return error;
 }
@@ -624,7 +621,9 @@ void git_reference_iterator_free(git_reference_iterator *iter)
 
 static int cb__reflist_add(const char *ref, void *data)
 {
-	return git_vector_insert((git_vector *)data, git__strdup(ref));
+	char *name = git__strdup(ref);
+	GITERR_CHECK_ALLOC(name);
+	return git_vector_insert((git_vector *)data, name);
 }
 
 int git_reference_list(
@@ -647,8 +646,8 @@ int git_reference_list(
 		return -1;
 	}
 
-	array->strings = (char **)ref_list.contents;
-	array->count = ref_list.length;
+	array->strings = (char **)git_vector_detach(&array->count, NULL, &ref_list);
+
 	return 0;
 }
 
diff --git a/src/remote.c b/src/remote.c
index 3d890a5..294a870 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -45,7 +45,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
 
 static int download_tags_value(git_remote *remote, git_config *cfg)
 {
-	const char *val;
+	const git_config_entry *ce;
 	git_buf buf = GIT_BUF_INIT;
 	int error;
 
@@ -53,16 +53,14 @@ static int download_tags_value(git_remote *remote, git_config *cfg)
 	if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0)
 		return -1;
 
-	error = git_config_get_string(&val, cfg, git_buf_cstr(&buf));
+	error = git_config__lookup_entry(&ce, cfg, git_buf_cstr(&buf), false);
 	git_buf_free(&buf);
-	if (!error && !strcmp(val, "--no-tags"))
-		remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE;
-	else if (!error && !strcmp(val, "--tags"))
-		remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
 
-	if (error == GIT_ENOTFOUND) {
-		giterr_clear();
-		error = 0;
+	if (!error && ce && ce->value) {
+		if (!strcmp(ce->value, "--no-tags"))
+			remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE;
+		else if (!strcmp(ce->value, "--tags"))
+			remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
 	}
 
 	return error;
@@ -104,12 +102,7 @@ static int get_check_cert(int *out, git_repository *repo)
 	if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
 		return error;
 
-	if ((error = git_config_get_bool(out, cfg, "http.sslVerify")) == 0)
-		return 0;
-	else if (error != GIT_ENOTFOUND)
-		return error;
-
-	giterr_clear();
+	*out = git_config__get_bool_force(cfg, "http.sslverify", 1);
 	return 0;
 }
 
@@ -262,8 +255,7 @@ struct refspec_cb_data {
 
 static int refspec_cb(const git_config_entry *entry, void *payload)
 {
-	const struct refspec_cb_data *data = (struct refspec_cb_data *)payload;
-
+	struct refspec_cb_data *data = (struct refspec_cb_data *)payload;
 	return add_refspec(data->remote, entry->value, data->fetch);
 }
 
@@ -290,9 +282,6 @@ static int get_optional_config(
 		error = 0;
 	}
 
-	if (error < 0)
-		error = -1;
-
 	return error;
 }
 
@@ -303,7 +292,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
 	const char *val;
 	int error = 0;
 	git_config *config;
-	struct refspec_cb_data data;
+	struct refspec_cb_data data = { NULL };
 	bool optional_setting_found = false, found;
 
 	assert(out && repo && name);
@@ -325,17 +314,15 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
 	if ((error = get_check_cert(&remote->check_cert, repo)) < 0)
 		goto cleanup;
 
-	if ((git_vector_init(&remote->refs, 32, NULL) < 0) ||
-	    (git_vector_init(&remote->refspecs, 2, NULL) < 0) ||
-	    (git_vector_init(&remote->active_refspecs, 2, NULL) < 0)) {
+	if (git_vector_init(&remote->refs, 32, NULL) < 0 ||
+	    git_vector_init(&remote->refspecs, 2, NULL) < 0 ||
+	    git_vector_init(&remote->active_refspecs, 2, NULL) < 0) {
 		error = -1;
 		goto cleanup;
 	}
 
-	if (git_buf_printf(&buf, "remote.%s.url", name) < 0) {
-		error = -1;
+	if ((error = git_buf_printf(&buf, "remote.%s.url", name)) < 0)
 		goto cleanup;
-	}
 
 	if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0)
 		goto cleanup;
@@ -370,6 +357,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
 
 	data.remote = remote;
 	data.fetch = true;
+
 	git_buf_clear(&buf);
 	git_buf_printf(&buf, "remote.%s.fetch", name);
 
@@ -493,7 +481,7 @@ int git_remote_save(const git_remote *remote)
 		}
 		if (error < 0) {
 			git_buf_free(&buf);
-			return -1;
+			return error;
 		}
 	}
 
@@ -641,7 +629,7 @@ int git_remote_connect(git_remote *remote, git_direction direction)
 	if (!remote->check_cert)
 		flags |= GIT_TRANSPORTFLAGS_NO_CHECK_CERT;
 
-	if ((error = t->connect(t, url, remote->callbacks.credentials, remote->callbacks.payload, direction, flags)) < 0)
+	if ((error = t->connect(t, url, remote->callbacks.credentials, remote->callbacks.payload, direction, flags)) != 0)
 		goto on_error;
 
 	remote->transport = t;
@@ -667,7 +655,8 @@ int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote
 int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url)
 {
 	git_config *cfg;
-	const char *val;
+	const git_config_entry *ce;
+	const char *val = NULL;
 	int error;
 
 	assert(remote);
@@ -684,44 +673,39 @@ int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_ur
 	 * to least specific. */
 
 	/* remote.<name>.proxy config setting */
-	if (remote->name && 0 != *(remote->name)) {
+	if (remote->name && remote->name[0]) {
 		git_buf buf = GIT_BUF_INIT;
 
 		if ((error = git_buf_printf(&buf, "remote.%s.proxy", remote->name)) < 0)
 			return error;
 
-		if ((error = git_config_get_string(&val, cfg, git_buf_cstr(&buf))) == 0 &&
-			val && ('\0' != *val)) {
-			git_buf_free(&buf);
+		error = git_config__lookup_entry(&ce, cfg, git_buf_cstr(&buf), false);
+		git_buf_free(&buf);
 
-			*proxy_url = git__strdup(val);
-			GITERR_CHECK_ALLOC(*proxy_url);
-			return 0;
-		} else if (error != GIT_ENOTFOUND)
+		if (error < 0)
 			return error;
 
-		giterr_clear();
-		git_buf_free(&buf);
+		if (ce && ce->value) {
+			val = ce->value;
+			goto found;
+		}
 	}
 
 	/* http.proxy config setting */
-	if ((error = git_config_get_string(&val, cfg, "http.proxy")) == 0 &&
-		val && ('\0' != *val)) {
-		*proxy_url = git__strdup(val);
-		GITERR_CHECK_ALLOC(*proxy_url);
-		return 0;
-	} else if (error != GIT_ENOTFOUND)
+	if ((error = git_config__lookup_entry(&ce, cfg, "http.proxy", false)) < 0)
 		return error;
-
-	giterr_clear();
+	if (ce && ce->value) {
+		val = ce->value;
+		goto found;
+	}
 
 	/* HTTP_PROXY / HTTPS_PROXY environment variables */
 	val = use_ssl ? getenv("HTTPS_PROXY") : getenv("HTTP_PROXY");
 
-	if (val && ('\0' != *val)) {
+found:
+	if (val && val[0]) {
 		*proxy_url = git__strdup(val);
 		GITERR_CHECK_ALLOC(*proxy_url);
-		return 0;
 	}
 
 	return 0;
@@ -797,7 +781,7 @@ int git_remote_download(git_remote *remote)
 	git_vector_free(&refs);
 
 	if (error < 0)
-		return -1;
+		return error;
 
 	if ((error = git_fetch_negotiate(remote)) < 0)
 		return error;
@@ -810,10 +794,10 @@ int git_remote_fetch(git_remote *remote)
 	int error;
 
 	/* Connect and download everything */
-	if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) < 0)
+	if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0)
 		return error;
 
-	if ((error = git_remote_download(remote)) < 0)
+	if ((error = git_remote_download(remote)) != 0)
 		return error;
 
 	/* We don't need to be connected anymore */
@@ -1041,7 +1025,6 @@ int git_remote_update_tips(git_remote *remote)
 	int error;
 	size_t i;
 
-
 	if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
 		return -1;
 
@@ -1141,40 +1124,28 @@ static int remote_list_cb(const git_config_entry *entry, void *payload)
 
 int git_remote_list(git_strarray *remotes_list, git_repository *repo)
 {
-	git_config *cfg;
-	git_vector list;
 	int error;
+	git_config *cfg;
+	git_vector list = GIT_VECTOR_INIT;
 
-	if (git_repository_config__weakptr(&cfg, repo) < 0)
-		return -1;
+	if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
+		return error;
 
-	if (git_vector_init(&list, 4, git__strcmp_cb) < 0)
-		return -1;
+	if ((error = git_vector_init(&list, 4, git__strcmp_cb)) < 0)
+		return error;
 
 	error = git_config_foreach_match(
 		cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &list);
 
 	if (error < 0) {
-		size_t i;
-		char *elem;
-
-		git_vector_foreach(&list, i, elem) {
-			git__free(elem);
-		}
-
-		git_vector_free(&list);
-
-		/* cb error is converted to GIT_EUSER by git_config_foreach */
-		if (error == GIT_EUSER)
-			error = -1;
-
+		git_vector_free_deep(&list);
 		return error;
 	}
 
 	git_vector_uniq(&list, git__free);
 
-	remotes_list->strings = (char **)list.contents;
-	remotes_list->count = list.length;
+	remotes_list->strings =
+		(char **)git_vector_detach(&remotes_list->count, NULL, &list);
 
 	return 0;
 }
@@ -1261,8 +1232,7 @@ cleanup:
 	return error;
 }
 
-struct update_data
-{
+struct update_data {
 	git_config *config;
 	const char *old_remote_name;
 	const char *new_remote_name;
@@ -1278,9 +1248,7 @@ static int update_config_entries_cb(
 		return 0;
 
 	return git_config_set_string(
-		data->config,
-		entry->name,
-		data->new_remote_name);
+		data->config, entry->name, data->new_remote_name);
 }
 
 static int update_branch_remote_config_entry(
@@ -1288,20 +1256,17 @@ static int update_branch_remote_config_entry(
 	const char *old_name,
 	const char *new_name)
 {
-	git_config *config;
-	struct update_data data;
+	int error;
+	struct update_data data = { NULL };
 
-	if (git_repository_config__weakptr(&config, repo) < 0)
-		return -1;
+	if ((error = git_repository_config__weakptr(&data.config, repo)) < 0)
+		return error;
 
-	data.config = config;
 	data.old_remote_name = old_name;
 	data.new_remote_name = new_name;
 
 	return git_config_foreach_match(
-		config,
-		"branch\\..+\\.remote",
-		update_config_entries_cb, &data);
+		data.config, "branch\\..+\\.remote", update_config_entries_cb, &data);
 }
 
 static int rename_one_remote_reference(
@@ -1309,18 +1274,20 @@ static int rename_one_remote_reference(
 	const char *old_remote_name,
 	const char *new_remote_name)
 {
-	int error = -1;
+	int error;
 	git_buf new_name = GIT_BUF_INIT;
 
-	if (git_buf_printf(
+	error = git_buf_printf(
 		&new_name,
 		GIT_REFS_REMOTES_DIR "%s%s",
 		new_remote_name,
-		reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
-			return -1;
+		reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name));
 
-	error = git_reference_rename(NULL, reference, git_buf_cstr(&new_name), 0);
-	git_reference_free(reference);
+	if (!error) {
+		error = git_reference_rename(
+			NULL, reference, git_buf_cstr(&new_name), 0);
+		git_reference_free(reference);
+	}
 
 	git_buf_free(&new_name);
 	return error;
@@ -1331,12 +1298,12 @@ static int rename_remote_references(
 	const char *old_name,
 	const char *new_name)
 {
-	int error = -1;
+	int error;
 	git_reference *ref;
 	git_reference_iterator *iter;
 
-	if (git_reference_iterator_new(&iter, repo) < 0)
-		return -1;
+	if ((error = git_reference_iterator_new(&iter, repo)) < 0)
+		return error;
 
 	while ((error = git_reference_next(&ref, iter)) == 0) {
 		if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) {
@@ -1344,18 +1311,13 @@ static int rename_remote_references(
 			continue;
 		}
 
-		if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) {
-			git_reference_iterator_free(iter);
-			return error;
-		}
+		if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0)
+			break;
 	}
 
 	git_reference_iterator_free(iter);
 
-	if (error == GIT_ITEROVER)
-		return 0;
-
-	return error;
+	return (error == GIT_ITEROVER) ? 0 : error;
 }
 
 static int rename_fetch_refspecs(
@@ -1368,52 +1330,50 @@ static int rename_fetch_refspecs(
 	git_buf base = GIT_BUF_INIT, var = GIT_BUF_INIT, val = GIT_BUF_INIT;
 	const git_refspec *spec;
 	size_t i;
-	int error = -1;
+	int error = 0;
 
-	if (git_buf_printf(&base, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0)
-		goto cleanup;
+	if ((error = git_repository_config__weakptr(&config, remote->repo)) < 0)
+		return error;
+
+	if ((error = git_buf_printf(
+			&base, "+refs/heads/*:refs/remotes/%s/*", remote->name)) < 0)
+		return error;
 
 	git_vector_foreach(&remote->refspecs, i, spec) {
 		if (spec->push)
 			continue;
 
-		/* Every refspec is a problem refspec for an in-memory remote */
-		if (!remote->name) {
-			if (callback(spec->string, payload) < 0) {
-				error = GIT_EUSER;
-				goto cleanup;
-			}
-
-			continue;
-		}
+		/* Every refspec is a problem refspec for an in-memory remote, OR */
+		/* Does the dst part of the refspec follow the expected format? */
+		if (!remote->name ||
+			strcmp(git_buf_cstr(&base), spec->string)) {
 
-		/* Does the dst part of the refspec follow the extected standard format? */
-		if (strcmp(git_buf_cstr(&base), spec->string)) {
-			if (callback(spec->string, payload) < 0) {
-				error = GIT_EUSER;
-				goto cleanup;
+			if ((error = callback(spec->string, payload)) != 0) {
+				giterr_set_after_callback(error);
+				break;
 			}
 
 			continue;
 		}
 
 		/* If we do want to move it to the new section */
-		if (git_buf_printf(&val, "+refs/heads/*:refs/remotes/%s/*", new_name) < 0)
-			goto cleanup;
 
-		if (git_buf_printf(&var, "remote.%s.fetch", new_name) < 0)
-			goto cleanup;
+		git_buf_clear(&val);
+		git_buf_clear(&var);
 
-		if (git_repository_config__weakptr(&config, remote->repo) < 0)
-			goto cleanup;
+		if (git_buf_printf(
+				&val, "+refs/heads/*:refs/remotes/%s/*", new_name) < 0 ||
+			git_buf_printf(&var, "remote.%s.fetch", new_name) < 0)
+		{
+			error = -1;
+			break;
+		}
 
-		if (git_config_set_string(config, git_buf_cstr(&var), git_buf_cstr(&val)) < 0)
-			goto cleanup;
+		if ((error = git_config_set_string(
+				config, git_buf_cstr(&var), git_buf_cstr(&val))) < 0)
+			break;
 	}
 
-	error = 0;
-
-cleanup:
 	git_buf_free(&base);
 	git_buf_free(&var);
 	git_buf_free(&val);
@@ -1448,11 +1408,11 @@ int git_remote_rename(
 				new_name,
 				callback,
 				payload)) < 0)
-				return error;
+					return error;
 
 			remote->name = git__strdup(new_name);
+			GITERR_CHECK_ALLOC(remote->name);
 
-			if (!remote->name) return 0;
 			return git_remote_save(remote);
 		}
 
@@ -1479,11 +1439,13 @@ int git_remote_rename(
 			new_name,
 			callback,
 			payload)) < 0)
-			return error;
+				return error;
 	}
 
 	git__free(remote->name);
+
 	remote->name = git__strdup(new_name);
+	GITERR_CHECK_ALLOC(remote->name);
 
 	return 0;
 }
@@ -1655,9 +1617,7 @@ static int copy_refspecs(git_strarray *array, git_remote *remote, unsigned int p
 	return 0;
 
 on_error:
-	git_vector_foreach(&refspecs, i, dup)
-		git__free(dup);
-	git_vector_free(&refspecs);
+	git_vector_free_deep(&refspecs);
 
 	return -1;
 }
diff --git a/src/repository.c b/src/repository.c
index 278c038..94f6603 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -186,39 +186,37 @@ static int load_workdir(git_repository *repo, git_buf *parent_path)
 {
 	int         error;
 	git_config *config;
-	const char *worktree;
-	git_buf     worktree_buf = GIT_BUF_INIT;
+	const git_config_entry *ce;
+	git_buf     worktree = GIT_BUF_INIT;
 
 	if (repo->is_bare)
 		return 0;
 
-	if (git_repository_config__weakptr(&config, repo) < 0)
-		return -1;
+	if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+		return error;
 
-	error = git_config_get_string(&worktree, config, "core.worktree");
-	if (!error && worktree != NULL) {
-		error = git_path_prettify_dir(
-			&worktree_buf, worktree, repo->path_repository);
-		if (error < 0)
+	if ((error = git_config__lookup_entry(
+			&ce, config, "core.worktree", false)) < 0)
+		return error;
+
+	if (ce && ce->value) {
+		if ((error = git_path_prettify_dir(
+				&worktree, ce->value, repo->path_repository)) < 0)
 			return error;
-		repo->workdir = git_buf_detach(&worktree_buf);
+
+		repo->workdir = git_buf_detach(&worktree);
 	}
-	else if (error != GIT_ENOTFOUND)
-		return error;
+	else if (parent_path && git_path_isdir(parent_path->ptr))
+		repo->workdir = git_buf_detach(parent_path);
 	else {
-		giterr_clear();
+		if (git_path_dirname_r(&worktree, repo->path_repository) < 0 ||
+			git_path_to_dir(&worktree) < 0)
+			return -1;
 
-		if (parent_path && git_path_isdir(parent_path->ptr))
-			repo->workdir = git_buf_detach(parent_path);
-		else {
-			git_path_dirname_r(&worktree_buf, repo->path_repository);
-			git_path_to_dir(&worktree_buf);
-			repo->workdir = git_buf_detach(&worktree_buf);
-		}
+		repo->workdir = git_buf_detach(&worktree);
 	}
 
 	GITERR_CHECK_ALLOC(repo->workdir);
-
 	return 0;
 }
 
@@ -1610,15 +1608,14 @@ static int at_least_one_cb(const char *refname, void *payload)
 {
 	GIT_UNUSED(refname);
 	GIT_UNUSED(payload);
-
-	return GIT_EUSER;
+	return GIT_PASSTHROUGH;
 }
 
 static int repo_contains_no_reference(git_repository *repo)
 {
 	int error = git_reference_foreach_name(repo, &at_least_one_cb, NULL);
 
-	if (error == GIT_EUSER)
+	if (error == GIT_PASSTHROUGH)
 		return 0;
 
 	if (!error)
diff --git a/src/revwalk.c b/src/revwalk.c
index 3dd14b4..c0a0532 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -112,12 +112,13 @@ static int process_commit_parents(git_revwalk *walk, git_commit_list_node *commi
 
 static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
 {
+	int error;
 	git_object *obj;
 	git_otype type;
 	git_commit_list_node *commit;
 
-	if (git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY) < 0)
-		return -1;
+	if ((error = git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY)) < 0)
+		return error;
 
 	type = git_object_type(obj);
 	git_object_free(obj);
@@ -173,7 +174,6 @@ struct push_cb_data {
 static int push_glob_cb(const char *refname, void *data_)
 {
 	struct push_cb_data *data = (struct push_cb_data *)data_;
-
 	return push_ref(data->walk, refname, data->hide);
 }
 
@@ -191,6 +191,8 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
 		git_buf_joinpath(&buf, GIT_REFS_DIR, glob);
 	else
 		git_buf_puts(&buf, glob);
+	if (git_buf_oom(&buf))
+		return -1;
 
 	/* If no '?', '*' or '[' exist, we append '/ *' to the glob */
 	wildcard = strcspn(glob, "?*[");
@@ -200,11 +202,8 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
 	data.walk = walk;
 	data.hide = hide;
 
-	if (git_buf_oom(&buf))
-		error = -1;
-	else
-		error = git_reference_foreach_glob(
-			walk->repo, git_buf_cstr(&buf), push_glob_cb, &data);
+	error = git_reference_foreach_glob(
+		walk->repo, git_buf_cstr(&buf), push_glob_cb, &data);
 
 	git_buf_free(&buf);
 	return error;
diff --git a/src/stash.c b/src/stash.c
index 083c2a4..eae5696 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -440,7 +440,7 @@ static int is_dirty_cb(const char *path, unsigned int status, void *payload)
 	GIT_UNUSED(status);
 	GIT_UNUSED(payload);
 
-	return 1;
+	return GIT_PASSTHROUGH;
 }
 
 static int ensure_there_are_changes_to_stash(
@@ -463,7 +463,7 @@ static int ensure_there_are_changes_to_stash(
 
 	error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
 
-	if (error == GIT_EUSER)
+	if (error == GIT_PASSTHROUGH)
 		return 0;
 
 	if (!error)
@@ -582,12 +582,14 @@ int git_stash_foreach(
 	for (i = 0; i < max; i++) {
 		entry = git_reflog_entry_byindex(reflog, i);
 
-		if (callback(i,
+		error = callback(i,
 			git_reflog_entry_message(entry),
 			git_reflog_entry_id_new(entry),
-			payload)) {
-				error = GIT_EUSER;
-				break;
+			payload);
+
+		if (error) {
+			giterr_set_after_callback(error);
+			break;
 		}
 	}
 
diff --git a/src/status.c b/src/status.c
index 07fdcb5..7a1472d 100644
--- a/src/status.c
+++ b/src/status.c
@@ -314,8 +314,9 @@ int git_status_list_new(
 			goto done;
 	}
 
-	if ((error = git_diff__paired_foreach(
-			status->head2idx, status->idx2wd, status_collect, status)) < 0)
+	error = git_diff__paired_foreach(
+		status->head2idx, status->idx2wd, status_collect, status);
+	if (error < 0)
 		goto done;
 
 	if (flags & GIT_STATUS_OPT_SORT_CASE_SENSITIVELY)
@@ -360,19 +361,13 @@ const git_status_entry *git_status_byindex(git_status_list *status, size_t i)
 
 void git_status_list_free(git_status_list *status)
 {
-	git_status_entry *status_entry;
-	size_t i;
-
 	if (status == NULL)
 		return;
 
 	git_diff_free(status->head2idx);
 	git_diff_free(status->idx2wd);
 
-	git_vector_foreach(&status->paired, i, status_entry)
-		git__free(status_entry);
-
-	git_vector_free(&status->paired);
+	git_vector_free_deep(&status->paired);
 
 	git__memzero(status, sizeof(*status));
 	git__free(status);
@@ -397,9 +392,8 @@ int git_status_foreach_ext(
 			status_entry->head_to_index->old_file.path :
 			status_entry->index_to_workdir->old_file.path;
 
-		if (cb(path, status_entry->status, payload) != 0) {
-			error = GIT_EUSER;
-			giterr_clear();
+		if ((error = cb(path, status_entry->status, payload)) != 0) {
+			giterr_set_after_callback(error);
 			break;
 		}
 	}
diff --git a/src/submodule.c b/src/submodule.c
index 586494f..f6660a8 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -168,9 +168,8 @@ int git_submodule_foreach(
 				break;
 		}
 
-		if (callback(sm, sm->name, payload)) {
-			giterr_clear();
-			error = GIT_EUSER;
+		if ((error = callback(sm, sm->name, payload)) != 0) {
+			giterr_set_after_callback(error);
 			break;
 		}
 	});
@@ -825,17 +824,14 @@ int git_submodule_reload(git_submodule *submodule)
 	assert(submodule);
 
 	/* refresh index data */
-
-	if (submodule_update_index(submodule) < 0)
-		return -1;
+	if ((error = submodule_update_index(submodule)) < 0)
+		return error;
 
 	/* refresh HEAD tree data */
-
-	if (submodule_update_head(submodule) < 0)
-		return -1;
+	if ((error = submodule_update_head(submodule)) < 0)
+		return error;
 
 	/* refresh config data */
-
 	mods = open_gitmodules(submodule->repo, false, NULL);
 	if (mods != NULL) {
 		git_buf path = GIT_BUF_INIT;
@@ -852,19 +848,17 @@ int git_submodule_reload(git_submodule *submodule)
 
 		git_buf_free(&path);
 		git_config_file_free(mods);
-	}
 
-	if (error < 0)
-		return error;
+		if (error < 0)
+			return error;
+	}
 
 	/* refresh wd data */
-
 	submodule->flags = submodule->flags &
 		~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID);
 
-	error = submodule_load_from_wd_lite(submodule, submodule->path, NULL);
-
-	return error;
+	return submodule_load_from_wd_lite(
+		submodule, submodule->path, submodule->repo);
 }
 
 static void submodule_copy_oid_maybe(
@@ -1087,15 +1081,14 @@ int git_submodule_parse_update(git_submodule_update_t *out, const char *value)
 }
 
 static int submodule_load_from_config(
-	const git_config_entry *entry, void *data)
+	const git_config_entry *entry, void *payload)
 {
-	git_repository *repo = data;
+	git_repository *repo = payload;
 	git_strmap *smcfg = repo->submodules;
 	const char *namestart, *property, *alternate = NULL;
-	const char *key = entry->name, *value = entry->value;
+	const char *key = entry->name, *value = entry->value, *path;
 	git_buf name = GIT_BUF_INIT;
 	git_submodule *sm;
-	bool is_path;
 	int error = 0;
 
 	if (git__prefixcmp(key, "submodule.") != 0)
@@ -1108,15 +1101,11 @@ static int submodule_load_from_config(
 		return 0;
 
 	property++;
-	is_path = (strcasecmp(property, "path") == 0);
+	path = !strcasecmp(property, "path") ? value : NULL;
 
-	if (git_buf_set(&name, namestart, property - namestart - 1) < 0)
-		return -1;
-
-	if (submodule_get(&sm, repo, name.ptr, is_path ? value : NULL) < 0) {
-		git_buf_free(&name);
-		return -1;
-	}
+	if ((error = git_buf_set(&name, namestart, property - namestart - 1)) < 0 ||
+		(error = submodule_get(&sm, repo, name.ptr, path)) < 0)
+		goto done;
 
 	sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;
 
@@ -1130,15 +1119,20 @@ static int submodule_load_from_config(
 
 	if (strcmp(sm->name, name.ptr) != 0) {
 		alternate = sm->name = git_buf_detach(&name);
-	} else if (is_path && value && strcmp(sm->path, value) != 0) {
+	} else if (path && strcmp(path, sm->path) != 0) {
 		alternate = sm->path = git__strdup(value);
-		if (!sm->path)
+		if (!sm->path) {
 			error = -1;
+			goto done;
+		}
 	}
+
 	if (alternate) {
 		void *old_sm = NULL;
 		git_strmap_insert2(smcfg, alternate, sm, old_sm, error);
 
+		if (error < 0)
+			goto done;
 		if (error >= 0)
 			GIT_REFCOUNT_INC(sm); /* inserted under a new key */
 
@@ -1149,42 +1143,44 @@ static int submodule_load_from_config(
 		}
 	}
 
-	git_buf_free(&name);
-	if (error < 0)
-		return error;
-
 	/* TODO: Look up path in index and if it is present but not a GITLINK
 	 * then this should be deleted (at least to match git's behavior)
 	 */
 
-	if (is_path)
-		return 0;
+	if (path)
+		goto done;
 
 	/* copy other properties into submodule entry */
 	if (strcasecmp(property, "url") == 0) {
 		git__free(sm->url);
 		sm->url = NULL;
 
-		if (value != NULL && (sm->url = git__strdup(value)) == NULL)
-			return -1;
+		if (value != NULL && (sm->url = git__strdup(value)) == NULL) {
+			error = -1;
+			goto done;
+		}
 	}
 	else if (strcasecmp(property, "update") == 0) {
-		if (git_submodule_parse_update(&sm->update, value) < 0)
-			return -1;
+		if ((error = git_submodule_parse_update(&sm->update, value)) < 0)
+			goto done;
 		sm->update_default = sm->update;
 	}
 	else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
-		if (git__parse_bool(&sm->fetch_recurse, value) < 0)
-			return submodule_config_error("fetchRecurseSubmodules", value);
+		if (git__parse_bool(&sm->fetch_recurse, value) < 0) {
+			error = submodule_config_error("fetchRecurseSubmodules", value);
+			goto done;
+		}
 	}
 	else if (strcasecmp(property, "ignore") == 0) {
-		if (git_submodule_parse_ignore(&sm->ignore, value) < 0)
-			return -1;
+		if ((error = git_submodule_parse_ignore(&sm->ignore, value)) < 0)
+			goto done;
 		sm->ignore_default = sm->ignore;
 	}
 	/* ignore other unknown submodule properties */
 
-	return 0;
+done:
+	git_buf_free(&name);
+	return error;
 }
 
 static int submodule_load_from_wd_lite(
@@ -1192,8 +1188,7 @@ static int submodule_load_from_wd_lite(
 {
 	git_buf path = GIT_BUF_INIT;
 
-	GIT_UNUSED(name);
-	GIT_UNUSED(payload);
+	GIT_UNUSED(name); GIT_UNUSED(payload);
 
 	if (git_repository_is_bare(sm->repo))
 		return 0;
@@ -1208,7 +1203,6 @@ static int submodule_load_from_wd_lite(
 		sm->flags |= GIT_SUBMODULE_STATUS_IN_WD;
 
 	git_buf_free(&path);
-
 	return 0;
 }
 
@@ -1342,7 +1336,6 @@ static int load_submodule_config(git_repository *repo)
 {
 	int error;
 	git_oid gitmodules_oid;
-	git_buf path = GIT_BUF_INIT;
 	git_config_backend *mods = NULL;
 
 	if (repo->submodules)
@@ -1370,10 +1363,9 @@ static int load_submodule_config(git_repository *repo)
 
 	/* add submodule information from .gitmodules */
 
-	if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL)
-		error = git_config_file_foreach(mods, submodule_load_from_config, repo);
-
-	if (error != 0)
+	if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL &&
+		(error = git_config_file_foreach(
+			mods, submodule_load_from_config, repo)) < 0)
 		goto cleanup;
 
 	/* shallow scan submodules in work tree */
@@ -1382,8 +1374,6 @@ static int load_submodule_config(git_repository *repo)
 		error = git_submodule_foreach(repo, submodule_load_from_wd_lite, NULL);
 
 cleanup:
-	git_buf_free(&path);
-
 	if (mods != NULL)
 		git_config_file_free(mods);
 
@@ -1468,7 +1458,7 @@ static int submodule_update_config(
 	int error;
 	git_config *config;
 	git_buf key = GIT_BUF_INIT;
-	const char *old = NULL;
+	const git_config_entry *ce = NULL;
 
 	assert(submodule);
 
@@ -1480,14 +1470,16 @@ static int submodule_update_config(
 	if (error < 0)
 		goto cleanup;
 
-	if (git_config_get_string(&old, config, key.ptr) < 0)
-		giterr_clear();
+	if ((error = git_config__lookup_entry(&ce, config, key.ptr, false)) < 0)
+		goto cleanup;
 
-	if (!old && only_existing)
+	if (!ce && only_existing)
+		goto cleanup;
+	if (ce && !overwrite)
 		goto cleanup;
-	if (old && !overwrite)
+	if (value && ce && ce->value && !strcmp(ce->value, value))
 		goto cleanup;
-	if ((!old && !value) || (old && value && strcmp(old, value) == 0))
+	if (!value && (!ce || !ce->value))
 		goto cleanup;
 
 	if (!value)
diff --git a/src/tag.c b/src/tag.c
index 31a3c8b..be56b05 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -418,16 +418,19 @@ typedef struct {
 
 static int tags_cb(const char *ref, void *data)
 {
+	int error;
 	git_oid oid;
 	tag_cb_data *d = (tag_cb_data *)data;
 
 	if (git__prefixcmp(ref, GIT_REFS_TAGS_DIR) != 0)
 		return 0; /* no tag */
 
-	if (git_reference_name_to_id(&oid, d->repo, ref) < 0)
-		return -1;
+	if (!(error = git_reference_name_to_id(&oid, d->repo, ref))) {
+		if ((error = d->cb(ref, &oid, d->cb_data)) != 0)
+			giterr_set_after_callback_function(error, "git_tag_foreach");
+	}
 
-	return d->cb(ref, &oid, d->cb_data);
+	return error;
 }
 
 int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data)
@@ -455,8 +458,14 @@ static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
 	tag_filter_data *filter = (tag_filter_data *)data;
 	GIT_UNUSED(oid);
 
-	if (!*filter->pattern || p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
-		return git_vector_insert(filter->taglist, git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN));
+	if (!*filter->pattern ||
+		p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
+	{
+		char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN);
+		GITERR_CHECK_ALLOC(matched);
+
+		return git_vector_insert(filter->taglist, matched);
+	}
 
 	return 0;
 }
@@ -469,20 +478,20 @@ int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_reposit
 
 	assert(tag_names && repo && pattern);
 
-	if (git_vector_init(&taglist, 8, NULL) < 0)
-		return -1;
+	if ((error = git_vector_init(&taglist, 8, NULL)) < 0)
+		return error;
 
 	filter.taglist = &taglist;
 	filter.pattern = pattern;
 
 	error = git_tag_foreach(repo, &tag_list_cb, (void *)&filter);
-	if (error < 0) {
+
+	if (error < 0)
 		git_vector_free(&taglist);
-		return -1;
-	}
 
-	tag_names->strings = (char **)taglist.contents;
-	tag_names->count = taglist.length;
+	tag_names->strings =
+		(char **)git_vector_detach(&tag_names->count, NULL, &taglist);
+
 	return 0;
 }
 
diff --git a/src/transports/git.c b/src/transports/git.c
index 5dcd4ef..21507c1 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -93,18 +93,19 @@ static int git_stream_read(
 	size_t buf_size,
 	size_t *bytes_read)
 {
+	int error;
 	git_stream *s = (git_stream *)stream;
 	gitno_buffer buf;
 
 	*bytes_read = 0;
 
-	if (!s->sent_command && send_command(s) < 0)
-		return -1;
+	if (!s->sent_command && (error = send_command(s)) < 0)
+		return error;
 
 	gitno_buffer_setup(&s->socket, &buf, buffer, buf_size);
 
-	if (gitno_recv(&buf) < 0)
-		return -1;
+	if ((error = gitno_recv(&buf)) < 0)
+		return error;
 
 	*bytes_read = buf.offset;
 
@@ -116,10 +117,11 @@ static int git_stream_write(
 	const char *buffer,
 	size_t len)
 {
+	int error;
 	git_stream *s = (git_stream *)stream;
 
-	if (!s->sent_command && send_command(s) < 0)
-		return -1;
+	if (!s->sent_command && (error = send_command(s)) < 0)
+		return error;
 
 	return gitno_send(&s->socket, buffer, len, 0);
 }
@@ -140,7 +142,7 @@ static void git_stream_free(git_smart_subtransport_stream *stream)
 	}
 
 	git__free(s->url);
-	git__free(s);	
+	git__free(s);
 }
 
 static int git_stream_alloc(
@@ -182,18 +184,21 @@ static int _git_uploadpack_ls(
 	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
 	const char *stream_url = url;
 	git_stream *s;
-	int error = -1;
+	int error;
 
 	*stream = NULL;
+
 	if (!git__prefixcmp(url, prefix_git))
 		stream_url += strlen(prefix_git);
 
-	if (git_stream_alloc(t, stream_url, cmd_uploadpack, stream) < 0)
-		return -1;
+	if ((error = git_stream_alloc(t, stream_url, cmd_uploadpack, stream)) < 0)
+		return error;
 
 	s = (git_stream *)*stream;
 
-	if (!(error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT))) {
+	if (!(error = gitno_extract_url_parts(
+			&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT))) {
+
 		if (!(error = gitno_connect(&s->socket, host, port, 0)))
 			t->current_stream = s;
 
diff --git a/src/transports/http.c b/src/transports/http.c
index ace0d97..c6aaeb9 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -382,9 +382,6 @@ static int on_body_fill_buffer(http_parser *parser, const char *str, size_t len)
 
 static void clear_parser_state(http_subtransport *t)
 {
-	unsigned i;
-	char *entry;
-
 	http_parser_init(&t->parser, HTTP_RESPONSE);
 	gitno_buffer_setup(&t->socket,
 		&t->parse_buffer,
@@ -407,10 +404,7 @@ static void clear_parser_state(http_subtransport *t)
 	git__free(t->location);
 	t->location = NULL;
 
-	git_vector_foreach(&t->www_authenticate, i, entry)
-		git__free(entry);
-
-	git_vector_free(&t->www_authenticate);
+	git_vector_free_deep(&t->www_authenticate);
 }
 
 static int write_chunk(gitno_socket *socket, const char *buffer, size_t len)
diff --git a/src/transports/local.c b/src/transports/local.c
index 4502f02..4635d5d 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -43,43 +43,43 @@ typedef struct {
 static int add_ref(transport_local *t, const char *name)
 {
 	const char peeled[] = "^{}";
+	git_oid head_oid;
 	git_remote_head *head;
 	git_object *obj = NULL, *target = NULL;
 	git_buf buf = GIT_BUF_INIT;
 	int error;
 
-	head = git__calloc(1, sizeof(git_remote_head));
-	GITERR_CHECK_ALLOC(head);
-
-	head->name = git__strdup(name);
-	GITERR_CHECK_ALLOC(head->name);
-
-	error = git_reference_name_to_id(&head->oid, t->repo, name);
+	error = git_reference_name_to_id(&head_oid, t->repo, name);
 	if (error < 0) {
-		git__free(head->name);
-		git__free(head);
 		if (!strcmp(name, GIT_HEAD_FILE) && error == GIT_ENOTFOUND) {
-			/* This is actually okay.  Empty repos often have a HEAD that points to
-			 * a nonexistent "refs/heads/master". */
+			/* This is actually okay.  Empty repos often have a HEAD that
+			 * points to a nonexistent "refs/heads/master". */
 			giterr_clear();
 			return 0;
 		}
 		return error;
 	}
 
-	if (git_vector_insert(&t->refs, head) < 0)
-	{
+	head = git__calloc(1, sizeof(git_remote_head));
+	GITERR_CHECK_ALLOC(head);
+
+	head->name = git__strdup(name);
+	GITERR_CHECK_ALLOC(head->name);
+
+	git_oid_cpy(&head->oid, &head_oid);
+
+	if ((error = git_vector_insert(&t->refs, head)) < 0) {
 		git__free(head->name);
 		git__free(head);
-		return -1;
+		return error;
 	}
 
 	/* If it's not a tag, we don't need to try to peel it */
 	if (git__prefixcmp(name, GIT_REFS_TAGS_DIR))
 		return 0;
 
-	if (git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJ_ANY) < 0)
-		return -1;
+	if ((error = git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJ_ANY)) < 0)
+		return error;
 
 	head = NULL;
 
@@ -94,27 +94,24 @@ static int add_ref(transport_local *t, const char *name)
 	/* And if it's a tag, peel it, and add it to the list */
 	head = git__calloc(1, sizeof(git_remote_head));
 	GITERR_CHECK_ALLOC(head);
+
 	if (git_buf_join(&buf, 0, name, peeled) < 0)
 		return -1;
-
 	head->name = git_buf_detach(&buf);
 
-	if (git_tag_peel(&target, (git_tag *) obj) < 0)
-		goto on_error;
-
-	git_oid_cpy(&head->oid, git_object_id(target));
-	git_object_free(obj);
-	git_object_free(target);
-
-	if (git_vector_insert(&t->refs, head) < 0)
-		return -1;
+	if (!(error = git_tag_peel(&target, (git_tag *)obj))) {
+		git_oid_cpy(&head->oid, git_object_id(target));
 
-	return 0;
+		if ((error = git_vector_insert(&t->refs, head)) < 0) {
+			git__free(head->name);
+			git__free(head);
+		}
+	}
 
-on_error:
 	git_object_free(obj);
 	git_object_free(target);
-	return -1;
+
+	return error;
 }
 
 static int store_refs(transport_local *t)
@@ -222,7 +219,7 @@ static int local_ls(const git_remote_head ***out, size_t *size, git_transport *t
 		return -1;
 	}
 
-	*out = (const git_remote_head **) t->refs.contents;
+	*out = (const git_remote_head **)t->refs.contents;
 	*size = t->refs.length;
 
 	return 0;
@@ -250,8 +247,9 @@ static int local_negotiate_fetch(
 			git_oid_cpy(&rhead->loid, git_object_id(obj));
 		else if (error != GIT_ENOTFOUND)
 			return error;
+		else
+			giterr_clear();
 		git_object_free(obj);
-		giterr_clear();
 	}
 
 	return 0;
@@ -528,7 +526,7 @@ static int local_download_pack(
 		}
 	}
 
-	if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) < 0)
+	if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)
 		goto cleanup;
 
 	/* Write the data to the ODB */
@@ -539,7 +537,7 @@ static int local_download_pack(
 		data.progress_payload = progress_payload;
 		data.writepack = writepack;
 
-		if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) < 0)
+		if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) != 0)
 			goto cleanup;
 	}
 	error = writepack->commit(writepack, stats);
diff --git a/src/transports/smart.c b/src/transports/smart.c
index 5242beb..69eaf9b 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -23,13 +23,13 @@ static int git_smart__recv_cb(gitno_buffer *buf)
 
 	buf->offset += bytes_read;
 
-	if (t->packetsize_cb && !t->cancelled.val)
-		if (t->packetsize_cb(bytes_read, t->packetsize_payload)) {
+	if (t->packetsize_cb && !t->cancelled.val) {
+		error = t->packetsize_cb(bytes_read, t->packetsize_payload);
+		if (error) {
 			git_atomic_set(&t->cancelled, 1);
-
-			giterr_clear();
 			return GIT_EUSER;
 		}
+	}
 
 	return (int)(buf->offset - old_len);
 }
@@ -342,7 +342,7 @@ int git_transport_smart(git_transport **out, git_remote *owner, void *param)
 	t->parent.is_connected = git_smart__is_connected;
 	t->parent.read_flags = git_smart__read_flags;
 	t->parent.cancel = git_smart__cancel;
-	
+
 	t->owner = owner;
 	t->rpc = definition->rpc;
 
@@ -359,7 +359,7 @@ int git_transport_smart(git_transport **out, git_remote *owner, void *param)
 	if (definition->callback(&t->wrapped, &t->parent) < 0) {
 		git__free(t);
 		return -1;
-	}	
+	}
 
 	*out = (git_transport *) t;
 	return 0;
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index a4046ee..dd9b5e0 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -45,7 +45,7 @@ int git_smart__store_refs(transport_smart *t, int flushes)
 			error = GIT_EBUFS;
 
 		if (error < 0 && error != GIT_EBUFS)
-			return -1;
+			return error;
 
 		if (error == GIT_EBUFS) {
 			if ((recvd = gitno_recv(buf)) < 0)
@@ -209,12 +209,13 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 	git_strarray refs;
 	unsigned int i;
 	git_reference *ref;
+	int error;
 
-	if (git_reference_list(&refs, repo) < 0)
-		return -1;
+	if ((error = git_reference_list(&refs, repo)) < 0)
+		return error;
 
-	if (git_revwalk_new(&walk, repo) < 0)
-		return -1;
+	if ((error = git_revwalk_new(&walk, repo)) < 0)
+		return error;
 
 	git_revwalk_sorting(walk, GIT_SORT_TIME);
 
@@ -223,13 +224,13 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 		if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
 			continue;
 
-		if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0)
+		if ((error = git_reference_lookup(&ref, repo, refs.strings[i])) < 0)
 			goto on_error;
 
 		if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
 			continue;
 
-		if (git_revwalk_push(walk, git_reference_target(ref)) < 0)
+		if ((error = git_revwalk_push(walk, git_reference_target(ref))) < 0)
 			goto on_error;
 
 		git_reference_free(ref);
@@ -242,7 +243,7 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 on_error:
 	git_reference_free(ref);
 	git_strarray_free(&refs);
-	return -1;
+	return error;
 }
 
 static int wait_while_ack(gitno_buffer *buf)
@@ -503,7 +504,7 @@ int git_smart__download_pack(
 	}
 
 	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
-		((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) < 0))
+		((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0))
 		goto done;
 
 	/*
@@ -539,11 +540,9 @@ int git_smart__download_pack(
 		if (pkt->type == GIT_PKT_PROGRESS) {
 			if (t->progress_cb) {
 				git_pkt_progress *p = (git_pkt_progress *) pkt;
-				if (t->progress_cb(p->data, p->len, t->message_cb_payload)) {
-					giterr_clear();
-					error = GIT_EUSER;
+				error = t->progress_cb(p->data, p->len, t->message_cb_payload);
+				if (error)
 					goto done;
-				}
 			}
 			git__free(pkt);
 		} else if (pkt->type == GIT_PKT_DATA) {
@@ -551,7 +550,7 @@ int git_smart__download_pack(
 			error = writepack->append(writepack, p->data, p->len, stats);
 
 			git__free(pkt);
-			if (error < 0)
+			if (error != 0)
 				goto done;
 		} else if (pkt->type == GIT_PKT_FLUSH) {
 			/* A flush indicates the end of the packfile */
@@ -564,17 +563,15 @@ int git_smart__download_pack(
 	 * Trailing execution of progress_cb, if necessary...
 	 * Only the callback through the npp datastructure currently
 	 * updates the last_fired_bytes value. It is possible that
-	 * progress has already been reported with the correct 
+	 * progress has already been reported with the correct
 	 * "received_bytes" value, but until (if?) this is unified
 	 * then we will report progress again to be sure that the
 	 * correct last received_bytes value is reported.
 	 */
 	if (npp.callback && npp.stats->received_bytes > npp.last_fired_bytes) {
-		if (npp.callback(npp.stats, npp.payload) < 0) {
-			giterr_clear();
-			error = GIT_EUSER;
+		error = npp.callback(npp.stats, npp.payload);
+		if (error != 0)
 			goto done;
-		}
 	}
 
 	error = writepack->commit(writepack, stats);
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 673cd0f..e47e19c 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -667,9 +667,11 @@ replay:
 			if (allowed_types &&
 				(!t->cred || 0 == (t->cred->credtype & allowed_types))) {
 
-				if (t->owner->cred_acquire_cb(&t->cred, t->owner->url, t->connection_data.user, allowed_types, 
-					t->owner->cred_acquire_payload) < 0)
-					return GIT_EUSER;
+				int error = t->owner->cred_acquire_cb(
+					&t->cred, t->owner->url, t->connection_data.user,
+					allowed_types, t->owner->cred_acquire_payload);
+				if (error < 0)
+					return error;
 
 				assert(t->cred);
 
diff --git a/src/tree.c b/src/tree.c
index bb59ff8..4d77ff7 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -884,22 +884,22 @@ static int tree_walk(
 	git_vector_foreach(&tree->entries, i, entry) {
 		if (preorder) {
 			error = callback(path->ptr, entry, payload);
-			if (error > 0) {
+			if (error < 0) { /* negative value stops iteration */
+				giterr_set_after_callback_function(error, "git_tree_walk");
+				break;
+			}
+			if (error > 0) { /* positive value skips this entry */
 				error = 0;
 				continue;
 			}
-			if (error < 0) {
-				giterr_clear();
-				return GIT_EUSER;
-			}
 		}
 
 		if (git_tree_entry__is_tree(entry)) {
 			git_tree *subtree;
 			size_t path_len = git_buf_len(path);
 
-			if ((error = git_tree_lookup(
-				&subtree, tree->object.repo, &entry->oid)) < 0)
+			error = git_tree_lookup(&subtree, tree->object.repo, &entry->oid);
+			if (error < 0)
 				break;
 
 			/* append the next entry to the path */
@@ -907,21 +907,24 @@ static int tree_walk(
 			git_buf_putc(path, '/');
 
 			if (git_buf_oom(path))
-				return -1;
+				error = -1;
+			else
+				error = tree_walk(subtree, callback, path, payload, preorder);
 
-			error = tree_walk(subtree, callback, path, payload, preorder);
 			git_tree_free(subtree);
-
 			if (error != 0)
 				break;
 
 			git_buf_truncate(path, path_len);
 		}
 
-		if (!preorder && callback(path->ptr, entry, payload) < 0) {
-			giterr_clear();
-			error = GIT_EUSER;
-			break;
+		if (!preorder) {
+			error = callback(path->ptr, entry, payload);
+			if (error < 0) { /* negative value stops iteration */
+				giterr_set_after_callback_function(error, "git_tree_walk");
+				break;
+			}
+			error = 0;
 		}
 	}
 
diff --git a/src/unix/map.c b/src/unix/map.c
index 7de99c9..e62ab3e 100644
--- a/src/unix/map.c
+++ b/src/unix/map.c
@@ -6,7 +6,7 @@
  */
 #include <git2/common.h>
 
-#ifndef GIT_WIN32
+#if !defined(GIT_WIN32) && !defined(NO_MMAP)
 
 #include "map.h"
 #include <sys/mman.h>
diff --git a/src/vector.c b/src/vector.c
index 362e7b0..050e032 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -77,6 +77,20 @@ void git_vector_free(git_vector *v)
 	v->_alloc_size = 0;
 }
 
+void git_vector_free_deep(git_vector *v)
+{
+	size_t i;
+
+	assert(v);
+
+	for (i = 0; i < v->length; ++i) {
+		git__free(v->contents[i]);
+		v->contents[i] = NULL;
+	}
+
+	git_vector_free(v);
+}
+
 int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp)
 {
 	assert(v);
@@ -90,6 +104,22 @@ int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp)
 	return resize_vector(v, max(initial_size, MIN_ALLOCSIZE));
 }
 
+void **git_vector_detach(size_t *size, size_t *asize, git_vector *v)
+{
+	void **data = v->contents;
+
+	if (size)
+		*size = v->length;
+	if (asize)
+		*asize = v->_alloc_size;
+
+	v->_alloc_size = 0;
+	v->length   = 0;
+	v->contents = NULL;
+
+	return data;
+}
+
 int git_vector_insert(git_vector *v, void *element)
 {
 	assert(v);
diff --git a/src/vector.h b/src/vector.h
index 279f5c6..d318463 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -23,10 +23,13 @@ typedef struct git_vector {
 
 int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
 void git_vector_free(git_vector *v);
+void git_vector_free_deep(git_vector *v); /* free each entry and self */
 void git_vector_clear(git_vector *v);
 int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
 void git_vector_swap(git_vector *a, git_vector *b);
 
+void **git_vector_detach(size_t *size, size_t *asize, git_vector *v);
+
 void git_vector_sort(git_vector *v);
 
 /** Linear search for matching entry using internal comparison function */
diff --git a/src/win32/map.c b/src/win32/map.c
index 44c6c4e..902ea39 100644
--- a/src/win32/map.c
+++ b/src/win32/map.c
@@ -8,6 +8,7 @@
 #include "map.h"
 #include <errno.h>
 
+#ifndef NO_MMAP
 
 static DWORD get_page_size(void)
 {
@@ -112,4 +113,4 @@ int p_munmap(git_map *map)
 	return error;
 }
 
-
+#endif
diff --git a/tests/attr/repo.c b/tests/attr/repo.c
index ef2ad5c..f9ba585 100644
--- a/tests/attr/repo.c
+++ b/tests/attr/repo.c
@@ -129,6 +129,8 @@ static int count_attrs(
 	return 0;
 }
 
+#define CANCEL_VALUE 12345
+
 static int cancel_iteration(
 	const char *name,
 	const char *value,
@@ -140,7 +142,7 @@ static int cancel_iteration(
 	*((int *)payload) -= 1;
 
 	if (*((int *)payload) < 0)
-		return -1;
+		return CANCEL_VALUE;
 
 	return 0;
 }
@@ -166,7 +168,7 @@ void test_attr_repo__foreach(void)
 
 	count = 2;
 	cl_assert_equal_i(
-		GIT_EUSER, git_attr_foreach(
+		CANCEL_VALUE, git_attr_foreach(
 			g_repo, 0, "sub/subdir_test1", &cancel_iteration, &count)
 	);
 }
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index 66b01bc..d2e92f8 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -486,6 +486,84 @@ void test_checkout_tree__donot_update_deleted_file_by_default(void)
 	git_index_free(index);
 }
 
+struct checkout_cancel_at {
+	const char *filename;
+	int error;
+	int count;
+};
+
+static int checkout_cancel_cb(
+	git_checkout_notify_t why,
+	const char *path,
+	const git_diff_file *b,
+	const git_diff_file *t,
+	const git_diff_file *w,
+	void *payload)
+{
+	struct checkout_cancel_at *ca = payload;
+
+	GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
+
+	ca->count++;
+
+	if (!strcmp(path, ca->filename))
+		return ca->error;
+
+	return 0;
+}
+
+void test_checkout_tree__can_cancel_checkout_from_notify(void)
+{
+	struct checkout_cancel_at ca;
+	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+	git_oid oid;
+	git_object *obj = NULL;
+
+	assert_on_branch(g_repo, "master");
+
+	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
+	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
+
+	ca.filename = "new.txt";
+	ca.error = -5555;
+	ca.count = 0;
+
+	opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
+	opts.notify_cb = checkout_cancel_cb;
+	opts.notify_payload = &ca;
+	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+
+	cl_assert(!git_path_exists("testrepo/new.txt"));
+
+	cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555);
+
+	cl_assert(!git_path_exists("testrepo/new.txt"));
+
+	/* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
+	/* on case-sensitive FS   = README, then above */
+
+	if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
+		cl_assert_equal_i(3, ca.count);
+	else
+		cl_assert_equal_i(4, ca.count);
+
+	/* and again with a different stopping point and return code */
+	ca.filename = "README";
+	ca.error = 123;
+	ca.count = 0;
+
+	cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123);
+
+	cl_assert(!git_path_exists("testrepo/new.txt"));
+
+	if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
+		cl_assert_equal_i(4, ca.count);
+	else
+		cl_assert_equal_i(1, ca.count);
+
+	git_object_free(obj);
+}
+
 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
 {
 	git_index *index = NULL;
diff --git a/tests/clone/nonetwork.c b/tests/clone/nonetwork.c
index a286e2a..3cd5fb7 100644
--- a/tests/clone/nonetwork.c
+++ b/tests/clone/nonetwork.c
@@ -22,7 +22,7 @@ void test_clone_nonetwork__initialize(void)
 	memset(&g_options, 0, sizeof(git_clone_options));
 	g_options.version = GIT_CLONE_OPTIONS_VERSION;
 	g_options.checkout_opts = dummy_opts;
-	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
 	g_options.remote_callbacks = dummy_callbacks;
 }
 
@@ -151,6 +151,61 @@ void test_clone_nonetwork__can_checkout_given_branch(void)
 
 	cl_git_pass(git_repository_head(&g_ref, g_repo));
 	cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
+
+	cl_assert(git_path_exists("foo/readme.txt"));
+}
+
+static int clone_cancel_fetch_transfer_progress_cb(
+	const git_transfer_progress *stats, void *data)
+{
+	GIT_UNUSED(stats); GIT_UNUSED(data);
+	return -54321;
+}
+
+void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
+{
+	g_options.checkout_branch = "test";
+
+	g_options.remote_callbacks.transfer_progress =
+		clone_cancel_fetch_transfer_progress_cb;
+
+	cl_git_fail_with(git_clone(
+		&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
+		-54321);
+
+	cl_assert(!g_repo);
+	cl_assert(!git_path_exists("foo/readme.txt"));
+}
+
+static int clone_cancel_checkout_cb(
+	git_checkout_notify_t why,
+	const char *path,
+	const git_diff_file *b,
+	const git_diff_file *t,
+	const git_diff_file *w,
+	void *payload)
+{
+	const char *at_file = payload;
+	GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
+	if (!strcmp(path, at_file))
+		return -12345;
+	return 0;
+}
+
+void test_clone_nonetwork__can_cancel_clone_in_checkout(void)
+{
+	g_options.checkout_branch = "test";
+
+	g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
+	g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb;
+	g_options.checkout_opts.notify_payload = "readme.txt";
+
+	cl_git_fail_with(git_clone(
+		&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
+		-12345);
+
+	cl_assert(!g_repo);
+	cl_assert(!git_path_exists("foo/readme.txt"));
 }
 
 void test_clone_nonetwork__can_detached_head(void)
diff --git a/tests/config/read.c b/tests/config/read.c
index abc088d..2567272 100644
--- a/tests/config/read.c
+++ b/tests/config/read.c
@@ -247,7 +247,7 @@ void test_config_read__foreach(void)
 
 	count = 3;
 	cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
-	cl_assert_equal_i(GIT_EUSER, ret);
+	cl_assert_equal_i(-100, ret);
 
 	git_config_free(cfg);
 }
diff --git a/tests/config/rename.c b/tests/config/rename.c
new file mode 100644
index 0000000..db07c79
--- /dev/null
+++ b/tests/config/rename.c
@@ -0,0 +1,85 @@
+#include "clar_libgit2.h"
+#include "config.h"
+
+static git_repository *g_repo = NULL;
+static git_config *g_config = NULL;
+
+void test_config_rename__initialize(void)
+{
+    g_repo = cl_git_sandbox_init("testrepo.git");
+	cl_git_pass(git_repository_config(&g_config, g_repo));
+}
+
+void test_config_rename__cleanup(void)
+{
+	git_config_free(g_config);
+	g_config = NULL;
+
+	cl_git_sandbox_cleanup();
+	g_repo = NULL;
+}
+
+void test_config_rename__can_rename(void)
+{
+	const git_config_entry *ce;
+
+	cl_git_pass(git_config_get_entry(
+		&ce, g_config, "branch.track-local.remote"));
+	cl_assert_equal_s(".", ce->value);
+
+	cl_git_fail(git_config_get_entry(
+		&ce, g_config, "branch.local-track.remote"));
+
+	cl_git_pass(git_config_rename_section(
+		g_repo, "branch.track-local", "branch.local-track"));
+
+	cl_git_pass(git_config_get_entry(
+		&ce, g_config, "branch.local-track.remote"));
+	cl_assert_equal_s(".", ce->value);
+
+	cl_git_fail(git_config_get_entry(
+		&ce, g_config, "branch.track-local.remote"));
+}
+
+void test_config_rename__prevent_overwrite(void)
+{
+	const git_config_entry *ce;
+
+	cl_git_pass(git_config_set_string(
+		g_config, "branch.local-track.remote", "yellow"));
+
+	cl_git_pass(git_config_get_entry(
+		&ce, g_config, "branch.local-track.remote"));
+	cl_assert_equal_s("yellow", ce->value);
+
+	cl_git_pass(git_config_rename_section(
+		g_repo, "branch.track-local", "branch.local-track"));
+
+	cl_git_pass(git_config_get_entry(
+		&ce, g_config, "branch.local-track.remote"));
+	cl_assert_equal_s(".", ce->value);
+
+	/* so, we don't currently prevent overwrite... */
+	/* {
+		const git_error *err;
+		cl_assert((err = giterr_last()) != NULL);
+		cl_assert(err->message != NULL);
+	} */
+}
+
+static void assert_invalid_config_section_name(
+	git_repository *repo, const char *name)
+{
+	cl_git_fail_with(
+		git_config_rename_section(repo, "branch.remoteless", name),
+		GIT_EINVALIDSPEC);
+}
+
+void test_config_rename__require_a_valid_new_name(void)
+{
+	assert_invalid_config_section_name(g_repo, "");
+	assert_invalid_config_section_name(g_repo, "bra\nch");
+	assert_invalid_config_section_name(g_repo, "branc#");
+	assert_invalid_config_section_name(g_repo, "bra\nch.duh");
+	assert_invalid_config_section_name(g_repo, "branc#.duh");
+}
diff --git a/tests/config/validkeyname.c b/tests/config/validkeyname.c
index 3369973..0ef4a9a 100644
--- a/tests/config/validkeyname.c
+++ b/tests/config/validkeyname.c
@@ -46,23 +46,3 @@ void test_config_validkeyname__accessing_requires_a_valid_name(void)
 	assert_invalid_config_key_name("dif.dir\nstat.lines");
 	assert_invalid_config_key_name("dif.dirstat.li\nes");
 }
-
-static void assert_invalid_config_section_name(git_repository *repo, const char *name)
-{
-	cl_git_fail_with(git_config_rename_section(repo, "branch.remoteless", name), GIT_EINVALIDSPEC);
-}
-
-void test_config_validkeyname__renaming_a_section_requires_a_valid_name(void)
-{
-	git_repository *repo;
-
-	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
-
-	assert_invalid_config_section_name(repo, "");
-	assert_invalid_config_section_name(repo, "bra\nch");
-	assert_invalid_config_section_name(repo, "branc#");
-	assert_invalid_config_section_name(repo, "bra\nch.duh");
-	assert_invalid_config_section_name(repo, "branc#.duh");
-
-	git_repository_free(repo);
-}
diff --git a/tests/config/write.c b/tests/config/write.c
index 15f750d..922d755 100644
--- a/tests/config/write.c
+++ b/tests/config/write.c
@@ -303,3 +303,4 @@ void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void)
 
 	git_config_free(cfg);
 }
+
diff --git a/tests/core/path.c b/tests/core/path.c
index cf2d5e9..471491b 100644
--- a/tests/core/path.c
+++ b/tests/core/path.c
@@ -350,15 +350,26 @@ void test_core_path__10_fromurl(void)
 
 typedef struct {
 	int expect_idx;
+	int cancel_after;
 	char **expect;
 } check_walkup_info;
 
+#define CANCEL_VALUE 1234
+
 static int check_one_walkup_step(void *ref, git_buf *path)
 {
 	check_walkup_info *info = (check_walkup_info *)ref;
+
+	if (!info->cancel_after) {
+		cl_assert_equal_s(info->expect[info->expect_idx], "[CANCEL]");
+		return CANCEL_VALUE;
+	}
+	info->cancel_after--;
+
 	cl_assert(info->expect[info->expect_idx] != NULL);
 	cl_assert_equal_s(info->expect[info->expect_idx], path->ptr);
 	info->expect_idx++;
+
 	return 0;
 }
 
@@ -381,6 +392,7 @@ void test_core_path__11_walkup(void)
 	check_walkup_info info;
 
 	info.expect = expect;
+	info.cancel_after = -1;
 
 	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
 
@@ -400,6 +412,42 @@ void test_core_path__11_walkup(void)
 	git_buf_free(&p);
 }
 
+void test_core_path__11a_walkup_cancel(void)
+{
+	git_buf p = GIT_BUF_INIT;
+	int cancel[] = { 3, 2, 1, 0 };
+	char *expect[] = {
+		"/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "[CANCEL]", NULL,
+		"/a/b/c/d/e", "/a/b/c/d/", "[CANCEL]", NULL,
+		"/a/b/c/d/e", "[CANCEL]", NULL,
+		"[CANCEL]", NULL,
+		NULL
+	};
+	char *root[] = { NULL, NULL, "/", "", NULL };
+	int i, j;
+	check_walkup_info info;
+
+	info.expect = expect;
+
+	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
+
+		git_buf_sets(&p, expect[i]);
+
+		info.cancel_after = cancel[j];
+		info.expect_idx = i;
+
+		cl_assert_equal_i(
+			CANCEL_VALUE,
+			git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
+		);
+
+		/* skip to next run of expectations */
+		while (expect[i] != NULL) i++;
+	}
+
+	git_buf_free(&p);
+}
+
 void test_core_path__12_offset_to_path_root(void)
 {
 	cl_assert(git_path_root("non/rooted/path") == -1);
diff --git a/tests/core/pool.c b/tests/core/pool.c
index 3073c4a..351d0c2 100644
--- a/tests/core/pool.c
+++ b/tests/core/pool.c
@@ -139,7 +139,8 @@ void test_core_pool__strndup_limit(void)
 	git_pool p;
 
 	cl_git_pass(git_pool_init(&p, 1, 100));
-	cl_assert(git_pool_strndup(&p, "foo", -1) == NULL);
+	/* ensure 64 bit doesn't overflow */
+	cl_assert(git_pool_strndup(&p, "foo", (size_t)-1) == NULL);
 	git_pool_clear(&p);
 }
 
diff --git a/tests/diff/index.c b/tests/diff/index.c
index 8f45671..21afe8d 100644
--- a/tests/diff/index.c
+++ b/tests/diff/index.c
@@ -128,9 +128,7 @@ void test_diff_index__1(void)
 	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
 
 	cl_assert_equal_i(
-		GIT_EUSER,
-		git_diff_foreach(diff, diff_stop_after_2_files, NULL, NULL, &exp)
-	);
+		1, git_diff_foreach(diff, diff_stop_after_2_files, NULL, NULL, &exp) );
 
 	cl_assert_equal_i(2, exp.files);
 
diff --git a/tests/diff/notify.c b/tests/diff/notify.c
index cc33cb7..da7390d 100644
--- a/tests/diff/notify.c
+++ b/tests/diff/notify.c
@@ -20,7 +20,7 @@ static int assert_called_notifications(
 {
 	bool found = false;
 	notify_expected *exp = (notify_expected*)payload;
-	notify_expected *e;;
+	notify_expected *e;
 
 	GIT_UNUSED(diff_so_far);
 
@@ -182,10 +182,12 @@ void test_diff_notify__notify_cb_can_abort_diff(void)
 	opts.pathspec.count   = 1;
 
 	pathspec = "file_deleted";
-	cl_git_fail(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
+	cl_git_fail_with(
+		git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
 
 	pathspec = "staged_changes_modified_file";
-	cl_git_fail(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
+	cl_git_fail_with(
+		git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
 }
 
 static int filter_all(
diff --git a/tests/diff/patch.c b/tests/diff/patch.c
index bd1598b..0cef3bd 100644
--- a/tests/diff/patch.c
+++ b/tests/diff/patch.c
@@ -30,8 +30,6 @@ static int check_removal_cb(
 	const git_diff_line *line,
 	void *payload)
 {
-	GIT_UNUSED(payload);
-
 	switch (line->origin) {
 	case GIT_DIFF_LINE_FILE_HDR:
 		cl_assert_equal_s(EXPECTED_HEADER, line->content);
@@ -40,10 +38,12 @@ static int check_removal_cb(
 
 	case GIT_DIFF_LINE_HUNK_HDR:
 		cl_assert_equal_s(EXPECTED_HUNK, line->content);
-		/* Fall through */
+		goto check_hunk;
 
 	case GIT_DIFF_LINE_CONTEXT:
 	case GIT_DIFF_LINE_DELETION:
+		if (payload != NULL)
+			return *(int *)payload;
 		goto check_hunk;
 
 	default:
@@ -101,6 +101,39 @@ void test_diff_patch__can_properly_display_the_removal_of_a_file(void)
 	git_tree_free(one);
 }
 
+void test_diff_patch__can_cancel_diff_print(void)
+{
+	const char *one_sha = "26a125e";
+	const char *another_sha = "735b6a2";
+	git_tree *one, *another;
+	git_diff *diff;
+	int fail_with;
+
+	g_repo = cl_git_sandbox_init("status");
+
+	one = resolve_commit_oid_to_tree(g_repo, one_sha);
+	another = resolve_commit_oid_to_tree(g_repo, another_sha);
+
+	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));
+
+	fail_with = -2323;
+
+	cl_git_fail_with(git_diff_print(
+		diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with),
+		fail_with);
+
+	fail_with = 45;
+
+	cl_git_fail_with(git_diff_print(
+		diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with),
+		fail_with);
+
+	git_diff_free(diff);
+
+	git_tree_free(another);
+	git_tree_free(one);
+}
+
 void test_diff_patch__to_string(void)
 {
 	const char *one_sha = "26a125e";
diff --git a/tests/diff/rename.c b/tests/diff/rename.c
index ca6d076..93e69f4 100644
--- a/tests/diff/rename.c
+++ b/tests/diff/rename.c
@@ -111,6 +111,28 @@ void test_diff_rename__match_oid(void)
 
 	git_diff_free(diff);
 
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	/* git diff --find-copies-harder -M100 -B100 \
+	 *          31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
+	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
+	 */
+	opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED |
+		GIT_DIFF_FIND_EXACT_MATCH_ONLY;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(3, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
+
+	git_diff_free(diff);
+
 	git_tree_free(old_tree);
 	git_tree_free(new_tree);
 }
diff --git a/tests/index/addall.c b/tests/index/addall.c
index 44c5127..4527337 100644
--- a/tests/index/addall.c
+++ b/tests/index/addall.c
@@ -4,6 +4,7 @@
 #include "fileops.h"
 
 git_repository *g_repo = NULL;
+#define TEST_DIR "addall"
 
 void test_index_addall__initialize(void)
 {
@@ -13,6 +14,8 @@ void test_index_addall__cleanup(void)
 {
 	git_repository_free(g_repo);
 	g_repo = NULL;
+
+	cl_fixture_cleanup(TEST_DIR);
 }
 
 #define STATUS_INDEX_FLAGS \
@@ -132,6 +135,25 @@ static void check_stat_data(git_index *index, const char *path, bool match)
 	}
 }
 
+static void addall_create_test_repo(bool check_every_step)
+{
+	cl_git_pass(git_repository_init(&g_repo, TEST_DIR, false));
+	if (check_every_step)
+		check_status(g_repo, 0, 0, 0, 0, 0, 0, 0);
+
+	cl_git_mkfile(TEST_DIR "/file.foo", "a file");
+	if (check_every_step)
+		check_status(g_repo, 0, 0, 0, 1, 0, 0, 0);
+
+	cl_git_mkfile(TEST_DIR "/.gitignore", "*.foo\n");
+	if (check_every_step)
+		check_status(g_repo, 0, 0, 0, 1, 0, 0, 1);
+
+	cl_git_mkfile(TEST_DIR "/file.bar", "another file");
+	if (check_every_step)
+		check_status(g_repo, 0, 0, 0, 2, 0, 0, 1);
+}
+
 void test_index_addall__repo_lifecycle(void)
 {
 	int error;
@@ -139,43 +161,33 @@ void test_index_addall__repo_lifecycle(void)
 	git_strarray paths = { NULL, 0 };
 	char *strs[1];
 
-	cl_git_pass(git_repository_init(&g_repo, "addall", false));
-	check_status(g_repo, 0, 0, 0, 0, 0, 0, 0);
+	addall_create_test_repo(true);
 
 	cl_git_pass(git_repository_index(&index, g_repo));
 
-	cl_git_mkfile("addall/file.foo", "a file");
-	check_status(g_repo, 0, 0, 0, 1, 0, 0, 0);
-
-	cl_git_mkfile("addall/.gitignore", "*.foo\n");
-	check_status(g_repo, 0, 0, 0, 1, 0, 0, 1);
-
-	cl_git_mkfile("addall/file.bar", "another file");
-	check_status(g_repo, 0, 0, 0, 2, 0, 0, 1);
-
 	strs[0] = "file.*";
 	paths.strings = strs;
 	paths.count   = 1;
 
 	cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
-	check_stat_data(index, "addall/file.bar", true);
+	check_stat_data(index, TEST_DIR "/file.bar", true);
 	check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
 
-	cl_git_rewritefile("addall/file.bar", "new content for file");
-	check_stat_data(index, "addall/file.bar", false);
+	cl_git_rewritefile(TEST_DIR "/file.bar", "new content for file");
+	check_stat_data(index, TEST_DIR "/file.bar", false);
 	check_status(g_repo, 1, 0, 0, 1, 0, 1, 1);
 
-	cl_git_mkfile("addall/file.zzz", "yet another one");
-	cl_git_mkfile("addall/other.zzz", "yet another one");
-	cl_git_mkfile("addall/more.zzz", "yet another one");
+	cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
+	cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
+	cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
 	check_status(g_repo, 1, 0, 0, 4, 0, 1, 1);
 
 	cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
-	check_stat_data(index, "addall/file.bar", true);
+	check_stat_data(index, TEST_DIR "/file.bar", true);
 	check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
 
 	cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
-	check_stat_data(index, "addall/file.zzz", true);
+	check_stat_data(index, TEST_DIR "/file.zzz", true);
 	check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
 
 	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
@@ -195,27 +207,27 @@ void test_index_addall__repo_lifecycle(void)
 	/* add with force - should allow */
 	cl_git_pass(git_index_add_all(
 		index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
-	check_stat_data(index, "addall/file.foo", true);
+	check_stat_data(index, TEST_DIR "/file.foo", true);
 	check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
 
 	/* now it's in the index, so regular add should work */
-	cl_git_rewritefile("addall/file.foo", "new content for file");
-	check_stat_data(index, "addall/file.foo", false);
+	cl_git_rewritefile(TEST_DIR "/file.foo", "new content for file");
+	check_stat_data(index, TEST_DIR "/file.foo", false);
 	check_status(g_repo, 1, 0, 0, 3, 0, 1, 0);
 
 	cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
-	check_stat_data(index, "addall/file.foo", true);
+	check_stat_data(index, TEST_DIR "/file.foo", true);
 	check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
 
 	cl_git_pass(git_index_add_bypath(index, "more.zzz"));
-	check_stat_data(index, "addall/more.zzz", true);
+	check_stat_data(index, TEST_DIR "/more.zzz", true);
 	check_status(g_repo, 2, 0, 0, 2, 0, 0, 0);
 
-	cl_git_rewritefile("addall/file.zzz", "new content for file");
+	cl_git_rewritefile(TEST_DIR "/file.zzz", "new content for file");
 	check_status(g_repo, 2, 0, 0, 2, 0, 1, 0);
 
 	cl_git_pass(git_index_add_bypath(index, "file.zzz"));
-	check_stat_data(index, "addall/file.zzz", true);
+	check_stat_data(index, TEST_DIR "/file.zzz", true);
 	check_status(g_repo, 2, 0, 1, 2, 0, 0, 0);
 
 	strs[0] = "*.zzz";
@@ -228,7 +240,7 @@ void test_index_addall__repo_lifecycle(void)
 	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
 	check_status(g_repo, 0, 0, 0, 3, 0, 0, 0);
 
-	cl_must_pass(p_unlink("addall/file.zzz"));
+	cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
 	check_status(g_repo, 0, 0, 0, 3, 1, 0, 0);
 
 	/* update_all should be able to remove entries */
@@ -240,9 +252,9 @@ void test_index_addall__repo_lifecycle(void)
 	check_status(g_repo, 3, 1, 0, 0, 0, 0, 0);
 
 	/* must be able to remove at any position while still updating other files */
-	cl_must_pass(p_unlink("addall/.gitignore"));
-	cl_git_rewritefile("addall/file.zzz", "reconstructed file");
-	cl_git_rewritefile("addall/more.zzz", "altered file reality");
+	cl_must_pass(p_unlink(TEST_DIR "/.gitignore"));
+	cl_git_rewritefile(TEST_DIR "/file.zzz", "reconstructed file");
+	cl_git_rewritefile(TEST_DIR "/more.zzz", "altered file reality");
 	check_status(g_repo, 3, 1, 0, 1, 1, 1, 0);
 
 	cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
@@ -256,3 +268,89 @@ void test_index_addall__repo_lifecycle(void)
 
 	git_index_free(index);
 }
+
+static int addall_match_prefix(
+	const char *path, const char *matched_pathspec, void *payload)
+{
+	GIT_UNUSED(matched_pathspec);
+	return !git__prefixcmp(path, payload) ? 0 : 1;
+}
+
+static int addall_match_suffix(
+	const char *path, const char *matched_pathspec, void *payload)
+{
+	GIT_UNUSED(matched_pathspec);
+	return !git__suffixcmp(path, payload) ? 0 : 1;
+}
+
+static int addall_cancel_at(
+	const char *path, const char *matched_pathspec, void *payload)
+{
+	GIT_UNUSED(matched_pathspec);
+	return !strcmp(path, payload) ? -123 : 0;
+}
+
+void test_index_addall__callback_filtering(void)
+{
+	git_index *index;
+
+	addall_create_test_repo(false);
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	cl_git_pass(
+		git_index_add_all(index, NULL, 0, addall_match_prefix, "file."));
+	check_stat_data(index, TEST_DIR "/file.bar", true);
+	check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
+
+	cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
+	cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
+	cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
+
+	cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
+	check_stat_data(index, TEST_DIR "/file.bar", true);
+	check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
+
+	cl_git_pass(
+		git_index_add_all(index, NULL, 0, addall_match_prefix, "other"));
+	check_stat_data(index, TEST_DIR "/other.zzz", true);
+	check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
+
+	cl_git_pass(
+		git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
+	check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
+
+	cl_git_pass(
+		git_index_remove_all(index, NULL, addall_match_suffix, ".zzz"));
+	check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
+
+	cl_git_fail_with(
+		git_index_add_all(index, NULL, 0, addall_cancel_at, "more.zzz"), -123);
+	check_status(g_repo, 3, 0, 0, 2, 0, 0, 1);
+
+	cl_git_fail_with(
+		git_index_add_all(index, NULL, 0, addall_cancel_at, "other.zzz"), -123);
+	check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
+
+	cl_git_pass(
+		git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
+	check_status(g_repo, 5, 0, 0, 0, 0, 0, 1);
+
+	cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
+	cl_must_pass(p_unlink(TEST_DIR "/more.zzz"));
+	cl_must_pass(p_unlink(TEST_DIR "/other.zzz"));
+
+	cl_git_fail_with(
+		git_index_update_all(index, NULL, addall_cancel_at, "more.zzz"), -123);
+	/* file.zzz removed from index (so Index Adds 5 -> 4) and
+	 * more.zzz + other.zzz removed (so Worktree Dels 0 -> 2) */
+	check_status(g_repo, 4, 0, 0, 0, 2, 0, 1);
+
+	cl_git_fail_with(
+		git_index_update_all(index, NULL, addall_cancel_at, "other.zzz"), -123);
+	/* more.zzz removed from index (so Index Adds 4 -> 3) and
+	 * Just other.zzz removed (so Worktree Dels 2 -> 1) */
+	check_status(g_repo, 3, 0, 0, 0, 1, 0, 1);
+
+	git_index_free(index);
+}
diff --git a/tests/notes/notes.c b/tests/notes/notes.c
index 82dcaf8..c2579a2 100644
--- a/tests/notes/notes.c
+++ b/tests/notes/notes.c
@@ -129,7 +129,7 @@ void test_notes_notes__can_cancel_foreach(void)
 	create_note(&note_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
 
 	cl_assert_equal_i(
-		GIT_EUSER,
+		1,
 		git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes",
 			note_cancel_cb, &retrieved_notes));
 }
diff --git a/tests/object/blob/fromchunks.c b/tests/object/blob/fromchunks.c
index 03ed4ef..b61cabf 100644
--- a/tests/object/blob/fromchunks.c
+++ b/tests/object/blob/fromchunks.c
@@ -59,7 +59,7 @@ void test_object_blob_fromchunks__doesnot_overwrite_an_already_existing_object(v
 	git_buf content = GIT_BUF_INIT;
 	git_oid expected_oid, oid;
 	int howmany = 7;
-	
+
 	cl_git_pass(git_oid_fromstr(&expected_oid, "321cbdf08803c744082332332838df6bd160f8f9"));
 
 	cl_git_pass(git_blob_create_fromchunks(&oid, repo, NULL, text_chunked_source_cb, &howmany));
@@ -117,3 +117,40 @@ void test_object_blob_fromchunks__creating_a_blob_from_chunks_honors_the_attribu
 	assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.txt");
 	assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.dunno");
 }
+
+static int failing_chunked_source_cb(
+	char *content, size_t max_length, void *payload)
+{
+	int *count = (int *)payload;
+
+	GIT_UNUSED(max_length);
+
+	(*count)--;
+	if (*count == 0)
+		return -1234;
+
+	strcpy(content, textual_content);
+	return (int)strlen(textual_content);
+}
+
+void test_object_blob_fromchunks__can_stop_with_error(void)
+{
+	git_oid expected_oid, oid;
+	git_object *blob;
+	int howmany = 7;
+
+	cl_git_pass(git_oid_fromstr(
+		&expected_oid, "321cbdf08803c744082332332838df6bd160f8f9"));
+
+	cl_git_fail_with(
+		git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY),
+		GIT_ENOTFOUND);
+
+	cl_git_fail_with(git_blob_create_fromchunks(
+		&oid, repo, NULL, failing_chunked_source_cb, &howmany), -1234);
+
+	cl_git_fail_with(
+		git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY),
+		GIT_ENOTFOUND);
+}
+
diff --git a/tests/object/tree/walk.c b/tests/object/tree/walk.c
index 1207e86..f8005e5 100644
--- a/tests/object/tree/walk.c
+++ b/tests/object/tree/walk.c
@@ -59,7 +59,7 @@ static int treewalk_stop_cb(
 
 	(*count) += 1;
 
-	return (*count == 2) ? -1 : 0;
+	return (*count == 2) ? -123 : 0;
 }
 
 static int treewalk_stop_immediately_cb(
@@ -83,20 +83,20 @@ void test_object_tree_walk__1(void)
 
 	ct = 0;
 	cl_assert_equal_i(
-		GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_stop_cb, &ct));
+		-123, git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_stop_cb, &ct));
 	cl_assert_equal_i(2, ct);
 
 	ct = 0;
 	cl_assert_equal_i(
-		GIT_EUSER, git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_stop_cb, &ct));
+		-123, git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_stop_cb, &ct));
 	cl_assert_equal_i(2, ct);
 
 	cl_assert_equal_i(
-		GIT_EUSER, git_tree_walk(
+		-100, git_tree_walk(
 			tree, GIT_TREEWALK_PRE, treewalk_stop_immediately_cb, NULL));
 
 	cl_assert_equal_i(
-		GIT_EUSER, git_tree_walk(
+		-100, git_tree_walk(
 			tree, GIT_TREEWALK_POST, treewalk_stop_immediately_cb, NULL));
 
 	git_tree_free(tree);
@@ -152,7 +152,7 @@ void test_object_tree_walk__2(void)
 	memset(&data, 0, sizeof(data));
 	data.stop = "3.txt";
 
-	cl_assert_equal_i(GIT_EUSER, git_tree_walk(
+	cl_assert_equal_i(-1, git_tree_walk(
 		tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
 	cl_assert_equal_i(3, data.files);
 	cl_assert_equal_i(2, data.dirs);
@@ -168,7 +168,7 @@ void test_object_tree_walk__2(void)
 	memset(&data, 0, sizeof(data));
 	data.stop = "new.txt";
 
-	cl_assert_equal_i(GIT_EUSER, git_tree_walk(
+	cl_assert_equal_i(-1, git_tree_walk(
 		tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
 	cl_assert_equal_i(7, data.files);
 	cl_assert_equal_i(4, data.dirs);
diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c
index 468c0cc..3bea0ed 100644
--- a/tests/object/tree/write.c
+++ b/tests/object/tree/write.c
@@ -164,24 +164,25 @@ void test_object_tree_write__sorted_subtrees(void)
 	git_treebuilder_free(builder);
 }
 
+static struct {
+	unsigned int attr;
+	const char *filename;
+} _entries[] = {
+	{ GIT_FILEMODE_BLOB, "aardvark" },
+	{ GIT_FILEMODE_BLOB, ".first" },
+	{ GIT_FILEMODE_BLOB, "apple" },
+	{ GIT_FILEMODE_BLOB, "last"},
+	{ GIT_FILEMODE_BLOB, "apple_after"},
+	{ GIT_FILEMODE_BLOB, "after_aardvark"},
+	{ 0, NULL },
+};
+
 void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
 {
 	git_treebuilder *builder;
-	int i,  aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
+	int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
 	git_oid blank_oid, tree_oid;
 	git_tree *tree;
-	struct {
-		unsigned int attr;
-		const char *filename;
-	} entries[] = {
-		{ GIT_FILEMODE_BLOB, "aardvark" },
-		{ GIT_FILEMODE_BLOB, ".first" },
-		{ GIT_FILEMODE_BLOB, "apple" },
-		{ GIT_FILEMODE_BLOB, "last"},
-		{ GIT_FILEMODE_BLOB, "apple_after"},
-		{ GIT_FILEMODE_BLOB, "after_aardvark"},
-		{ 0, NULL },
-	};
 
 	memset(&blank_oid, 0x0, sizeof(blank_oid));
 
@@ -189,9 +190,9 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
 
 	cl_assert_equal_i(0, (int)git_treebuilder_entrycount(builder));
 
-	for (i = 0; entries[i].filename; ++i)
+	for (i = 0; _entries[i].filename; ++i)
 		cl_git_pass(git_treebuilder_insert(NULL,
-			builder, entries[i].filename, &blank_oid, entries[i].attr));
+			builder, _entries[i].filename, &blank_oid, _entries[i].attr));
 
 	cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
 
@@ -260,3 +261,56 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
 
 	git_tree_free(tree);
 }
+
+static int treebuilder_filter_prefixed(
+	const git_tree_entry *entry, void *payload)
+{
+	return !git__prefixcmp(git_tree_entry_name(entry), payload);
+}
+
+void test_object_tree_write__filtering(void)
+{
+	git_treebuilder *builder;
+	int i;
+	git_oid blank_oid, tree_oid;
+	git_tree *tree;
+
+	memset(&blank_oid, 0x0, sizeof(blank_oid));
+
+	cl_git_pass(git_treebuilder_create(&builder, NULL));
+
+	for (i = 0; _entries[i].filename; ++i)
+		cl_git_pass(git_treebuilder_insert(NULL,
+			builder, _entries[i].filename, &blank_oid, _entries[i].attr));
+
+	cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
+
+	cl_assert(git_treebuilder_get(builder, "apple") != NULL);
+	cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
+	cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+	git_treebuilder_filter(builder, treebuilder_filter_prefixed, "apple");
+
+	cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder));
+
+	cl_assert(git_treebuilder_get(builder, "apple") == NULL);
+	cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
+	cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+	git_treebuilder_filter(builder, treebuilder_filter_prefixed, "a");
+
+	cl_assert_equal_i(2, (int)git_treebuilder_entrycount(builder));
+
+	cl_assert(git_treebuilder_get(builder, "aardvark") == NULL);
+	cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+	cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder));
+
+	git_treebuilder_free(builder);
+
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
+
+	cl_assert_equal_i(2, (int)git_tree_entrycount(tree));
+
+	git_tree_free(tree);
+}
diff --git a/tests/odb/foreach.c b/tests/odb/foreach.c
index ebb8866..256ae9c 100644
--- a/tests/odb/foreach.c
+++ b/tests/odb/foreach.c
@@ -5,7 +5,6 @@
 
 static git_odb *_odb;
 static git_repository *_repo;
-static int nobj;
 
 void test_odb_foreach__cleanup(void)
 {
@@ -18,10 +17,10 @@ void test_odb_foreach__cleanup(void)
 
 static int foreach_cb(const git_oid *oid, void *data)
 {
-	GIT_UNUSED(data);
-	GIT_UNUSED(oid);
+	int *nobj = data;
+	(*nobj)++;
 
-	nobj++;
+	GIT_UNUSED(oid);
 
 	return 0;
 }
@@ -38,43 +37,46 @@ static int foreach_cb(const git_oid *oid, void *data)
  */
 void test_odb_foreach__foreach(void)
 {
+	int nobj = 0;
+
 	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
 	git_repository_odb(&_odb, _repo);
 
-	cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL));
+	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
 	cl_assert_equal_i(47 + 1640, nobj); /* count + in-pack */
 }
 
 void test_odb_foreach__one_pack(void)
 {
 	git_odb_backend *backend = NULL;
+	int nobj = 0;
 
 	cl_git_pass(git_odb_new(&_odb));
 	cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
 	cl_git_pass(git_odb_add_backend(_odb, backend, 1));
 	_repo = NULL;
 
-	nobj = 0;
-	cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL));
+	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
 	cl_assert(nobj == 1628);
 }
 
 static int foreach_stop_cb(const git_oid *oid, void *data)
 {
-	GIT_UNUSED(data);
-	GIT_UNUSED(oid);
+	int *nobj = data;
+	(*nobj)++;
 
-	nobj++;
+	GIT_UNUSED(oid);
 
-	return (nobj == 1000);
+	return (*nobj == 1000) ? -321 : 0;
 }
 
 void test_odb_foreach__interrupt_foreach(void)
 {
-	nobj = 0;
+	int nobj = 0;
+
 	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
 	git_repository_odb(&_odb, _repo);
 
-	cl_assert_equal_i(GIT_EUSER, git_odb_foreach(_odb, foreach_stop_cb, NULL));
+	cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
 	cl_assert(nobj == 1000);
 }
diff --git a/tests/online/clone.c b/tests/online/clone.c
index efc76d9..be4421a 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -273,7 +273,7 @@ static int cancel_at_half(const git_transfer_progress *stats, void *payload)
 	GIT_UNUSED(payload);
 
 	if (stats->received_objects > (stats->total_objects/2))
-		return 1;
+		return 4321;
 	return 0;
 }
 
@@ -281,7 +281,8 @@ void test_online_clone__can_cancel(void)
 {
 	g_options.remote_callbacks.transfer_progress = cancel_at_half;
 
-	cl_git_fail_with(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), GIT_EUSER);
+	cl_git_fail_with(
+		git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
 }
 
 
diff --git a/tests/online/fetch.c b/tests/online/fetch.c
index 5153a7a..7e9dfdb 100644
--- a/tests/online/fetch.c
+++ b/tests/online/fetch.c
@@ -129,7 +129,7 @@ static int cancel_at_half(const git_transfer_progress *stats, void *payload)
 	GIT_UNUSED(payload);
 
 	if (stats->received_objects > (stats->total_objects/2))
-		return -1;
+		return -4321;
 	return 0;
 }
 
@@ -147,7 +147,7 @@ void test_online_fetch__can_cancel(void)
 	git_remote_set_callbacks(remote, &callbacks);
 
 	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
-	cl_git_fail_with(git_remote_download(remote), GIT_EUSER);
+	cl_git_fail_with(git_remote_download(remote), -4321);
 	git_remote_disconnect(remote);
 	git_remote_free(remote);
 }
diff --git a/tests/online/push.c b/tests/online/push.c
index be505c3..33f1746 100644
--- a/tests/online/push.c
+++ b/tests/online/push.c
@@ -207,6 +207,7 @@ static void verify_tracking_branches(git_remote *remote, expected_ref expected_r
 	}
 
 	cl_assert_equal_i(error, GIT_ITEROVER);
+	git_branch_iterator_free(iter);
 
 	/* Loop through expected refs, make sure they exist */
 	for (i = 0; i < expected_refs_len; i++) {
@@ -371,19 +372,25 @@ void test_online_push__cleanup(void)
 	cl_git_sandbox_cleanup();
 }
 
-static int push_pack_progress_cb(int stage, unsigned int current, unsigned int total, void* payload)
+static int push_pack_progress_cb(
+	int stage, unsigned int current, unsigned int total, void* payload)
 {
-	int *was_called = (int *) payload;
+	int *calls = (int *)payload;
 	GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
-	*was_called = 1;
+	if (*calls < 0)
+		return *calls;
+	(*calls)++;
 	return 0;
 }
 
-static int push_transfer_progress_cb(unsigned int current, unsigned int total, size_t bytes, void* payload)
+static int push_transfer_progress_cb(
+	unsigned int current, unsigned int total, size_t bytes, void* payload)
 {
-	int *was_called = (int *) payload;
+	int *calls = (int *)payload;
 	GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
-	*was_called = 1;
+	if (*calls < 0)
+		return *calls;
+	(*calls)++;
 	return 0;
 }
 
@@ -397,15 +404,16 @@ static int push_transfer_progress_cb(unsigned int current, unsigned int total, s
  * @param expected_ret expected return value from git_push_finish()
  * @param check_progress_cb Check that the push progress callbacks are called
  */
-static void do_push(const char *refspecs[], size_t refspecs_len,
+static void do_push(
+	const char *refspecs[], size_t refspecs_len,
 	push_status expected_statuses[], size_t expected_statuses_len,
-	expected_ref expected_refs[], size_t expected_refs_len, int expected_ret, int check_progress_cb)
+	expected_ref expected_refs[], size_t expected_refs_len,
+	int expected_ret, int check_progress_cb)
 {
 	git_push *push;
 	git_push_options opts = GIT_PUSH_OPTIONS_INIT;
 	size_t i;
-	int ret;
-	int pack_progress_called = 0, transfer_progress_called = 0;
+	int pack_progress_calls = 0, transfer_progress_calls = 0;
 
 	if (_remote) {
 		/* Auto-detect the number of threads to use */
@@ -416,30 +424,35 @@ static void do_push(const char *refspecs[], size_t refspecs_len,
 		cl_git_pass(git_push_new(&push, _remote));
 		cl_git_pass(git_push_set_options(push, &opts));
 
-		if (check_progress_cb)
-			cl_git_pass(git_push_set_callbacks(push, push_pack_progress_cb, &pack_progress_called, push_transfer_progress_cb, &transfer_progress_called));
+		if (check_progress_cb) {
+			/* if EUSER, then abort in transfer */
+			if (expected_ret == GIT_EUSER)
+				transfer_progress_calls = GIT_EUSER;
+
+			cl_git_pass(
+				git_push_set_callbacks(
+					push, push_pack_progress_cb, &pack_progress_calls,
+					push_transfer_progress_cb, &transfer_progress_calls));
+		}
 
 		for (i = 0; i < refspecs_len; i++)
 			cl_git_pass(git_push_add_refspec(push, refspecs[i]));
 
 		if (expected_ret < 0) {
-			cl_git_fail(ret = git_push_finish(push));
+			cl_git_fail_with(git_push_finish(push), expected_ret);
 			cl_assert_equal_i(0, git_push_unpack_ok(push));
-		}
-		else {
-			cl_git_pass(ret = git_push_finish(push));
+		} else {
+			cl_git_pass(git_push_finish(push));
 			cl_assert_equal_i(1, git_push_unpack_ok(push));
 		}
 
-		if (check_progress_cb) {
-			cl_assert_equal_i(1, pack_progress_called);
-			cl_assert_equal_i(1, transfer_progress_called);
+		if (check_progress_cb && !expected_ret) {
+			cl_assert(pack_progress_calls > 0);
+			cl_assert(transfer_progress_calls > 0);
 		}
 
 		do_verify_push_status(push, expected_statuses, expected_statuses_len);
 
-		cl_assert_equal_i(expected_ret, ret);
-
 		verify_refs(_remote, expected_refs, expected_refs_len);
 
 		cl_git_pass(git_push_update_tips(push));
@@ -507,6 +520,12 @@ void test_online_push__b5(void)
 		exp_refs, ARRAY_SIZE(exp_refs), 0, 1);
 }
 
+void test_online_push__b5_cancel(void)
+{
+	const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
+	do_push(specs, ARRAY_SIZE(specs), NULL, 0, NULL, 0, GIT_EUSER, 1);
+}
+
 void test_online_push__multi(void)
 {
 	const char *specs[] = {
@@ -731,7 +750,7 @@ void test_online_push__bad_refspecs(void)
 	git_push *push;
 
 	if (_remote) {
-//		cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH));
+/*		cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); */
 		cl_git_pass(git_push_new(&push, _remote));
 
 		/* Unexpanded branch names not supported */
diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c
index 07963a9..084f8e6 100644
--- a/tests/pack/indexer.c
+++ b/tests/pack/indexer.c
@@ -11,7 +11,7 @@
  * This is a packfile with three objects. The second is a delta which
  * depends on the third, which is also a delta.
  */
-unsigned char out_of_order_pack[] = {
+static const unsigned char out_of_order_pack[] = {
   0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
   0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
   0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
@@ -23,13 +23,13 @@ unsigned char out_of_order_pack[] = {
   0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92,
   0x6f, 0xae, 0x66, 0x75
 };
-unsigned int out_of_order_pack_len = 112;
+static const unsigned int out_of_order_pack_len = 112;
 
 /*
  * Packfile with two objects. The second is a delta against an object
  * which is not in the packfile
  */
-unsigned char thin_pack[] = {
+static const unsigned char thin_pack[] = {
   0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
   0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
   0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
@@ -38,18 +38,19 @@ unsigned char thin_pack[] = {
   0x3a, 0x6f, 0x39, 0xd1, 0xfe, 0x66, 0x68, 0x6b, 0xa5, 0xe5, 0xe2, 0x97,
   0xac, 0x94, 0x6c, 0x76, 0x0b, 0x04
 };
-unsigned int thin_pack_len = 78;
+static const unsigned int thin_pack_len = 78;
 
-unsigned char base_obj[] = { 07, 076 };
-unsigned int base_obj_len = 2;
+static const unsigned char base_obj[] = { 07, 076 };
+static const unsigned int base_obj_len = 2;
 
 void test_pack_indexer__out_of_order(void)
 {
-	git_indexer *idx;
-	git_transfer_progress stats;
+	git_indexer *idx = 0;
+	git_transfer_progress stats = { 0 };
 
 	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
-	cl_git_pass(git_indexer_append(idx, out_of_order_pack, out_of_order_pack_len, &stats));
+	cl_git_pass(git_indexer_append(
+		idx, out_of_order_pack, out_of_order_pack_len, &stats));
 	cl_git_pass(git_indexer_commit(idx, &stats));
 
 	cl_assert_equal_i(stats.total_objects, 3);
@@ -61,8 +62,8 @@ void test_pack_indexer__out_of_order(void)
 
 void test_pack_indexer__fix_thin(void)
 {
-	git_indexer *idx;
-	git_transfer_progress stats;
+	git_indexer *idx = NULL;
+	git_transfer_progress stats = { 0 };
 	git_repository *repo;
 	git_odb *odb;
 	git_oid id, should_id;
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 1ae2322..53db818 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -12,6 +12,7 @@ static git_packbuilder *_packbuilder;
 static git_indexer *_indexer;
 static git_vector _commits;
 static int _commits_is_initialized;
+static git_transfer_progress _stats;
 
 void test_pack_packbuilder__initialize(void)
 {
@@ -20,6 +21,7 @@ void test_pack_packbuilder__initialize(void)
 	cl_git_pass(git_packbuilder_new(&_packbuilder, _repo));
 	cl_git_pass(git_vector_init(&_commits, 0, NULL));
 	_commits_is_initialized = 1;
+	memset(&_stats, 0, sizeof(_stats));
 }
 
 void test_pack_packbuilder__cleanup(void)
@@ -184,11 +186,10 @@ void test_pack_packbuilder__permissions_readwrite(void)
 	test_write_pack_permission(0666, 0666);
 }
 
-static git_transfer_progress stats;
 static int foreach_cb(void *buf, size_t len, void *payload)
 {
 	git_indexer *idx = (git_indexer *) payload;
-	cl_git_pass(git_indexer_append(idx, buf, len, &stats));
+	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
 	return 0;
 }
 
@@ -199,6 +200,24 @@ void test_pack_packbuilder__foreach(void)
 	seed_packbuilder();
 	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
 	cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx));
-	cl_git_pass(git_indexer_commit(idx, &stats));
+	cl_git_pass(git_indexer_commit(idx, &_stats));
+	git_indexer_free(idx);
+}
+
+static int foreach_cancel_cb(void *buf, size_t len, void *payload)
+{
+	git_indexer *idx = (git_indexer *)payload;
+	cl_git_pass(git_indexer_append(idx, buf, len, &_stats));
+	return (_stats.total_objects > 2) ? -1111 : 0;
+}
+
+void test_pack_packbuilder__foreach_with_cancel(void)
+{
+	git_indexer *idx;
+
+	seed_packbuilder();
+	cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
+	cl_git_fail_with(
+		git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111);
 	git_indexer_free(idx);
 }
diff --git a/tests/refs/branches/move.c b/tests/refs/branches/move.c
index ecf14e0..9d233de 100644
--- a/tests/refs/branches/move.c
+++ b/tests/refs/branches/move.c
@@ -66,12 +66,54 @@ void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_n
 void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
 {
 	git_reference *original_ref, *new_ref;
+	git_config *config;
+	const git_config_entry *ce;
+	char *original_remote, *original_merge;
+
+	cl_git_pass(git_repository_config(&config, repo));
+
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+	original_remote = strdup(ce->value);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+	original_merge  = strdup(ce->value);
+
 
 	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
 
-	cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0));
+	cl_assert_equal_i(GIT_EEXISTS,
+		git_branch_move(&new_ref, original_ref, "master", 0));
+
+	cl_assert(giterr_last()->message != NULL);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+	cl_assert_equal_s(original_remote, ce->value);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+	cl_assert_equal_s(original_merge,  ce->value);
+
+
+	cl_assert_equal_i(GIT_EEXISTS,
+		git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
+
+	cl_assert(giterr_last()->message != NULL);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+	cl_assert_equal_s(original_remote, ce->value);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+	cl_assert_equal_s(original_merge,  ce->value);
+
+	git_reference_free(original_ref);
+	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
+
+	cl_assert_equal_i(GIT_EEXISTS,
+		git_branch_move(&new_ref, original_ref, "master", 0));
+
+	cl_assert(giterr_last()->message != NULL);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+	cl_assert_equal_s(original_remote, ce->value);
+	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+	cl_assert_equal_s(original_merge,  ce->value);
 
+	free(original_remote); free(original_merge);
 	git_reference_free(original_ref);
+	git_config_free(config);
 }
 
 void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
diff --git a/tests/refs/foreachglob.c b/tests/refs/foreachglob.c
index 2c45808..c0f6ce7 100644
--- a/tests/refs/foreachglob.c
+++ b/tests/refs/foreachglob.c
@@ -81,14 +81,14 @@ static int interrupt_cb(const char *reference_name, void *payload)
 
 	(*count)++;
 
-	return (*count == 11);
+	return (*count == 11) ? -1000 : 0;
 }
 
 void test_refs_foreachglob__can_cancel(void)
 {
 	int count = 0;
 
-	cl_assert_equal_i(GIT_EUSER, git_reference_foreach_glob(
+	cl_assert_equal_i(-1000, git_reference_foreach_glob(
 		repo, "*", interrupt_cb, &count) );
 
 	cl_assert_equal_i(11, count);
diff --git a/tests/refs/iterator.c b/tests/refs/iterator.c
index 266410f..a29b0cf 100644
--- a/tests/refs/iterator.c
+++ b/tests/refs/iterator.c
@@ -46,36 +46,43 @@ static int refcmp_cb(const void *a, const void *b)
 	return strcmp(refa->name, refb->name);
 }
 
+static void assert_all_refnames_match(git_vector *output)
+{
+	size_t i;
+	git_reference *ref;
+
+	cl_assert_equal_sz(output->length, ARRAY_SIZE(refnames));
+
+	git_vector_sort(output);
+
+	git_vector_foreach(output, i, ref) {
+		cl_assert_equal_s(ref->name, refnames[i]);
+		git_reference_free(ref);
+	}
+
+	git_vector_free(output);
+}
+
 void test_refs_iterator__list(void)
 {
 	git_reference_iterator *iter;
 	git_vector output;
 	git_reference *ref;
-	int error;
-	size_t i;
 
 	cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
 	cl_git_pass(git_reference_iterator_new(&iter, repo));
 
-	do {
-		error = git_reference_next(&ref, iter);
-		cl_assert(error == 0 || error == GIT_ITEROVER);
-		if (error != GIT_ITEROVER) {
-			cl_git_pass(git_vector_insert(&output, ref));
-		}
-	} while (!error);
+	while (1) {
+		int error = git_reference_next(&ref, iter);
+		if (error == GIT_ITEROVER)
+			break;
+		cl_git_pass(error);
+		cl_git_pass(git_vector_insert(&output, ref));
+	}
 
 	git_reference_iterator_free(iter);
-	cl_assert_equal_sz(output.length, ARRAY_SIZE(refnames));
 
-	git_vector_sort(&output);
-
-	git_vector_foreach(&output, i, ref) {
-		cl_assert_equal_s(ref->name, refnames[i]);
-		git_reference_free(ref);
-	}
-
-	git_vector_free(&output);
+	assert_all_refnames_match(&output);
 }
 
 void test_refs_iterator__empty(void)
@@ -95,3 +102,87 @@ void test_refs_iterator__empty(void)
 	git_odb_free(odb);
 	git_repository_free(empty);
 }
+
+static int refs_foreach_cb(git_reference *reference, void *payload)
+{
+	git_vector *output = payload;
+	cl_git_pass(git_vector_insert(output, reference));
+	return 0;
+}
+
+void test_refs_iterator__foreach(void)
+{
+	git_vector output;
+	cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
+	cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
+	assert_all_refnames_match(&output);
+}
+
+static int refs_foreach_cancel_cb(git_reference *reference, void *payload)
+{
+	int *cancel_after = payload;
+
+	git_reference_free(reference);
+
+	if (!*cancel_after)
+		return -333;
+	(*cancel_after)--;
+	return 0;
+}
+
+void test_refs_iterator__foreach_can_cancel(void)
+{
+	int cancel_after = 3;
+	cl_git_fail_with(
+		git_reference_foreach(repo, refs_foreach_cancel_cb, &cancel_after),
+		-333);
+	cl_assert_equal_i(0, cancel_after);
+}
+
+static int refs_foreach_name_cb(const char *name, void *payload)
+{
+	git_vector *output = payload;
+	cl_git_pass(git_vector_insert(output, git__strdup(name)));
+	return 0;
+}
+
+void test_refs_iterator__foreach_name(void)
+{
+	git_vector output;
+	size_t i;
+	char *name;
+
+	cl_git_pass(git_vector_init(&output, 32, &git__strcmp_cb));
+	cl_git_pass(
+		git_reference_foreach_name(repo, refs_foreach_name_cb, &output));
+
+	cl_assert_equal_sz(output.length, ARRAY_SIZE(refnames));
+	git_vector_sort(&output);
+
+	git_vector_foreach(&output, i, name) {
+		cl_assert_equal_s(name, refnames[i]);
+		git__free(name);
+	}
+
+	git_vector_free(&output);
+}
+
+static int refs_foreach_name_cancel_cb(const char *name, void *payload)
+{
+	int *cancel_after = payload;
+	if (!*cancel_after)
+		return -333;
+	GIT_UNUSED(name);
+	(*cancel_after)--;
+	return 0;
+}
+
+void test_refs_iterator__foreach_name_can_cancel(void)
+{
+	int cancel_after = 5;
+	cl_git_fail_with(
+		git_reference_foreach_name(
+			repo, refs_foreach_name_cancel_cb, &cancel_after),
+		-333);
+	cl_assert_equal_i(0, cancel_after);
+}
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index 34be6d3..fd57fcc 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -552,7 +552,7 @@ static int cb_status__interrupt(const char *p, unsigned int s, void *payload)
 
 	(*count)++;
 
-	return (*count == 8);
+	return (*count == 8) ? -111 : 0;
 }
 
 void test_status_worktree__interruptable_foreach(void)
@@ -561,7 +561,7 @@ void test_status_worktree__interruptable_foreach(void)
 	git_repository *repo = cl_git_sandbox_init("status");
 
 	cl_assert_equal_i(
-		GIT_EUSER, git_status_foreach(repo, cb_status__interrupt, &count)
+		-111, git_status_foreach(repo, cb_status__interrupt, &count)
 	);
 
 	cl_assert_equal_i(8, count);
diff --git a/tests/valgrind-supp-mac.txt b/tests/valgrind-supp-mac.txt
index 99833d0..0cdc975 100644
--- a/tests/valgrind-supp-mac.txt
+++ b/tests/valgrind-supp-mac.txt
@@ -103,14 +103,6 @@
 	fun:ssl23_connect
 }
 {
-	mac-ssl-uninitialized-4
-	Memcheck:Param
-	...
-	obj:/usr/lib/libcrypto.0.9.8.dylib
-	...
-	fun:ssl23_connect
-}
-{
 	mac-ssl-leak-1
 	Memcheck:Leak
 	...