Commit cb8a79617b15e347f26d21cedde0f2b8670c1876

Vicent Martí 2012-03-07T00:02:55

error-handling: Repository This also includes droping `git_buf_lasterror` because it makes no sense in the new system. Note that in most of the places were it has been dropped, the code needs cleanup. I.e. GIT_ENOMEM is going away, so instead it should return a generic `-1` and obviously not throw anything.

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
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 9b28093..7daa067 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -125,7 +125,7 @@ typedef enum {
 	GITERR_OS,
 	GITERR_REFERENCE,
 	GITERR_ZLIB,
-
+	GITERR_REPOSITORY,
 } git_error_class;
 
 #define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
diff --git a/src/buffer.c b/src/buffer.c
index 3098f6d..dd245e2 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -70,7 +70,7 @@ int git_buf_try_grow(git_buf *buf, size_t target_size)
 
 	new_ptr = git__realloc(new_ptr, new_size);
 	if (!new_ptr)
-		return GIT_ENOMEM;
+		return -1;
 
 	buf->asize = new_size;
 	buf->ptr   = new_ptr;
@@ -100,16 +100,11 @@ void git_buf_clear(git_buf *buf)
 		buf->ptr[0] = '\0';
 }
 
-int git_buf_oom(const git_buf *buf)
+bool git_buf_oom(const git_buf *buf)
 {
 	return (buf->ptr == &git_buf__oom);
 }
 
-int git_buf_lasterror(const git_buf *buf)
-{
-	return (buf->ptr == &git_buf__oom) ? GIT_ENOMEM : GIT_SUCCESS;
-}
-
 int git_buf_set(git_buf *buf, const char *data, size_t len)
 {
 	if (len == 0 || data == NULL) {
diff --git a/src/buffer.h b/src/buffer.h
index 3cdd794..6f59dce 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -57,15 +57,10 @@ void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
  * further calls to modify the buffer will fail.  Check git_buf_oom() at the
  * end of your sequence and it will be true if you ran out of memory at any
  * point with that buffer.
- * @return 0 if no error, 1 if allocation error.
- */
-int git_buf_oom(const git_buf *buf);
-
-/**
- * Just like git_buf_oom, except returns appropriate error code.
- * @return GIT_ENOMEM if allocation error, GIT_SUCCESS if not.
+ *
+ * @return false if no error, true if allocation error
  */
-int git_buf_lasterror(const git_buf *buf);
+bool git_buf_oom(const git_buf *buf);
 
 /*
  * The functions below that return int values, will return GIT_ENOMEM
diff --git a/src/commit.c b/src/commit.c
index 189d8fe..2e35992 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -129,7 +129,7 @@ int git_commit_create(
 	git_buf_puts(&commit, message);
 
 	if (git_buf_oom(&commit)) {
-		error = git__throw(git_buf_lasterror(&commit),
+		error = git__throw(GIT_ENOMEM,
 			"Not enough memory to build the commit data");
 		goto cleanup;
 	}
diff --git a/src/crlf.c b/src/crlf.c
index f0ec7b7..536b50f 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -128,8 +128,7 @@ static int drop_crlf(git_buf *dest, const git_buf *source)
 
 	/* Copy remaining input into dest */
 	git_buf_put(dest, scan, scan_end - scan);
-
-	return git_buf_lasterror(dest);
+	return 0;
 }
 
 static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *source)
diff --git a/src/diff_output.c b/src/diff_output.c
index 5e7486a..9c34dcf 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -482,8 +482,8 @@ static int print_compact(void *data, git_diff_delta *delta, float progress)
 	else
 		git_buf_printf(pi->buf, "%c\t%s\n", code, delta->old.path);
 
-	if (git_buf_lasterror(pi->buf) != GIT_SUCCESS)
-		return git_buf_lasterror(pi->buf);
+	if (git_buf_oom(pi->buf) == true)
+		return GIT_ENOMEM;
 
 	return pi->print_cb(pi->cb_data, GIT_DIFF_LINE_FILE_HDR, pi->buf->ptr);
 }
@@ -534,7 +534,10 @@ static int print_oid_range(diff_print_info *pi, git_diff_delta *delta)
 		git_buf_printf(pi->buf, "index %s..%s\n", start_oid, end_oid);
 	}
 
-	return git_buf_lasterror(pi->buf);
+	if (git_buf_oom(pi->buf))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 static int print_patch_file(void *data, git_diff_delta *delta, float progress)
@@ -567,8 +570,8 @@ static int print_patch_file(void *data, git_diff_delta *delta, float progress)
 		git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
 	}
 
-	if (git_buf_lasterror(pi->buf) != GIT_SUCCESS)
-		return git_buf_lasterror(pi->buf);
+	if (git_buf_oom(pi->buf))
+		return GIT_ENOMEM;
 
 	error = pi->print_cb(pi->cb_data, GIT_DIFF_LINE_FILE_HDR, pi->buf->ptr);
 	if (error != GIT_SUCCESS || delta->binary != 1)
@@ -578,8 +581,8 @@ static int print_patch_file(void *data, git_diff_delta *delta, float progress)
 	git_buf_printf(
 		pi->buf, "Binary files %s%s and %s%s differ\n",
 		oldpfx, oldpath, newpfx, newpath);
-	if (git_buf_lasterror(pi->buf) != GIT_SUCCESS)
-		return git_buf_lasterror(pi->buf);
+	if (git_buf_oom(pi->buf))
+		return GIT_ENOMEM;
 
 	return pi->print_cb(pi->cb_data, GIT_DIFF_LINE_BINARY, pi->buf->ptr);
 }
@@ -601,7 +604,7 @@ static int print_patch_hunk(
 	if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) == GIT_SUCCESS)
 		return pi->print_cb(pi->cb_data, GIT_DIFF_LINE_HUNK_HDR, pi->buf->ptr);
 	else
