Commit dda708e78f3c3f43d814d46c29ab9f2b9d47ed5c

Vicent Martí 2012-03-09T19:55:50

error-handling: On-disk config file backend Includes: - Proper error reporting when encountering syntax errors in a config file (file, line number, column). - Rewritten `config_write`, now with 99% less goto-spaghetti - Error state in `git_filebuf`: filebuf write functions no longer need to be checked for error returns. If any of the writes performed on a buffer fail, the last call to `git_filebuf_commit` or `git_filebuf_hash` will fail accordingly and set the appropiate error message. Baller!

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
diff --git a/include/git2/errors.h b/include/git2/errors.h
index bc420d1..085dd52 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -127,14 +127,9 @@ typedef enum {
 	GITERR_ZLIB,
 	GITERR_REPOSITORY,
 	GITERR_CONFIG,
+	GITERR_REGEX,
 } git_error_class;
 
-#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
-
-GIT_EXTERN(void) giterr_set_oom(void);
-GIT_EXTERN(void) giterr_set(int error_class, const char *string, ...);
-GIT_EXTERN(void) giterr_clear(void);
-
 /**
  * Return a detailed error string with the latest error
  * that occurred in the library.
diff --git a/src/common.h b/src/common.h
index 0b4dc2e..30757de 100644
--- a/src/common.h
+++ b/src/common.h
@@ -46,6 +46,8 @@
 #include "thread-utils.h"
 #include "bswap.h"
 
+#include <regex.h>
+
 extern void git___throw(const char *, ...) GIT_FORMAT_PRINTF(1, 2);
 #define git__throw(error, ...) \
 	(git___throw(__VA_ARGS__), error)
@@ -54,6 +56,16 @@ extern void git___rethrow(const char *, ...) GIT_FORMAT_PRINTF(1, 2);
 #define git__rethrow(error, ...) \
 	(git___rethrow(__VA_ARGS__), error)
 
+
+#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
+
+void giterr_set_oom(void);
+void giterr_set(int error_class, const char *string, ...);
+void giterr_clear(void);
+void giterr_set_str(int error_class, const char *string);
+void giterr_set_regex(const regex_t *regex, int error_code);
+
+
 #include "util.h"
 
 
diff --git a/src/config.c b/src/config.c
index 850c9b1..77598d6 100644
--- a/src/config.c
+++ b/src/config.c
@@ -401,13 +401,15 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out)
 		if (ret == 0)
 			return 0;
 
+		/* File backend doesn't set error message on variable
+		 * not found */
 		if (ret == GIT_ENOTFOUND)
 			continue;
 
 		return ret;
 	}
 
-	giterr_set(GITERR_CONFIG, "Config value '%s' not found", name);
+	giterr_set(GITERR_CONFIG, "Config variable '%s' not found", name);
 	return GIT_ENOTFOUND;
 }
 
diff --git a/src/config_file.c b/src/config_file.c
index 3c7c593..077e2c0 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -86,6 +86,12 @@ static int config_parse(diskfile_backend *cfg_file);
 static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value);
 static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char *value);
 