-		return git_buf_lasterror(pi->buf);
+		return GIT_ENOMEM;
 }
 
 static int print_patch_line(
@@ -624,8 +627,8 @@ static int print_patch_line(
 	else if (content_len > 0)
 		git_buf_printf(pi->buf, "%.*s", (int)content_len, content);
 
-	if (git_buf_lasterror(pi->buf) != GIT_SUCCESS)
-		return git_buf_lasterror(pi->buf);
+	if (git_buf_oom(pi->buf))
+		return GIT_ENOMEM;
 
 	return pi->print_cb(pi->cb_data, line_origin, pi->buf->ptr);
 }
diff --git a/src/fileops.c b/src/fileops.c
index ffaf831..4414c86 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -211,20 +211,21 @@ void git_futils_mmap_free(git_map *out)
 
 int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
 {
-	int error, root_path_offset;
+	int root_path_offset;
 	git_buf make_path = GIT_BUF_INIT;
 	size_t start;
 	char *pp, *sp;
+	bool failed = false;
 
 	if (base != NULL) {
 		start = strlen(base);
-		error = git_buf_joinpath(&make_path, base, path);
+		if (git_buf_joinpath(&make_path, base, path) < 0)
+			return -1;
 	} else {
 		start = 0;
-		error = git_buf_puts(&make_path, path);
+		if (git_buf_puts(&make_path, path) < 0)
+			return -1;
 	}
-	if (error < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to create `%s` tree structure", path);
 
 	pp = make_path.ptr + start;
 
@@ -232,14 +233,13 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
 	if (root_path_offset > 0)
 		pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */
 
-	while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != NULL) {
+	while (!failed && (sp = strchr(pp, '/')) != NULL) {
 		if (sp != pp && git_path_isdir(make_path.ptr) == false) {
 			*sp = 0;
-			error = p_mkdir(make_path.ptr, mode);
 
 			/* Do not choke while trying to recreate an existing directory */
-			if (errno == EEXIST)
-				error = GIT_SUCCESS;
+			if (p_mkdir(make_path.ptr, mode) < 0 && errno != EEXIST)
+				failed = true;
 
 			*sp = '/';
 		}
@@ -247,18 +247,20 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
 		pp = sp + 1;
 	}
 
-	if (*pp != '\0' && error == GIT_SUCCESS) {
-		error = p_mkdir(make_path.ptr, mode);
-		if (errno == EEXIST)
-			error = GIT_SUCCESS;
+	if (*pp != '\0' && !failed) {
+		if (p_mkdir(make_path.ptr, mode) < 0 && errno != EEXIST)
+			failed = true;
 	}
 
 	git_buf_free(&make_path);
 
-	if (error < GIT_SUCCESS)
-		return git__throw(error, "Failed to recursively create `%s` tree structure", path);
+	if (failed) {
+		giterr_set(GITERR_OS,
+			"Failed to create directory structure at '%s'", path);
+		return -1;
+	}
 
-	return GIT_SUCCESS;
+	return 0;
 }
 
 static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
@@ -407,8 +409,8 @@ cleanup:
 
 int git_futils_find_system_file(git_buf *path, const char *filename)
 {
-	if (git_buf_joinpath(path, "/etc", filename) < GIT_SUCCESS)
-		return git_buf_lasterror(path);
+	if (git_buf_joinpath(path, "/etc", filename) < 0)
+		return -1;
 
 	if (git_path_exists(path->ptr) == true)
 		return GIT_SUCCESS;
diff --git a/src/indexer.c b/src/indexer.c
index de1e5dc..dd7c719 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -171,7 +171,10 @@ static int index_path(git_buf *path, git_indexer *idx)
 	path->size += GIT_OID_HEXSZ;
 	git_buf_puts(path, suffix);
 
-	return git_buf_lasterror(path);
+	if (git_buf_oom(path))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 int git_indexer_write(git_indexer *idx)
@@ -192,8 +195,8 @@ int git_indexer_write(git_indexer *idx)
 	git_buf_truncate(&filename, filename.size - strlen("pack"));
 	git_buf_puts(&filename, "idx");
 
-	if ((error = git_buf_lasterror(&filename)) < GIT_SUCCESS)
-		goto cleanup;
+	if (git_buf_oom(&filename))
+		return GIT_ENOMEM;
 
 	error = git_filebuf_open(&idx->file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS);
 	if (error < GIT_SUCCESS)
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 2dd7004..17fede4 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -387,8 +387,8 @@ static int read_loose(git_rawobj *out, git_buf *loc)
 
 	assert(out && loc);
 
-	if ((error = git_buf_lasterror(loc)) < GIT_SUCCESS)
-		return error;
+	if (git_buf_oom(loc))
+		return GIT_ENOMEM;
 
 	out->data = NULL;
 	out->len = 0;
@@ -413,8 +413,8 @@ static int read_header_loose(git_rawobj *out, git_buf *loc)
 
 	assert(out && loc);
 
-	if ((error = git_buf_lasterror(loc)) < GIT_SUCCESS)
-		return error;
+	if (git_buf_oom(loc))
+		return GIT_ENOMEM;
 
 	out->data = NULL;
 
@@ -704,8 +704,8 @@ static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream)
 	if ((error = object_file_name(&final_path, backend->objects_dir, oid)) < GIT_SUCCESS)
 		goto cleanup;
 
-	if ((error = git_buf_lasterror(&final_path)) < GIT_SUCCESS)
-		goto cleanup;
+	if (git_buf_oom(&final_path))
+		return GIT_ENOMEM;
 
 	if ((error = git_futils_mkpath2file(final_path.ptr, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS)
 		goto cleanup;
diff --git a/src/path.c b/src/path.c
index 5d35e0e..8d0cf28 100644
--- a/src/path.c
+++ b/src/path.c
@@ -56,7 +56,7 @@ Exit:
 
 	if (buffer != NULL) {
 		if (git_buf_set(buffer, startp, len) < GIT_SUCCESS)
-			return git__rethrow(git_buf_lasterror(buffer),
+			return git__rethrow(GIT_ENOMEM,
 				"Could not get basename of '%s'", path);
 	}
 
@@ -116,7 +116,7 @@ Exit:
 
 	if (buffer != NULL) {
 		if (git_buf_set(buffer, path, len) < GIT_SUCCESS)
-			return git__rethrow(git_buf_lasterror(buffer),
+			return git__rethrow(GIT_ENOMEM,
 				"Could not get dirname of '%s'", path);
 	}
 
@@ -185,34 +185,36 @@ int git_path_root(const char *path)
 
 int git_path_prettify(git_buf *path_out, const char *path, const char *base)
 {
-	int  error = GIT_SUCCESS;
 	char buf[GIT_PATH_MAX];
 
+	assert(path && path_out);
 	git_buf_clear(path_out);
 
 	/* construct path if needed */
 	if (base != NULL && git_path_root(path) < 0) {
-		if ((error = git_buf_joinpath(path_out, base, path)) < GIT_SUCCESS)
-			return error;
+		if (git_buf_joinpath(path_out, base, path) < 0)
+			return -1;
+
 		path = path_out->ptr;
 	}
 
-	if (path == NULL || p_realpath(path, buf) == NULL)
-		error = GIT_EOSERR;
-	else
-		error = git_buf_sets(path_out, buf);
+	if (p_realpath(path, buf) == NULL) {
+		giterr_set(GITERR_OS, "Failed to resolve path '%s': %s", path, strerror(errno));
+		return (errno == ENOENT) ? GIT_ENOTFOUND : -1;
+	}
 
-	return error;
+	if (git_buf_sets(path_out, buf) < 0)
+		return -1;
+
+	return 0;
 }
 
 int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base)
 {
-	int error = git_path_prettify(path_out, path, base);
-
-	if (error == GIT_SUCCESS)
-		error = git_path_to_dir(path_out);
+	if (git_path_prettify(path_out, path, base) < 0)
+		return -1;
 
-	return error;
+	return git_path_to_dir(path_out);
 }
 
 int git_path_to_dir(git_buf *path)
@@ -222,7 +224,10 @@ int git_path_to_dir(git_buf *path)
 		path->ptr[path->size - 1] != '/')
 		git_buf_putc(path, '/');
 
-	return git_buf_lasterror(path);
+	if (git_buf_oom(path))
+		return -1;
+
+	return 0;
 }
 
 void git_path_string_to_dir(char* path, size_t size)
@@ -445,7 +450,7 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base)
 	/* call dirname if this is not a directory */
 	if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) == false)
 		if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS)
-			error = git_buf_lasterror(dir);
+			error = GIT_ENOMEM;
 
 	if (error == GIT_SUCCESS)
 		error = git_path_to_dir(dir);
diff --git a/src/reflog.c b/src/reflog.c
index 8f68a3a..5352760 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -65,9 +65,9 @@ static int reflog_write(const char *log_path, const char *oid_old,
 
 	git_buf_putc(&log, '\n');
 
-	if ((error = git_buf_lasterror(&log)) < GIT_SUCCESS) {
+	if (git_buf_oom(&log)) {
 		git_buf_free(&log);
-		return git__rethrow(error, "Failed to write reflog. Memory allocation failure");
+		return git__throw(GIT_ENOMEM, "Failed to write reflog. Memory allocation failure");
 	}
 
 	if ((error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND)) < GIT_SUCCESS) {
diff --git a/src/refspec.c b/src/refspec.c
index a271414..d51fd4c 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -97,7 +97,7 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con
 int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name)
 {
 	if (git_buf_sets(out, spec->dst) < GIT_SUCCESS)
-		return git_buf_lasterror(out);
+		return GIT_ENOMEM;
 
 	/*
 	 * No '*' at the end means that it's mapped to one specific local
@@ -109,5 +109,9 @@ int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *n
 	git_buf_truncate(out, out->size - 1); /* remove trailing '*' */
 	git_buf_puts(out, name + strlen(spec->src) - 1);
 
-	return git_buf_lasterror(out);
+	if (git_buf_oom(out))
+		return GIT_ENOMEM;
+
+	return 0;
 }
+
diff --git a/src/repository.c b/src/repository.c
index e274252..7d7b3c4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -80,35 +80,31 @@ void git_repository_free(git_repository *repo)
  *
  * Open a repository object from its path
  */
-static int quickcheck_repository_dir(git_buf *repository_path)
+static bool valid_repository_path(git_buf *repository_path)
 {
 	/* Check OBJECTS_DIR first, since it will generate the longest path name */
 	if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR) == false)
-		return GIT_ERROR;
+		return false;
 
 	/* Ensure HEAD file exists */
 	if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
-		return GIT_ERROR;
+		return false;
 
 	if (git_path_contains_dir(repository_path, GIT_REFS_DIR)  == false)
-		return GIT_ERROR;
+		return false;
 
-	return GIT_SUCCESS;
+	return true;
 }
 
-
 static git_repository *repository_alloc(void)
 {
-	int error;
-
 	git_repository *repo = git__malloc(sizeof(git_repository));
 	if (!repo)
 		return NULL;
 
 	memset(repo, 0x0, sizeof(git_repository));
 
-	error = git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free);
-	if (error < GIT_SUCCESS) {
+	if (git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free) < 0) {
 		git__free(repo);
 		return NULL;
 	}