+static void set_parse_error(diskfile_backend *backend, int col, const char *error_str)
+{
+	giterr_set(GITERR_CONFIG, "Failed to parse config file: %s (in %s:%d, column %d)",
+		error_str, backend->file_path, backend->reader.line_number, col);
+}
+
 static void cvar_free(cvar_t *var)
 {
 	if (var == NULL)
@@ -104,15 +110,16 @@ static int normalize_name(const char *in, char **out)
 	assert(in && out);
 
 	name = git__strdup(in);
-	if (name == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(name);
 
 	fdot = strchr(name, '.');
 	ldot = strrchr(name, '.');
 
 	if (fdot == NULL || ldot == NULL) {
 		git__free(name);
-		return git__throw(GIT_EINVALIDARGS, "Bad format. No dot in '%s'", in);
+		giterr_set(GITERR_CONFIG,
+			"Invalid variable name: '%s'", in);
+		return -1;
 	}
 
 	/* Downcase up to the first dot and after the last one */
@@ -120,7 +127,7 @@ static int normalize_name(const char *in, char **out)
 	git__strtolower(ldot);
 
 	*out = name;
-	return GIT_SUCCESS;
+	return 0;
 }
 
 static void free_vars(git_hashtable *values)
@@ -143,37 +150,28 @@ static void free_vars(git_hashtable *values)
 
 static int config_open(git_config_file *cfg)
 {
-	int error;
+	int res;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 
 	b->values = git_hashtable_alloc (20, git_hash__strhash_cb, git_hash__strcmp_cb);
-	if (b->values == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(b->values);
 
 	git_buf_init(&b->reader.buffer, 0);
-	error = git_futils_readbuffer(&b->reader.buffer, b->file_path);
+	res = git_futils_readbuffer(&b->reader.buffer, b->file_path);
 
 	/* It's fine if the file doesn't exist */
-	if (error == GIT_ENOTFOUND)
-		return GIT_SUCCESS;
-
-	if (error < GIT_SUCCESS)
-		goto cleanup;
-
-	error = config_parse(b);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
-
-	git_buf_free(&b->reader.buffer);
-
-	return GIT_SUCCESS;
+	if (res == GIT_ENOTFOUND)
+		return 0;
+
+	if (res < 0 || config_parse(b) <  0) {
+		free_vars(b->values);
+		b->values = NULL;
+		git_buf_free(&b->reader.buffer);
+		return -1;
+	}
 
- cleanup:
-	free_vars(b->values);
-	b->values = NULL;
 	git_buf_free(&b->reader.buffer);
-
-	return git__rethrow(error, "Failed to open config");
+	return 0;
 }
 
 static void backend_free(git_config_file *_backend)
@@ -184,42 +182,37 @@ static void backend_free(git_config_file *_backend)
 		return;
 
 	git__free(backend->file_path);
-
 	free_vars(backend->values);
-
 	git__free(backend);
 }
 
 static int file_foreach(git_config_file *backend, int (*fn)(const char *, const char *, void *), void *data)
 {
-	int ret = GIT_SUCCESS;
-	cvar_t *var;
 	diskfile_backend *b = (diskfile_backend *)backend;
+	cvar_t *var;
 	const char *key;
 
 	GIT_HASHTABLE_FOREACH(b->values, key, var,
-	      do {
-		      ret = fn(key, var->value, data);
-		      if (ret)
-			      break;
-		      var = CVAR_LIST_NEXT(var);
-	      } while (var != NULL);
-	)
+		do {
+			if (fn(key, var->value, data) < 0)
+				break;
 
+			var = CVAR_LIST_NEXT(var);
+		} while (var != NULL);
+	)
 
-	return ret;
+	return 0;
 }
 
 static int config_set(git_config_file *cfg, const char *name, const char *value)
 {
 	cvar_t *var = NULL;
 	cvar_t *existing = NULL, *old_value = NULL;
-	int error = GIT_SUCCESS;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	char *key;
 
-	if ((error = normalize_name(name, &key)) < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to normalize variable name '%s'", name);
+	if (normalize_name(name, &key) < 0)
+		return -1;
 
 	/*
 	 * Try to find it in the existing values and update it if it
@@ -227,15 +220,18 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
 	 */
 	existing = git_hashtable_lookup(b->values, key);
 	if (existing != NULL) {
-		char *tmp;
+		char *tmp = NULL;
 
 		git__free(key);
-		if (existing->next != NULL)
-			return git__throw(GIT_EINVALIDARGS, "Multivar incompatible with simple set");
+		if (existing->next != NULL) {
+			giterr_set(GITERR_CONFIG, "Multivar incompatible with simple set");
+			return -1;
+		}
 
-		tmp = value ? git__strdup(value) : NULL;
-		if (tmp == NULL && value != NULL)
-			return GIT_ENOMEM;
+		if (value) {
+			tmp = git__strdup(value);
+			GITERR_CHECK_ALLOC(tmp);
+		}
 
 		git__free(existing->value);
 		existing->value = tmp;
@@ -244,32 +240,29 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
 	}
 
 	var = git__malloc(sizeof(cvar_t));
-	if (var == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(var);
 
 	memset(var, 0x0, sizeof(cvar_t));
 
 	var->key = key;
+	var->value = NULL;
 
-	var->value = value ? git__strdup(value) : NULL;
-	if (var->value == NULL && value != NULL) {
-		error = GIT_ENOMEM;
-		goto out;
+	if (value) {
+		var->value = git__strdup(value);
+		GITERR_CHECK_ALLOC(var->value);
 	}
 
-	error = git_hashtable_insert2(b->values, key, var, (void **)&old_value);
-	if (error < GIT_SUCCESS)
-		goto out;
+	if (git_hashtable_insert2(b->values, key, var, (void **)&old_value) < 0)
+		return -1;
 
 	cvar_free(old_value);
 
-	error = config_write(b, key, NULL, value);
-
- out:
-	if (error < GIT_SUCCESS)
+	if (config_write(b, key, NULL, value) < 0) {
 		cvar_free(var);
+		return -1;
+	}
 
-	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to set config value");
+	return 0;
 }
 
 /*
@@ -278,171 +271,172 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
 static int config_get(git_config_file *cfg, const char *name, const char **out)
 {
 	cvar_t *var;
-	int error = GIT_SUCCESS;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	char *key;
 
-	if ((error = normalize_name(name, &key)) < GIT_SUCCESS)
-		return error;
+	if (normalize_name(name, &key) < 0)
+		return -1;
 
 	var = git_hashtable_lookup(b->values, key);
 	git__free(key);
 
+	/* no error message; the config system will write one */
 	if (var == NULL)
-		return git__throw(GIT_ENOTFOUND, "Variable '%s' not found", name);
+		return GIT_ENOTFOUND;
 
 	*out = var->value;
-
-	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to get config value for %s", name);
+	return 0;
 }
 
-static int config_get_multivar(git_config_file *cfg, const char *name, const char *regexp, int (*fn)(const char *, void *), void *data)
+static int config_get_multivar(
+	git_config_file *cfg,
+	const char *name,
+	const char *regex_str,
+	int (*fn)(const char *, void *),
+	void *data)
 {
 	cvar_t *var;
-	int error = GIT_SUCCESS;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	char *key;
-	regex_t preg;
 
-	if ((error = normalize_name(name, &key)) < GIT_SUCCESS)
-		return error;
+	if (normalize_name(name, &key) < 0)
+		return -1;
 
 	var = git_hashtable_lookup(b->values, key);
 	git__free(key);
 
 	if (var == NULL)
-		return git__throw(GIT_ENOTFOUND, "Variable '%s' not found", name);
+		return GIT_ENOTFOUND;
 
-	if (regexp != NULL) {
-		error = regcomp(&preg, regexp, REG_EXTENDED);
-		if (error < 0)
-			return git__throw(GIT_EINVALIDARGS, "Failed to compile regex");
-	}
+	if (regex_str != NULL) {
+		regex_t regex;
+		int result;
 
-	do {
-		if (regexp == NULL || !regexec(&preg, var->value, 0, NULL, 0)) {
-			error = fn(var->value, data);
-			if (error < GIT_SUCCESS)
-				goto exit;
+		/* regex matching; build the regex */
+		result = regcomp(&regex, regex_str, REG_EXTENDED);
+		if (result < 0) {
+			giterr_set_regex(&regex, result);
+			return -1;
 		}
 
-		var = var->next;
-	} while (var != NULL);
+		/* and throw the callback only on the variables that
+		 * match the regex */
+		do {
+			if (regexec(&regex, var->value, 0, NULL, 0) == 0) {
+				/* early termination by the user is not an error;
+				 * just break and return successfully */
+				if (fn(var->value, data) < 0)
+					break;
+			}
 
- exit:
-	if (regexp != NULL)
-		regfree(&preg);
-	return error;
+			var = var->next;
+		} while (var != NULL);
+	} else {
+		/* no regex; go through all the variables */
+		do {
+			/* early termination by the user is not an error;
+			 * just break and return successfully */
+			if (fn(var->value, data) < 0)
+				break;
+
+			var = var->next;
+		} while (var != NULL);
+	}
+
+	return 0;
 }
 
 static int config_set_multivar(git_config_file *cfg, const char *name, const char *regexp, const char *value)
 {
-	int error, replaced = 0;
+	int replaced = 0;
 	cvar_t *var, *newvar;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	char *key;
 	regex_t preg;
+	int result;
 
-	if (regexp == NULL)
-		return git__throw(GIT_EINVALIDARGS, "No regex supplied");
+	assert(regexp);
 
-	if ((error = normalize_name(name, &key)) < GIT_SUCCESS)
-		return error;
+	if (normalize_name(name, &key) < 0)
+		return -1;
 
 	var = git_hashtable_lookup(b->values, key);
-	if (var == NULL) {
-		free(key);
-		return git__throw(GIT_ENOTFOUND, "Variable '%s' not found", name);
-	}
+	if (var == NULL)
+		return GIT_ENOTFOUND;
 
-	error = regcomp(&preg, regexp, REG_EXTENDED);
-	if (error < 0) {
+	result = regcomp(&preg, regexp, REG_EXTENDED);
+	if (result < 0) {
 		free(key);
-		return git__throw(GIT_EINVALIDARGS, "Failed to compile regex");
+		giterr_set_regex(&preg, result);
+		return -1;
 	}
 
-	do {
-		if (!regexec(&preg, var->value, 0, NULL, 0)) {
+	for (;;) {
+		if (regexec(&preg, var->value, 0, NULL, 0) == 0) {
 			char *tmp = git__strdup(value);
-			if (tmp == NULL) {
-				error = GIT_ENOMEM;
-				goto exit;
-			}
+			GITERR_CHECK_ALLOC(tmp);
 
 			free(var->value);
 			var->value = tmp;
 			replaced = 1;
 		}
 
-		if (var->next != NULL)
-			var = var->next;
-		else
+		if (var->next == NULL)
 			break;
-	} while (var != NULL);
+
+		var = var->next;
+	}
 
 	/* If we've reached the end of the variables and we haven't found it yet, we need to append it */
 	if (!replaced) {
 		newvar = git__malloc(sizeof(cvar_t));
-		if (newvar == NULL) {
-			error = GIT_ENOMEM;
-			goto exit;
-		}
+		GITERR_CHECK_ALLOC(newvar);
 
 		memset(newvar, 0x0, sizeof(cvar_t));
+
 		newvar->key = git__strdup(var->key);
-		if (newvar->key == NULL) {
-			error = GIT_ENOMEM;
-			goto exit;
-		}
+		GITERR_CHECK_ALLOC(newvar->key);
+
 		newvar->value = git__strdup(value);
-		if (newvar->value == NULL) {
-			error = GIT_ENOMEM;
-			goto exit;
-		}
+		GITERR_CHECK_ALLOC(newvar->value);
 
 		var->next = newvar;
 	}
 
-	error = config_write(b, key, &preg, value);
-	if (error < GIT_SUCCESS) {
-		error = git__rethrow(error, "Failed to update value in file");
-		goto exit;
-	}
+	result = config_write(b, key, &preg, value);
 
- exit:
 	free(key);
 	regfree(&preg);
-	return error;
+
+	return result;
 }
 
 static int config_delete(git_config_file *cfg, const char *name)
 {
-	int error;
-	const cvar_t *var;
-	cvar_t *old_value;
+	cvar_t *var;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	char *key;
+	int result;
 
-	if ((error = normalize_name(name, &key)) < GIT_SUCCESS)
-		return error;
+	if (normalize_name(name, &key) < 0)
+		return -1;
 
 	var = git_hashtable_lookup(b->values, key);
 	free(key);
 
 	if (var == NULL)
-		return git__throw(GIT_ENOTFOUND, "Variable '%s' not found", name);
-
-	if (var->next != NULL)
-		return git__throw(GIT_EINVALIDARGS, "Multivar incompatible with simple delete");
+		return GIT_ENOTFOUND;
 
+	if (var->next != NULL) {
+		giterr_set(GITERR_CONFIG, "Cannot delete multivar with a single delete");
+		return -1;
+	}
 
-	if ((error = git_hashtable_remove2(b->values, var->key, (void **)&old_value)) < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to remove %s from hashtable", key);
-
-	error = config_write(b, var->key, NULL, NULL);
-	cvar_free(old_value);
+	git_hashtable_remove(b->values, var->key);
+	result = config_write(b, var->key, NULL, NULL);
 
-	return error;
+	cvar_free(var);
+	return result;
 }
 
 int git_config_file__ondisk(git_config_file **out, const char *path)
@@ -450,16 +444,12 @@ int git_config_file__ondisk(git_config_file **out, const char *path)
 	diskfile_backend *backend;
 
 	backend = git__malloc(sizeof(diskfile_backend));
-	if (backend == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(backend);
 
 	memset(backend, 0x0, sizeof(diskfile_backend));
 
 	backend->file_path = git__strdup(path);
-	if (backend->file_path == NULL) {
-		git__free(backend);
-		return GIT_ENOMEM;
-	}
+	GITERR_CHECK_ALLOC(backend->file_path);
 
 	backend->parent.open = config_open;
 	backend->parent.get = config_get;
@@ -472,7 +462,7 @@ int git_config_file__ondisk(git_config_file **out, const char *path)
 
 	*out = (git_config_file *)backend;
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 static int cfg_getchar_raw(diskfile_backend *cfg)
@@ -621,12 +611,11 @@ GIT_INLINE(int) config_keychar(int c)
 	return isalnum(c) || c == '-';
 }
 
-static int parse_section_header_ext(const char *line, const char *base_name, char **section_name)
+static int parse_section_header_ext(diskfile_backend *cfg, const char *line, const char *base_name, char **section_name)
 {
 	int c, rpos;
 	char *first_quote, *last_quote;
 	git_buf buf = GIT_BUF_INIT;
-	int error = GIT_SUCCESS;
 	int quote_marks;
 	/*
 	 * base_name is what came before the space. We should be at the
@@ -637,8 +626,10 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
 	first_quote = strchr(line, '"');
 	last_quote = strrchr(line, '"');
 
-	if (last_quote - first_quote == 0)
-		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. There is no final quotation mark");
+	if (last_quote - first_quote == 0) {
+		set_parse_error(cfg, 0, "Missing closing quotation mark in section header");
+		return -1;
+	}
 
 	git_buf_grow(&buf, strlen(base_name) + last_quote - first_quote + 2);
 	git_buf_printf(&buf, "%s.", base_name);
@@ -655,27 +646,30 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
 	 */
 	do {
 		if (quote_marks == 2) {
-			puts("too many marks");
-			error = git__throw(GIT_EOBJCORRUPTED, "Falied to parse ext header. Text after closing quote");
-			goto out;
-
+			set_parse_error(cfg, rpos, "Unexpected text after closing quotes");
+			git_buf_free(&buf);
+			return -1;
 		}
 
 		switch (c) {
 		case '"':
 			++quote_marks;
 			continue;
+
 		case '\\':
 			c = line[rpos++];
+
 			switch (c) {
 			case '"':
 			case '\\':
 				break;
+
 			default:
-				error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Unsupported escape char \\%c", c);
-				goto out;
+				set_parse_error(cfg, rpos, "Unsupported escape sequence");
+				git_buf_free(&buf);
+				return -1;
 			}
-			break;
+
 		default:
 			break;
 		}
@@ -683,61 +677,53 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
 		git_buf_putc(&buf, c);
 	} while ((c = line[rpos++]) != ']');
 
-	*section_name = git__strdup(git_buf_cstr(&buf));
- out:
-	git_buf_free(&buf);
-
-	return error;
+	*section_name = git_buf_detach(&buf);
+	return 0;
 }
 
 static int parse_section_header(diskfile_backend *cfg, char **section_out)
 {
 	char *name, *name_end;
 	int name_length, c, pos;
-	int error = GIT_SUCCESS;
+	int result;
 	char *line;
 
 	line = cfg_readline(cfg);
 	if (line == NULL)
-		return GIT_ENOMEM;
+		return -1;
 
 	/* find the end of the variable's name */
 	name_end = strchr(line, ']');
 	if (name_end == NULL) {
 		git__free(line);
-		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Can't find header name end");
+		set_parse_error(cfg, 0, "Missing ']' in section header");
+		return -1;
 	}
 
 	name = (char *)git__malloc((size_t)(name_end - line) + 1);
-	if (name == NULL) {
-		git__free(line);
-		return GIT_ENOMEM;
-	}
+	GITERR_CHECK_ALLOC(name);
 
 	name_length = 0;
 	pos = 0;
 
 	/* Make sure we were given a section header */
 	c = line[pos++];
-	if (c != '[') {
-		error = git__throw(GIT_ERROR, "Failed to parse header. Didn't get section header. This is a bug");
-		goto error;
-	}
+	assert(c == '[');
 
 	c = line[pos++];
 
 	do {
 		if (isspace(c)){
 			name[name_length] = '\0';
-			error = parse_section_header_ext(line, name, section_out);
+			result = parse_section_header_ext(cfg, line, name, section_out);
 			git__free(line);
 			git__free(name);
-			return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse header");
+			return result;
 		}
 
 		if (!config_keychar(c) && c != '.') {
-			error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Wrong format on header");
-			goto error;
+			set_parse_error(cfg, pos, "Unexpected character in header");
+			goto fail_parse;
 		}
 
 		name[name_length++] = (char) tolower(c);
@@ -745,20 +731,21 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
 	} while ((c = line[pos++]) != ']');
 
 	if (line[pos - 1] != ']') {
-		error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Config file ended unexpectedly");
-		goto error;
+		set_parse_error(cfg, pos, "Unexpected end of file");
+		goto fail_parse;
 	}
 
-	name[name_length] = 0;
 	git__free(line);
-	git__strtolower(name);
+
+	name[name_length] = 0;
 	*section_out = name;
-	return GIT_SUCCESS;
 
-error:
+	return 0;
+
+fail_parse:
 	git__free(line);
 	git__free(name);
-	return error;
+	return -1;
 }
 
 static int skip_bom(diskfile_backend *cfg)
@@ -766,7 +753,7 @@ static int skip_bom(diskfile_backend *cfg)
 	static const char utf8_bom[] = "\xef\xbb\xbf";
 
 	if (cfg->reader.buffer.size < sizeof(utf8_bom))
-		return GIT_SUCCESS;
+		return 0;
 
 	if (memcmp(cfg->reader.read_ptr, utf8_bom, sizeof(utf8_bom)) == 0)
 		cfg->reader.read_ptr += sizeof(utf8_bom);
@@ -775,7 +762,7 @@ static int skip_bom(diskfile_backend *cfg)
 		shit with the BoM
 	*/
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 /*
@@ -839,12 +826,13 @@ static void strip_comments(char *line)
 
 static int config_parse(diskfile_backend *cfg_file)
 {
-	int error = GIT_SUCCESS, c;
+	int c;
 	char *current_section = NULL;
 	char *var_name;
 	char *var_value;
 	cvar_t *var, *existing;
 	git_buf buf = GIT_BUF_INIT;
+	int result = 0;
 
 	/* Initialize the reading position */
 	cfg_file->reader.read_ptr = cfg_file->reader.buffer.ptr;
@@ -852,11 +840,11 @@ static int config_parse(diskfile_backend *cfg_file)
 
 	/* If the file is empty, there's nothing for us to do */
 	if (*cfg_file->reader.read_ptr == '\0')
-		return GIT_SUCCESS;
+		return 0;
 
 	skip_bom(cfg_file);
 
-	while (error == GIT_SUCCESS && !cfg_file->reader.eof) {
+	while (result == 0 && !cfg_file->reader.eof) {
 
 		c = cfg_peek(cfg_file, SKIP_WHITESPACE);
 
@@ -868,7 +856,7 @@ static int config_parse(diskfile_backend *cfg_file)
 		case '[': /* section header, new section begins */
 			git__free(current_section);
 			current_section = NULL;
-			error = parse_section_header(cfg_file, &current_section);
+			result = parse_section_header(cfg_file, &current_section);
 			break;
 
 		case ';':
@@ -877,16 +865,12 @@ static int config_parse(diskfile_backend *cfg_file)
 			break;
 
 		default: /* assume variable declaration */
-			error = parse_variable(cfg_file, &var_name, &var_value);
-
-			if (error < GIT_SUCCESS)
+			result = parse_variable(cfg_file, &var_name, &var_value);
+			if (result < 0)
 				break;
 
 			var = git__malloc(sizeof(cvar_t));
-			if (var == NULL) {
-				error = GIT_ENOMEM;
-				break;
-			}
+			GITERR_CHECK_ALLOC(var);
 
 			memset(var, 0x0, sizeof(cvar_t));
 
@@ -894,10 +878,8 @@ static int config_parse(diskfile_backend *cfg_file)
 			git_buf_printf(&buf, "%s.%s", current_section, var_name);
 			git__free(var_name);
 
-			if (git_buf_oom(&buf)) {
-				error = GIT_ENOMEM;
-				break;
-			}
+			if (git_buf_oom(&buf))
+				return -1;
 
 			var->key = git_buf_detach(&buf);
 			var->value = var_value;
@@ -905,7 +887,7 @@ static int config_parse(diskfile_backend *cfg_file)
 			/* Add or append the new config option */
 			existing = git_hashtable_lookup(cfg_file->values, var->key);
 			if (existing == NULL) {
-				error = git_hashtable_insert(cfg_file->values, var->key, var);
+				result = git_hashtable_insert(cfg_file->values, var->key, var);
 			} else {
 				while (existing->next != NULL) {
 					existing = existing->next;
@@ -918,13 +900,12 @@ static int config_parse(diskfile_backend *cfg_file)
 	}
 
 	git__free(current_section);
-
-	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse config");
+	return result;
 }
 
 static int write_section(git_filebuf *file, const char *key)
 {
-	int error;
+	int result;
 	const char *fdot, *ldot;
 	git_buf buf = GIT_BUF_INIT;
 
@@ -943,13 +924,14 @@ static int write_section(git_filebuf *file, const char *key)
 		git_buf_putc(&buf, '"');
 	}
 	git_buf_puts(&buf, "]\n");
+
 	if (git_buf_oom(&buf))
-		return GIT_ENOMEM;
+		return -1;
 
-	error = git_filebuf_write(file, git_buf_cstr(&buf), buf.size);
+	result = git_filebuf_write(file, git_buf_cstr(&buf), buf.size);
 	git_buf_free(&buf);
 
-	return error;
+	return result;
 }
 
 /*
@@ -957,50 +939,45 @@ static int write_section(git_filebuf *file, const char *key)
  */
 static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char* value)
 {
-	int error = GIT_SUCCESS, c;
-	int section_matches = 0, last_section_matched = 0, preg_replaced = 0;
+	int result, c;
+	int section_matches = 0, last_section_matched = 0, preg_replaced = 0, write_trailer = 0;
+	const char *pre_end = NULL, *post_start = NULL, *data_start;
 	char *current_section = NULL, *section, *name, *ldot;
-	char *var_name, *var_value;
 	git_filebuf file = GIT_FILEBUF_INIT;
-	const char *pre_end = NULL, *post_start = NULL, *data_start;
 
 	/* We need to read in our own config file */
-	error = git_futils_readbuffer(&cfg->reader.buffer, cfg->file_path);
-	if (error < GIT_SUCCESS && error != GIT_ENOTFOUND) {
-		return git__rethrow(error, "Failed to read existing config file %s", cfg->file_path);
-	}
+	result = git_futils_readbuffer(&cfg->reader.buffer, cfg->file_path);
 
 	/* Initialise the reading position */
-	if (error == GIT_ENOTFOUND) {
-		error = GIT_SUCCESS;
+	if (result == GIT_ENOTFOUND) {
 		cfg->reader.read_ptr = NULL;
 		cfg->reader.eof = 1;
 		data_start = NULL;
 		git_buf_clear(&cfg->reader.buffer);
-	} else {
+	} else if (result == 0) {
 		cfg->reader.read_ptr = cfg->reader.buffer.ptr;
 		cfg->reader.eof = 0;
 		data_start = cfg->reader.read_ptr;
+	} else {
+		return -1; /* OS error when reading the file */
 	}
 
 	/* Lock the file */
-	error = git_filebuf_open(&file, cfg->file_path, 0);
-	if (error < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to lock config file");
+	if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
+		return -1;
 
 	skip_bom(cfg);
 	ldot = strrchr(key, '.');
 	name = ldot + 1;
 	section = git__strndup(key, ldot - key);
 
-	while (error == GIT_SUCCESS && !cfg->reader.eof) {
+	while (!cfg->reader.eof) {
 		c = cfg_peek(cfg, SKIP_WHITESPACE);
 
-		switch (c) {
-		case '\0': /* We've arrived at the end of the file */
+		if (c == '\0') { /* We've arrived at the end of the file */
 			break;
 
-		case '[': /* section header, new section begins */
+		} else if (c == '[') { /* section header, new section begins */
 			/*
 			 * We set both positions to the current one in case we
 			 * need to add a variable to the end of a section. In that
@@ -1009,23 +986,21 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 			 * default case will take care of updating them.
 			 */
 			pre_end = post_start = cfg->reader.read_ptr;
-			if (current_section)
-				git__free(current_section);
-			error = parse_section_header(cfg, &current_section);
-			if (error < GIT_SUCCESS)
-				break;
+
+			git__free(current_section);
+			if (parse_section_header(cfg, &current_section) < 0)
+				goto rewrite_fail;
 
 			/* Keep track of when it stops matching */
 			last_section_matched = section_matches;
 			section_matches = !strcmp(current_section, section);
-			break;
+		}
 
-		case ';':
-		case '#':
+		else if (c == ';' || c == '#') {
 			cfg_consume_line(cfg);
-			break;
+		}
 
-		default:
+		else {
 			/*
 			 * If the section doesn't match, but the last section did,
 			 * it means we need to add a variable (so skip the line
@@ -1039,67 +1014,54 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 			if (!section_matches) {
 				if (!last_section_matched) {
 					cfg_consume_line(cfg);
-					break;
+					continue;
 				}
 			} else {
-				int cmp = -1;
+				int has_matched = 0;
+				char *var_name, *var_value;
 
 				pre_end = cfg->reader.read_ptr;
-				if ((error = parse_variable(cfg, &var_name, &var_value)) == GIT_SUCCESS)
-					cmp = strcasecmp(name, var_name);
+				if (parse_variable(cfg, &var_name, &var_value) < 0)
+					goto rewrite_fail;
 
-				if (cmp == 0 && preg != NULL)
-					cmp = regexec(preg, var_value, 0, NULL, 0);
+				/* First try to match the name of the variable */
+				if (strcasecmp(name, var_name) == 0)
+					has_matched = 1;
+
+				/* If the name matches, and we have a regex to match the
+				 * value, try to match it */
+				if (has_matched && preg != NULL)
+					has_matched = (regexec(preg, var_value, 0, NULL, 0) == 0);
 
 				git__free(var_name);
 				git__free(var_value);
 
-				if (cmp != 0)
-					break;
+				/* if there is no match, keep going */
+				if (!has_matched)
+					continue;
 
 				post_start = cfg->reader.read_ptr;
 			}
 
-			/*
-			 * We've found the variable we wanted to change, so
-			 * write anything up to it
-			 */
+			/* We've found the variable we wanted to change, so
+			 * write anything up to it */
+			git_filebuf_write(&file, data_start, pre_end - data_start);
 			preg_replaced = 1;
-			error = git_filebuf_write(&file, data_start, pre_end - data_start);
-			if (error < GIT_SUCCESS) {
-				git__rethrow(error, "Failed to write the first part of the file");
-				break;
-			}
 
-			/*
-			 * Then replace the variable. If the value is NULL, it
-			 * means we want to delete it, so pretend everything went
-			 * fine
-			 */
-			if (value == NULL)
-				error = GIT_SUCCESS;
-			else
-				error = git_filebuf_printf(&file, "\t%s = %s\n", name, value);
-			if (error < GIT_SUCCESS) {
-				git__rethrow(error, "Failed to overwrite the variable");
-				break;
+			/* Then replace the variable. If the value is NULL, it
+			 * means we want to delete it, so don't write anything. */
+			if (value != NULL) {
+				git_filebuf_printf(&file, "\t%s = %s\n", name, value);
 			}
 
+			/* multiline variable? we need to keep reading lines to match */
 			if (preg != NULL) {
 				data_start = post_start;
 				continue;
 			}
 
-			/* And then the write out rest of the file */
-			error = git_filebuf_write(&file, post_start,
-						cfg->reader.buffer.size - (post_start - data_start));
-
-			if (error < GIT_SUCCESS) {
-				git__rethrow(error, "Failed to write the rest of the file");
-					break;
-			}
-
-			goto cleanup;
+			write_trailer = 1;
+			break; /* break from the loop */
 		}
 	}
 
@@ -1119,132 +1081,108 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 	 * want to write the rest of the file. Otherwise we need to write
 	 * out the whole file and then the new variable.
 	 */
-	if (preg_replaced) {
-		error = git_filebuf_printf(&file, "\n%s", data_start);
-		if (error < GIT_SUCCESS)
-			error = git__rethrow(error, "Failed to write the rest of the file");
-
-		goto cleanup;
-	}
+	if (write_trailer) {
+		/* Write out rest of the file */
+		git_filebuf_write(&file, post_start, cfg->reader.buffer.size - (post_start - data_start));
+	} else {
+		if (preg_replaced) {
+			git_filebuf_printf(&file, "\n%s", data_start);
+		} else {
+			git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size);
+
+			/* And now if we just need to add a variable */
+			if (!section_matches && write_section(&file, section) < 0)
+				goto rewrite_fail;
+
+			/* Sanity check: if we are here, and value is NULL, that means that somebody
+			 * touched the config file after our intial read. We should probably assert()
+			 * this, but instead we'll handle it gracefully with an error. */
+			if (value == NULL) {
+				giterr_set(GITERR_CONFIG,
+					"Race condition when writing a config file (a cvar has been removed)");
+				goto rewrite_fail;
+			}
 
-	error = git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size);
-	if (error < GIT_SUCCESS) {
-		git__rethrow(error, "Failed to write original config content");
-		goto cleanup;
+			git_filebuf_printf(&file, "\t%s = %s\n", name, value);
+		}
 	}
 
-	/* And now if we just need to add a variable */
-	if (section_matches) {
-		error = git_filebuf_printf(&file, "\t%s = %s\n", name, value);
-		goto cleanup;
-	}
+	git__free(section);
+	git__free(current_section);
 
-	/* Or maybe we need to write out a whole section */
-	error = write_section(&file, section);
-	if (error < GIT_SUCCESS)
-		git__rethrow(error, "Failed to write new section");
+	result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
+	git_buf_free(&cfg->reader.buffer);
+	return result;
 
-	error = git_filebuf_printf(&file, "\t%s = %s\n", name, value);
- cleanup:
+rewrite_fail:
 	git__free(section);
 	git__free(current_section);
 
-	if (error < GIT_SUCCESS)
-		git_filebuf_cleanup(&file);
-	else
-		error = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
-
+	git_filebuf_cleanup(&file);
 	git_buf_free(&cfg->reader.buffer);
-	return error;
+	return -1;
 }
 
 static int is_multiline_var(const char *str)
 {
-	char *end = strrchr(str, '\0') - 1;
-
-	while (isspace(*end))
-		--end;
-
-	return *end == '\\';
+	const char *end = str + strlen(str);
+	return (end > str) && (end[-1] == '\\');
 }
 
-static int parse_multiline_variable(diskfile_backend *cfg, const char *first, char **out)
+static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value)
 {
-	char *line = NULL, *end;
-	int error = GIT_SUCCESS, ret;
-	size_t len;
-	char *buf;
+	char *line = NULL;
 
 	/* Check that the next line exists */
 	line = cfg_readline(cfg);
 	if (line == NULL)
-		return GIT_ENOMEM;
+		return -1;
 
 	/* We've reached the end of the file, there is input missing */
 	if (line[0] == '\0') {
-		error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse multiline var. File ended unexpectedly");
-		goto out;
+		set_parse_error(cfg, 0, "Unexpected end of file while parsing multine var");
+		git__free(line);
+		return -1;
 	}
 
 	strip_comments(line);
 
 	/* If it was just a comment, pretend it didn't exist */
 	if (line[0] == '\0') {
-		error = parse_multiline_variable(cfg, first, out);
-		goto out;
+		git__free(line);
+		return parse_multiline_variable(cfg, value);
+		/* TODO: unbounded recursion. This **could** be exploitable */
 	}
 
-	/* Find the continuation character '\' and strip the whitespace */
-	end = strrchr(first, '\\');
-	while (isspace(end[-1]))
-		--end;
-
-	*end = '\0'; /* Terminate the string here */
+	/* Drop the continuation character '\': to closely follow the UNIX
+	 * standard, this character **has** to be last one in the buf, with
+	 * no whitespace after it */
+	assert(is_multiline_var(value->ptr));
+	git_buf_truncate(value, value->size - 1);
 
-	len = strlen(first) + strlen(line) + 2;
-	buf = git__malloc(len);
-	if (buf == NULL) {
-		error = GIT_ENOMEM;
-		goto out;
-	}
-
-	ret = p_snprintf(buf, len, "%s %s", first, line);
-	if (ret < 0) {
-		error = git__throw(GIT_EOSERR, "Failed to parse multiline var. Failed to put together two lines. OS err: %s", strerror(errno));
-		git__free(buf);
-		goto out;
-	}
+	/* add this line to the multiline var */
+	git_buf_puts(value, line);
+	git__free(line);
 
 	/*
-	 * If we need to continue reading the next line, pretend
-	 * everything we've read up to now was in one line and call
-	 * ourselves.
+	 * If we need to continue reading the next line, let's just
+	 * keep putting stuff in the buffer
 	 */
-	if (is_multiline_var(buf)) {
-		char *final_val;
-		error = parse_multiline_variable(cfg, buf, &final_val);
-		git__free(buf);
-		buf = final_val;
-	}
-
-	*out = buf;
+	if (is_multiline_var(value->ptr))
+		return parse_multiline_variable(cfg, value);
 
- out:
-	git__free(line);
-	return error;
+	return 0;
 }
 
 static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value)
 {
-	char *tmp;
-	int error = GIT_SUCCESS;
 	const char *var_end = NULL;
 	const char *value_start = NULL;
 	char *line;
 
 	line = cfg_readline(cfg);
 	if (line == NULL)
-		return GIT_ENOMEM;
+		return -1;
 
 	strip_comments(line);
 
@@ -1260,52 +1198,39 @@ static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_val
 		while (isspace(var_end[0]));
 	}
 
-	tmp = git__strndup(line, var_end - line + 1);
-	if (tmp == NULL) {
-		error = GIT_ENOMEM;
-		goto out;
-	}
+	*var_name = git__strndup(line, var_end - line + 1);
+	GITERR_CHECK_ALLOC(*var_name);
 
-	*var_name = tmp;
+	/* If there is no value, boolean true is assumed */
+	*var_value = NULL;
 
 	/*
 	 * Now, let's try to parse the value
 	 */
 	if (value_start != NULL) {
-
 		while (isspace(value_start[0]))
 			value_start++;
 
-		if (value_start[0] == '\0') {
-			*var_value = NULL;
-			goto out;
-		}
-
 		if (is_multiline_var(value_start)) {
-			error = parse_multiline_variable(cfg, value_start, var_value);
-			if (error != GIT_SUCCESS)
-			{
-				*var_value = NULL;
+			git_buf multi_value = GIT_BUF_INIT;
+			git_buf_puts(&multi_value, value_start);
+
+			if (parse_multiline_variable(cfg, &multi_value) < 0 || git_buf_oom(&multi_value)) {
 				git__free(*var_name);
+				git__free(line);
+				git_buf_free(&multi_value);
+				return -1;
 			}
-			goto out;
-		}
 
-		tmp = git__strdup(value_start);
-		if (tmp == NULL) {
-			git__free(*var_name);
-			*var_value = NULL;
-			error = GIT_ENOMEM;
-			goto out;
+			*var_value = git_buf_detach(&multi_value);
+		}
+		else if (value_start[0] != '\0') {
+			*var_value = git__strdup(value_start);
+			GITERR_CHECK_ALLOC(*var_value);
 		}
 
-		*var_value = tmp;
-	} else {
-		/* If there is no value, boolean true is assumed */
-		*var_value = NULL;
 	}
 
- out:
 	git__free(line);
-	return error;
+	return 0;
 }
diff --git a/src/errors.c b/src/errors.c
index c25fa75..6fb7777 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -122,25 +122,28 @@ void giterr_set(int error_class, const char *string, ...)
 {
 	char error_str[1024];
 	va_list arglist;
-	git_error *error;
-	const char *oserr =
-		(error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL;
-
-	error = &GIT_GLOBAL->error_t;
-	free(error->message);
 
 	va_start(arglist, string);
 	p_vsnprintf(error_str, sizeof(error_str), string, arglist);
 	va_end(arglist);
 
 	/* automatically suffix strerror(errno) for GITERR_OS errors */
-	if (oserr != NULL) {
+	if (error_class == GITERR_OS) {
 		strncat(error_str, ": ", sizeof(error_str));
-		strncat(error_str, oserr, sizeof(error_str));
+		strncat(error_str, strerror(errno), sizeof(error_str));
 		errno = 0;
 	}
 
-	error->message = git__strdup(error_str);
+	giterr_set_str(error_class, error_str);
+}
+
+void giterr_set_str(int error_class, const char *string)
+{
+	git_error *error = &GIT_GLOBAL->error_t;
+
+	free(error->message);
+
+	error->message = git__strdup(string);
 	error->klass = error_class;
 
 	if (error->message == NULL) {
@@ -151,6 +154,13 @@ void giterr_set(int error_class, const char *string, ...)
 	GIT_GLOBAL->last_error = error;
 }
 
+void giterr_set_regex(const regex_t *regex, int error_code)
+{
+	char error_buf[1024];
+	regerror(error_code, regex, error_buf, sizeof(error_buf));
+	giterr_set_str(GITERR_REGEX, error_buf);
+}
+
 void giterr_clear(void)
 {
 	GIT_GLOBAL->last_error = NULL;
diff --git a/src/filebuf.c b/src/filebuf.c
index 8297b4f..5e206c5 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -14,6 +14,36 @@
 
 static const size_t WRITE_BUFFER_SIZE = (4096 * 2);
 
+enum buferr_t {
+	BUFERR_OK = 0,
+	BUFERR_WRITE,
+	BUFERR_ZLIB,
+	BUFERR_MEM
+};
+
+#define ENSURE_BUF_OK(buf) if ((buf)->last_error != BUFERR_OK) { return -1; }
+
+static int verify_last_error(git_filebuf *file)
+{
+	switch (file->last_error) {
+	case BUFERR_WRITE:
+		giterr_set(GITERR_OS, "Failed to write out file");
+		return -1;
+
+	case BUFERR_MEM:
+		giterr_set_oom();
+		return -1;
+
+	case BUFERR_ZLIB:
+		giterr_set(GITERR_ZLIB,
+			"Buffer error when writing out ZLib data");
+		return -1;
+
+	default:
+		return 0;
+	}
+}
+
 static int lock_file(git_filebuf *file, int flags)
 {
 	if (git_path_exists(file->path_lock) == true) {
@@ -100,20 +130,21 @@ GIT_INLINE(int) flush_buffer(git_filebuf *file)
 
 static int write_normal(git_filebuf *file, void *source, size_t len)
 {
-	int result = 0;
-
 	if (len > 0) {
-		result = p_write(file->fd, (void *)source, len);
+		if (p_write(file->fd, (void *)source, len) < 0) {
+			file->last_error = BUFERR_WRITE;
+			return -1;
+		}
+
 		if (file->digest)
 			git_hash_update(file->digest, source, len);
 	}
 
-	return result;
+	return 0;
 }
 
 static int write_deflate(git_filebuf *file, void *source, size_t len)
 {
-	int result = Z_OK;
 	z_stream *zs = &file->zs;
 
 	if (len > 0 || file->flush_mode == Z_FINISH) {
@@ -126,14 +157,17 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
 			zs->next_out = file->z_buf;
 			zs->avail_out = (uInt)file->buf_size;
 
-			result = deflate(zs, file->flush_mode);
-			if (result == Z_STREAM_ERROR)
-				return git__throw(GIT_ERROR, "Failed to deflate input");
+			if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) {
+				file->last_error = BUFERR_ZLIB;
+				return -1;
+			}
 
 			have = file->buf_size - (size_t)zs->avail_out;
 
-			if (p_write(file->fd, file->z_buf, have) < GIT_SUCCESS)
-				return git__throw(GIT_EOSERR, "Failed to write to file");
+			if (p_write(file->fd, file->z_buf, have) < 0) {
+				file->last_error = BUFERR_WRITE;
+				return -1;
+			}
 
 		} while (zs->avail_out == 0);
 
@@ -143,7 +177,7 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
 			git_hash_update(file->digest, source, len);
 	}
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_filebuf_open(git_filebuf *file, const char *path, int flags)
@@ -161,6 +195,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
 	file->buf_size = WRITE_BUFFER_SIZE;
 	file->buf_pos = 0;
 	file->fd = -1;
+	file->last_error = BUFERR_OK;
 
 	/* Allocate the main cache buffer */
 	file->buffer = git__malloc(file->buf_size);
@@ -237,58 +272,61 @@ cleanup:
 
 int git_filebuf_hash(git_oid *oid, git_filebuf *file)
 {
-	int error;
-
 	assert(oid && file && file->digest);
 
-	if ((error = flush_buffer(file)) < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to get hash for file");
+	flush_buffer(file);
+
+	if (verify_last_error(file) < 0)
+		return -1;
 
 	git_hash_final(oid, file->digest);
 	git_hash_free_ctx(file->digest);
 	file->digest = NULL;
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode)
 {
 	git__free(file->path_original);
 	file->path_original = git__strdup(path);
-	if (file->path_original == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(file->path_original);
 
 	return git_filebuf_commit(file, mode);
 }
 
 int git_filebuf_commit(git_filebuf *file, mode_t mode)
 {
-	int error;
-
 	/* temporary files cannot be committed */
 	assert(file && file->path_original);
 
 	file->flush_mode = Z_FINISH;
-	if ((error = flush_buffer(file)) < GIT_SUCCESS)
-		goto cleanup;
+	flush_buffer(file);
+
+	if (verify_last_error(file) < 0)
+		goto on_error;
 
 	p_close(file->fd);
 	file->fd = -1;
 
 	if (p_chmod(file->path_lock, mode)) {
-		error = git__throw(GIT_EOSERR, "Failed to chmod locked file before committing");
-		goto cleanup;
+		giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
+		goto on_error;
 	}
 
 	p_unlink(file->path_original);
 
-	error = p_rename(file->path_lock, file->path_original);
+	if (p_rename(file->path_lock, file->path_original) < 0) {
+		giterr_set(GITERR_OS, "Failed to rename lockfile to '%s'", file->path_original);
+		goto on_error;
+	}
 
-cleanup:
 	git_filebuf_cleanup(file);
-	if (error < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to commit locked file from buffer");
-	return GIT_SUCCESS;
+	return 0;
+
+on_error:
+	git_filebuf_cleanup(file);
+	return -1;
 }
 
 GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
@@ -299,22 +337,22 @@ GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
 
 int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
 {
-	int error;
 	const unsigned char *buf = buff;
 
+	ENSURE_BUF_OK(file);
+
 	for (;;) {
 		size_t space_left = file->buf_size - file->buf_pos;
 
 		/* cache if it's small */
 		if (space_left > len) {
 			add_to_cache(file, buf, len);
-			return GIT_SUCCESS;
+			return 0;
 		}
 
 		add_to_cache(file, buf, space_left);
-
-		if ((error = flush_buffer(file)) < GIT_SUCCESS)
-			return git__rethrow(error, "Failed to write to buffer");
+		if (flush_buffer(file) < 0)
+			return -1;
 
 		len -= space_left;
 		buf += space_left;
@@ -323,32 +361,37 @@ int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
 
 int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
 {
-	int error;
 	size_t space_left = file->buf_size - file->buf_pos;
 
 	*buffer = NULL;
 
-	if (len > file->buf_size)
-		return GIT_ENOMEM;
+	ENSURE_BUF_OK(file);
+
+	if (len > file->buf_size) {
+		file->last_error = BUFERR_MEM;
+		return -1;
+	}
 
 	if (space_left <= len) {
-		if ((error = flush_buffer(file)) < GIT_SUCCESS)
-			return git__rethrow(error, "Failed to reserve buffer");
+		if (flush_buffer(file) < 0)
+			return -1;
 	}
 
 	*buffer = (file->buffer + file->buf_pos);
 	file->buf_pos += len;
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_filebuf_printf(git_filebuf *file, const char *format, ...)
 {
 	va_list arglist;
 	size_t space_left;
-	int len, error;
+	int len, res;
 	char *tmp_buffer;
 
+	ENSURE_BUF_OK(file);
+
 	space_left = file->buf_size - file->buf_pos;
 
 	do {
@@ -356,24 +399,28 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
 		len = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
 		va_end(arglist);
 
-		if (len < 0)
-			return git__throw(GIT_EOSERR, "Failed to format string");
+		if (len < 0) {
+			file->last_error = BUFERR_MEM;
+			return -1;
+		}
 
 		if ((size_t)len + 1 <= space_left) {
 			file->buf_pos += len;
-			return GIT_SUCCESS;
+			return 0;
 		}
 
-		if ((error = flush_buffer(file)) < GIT_SUCCESS)
-			return git__rethrow(error, "Failed to output to buffer");
+		if (flush_buffer(file) < 0)
+			return -1;
 
 		space_left = file->buf_size - file->buf_pos;
 
 	} while ((size_t)len + 1 <= space_left);
 
 	tmp_buffer = git__malloc(len + 1);
-	if (!tmp_buffer)
-		return GIT_ENOMEM;
+	if (!tmp_buffer) {
+		file->last_error = BUFERR_MEM;
+		return -1;
+	}
 
 	va_start(arglist, format);
 	len = p_vsnprintf(tmp_buffer, len + 1, format, arglist);
@@ -381,12 +428,13 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
 
 	if (len < 0) {
 		git__free(tmp_buffer);
-		return git__throw(GIT_EOSERR, "Failed to format string");
+		file->last_error = BUFERR_MEM;
+		return -1;
 	}
 
-	error = git_filebuf_write(file, tmp_buffer, len);
+	res = git_filebuf_write(file, tmp_buffer, len);
 	git__free(tmp_buffer);
 
-	return error;
+	return res;
 }
 
diff --git a/src/filebuf.h b/src/filebuf.h
index 3712153..5f9d4ad 100644
--- a/src/filebuf.h
+++ b/src/filebuf.h
@@ -40,25 +40,35 @@ struct git_filebuf {
 
 	size_t buf_size, buf_pos;
 	git_file fd;
+	int last_error;
 };
 
 typedef struct git_filebuf git_filebuf;
 
 #define GIT_FILEBUF_INIT {0}
 
-/* The git_filebuf object lifecycle is:
+/*
+ * The git_filebuf object lifecycle is:
  * - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT.
+ *
  * - Call git_filebuf_open() to initialize the filebuf for use.
+ *
  * - Make as many calls to git_filebuf_write(), git_filebuf_printf(),
- *   git_filebuf_reserve() as you like.
+ *   git_filebuf_reserve() as you like. The error codes for these
+ *   functions don't need to be checked. They are stored internally
+ *   by the file buffer.
+ *
  * - While you are writing, you may call git_filebuf_hash() to get
- *   the hash of all you have written so far.
+ *   the hash of all you have written so far. This function will
+ *   fail if any of the previous writes to the buffer failed.
+ *
  * - To close the git_filebuf, you may call git_filebuf_commit() or
  *   git_filebuf_commit_at() to save the file, or
  *   git_filebuf_cleanup() to abandon the file.  All of these will
- *   clear the git_filebuf object.
+ *   free the git_filebuf object. Likewise, all of these will fail
+ *   if any of the previous writes to the buffer failed, and set
+ *   an error code accordingly.
  */
-
 int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len);
 int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
 int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
diff --git a/src/hashtable.c b/src/hashtable.c
index 73a6336..c081fc9 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -227,11 +227,11 @@ int git_hashtable_remove2(git_hashtable *self, const void *key, void **old_value
 			node->key = NULL;
 			node->value = NULL;
 			self->key_count--;
-			return GIT_SUCCESS;
+			return 0;
 		}
 	}
 
-	return git__throw(GIT_ENOTFOUND, "Entry not found in hash table");
+	return GIT_ENOTFOUND;
 }
 
 int git_hashtable_merge(git_hashtable *self, git_hashtable *other)
diff --git a/src/posix.c b/src/posix.c
index d2364d9..9d96d30 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -31,10 +31,9 @@ int p_getcwd(char *buffer_out, size_t size)
 	cwd_buffer = getcwd(buffer_out, size);
 
 	if (cwd_buffer == NULL)
-		return git__throw(GIT_EOSERR, "Failed to retrieve current working directory");
+		return -1;
 
 	git_path_mkposix(buffer_out);
-
 	git_path_string_to_dir(buffer_out, size);	//Ensure the path ends with a trailing slash
 
 	return GIT_SUCCESS;
@@ -44,14 +43,13 @@ int p_rename(const char *from, const char *to)
 {
 	if (!link(from, to)) {
 		p_unlink(from);
-		return GIT_SUCCESS;
+		return 0;
 	}
 
 	if (!rename(from, to))
-		return GIT_SUCCESS;
-
-	return GIT_ERROR;
+		return 0;
 
+	return -1;
 }
 
 #endif
@@ -64,7 +62,7 @@ int p_read(git_file fd, void *buf, size_t cnt)
 		if (r < 0) {
 			if (errno == EINTR || errno == EAGAIN)
 				continue;
-			return GIT_EOSERR;
+			return -1;
 		}
 		if (!r)
 			break;
@@ -82,14 +80,14 @@ int p_write(git_file fd, const void *buf, size_t cnt)
 		if (r < 0) {
 			if (errno == EINTR || errno == EAGAIN)
 				continue;
-			return GIT_EOSERR;
+			return -1;
 		}
 		if (!r) {
 			errno = EPIPE;
-			return GIT_EOSERR;
+			return -1;
 		}
 		cnt -= r;
 		b += r;
 	}
-	return GIT_SUCCESS;
+	return 0;
 }
diff --git a/src/util.h b/src/util.h
index 01755b5..afa3f72 100644
--- a/src/util.h
+++ b/src/util.h
@@ -7,8 +7,6 @@
 #ifndef INCLUDE_util_h__
 #define INCLUDE_util_h__
 
-#include "git2/errors.h"
-
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
@@ -24,24 +22,21 @@
 GIT_INLINE(void *) git__malloc(size_t len)
 {
 	void *ptr = malloc(len);
-	if (!ptr)
-		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)len);
+	if (!ptr) giterr_set_oom();
 	return ptr;
 }
 
 GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
 {
 	void *ptr = calloc(nelem, elsize);
-	if (!ptr)
-		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)nelem*elsize);
+	if (!ptr) giterr_set_oom();
 	return ptr;
 }
 
 GIT_INLINE(char *) git__strdup(const char *str)
 {
 	char *ptr = strdup(str);
-	if (!ptr)
-		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to duplicate string");
+	if (!ptr) giterr_set_oom();
 	return ptr;
 }
 
@@ -56,7 +51,7 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 
 	ptr = (char*)malloc(length + 1);
 	if (!ptr) {
-		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to duplicate string");
+		giterr_set_oom();
 		return NULL;
 	}
 
@@ -69,8 +64,7 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
 {
 	void *new_ptr = realloc(ptr, size);
-	if (!new_ptr)
-		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)size);
+	if (!new_ptr) giterr_set_oom();
 	return new_ptr;
 }