@@ -121,50 +117,48 @@ static git_repository *repository_alloc(void)
 
 static int load_config_data(git_repository *repo)
 {
-	int error, is_bare;
+	int is_bare;
 	git_config *config;
 
-	error = git_repository_config__weakptr(&config, repo);
-	if (error < GIT_SUCCESS)
-		return error;
-
-	error = git_config_get_bool(config, "core.bare", &is_bare);
-	if (error == GIT_SUCCESS)
-		repo->is_bare = is_bare;
+	if (git_repository_config__weakptr(&config, repo) < 0)
+		return -1;
 
-	/* TODO: what else can we load/cache here? */
+	if (git_config_get_bool(config, "core.bare", &is_bare) < 0)
+		return -1; /* FIXME: We assume that a missing core.bare
+					  variable is an error. Is this right? */
 
-	return GIT_SUCCESS;
+	repo->is_bare = is_bare;
+	return 0;
 }
 
 static int load_workdir(git_repository *repo)
 {
-	int error;
 	git_buf workdir_buf = GIT_BUF_INIT;
 
 	if (repo->is_bare)
-		return GIT_SUCCESS;
+		return 0;
 
 	git_path_dirname_r(&workdir_buf, repo->path_repository);
 	git_path_to_dir(&workdir_buf);
 
-	if ((error = git_buf_lasterror(&workdir_buf)) == GIT_SUCCESS)
-		repo->workdir = git_buf_detach(&workdir_buf);
+	if (git_buf_oom(&workdir_buf))
+		return -1;
 
+	repo->workdir = git_buf_detach(&workdir_buf);
 	git_buf_free(&workdir_buf);
 
-	return error;
+	return 0;
 }
 
 int git_repository_open(git_repository **repo_out, const char *path)
 {
-	int error = GIT_SUCCESS;
 	git_buf path_buf = GIT_BUF_INIT;
 	git_repository *repo = NULL;
+	int res;
 
-	error = git_path_prettify_dir(&path_buf, path, NULL);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	res = git_path_prettify_dir(&path_buf, path, NULL);
+	if (res < 0)
+		return res;
 
 	/**
 	 * Check if the path we've been given is actually the path
@@ -174,39 +168,32 @@ int git_repository_open(git_repository **repo_out, const char *path)
 	if (git_path_contains_dir(&path_buf, GIT_DIR) == true)
 		git_buf_joinpath(&path_buf, path_buf.ptr, GIT_DIR);
 
-	if (quickcheck_repository_dir(&path_buf) < GIT_SUCCESS) {
-		error = git__throw(GIT_ENOTAREPO,
+	if (valid_repository_path(&path_buf) == false) {
+		giterr_set(GITERR_REPOSITORY,
 			"The given path (%s) is not a valid Git repository", git_buf_cstr(&path_buf));
-		goto cleanup;
+		git_buf_free(&path_buf);
+		return GIT_ENOTFOUND;
 	}
 
 	repo = repository_alloc();
-	if (repo == NULL) {
-		error = GIT_ENOMEM;
-		goto cleanup;
-	}
+	GITERR_CHECK_ALLOC(repo);
 
 	repo->path_repository = git_buf_detach(&path_buf);
-	if (repo->path_repository == NULL) {
-		error = GIT_ENOMEM;
-		goto cleanup;
-	}
+	GITERR_CHECK_ALLOC(repo->path_repository);
 
-	error = load_config_data(repo);
-	if (error < GIT_SUCCESS)
+	if (load_config_data(repo) < 0)
 		goto cleanup;
 
-	error = load_workdir(repo);
-	if (error < GIT_SUCCESS)
+	if (load_workdir(repo) < 0)
 		goto cleanup;
 
 	*repo_out = repo;
-	return GIT_SUCCESS;
+	return 0;
 
  cleanup:
 	git_repository_free(repo);
 	git_buf_free(&path_buf);
-	return error;
+	return -1;
 }
 
 static int load_config(
@@ -216,85 +203,79 @@ static int load_config(
 		const char *system_config_path)
 {
 	git_buf config_path = GIT_BUF_INIT;
-	int error;
 	git_config *cfg = NULL;
 
 	assert(repo && out);
 
-	error = git_config_new(&cfg);
-	if (error < GIT_SUCCESS)
-		return error;
+	if (git_config_new(&cfg) < 0)
+		return -1;
 
-	error = git_buf_joinpath(&config_path, repo->path_repository,
-							 GIT_CONFIG_FILENAME_INREPO);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	if (git_buf_joinpath(
+		&config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO) < 0)
+		goto on_error;
 
-	error = git_config_add_file_ondisk(cfg, config_path.ptr, 3);
-	git_buf_free(&config_path); /* done with config_path now */
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	if (git_config_add_file_ondisk(cfg, config_path.ptr, 3) < 0)
+		goto on_error;
+
+	git_buf_free(&config_path);
 
 	if (global_config_path != NULL) {
-		error = git_config_add_file_ondisk(cfg, global_config_path, 2);
-		if (error < GIT_SUCCESS)
-			goto cleanup;
+		if (git_config_add_file_ondisk(cfg, global_config_path, 2) < 0)
+			goto on_error;
 	}
 
 	if (system_config_path != NULL) {
-		error = git_config_add_file_ondisk(cfg, system_config_path, 1);
-		if (error < GIT_SUCCESS)
-			goto cleanup;
+		if (git_config_add_file_ondisk(cfg, system_config_path, 1) < 0)
+			goto on_error;
 	}
 
 	*out = cfg;
-	return GIT_SUCCESS;
+	return 0;
 
-cleanup:
+on_error:
+	git_buf_free(&config_path);
 	git_config_free(cfg);
 	*out = NULL;
-	return error;
+	return -1;
 }
 
 int git_repository_config__weakptr(git_config **out, git_repository *repo)
 {
 	if (repo->_config == NULL) {
-		int error;
 		git_buf global_buf = GIT_BUF_INIT, system_buf = GIT_BUF_INIT;
+		int res;
 
 		const char *global_config_path = NULL;
 		const char *system_config_path = NULL;
 
-		if (git_config_find_global_r(&global_buf) == GIT_SUCCESS)
+		if (git_config_find_global_r(&global_buf) == 0)
 			global_config_path = global_buf.ptr;
 
-		if (git_config_find_system_r(&system_buf) == GIT_SUCCESS)
+		if (git_config_find_system_r(&system_buf) == 0)
 			system_config_path = system_buf.ptr;
 
-		error = load_config(&repo->_config, repo, global_config_path, system_config_path);
+		res = load_config(&repo->_config, repo, global_config_path, system_config_path);
 
 		git_buf_free(&global_buf);
 		git_buf_free(&system_buf);
 
-		if (error < GIT_SUCCESS)
-			return error;
+		if (res < 0)
+			return -1;
 
 		GIT_REFCOUNT_OWN(repo->_config, repo);
 	}
 
 	*out = repo->_config;
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_repository_config(git_config **out, git_repository *repo)
 {
-	int error = git_repository_config__weakptr(out, repo);
-
-	if (error == GIT_SUCCESS) {
-		GIT_REFCOUNT_INC(*out);
-	}
+	if (git_repository_config__weakptr(out, repo) < 0)
+		return -1;
 
-	return error;
+	GIT_REFCOUNT_INC(*out);
+	return 0;
 }
 
 void git_repository_set_config(git_repository *repo, git_config *config)
@@ -312,34 +293,32 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
 	assert(repo && out);
 
 	if (repo->_odb == NULL) {
-		int error;
 		git_buf odb_path = GIT_BUF_INIT;
+		int res;
 
-		error = git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR);
-		if (error < GIT_SUCCESS)
-			return error;
+		if (git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR) < 0)
+			return -1;
 
-		error = git_odb_open(&repo->_odb, odb_path.ptr);
+		res = git_odb_open(&repo->_odb, odb_path.ptr);
 		git_buf_free(&odb_path); /* done with path */
-		if (error < GIT_SUCCESS)
-			return error;
+
+		if (res < 0)
+			return -1;
 
 		GIT_REFCOUNT_OWN(repo->_odb, repo);
 	}
 
 	*out = repo->_odb;
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_repository_odb(git_odb **out, git_repository *repo)
 {
-	int error = git_repository_odb__weakptr(out, repo);
+	if (git_repository_odb__weakptr(out, repo) < 0)
+		return -1;
 
-	if (error == GIT_SUCCESS) {
-		GIT_REFCOUNT_INC(*out);
-	}
-
-	return error;
+	GIT_REFCOUNT_INC(*out);
+	return 0;
 }
 
 void git_repository_set_odb(git_repository *repo, git_odb *odb)
@@ -357,34 +336,32 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
 	assert(out && repo);
 
 	if (repo->_index == NULL) {
-		int error;
+		int res;
 		git_buf index_path = GIT_BUF_INIT;
 
-		error = git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE);
-		if (error < GIT_SUCCESS)
-			return error;
+		if (git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE) < 0)
+			return -1;
 
-		error = git_index_open(&repo->_index, index_path.ptr);
+		res = git_index_open(&repo->_index, index_path.ptr);
 		git_buf_free(&index_path); /* done with path */
-		if (error < GIT_SUCCESS)
-			return error;
+
+		if (res < 0)
+			return -1;
 
 		GIT_REFCOUNT_OWN(repo->_index, repo);
 	}
 
 	*out = repo->_index;
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_repository_index(git_index **out, git_repository *repo)
 {
-	int error = git_repository_index__weakptr(out, repo);
+	if (git_repository_index__weakptr(out, repo) < 0)
+		return -1;
 
-	if (error == GIT_SUCCESS) {
-		GIT_REFCOUNT_INC(*out);
-	}
-
-	return error;
+	GIT_REFCOUNT_INC(*out);
+	return 0;
 }
 
 void git_repository_set_index(git_repository *repo, git_index *index)
@@ -404,12 +381,13 @@ static int retrieve_device(dev_t *device_out, const char *path)
 
 	assert(device_out);
 
-	if (p_lstat(path, &path_info))
-		return git__throw(GIT_EOSERR, "Failed to get file informations: %s", path);
+	if (p_lstat(path, &path_info)) {
+		giterr_set(GITERR_OS, "Failed to retrieve file information: %s", strerror(errno));
+		return -1;
+	}
 
 	*device_out = path_info.st_dev;
-
-	return GIT_SUCCESS;
+	return 0;
 }
 
 /*
@@ -473,35 +451,31 @@ static int retrieve_ceiling_directories_offset(
 static int read_gitfile(git_buf *path_out, const char *file_path, const char *base_path)
 {
 	git_buf file = GIT_BUF_INIT;
-	int error;
 
 	assert(path_out && file_path);
 
-	error = git_futils_readbuffer(&file, file_path);
-	if (error < GIT_SUCCESS)
-		return error;
-
-	if (git__prefixcmp((char *)file.ptr, GIT_FILE_CONTENT_PREFIX)) {
-		git_buf_free(&file);
-		return git__throw(GIT_ENOTFOUND, "Invalid gitfile format `%s`", file_path);
-	}
+	if (git_futils_readbuffer(&file, file_path) < 0)
+		return -1;
 
 	git_buf_rtrim(&file);
 
-	if (strlen(GIT_FILE_CONTENT_PREFIX) == file.size) {
+	if (git__prefixcmp((char *)file.ptr, GIT_FILE_CONTENT_PREFIX) != 0 ||
+		strlen(GIT_FILE_CONTENT_PREFIX) == file.size) {
 		git_buf_free(&file);
-		return git__throw(GIT_ENOTFOUND, "No path in git file `%s`", file_path);
+		giterr_set(GITERR_REPOSITORY, "The `.git` file at '%s' is corrupted", file_path);
+		return -1;
 	}
 
-	error = git_path_prettify_dir(path_out,
-		((char *)file.ptr) + strlen(GIT_FILE_CONTENT_PREFIX), base_path);
+	if (git_path_prettify_dir(path_out,
+		((char *)file.ptr) + strlen(GIT_FILE_CONTENT_PREFIX), base_path) < 0) {
+		git_buf_free(&file);
+		giterr_set(GITERR_REPOSITORY,
+			"The `.git` file at '%s' points to an invalid path.", file_path);
+		return -1;
+	}
 
 	git_buf_free(&file);
-
-	if (error == GIT_SUCCESS && git_path_exists(path_out->ptr) == true)
-		return GIT_SUCCESS;
-
-	return git__throw(GIT_EOBJCORRUPTED, "The `.git` file points to a nonexistent path");
+	return 0;
 }
 
 int git_repository_discover(
@@ -511,7 +485,7 @@ int git_repository_discover(
 	int across_fs,
 	const char *ceiling_dirs)
 {
-	int error, ceiling_offset;
+	int res, ceiling_offset;
 	git_buf bare_path = GIT_BUF_INIT;
 	git_buf normal_path = GIT_BUF_INIT;
 	git_buf *found_path = NULL;
@@ -521,22 +495,20 @@ int git_repository_discover(
 
 	*repository_path = '\0';
 
-	error = git_path_prettify_dir(&bare_path, start_path, NULL);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	res = git_path_prettify_dir(&bare_path, start_path, NULL);
+	if (res < 0)
+		return res;
 
 	if (!across_fs) {
-		error = retrieve_device(&current_device, bare_path.ptr);
-		if (error < GIT_SUCCESS)
-			goto cleanup;
+		if (retrieve_device(&current_device, bare_path.ptr) < 0)
+			goto on_error;
 	}
 
 	ceiling_offset = retrieve_ceiling_directories_offset(bare_path.ptr, ceiling_dirs);
 
 	while(1) {
-		error = git_buf_joinpath(&normal_path, bare_path.ptr, DOT_GIT);
-		if (error < GIT_SUCCESS)
-			break;
+		if (git_buf_joinpath(&normal_path, bare_path.ptr, DOT_GIT) < 0)
+			goto on_error;
 
 		/**
 		 * If the `.git` file is regular instead of
@@ -545,18 +517,21 @@ int git_repository_discover(
 		if (git_path_isfile(normal_path.ptr) == true) {
 			git_buf gitfile_path = GIT_BUF_INIT;
 
-			error = read_gitfile(&gitfile_path, normal_path.ptr, bare_path.ptr);
-			if (error < GIT_SUCCESS)
-				git__rethrow(error, "Unable to read git file `%s`", normal_path.ptr);
-			else if ((error = quickcheck_repository_dir(&gitfile_path)) < GIT_SUCCESS)
-				git__throw(GIT_ENOTFOUND,
-					"The `.git` file found at '%s' points "
-					"to a nonexistent git folder", normal_path.ptr);
-			else {
-				git_buf_swap(&normal_path, &gitfile_path);
-				found_path = &normal_path;
+			if (read_gitfile(&gitfile_path, normal_path.ptr, bare_path.ptr) < 0) {
+				git_buf_free(&gitfile_path);
+				goto on_error;
 			}
 
+			if (valid_repository_path(&gitfile_path) == false) {
+				git_buf_free(&gitfile_path);
+				giterr_set(GITERR_REPOSITORY,
+					"The `.git` file found at '%s' points to an invalid git folder", normal_path.ptr);
+				goto on_error;
+			}
+
+			git_buf_swap(&normal_path, &gitfile_path);
+			found_path = &normal_path;
+
 			git_buf_free(&gitfile_path);
 			break;
 		}
@@ -565,8 +540,7 @@ int git_repository_discover(
 		 * If the `.git` file is a folder, we check inside of it
 		 */
 		if (git_path_isdir(normal_path.ptr) == true) {
-			error = quickcheck_repository_dir(&normal_path);
-			if (error == GIT_SUCCESS) {
+			if (valid_repository_path(&normal_path) == true) {
 				found_path = &normal_path;
 				break;
 			}
@@ -576,8 +550,7 @@ int git_repository_discover(
 		 * Otherwise, the repository may be bare, let's check
 		 * the root anyway
 		 */
-		error = quickcheck_repository_dir(&bare_path);
-		if (error == GIT_SUCCESS) {
+		if (valid_repository_path(&bare_path) == true) {
 			found_path = &bare_path;
 			break;
 		}
@@ -585,147 +558,146 @@ int git_repository_discover(
 		/**
 		 * If we didn't find it, walk up the tree
 		 */
-		error = git_path_dirname_r(&normal_path, bare_path.ptr);
-		if (error < GIT_SUCCESS) {
-			git__rethrow(GIT_EOSERR, "Failed to dirname '%s'", bare_path.ptr);
-			break;
-		}
+		if (git_path_dirname_r(&normal_path, bare_path.ptr) < 0)
+			goto on_error;
 
 		git_buf_swap(&bare_path, &normal_path);
 
 		if (!across_fs) {
 			dev_t new_device;
-			error = retrieve_device(&new_device, bare_path.ptr);
+			if (retrieve_device(&new_device, bare_path.ptr) < 0)
+				goto on_error;
+
+			if (current_device != new_device)
+				goto on_not_found;
 
-			if (error < GIT_SUCCESS || current_device != new_device) {
-				error = git__throw(GIT_ENOTAREPO,
-					"Not a git repository (or any parent up to mount parent %s)\n"
-					"Stopping at filesystem boundary.", normal_path.ptr);
-				break;
-			}
 			current_device = new_device;
 		}
 
 		/* nothing has been found, lets try the parent directory
 		 * but stop if we hit one of the ceiling directories
 		 */
-		if (bare_path.ptr[ceiling_offset] == '\0') {
-			error = git__throw(GIT_ENOTAREPO,
-				"Not a git repository (or any of the parent directories): %s", start_path);
-			break;
-		}
+		if (bare_path.ptr[ceiling_offset] == '\0')
+			goto on_not_found;
 	}
 
-	assert(found_path || error != GIT_SUCCESS);
-
-	if (found_path) {
-		if ((error = git_path_to_dir(found_path)) < GIT_SUCCESS)
-			git__rethrow(error, "Could not convert git repository to directory");
-		else if (size < (size_t)(found_path->size + 1))
-			error = git__throw(GIT_ESHORTBUFFER,
-				"The repository buffer is not long enough to "
-				"handle the repository path `%s`", found_path->ptr);
-		else
-			git_buf_copy_cstr(repository_path, size, found_path);
+	assert(found_path);
+
+	if (git_path_to_dir(found_path) < 0)
+		goto on_error;
+
+	if (size < (size_t)(found_path->size + 1)) {
+		giterr_set(GITERR_REPOSITORY,
+			"The given buffer is too long to store the discovered path");
+		goto on_error;
 	}
 
-cleanup:
+	/* success: we discovered a repository */
+	git_buf_copy_cstr(repository_path, size, found_path);
+
+	git_buf_free(&bare_path);
+	git_buf_free(&normal_path);
+	return 0;
+
+on_error: /* unrecoverable error */
 	git_buf_free(&bare_path);
 	git_buf_free(&normal_path);
-	return error;
+	return -1;
+
+on_not_found: /* failed to discover the repository */
+	git_buf_free(&bare_path);
+	git_buf_free(&normal_path);
+	return GIT_ENOTFOUND;
 }
 
 static int check_repositoryformatversion(git_repository *repo)
 {
 	git_config *config;
-	int version, error = GIT_SUCCESS;
-
-	if ((error = git_repository_config(&config, repo)) < GIT_SUCCESS)
-		return git__throw(error, "Failed to open config file.");
+	int version;
 
-	error = git_config_get_int32(config, GIT_CONFIG_CORE_REPOSITORYFORMATVERSION, &version);
+	if (git_repository_config__weakptr(&config, repo) < 0)
+		return -1;
 
-	if (GIT_REPOSITORYFORMATVERSION < version)
-		error = git__throw(GIT_ERROR, "Unsupported git repository version (Expected version <= %d, found %d).", GIT_REPOSITORYFORMATVERSION, version);
+	if (git_config_get_int32(config, GIT_CONFIG_CORE_REPOSITORYFORMATVERSION, &version) < 0)
+		return -1;
 
-	git_config_free(config);
+	if (GIT_REPOSITORYFORMATVERSION < version) {
+		giterr_set(GITERR_REPOSITORY,
+			"Unsupported repository version %d. Only versions up to %d are supported.",
+			version, GIT_REPOSITORYFORMATVERSION);
+		return -1;
+	}
 
-	return error;
+	return 0;
 }
 
 static int repo_init_reinit(git_repository **repo_out, const char *repository_path, int is_bare)
 {
-	int error;
 	git_repository *repo = NULL;
 
-	if ((error = git_repository_open(&repo, repository_path)) < GIT_SUCCESS)
-		goto error;
+	GIT_UNUSED(is_bare);
 
-	if ((error = check_repositoryformatversion(repo)) < GIT_SUCCESS)
-		goto error;
+	if (git_repository_open(&repo, repository_path) < 0)
+		return -1;
+
+	if (check_repositoryformatversion(repo) < 0) {
+		git_repository_free(repo);
+		return -1;
+	}
 
 	/* TODO: reinitialize the templates */
 
 	*repo_out = repo;
-
-	return GIT_SUCCESS;
-
-error:
-	git_repository_free(repo);
-
-	return git__rethrow(error,
-		"Failed to reinitialize the %srepository at '%s'. ",
-		is_bare ? "bare " : "", repository_path);
+	return 0;
 }
 
 static int repo_init_createhead(const char *git_dir)
 {
-	int error;
 	git_buf ref_path = GIT_BUF_INIT;
 	git_filebuf ref = GIT_FILEBUF_INIT;
 
-	if (!(error = git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) &&
-		!(error = git_filebuf_open(&ref, ref_path.ptr, 0)) &&
-		!(error = git_filebuf_printf(&ref, "ref: refs/heads/master\n")))
-		error = git_filebuf_commit(&ref, GIT_REFS_FILE_MODE);
+	if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
+		git_filebuf_open(&ref, ref_path.ptr, 0) < 0 ||
+		git_filebuf_printf(&ref, "ref: refs/heads/master\n") < 0 ||
+		git_filebuf_commit(&ref, GIT_REFS_FILE_MODE) < 0)
+		return -1;
 
 	git_buf_free(&ref_path);
-	return error;
+	return 0;
 }
 
 static int repo_init_config(const char *git_dir, int is_bare)
 {
 	git_buf cfg_path = GIT_BUF_INIT;
 	git_config *config = NULL;
-	int error = GIT_SUCCESS;
 
 #define SET_REPO_CONFIG(type, name, val) {\
-	error = git_config_set_##type(config, name, val);\
-	if (error < GIT_SUCCESS)\
-		goto cleanup;\
+	if (git_config_set_##type(config, name, val) < 0) { \
+		git_buf_free(&cfg_path); \
+		git_config_free(config); \
+		return -1; } \
 }
 
-	error = git_buf_joinpath(&cfg_path, git_dir, GIT_CONFIG_FILENAME_INREPO);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	if (git_buf_joinpath(&cfg_path, git_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
+		return -1;
 
-	error = git_config_open_ondisk(&config, cfg_path.ptr);
-	if (error < GIT_SUCCESS)
-		goto cleanup;
+	if (git_config_open_ondisk(&config, cfg_path.ptr) < 0) {
+		git_buf_free(&cfg_path);
+		return -1;
+	}
 
 	SET_REPO_CONFIG(bool, "core.bare", is_bare);
 	SET_REPO_CONFIG(int32, GIT_CONFIG_CORE_REPOSITORYFORMATVERSION, GIT_REPOSITORYFORMATVERSION);
 	/* TODO: what other defaults? */
 
-cleanup:
 	git_buf_free(&cfg_path);
 	git_config_free(config);
-	return error;
+	return 0;
 }
 
 static int repo_init_structure(const char *git_dir, int is_bare)
 {
-	int error, i;
+	int i;
 	struct { const char *dir; mode_t mode; } dirs[] = {
 		{ GIT_OBJECTS_INFO_DIR, GIT_OBJECT_DIR_MODE }, /* '/objects/info/' */
 		{ GIT_OBJECTS_PACK_DIR, GIT_OBJECT_DIR_MODE }, /* '/objects/pack/' */
@@ -735,100 +707,80 @@ static int repo_init_structure(const char *git_dir, int is_bare)
 	};
 
 	/* Make the base directory */
-	error = git_futils_mkdir_r(git_dir, NULL, is_bare ?
-							   GIT_BARE_DIR_MODE : GIT_DIR_MODE);
-	if (error < GIT_SUCCESS)
-		return git__rethrow(error, "Failed to initialize repository structure. Could not mkdir");
+	if (git_futils_mkdir_r(git_dir, NULL, is_bare ? GIT_BARE_DIR_MODE : GIT_DIR_MODE) < 0)
+		return -1;
 
 	/* Hides the ".git" directory */
 	if (!is_bare) {
 #ifdef GIT_WIN32
-		error = p_hide_directory__w32(git_dir);
-		if (error < GIT_SUCCESS)
-			return git__rethrow(error, "Failed to initialize repository structure");
+		if (p_hide_directory__w32(git_dir) < 0) {
+			giterr_set(GITERR_REPOSITORY,
+				"Failed to mark Git repository folder as hidden");
+			return -1;
+		}
 #endif
 	}
 
 	/* Make subdirectories as needed */
 	for (i = 0; dirs[i].dir != NULL; ++i) {
-		error = git_futils_mkdir_r(dirs[i].dir, git_dir, dirs[i].mode);
-		if (error < GIT_SUCCESS)
-			return git__rethrow(error,
-				"Failed to create repository folder `%s`", dirs[i].dir);
+		if (git_futils_mkdir_r(dirs[i].dir, git_dir, dirs[i].mode) < 0)
+			return -1;
 	}
 
 	/* TODO: what's left? templates? */
-
-	return error;
+	return 0;
 }
 
 int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
 {
-	int error = GIT_SUCCESS;
-	git_repository *repo = NULL;
 	git_buf repository_path = GIT_BUF_INIT;
 
 	assert(repo_out && path);
 
-	error = git_buf_joinpath(&repository_path, path, is_bare ? "" : GIT_DIR);
-	if (error < GIT_SUCCESS)
-		return error;
+	if (git_buf_joinpath(&repository_path, path, is_bare ? "" : GIT_DIR) < 0)
+		return -1;
 
 	if (git_path_isdir(repository_path.ptr) == true) {
-		if (quickcheck_repository_dir(&repository_path) == GIT_SUCCESS) {
-			error = repo_init_reinit(repo_out, repository_path.ptr, is_bare);
+		if (valid_repository_path(&repository_path) == true) {
+			int res = repo_init_reinit(repo_out, repository_path.ptr, is_bare);
 			git_buf_free(&repository_path);
-			return error;
+			return res;
 		}
 	}
 
-	if (!(error = repo_init_structure(repository_path.ptr, is_bare)) &&
-		!(error = repo_init_config(repository_path.ptr, is_bare)) &&
-		!(error = repo_init_createhead(repository_path.ptr)))
-		error = git_repository_open(repo_out, repository_path.ptr);
-	else
-		git_repository_free(repo);
+	if (repo_init_structure(repository_path.ptr, is_bare) < 0 ||
+		repo_init_config(repository_path.ptr, is_bare) < 0 || 
+		repo_init_createhead(repository_path.ptr) < 0 ||
+		git_repository_open(repo_out, repository_path.ptr) < 0) {
+		git_buf_free(&repository_path);
+		return -1;
+	}
 
 	git_buf_free(&repository_path);
-
-	if (error != GIT_SUCCESS)
-		git__rethrow(error, "Failed to (re)init the repository `%s`", path);
-
-	return error;
+	return 0;
 }
 
 int git_repository_head_detached(git_repository *repo)
 {
 	git_reference *ref;
-	int error;
-	size_t _size;
-	git_otype type;
 	git_odb *odb = NULL;
+	int exists;
 
-	error = git_repository_odb__weakptr(&odb, repo);
-	if (error < GIT_SUCCESS)
-		return error;
+	if (git_repository_odb__weakptr(&odb, repo) < 0)
+		return -1;
 
-	error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE);
-	if (error < GIT_SUCCESS)
-		return error;
+	if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
+		return -1;
 
 	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
 		git_reference_free(ref);
 		return 0;
 	}
 
-	error = git_odb_read_header(&_size, &type, odb, git_reference_oid(ref));
+	exists = git_odb_exists(odb, git_reference_oid(ref));
 
 	git_reference_free(ref);
-
-	if (error < GIT_SUCCESS)
-		return error;
-
-	if (type != GIT_OBJ_COMMIT)
-		return git__throw(GIT_EOBJCORRUPTED, "HEAD is not a commit");
-
-	return 1;
+	return exists;
 }
 
 int git_repository_head(git_reference **head_out, git_repository *repo)
@@ -839,32 +791,35 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
 	*head_out = NULL;
 
 	error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE);
-	if (error < GIT_SUCCESS)
-		return git__rethrow(GIT_ENOTAREPO, "Failed to locate the HEAD");
+	if (error < 0)
+		return error;
 
 	error = git_reference_resolve(&resolved_ref, ref);
-	if (error < GIT_SUCCESS) {
+	if (error < 0) {
 		git_reference_free(ref);
-		return git__rethrow(error, "Failed to resolve the HEAD");
+		return error;
 	}
 
 	git_reference_free(ref);
-
 	*head_out = resolved_ref;
-	return GIT_SUCCESS;
+	return 0;
 }
 
 int git_repository_head_orphan(git_repository *repo)
 {
-	git_reference *ref;
+	git_reference *ref = NULL;
 	int error;
 
 	error = git_repository_head(&ref, repo);
+	git_reference_free(ref);
 
-	if (error == GIT_SUCCESS)
-		git_reference_free(ref);
+	if (error == GIT_ENOTFOUND)
+		return 1;
 
-	return error == GIT_ENOTFOUND ? 1 : error;
+	if (error < 0)
+		return -1;
+
+	return 0;
 }
 
 int git_repository_is_empty(git_repository *repo)
@@ -872,9 +827,8 @@ int git_repository_is_empty(git_repository *repo)
 	git_reference *head = NULL, *branch = NULL;
 	int error;
 
-	error = git_reference_lookup(&head, repo, "HEAD");
-	if (error < GIT_SUCCESS)
-		return git__throw(error, "Corrupted repository. HEAD does not exist");
+	if (git_reference_lookup(&head, repo, "HEAD") < 0)
+		return -1;
 
 	if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
 		git_reference_free(head);
@@ -891,7 +845,13 @@ int git_repository_is_empty(git_repository *repo)
 	git_reference_free(head);
 	git_reference_free(branch);
 
-	return error == GIT_ENOTFOUND ? 1 : error;
+	if (error == GIT_ENOTFOUND)
+		return 1;
+
+	if (error < 0)
+		return -1;
+
+	return 0;
 }
 
 const char *git_repository_path(git_repository *repo)
@@ -917,8 +877,7 @@ int git_repository_set_workdir(git_repository *repo, const char *workdir)
 	free(repo->workdir);
 
 	repo->workdir = git__strdup(workdir);
-	if (repo->workdir == NULL)
-		return GIT_ENOMEM;
+	GITERR_CHECK_ALLOC(repo->workdir);
 
 	repo->is_bare = 0;
 	return GIT_SUCCESS;
diff --git a/src/status.c b/src/status.c
index e80fc02..6315d63 100644
--- a/src/status.c
+++ b/src/status.c
@@ -423,10 +423,8 @@ static int dirent_cb(void *state, git_buf *a)
 			if (git_tree_entry_type(m) == GIT_OBJ_TREE)
 				git_path_to_dir(&st->head_tree_relative_path);
 
-			error = git_buf_lasterror(&st->head_tree_relative_path);
-			if (error < GIT_SUCCESS)
-				return git__rethrow(error, "An error occured while "
-					"determining the status of '%s'", a->ptr);
+			if (git_buf_oom(&st->head_tree_relative_path))
+				return GIT_ENOMEM;
 
 			m_name = st->head_tree_relative_path.ptr;
 		} else
diff --git a/src/tag.c b/src/tag.c
index 6076eb6..a5089e7 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -193,10 +193,9 @@ static int write_tag_annotation(
 	git_buf_putc(&tag, '\n');
 	git_buf_puts(&tag, message);
 
-	error = git_buf_lasterror(&tag);
-	if (error < GIT_SUCCESS) {
+	if (git_buf_oom(&tag)) {
 		git_buf_free(&tag);
-		return git__rethrow(error, "Not enough memory to build the tag data");
+		return git__throw(GIT_ENOMEM, "Not enough memory to build the tag data");
 	}
 
 	error = git_repository_odb__weakptr(&odb, repo);
diff --git a/src/transports/git.c b/src/transports/git.c
index 88e7e81..befdec5 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -68,7 +68,10 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url)
 	git_buf_put(request, url, delim - url);
 	git_buf_putc(request, '\0');
 
-	return git_buf_lasterror(request);
+	if (git_buf_oom(request))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 static int send_request(GIT_SOCKET s, const char *cmd, const char *url)
diff --git a/src/transports/http.c b/src/transports/http.c
index 2842d08..f16a802 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -77,7 +77,10 @@ static int gen_request(git_buf *buf, const char *url, const char *host, const ch
 	}
 	git_buf_puts(buf, "\r\n");
 
-	return git_buf_lasterror(buf);
+	if (git_buf_oom(buf))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 static int do_connect(transport_http *t, const char *host, const char *port)
diff --git a/src/tree.c b/src/tree.c
index 19681e3..5957f7a 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -569,9 +569,9 @@ int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *b
 		git_buf_put(&tree, (char *)entry->oid.id, GIT_OID_RAWSZ);
 	}
 
-	if ((error = git_buf_lasterror(&tree)) < GIT_SUCCESS) {
+	if (git_buf_oom(&tree)) {
 		git_buf_free(&tree);
-		return git__rethrow(error, "Not enough memory to build the tree data");
+		return git__throw(GIT_ENOMEM, "Not enough memory to build the tree data");
 	}
 
 	error = git_repository_odb__weakptr(&odb, repo);
@@ -720,8 +720,9 @@ static int tree_walk_post(
 			/* append the next entry to the path */
 			git_buf_puts(path, entry->filename);
 			git_buf_putc(path, '/');
-			if ((error = git_buf_lasterror(path)) < GIT_SUCCESS)
-				break;
+
+			if (git_buf_oom(path))
+				return GIT_ENOMEM;
 
 			error = tree_walk_post(subtree, callback, path, payload);
 			if (error < GIT_SUCCESS)
diff --git a/src/util.h b/src/util.h
index f77c91d..803c63d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -7,6 +7,8 @@
 #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))))
@@ -23,7 +25,7 @@ GIT_INLINE(void *) git__malloc(size_t len)
 {
 	void *ptr = malloc(len);
 	if (!ptr)
-		git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)len);
+		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)len);
 	return ptr;
 }
 
@@ -31,7 +33,7 @@ GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
 {
 	void *ptr = calloc(nelem, elsize);
 	if (!ptr)
-		git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)elsize);
+		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)nelem*elsize);
 	return ptr;
 }
 
@@ -39,7 +41,7 @@ GIT_INLINE(char *) git__strdup(const char *str)
 {
 	char *ptr = strdup(str);
 	if (!ptr)
-		git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
+		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to duplicate string");
 	return ptr;
 }
 
@@ -54,7 +56,7 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 
 	ptr = (char*)malloc(length + 1);
 	if (!ptr) {
-		git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
+		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to duplicate string");
 		return NULL;
 	}
 
@@ -68,7 +70,7 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
 {
 	void *new_ptr = realloc(ptr, size);
 	if (!new_ptr)
-		git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)size);
+		giterr_set(GITERR_NOMEMORY, "Out of memory. Failed to allocate %d bytes.", (int)size);
 	return new_ptr;
 }
 
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 8e70719..2d7c2e3 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -1,4 +1,4 @@
-/*
+/ < 0)
  * Copyright (C) 2009-2012 the libgit2 contributors
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
@@ -288,18 +288,13 @@ int p_rmdir(const char* path)
 
 int p_hide_directory__w32(const char *path)
 {
-	int error;
+	int res;
 	wchar_t* buf = gitwin_to_utf16(path);
 
-	error = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0 ?
-		GIT_SUCCESS : GIT_ERROR; /* MSDN states a "non zero" value indicates a success */
-
+	res = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN);
 	git__free(buf);
-
-	if (error < GIT_SUCCESS)
-		error = git__throw(GIT_EOSERR, "Failed to hide directory '%s'", path);
-
-	return error;
+	
+	return (res != 0) ?  GIT_SUCCESS : GIT_ERROR; /* MSDN states a "non zero" value indicates a success */
 }
 
 char *p_realpath(const char *orig_path, char *buffer)
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index 7c45e01..764af15 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -99,11 +99,15 @@ static int append_ceiling_dir(git_buf *ceiling_dirs, const char *path)
 
 	if (ceiling_dirs->size > 0)
 		git_buf_puts(ceiling_dirs, ceiling_separator);
+
 	git_buf_puts(ceiling_dirs, pretty_path.ptr);
 
 	git_buf_free(&pretty_path);
 
-	return git_buf_lasterror(ceiling_dirs);
+	if (git_buf_oom(ceiling_dirs))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 BEGIN_TEST(discover0, "test discover")
@@ -119,7 +123,7 @@ BEGIN_TEST(discover0, "test discover")
 	must_pass(append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER));
 	ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
 
-	must_be_true(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
+	must_fail(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));
 
 	must_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1));
 	must_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));