Commit 8bd6c0ab8354fff51f0afaa15a56a9f864da7ab5

Carlos Martín Nieto 2011-04-06T15:49:29

Merge remote-tracking branch 'upstream/development' into config

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
diff --git a/README.md b/README.md
index 1254adc..dae6a76 100644
--- a/README.md
+++ b/README.md
@@ -85,8 +85,8 @@ The waf build system for libgit2 accepts the following flags:
 	--arch=[ia64|x64|x86|x86_amd64|x86_ia64]
 		Force a specific architecture for compilers that support it.
 
-	--without-sqlite
-		Disable sqlite support.
+	--with-sqlite
+		Enable sqlite support.
 
 You can run `./waf --help` to see a full list of install options and
 targets.
diff --git a/include/git2/tag.h b/include/git2/tag.h
index ee92cd5..c751a13 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -140,7 +140,8 @@ GIT_EXTERN(const char *) git_tag_message(git_tag *t);
  * @param repo Repository where to store the tag
  *
  * @param tag_name Name for the tag; this name is validated
- * for consistency
+ * for consistency. It should also not conflict with an 
+ * already existing tag name
  *
  * @param target OID to which this tag points; note that no
  *	validation is done on this OID. Use the _o version of this
@@ -188,6 +189,92 @@ GIT_EXTERN(int) git_tag_create_o(
 		const git_signature *tagger,
 		const char *message);
 
+/**
+ * Create a new tag in the repository from a buffer
+ *
+ * @param oid Pointer where to store the OID of the newly created tag
+ *
+ * @param repo Repository where to store the tag
+ *
+ * @param buffer Raw tag data
+ */
+GIT_EXTERN(int) git_tag_create_frombuffer(
+		git_oid *oid,
+		git_repository *repo,
+		const char *buffer);
+
+/**
+ * Create a new tag in the repository from an OID
+ * and overwrite an already existing tag reference, if any.
+ *
+ * @param oid Pointer where to store the OID of the
+ *	newly created tag
+ *
+ * @param repo Repository where to store the tag
+ *
+ * @param tag_name Name for the tag; this name is validated
+ * for consistency.
+ *
+ * @param target OID to which this tag points; note that no
+ *	validation is done on this OID. Use the _fo version of this
+ *	method to assure a proper object is being tagged
+ *
+ * @param target_type Type of the tagged OID; note that no
+ *	validation is performed here either
+ *
+ * @param tagger Signature of the tagger for this tag, and
+ *  of the tagging time
+ *
+ * @param message Full message for this tag
+ *
+ * @return 0 on success; error code otherwise.
+ *	A tag object is written to the ODB, and a proper reference
+ *	is written in the /refs/tags folder, pointing to it
+ */
+GIT_EXTERN(int) git_tag_create_f(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_oid *target,
+		git_otype target_type,
+		const git_signature *tagger,
+		const char *message);
+
+/**
+ * Create a new tag in the repository from an existing
+ * `git_object` instance and overwrite an already existing 
+ * tag reference, if any.
+ *
+ * This method replaces the `target` and `target_type`
+ * paremeters of `git_tag_create_f` by a single instance
+ * of a `const git_object *`, which is assured to be
+ * a proper object in the ODB and hence will create
+ * a valid tag
+ *
+ * @see git_tag_create_f
+ */
+GIT_EXTERN(int) git_tag_create_fo(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_object *target,
+		const git_signature *tagger,
+		const char *message);
+
+/**
+ * Delete an existing tag reference.
+ *
+ * @param repo Repository where lives the tag
+ *
+ * @param tag_name Name of the tag to be deleted;
+ * this name is validated for consistency.
+ *
+ * @return 0 on success; error code otherwise.
+ */
+GIT_EXTERN(int) git_tag_delete(
+		git_repository *repo,
+		const char *tag_name);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 164aec9..0caf60a 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -93,7 +93,7 @@ GIT_EXTERN(size_t) git_tree_entrycount(git_tree *tree);
  * @param filename the filename of the desired entry
  * @return the tree entry; NULL if not found
  */
-GIT_EXTERN(git_tree_entry *) git_tree_entry_byname(git_tree *tree, const char *filename);
+GIT_EXTERN(const git_tree_entry *) git_tree_entry_byname(git_tree *tree, const char *filename);
 
 /**
  * Lookup a tree entry by its position in the tree
@@ -102,7 +102,7 @@ GIT_EXTERN(git_tree_entry *) git_tree_entry_byname(git_tree *tree, const char *f
  * @param idx the position in the entry list
  * @return the tree entry; NULL if not found
  */
-GIT_EXTERN(git_tree_entry *) git_tree_entry_byindex(git_tree *tree, int idx);
+GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex(git_tree *tree, int idx);
 
 /**
  * Get the UNIX file attributes of a tree entry
@@ -110,7 +110,7 @@ GIT_EXTERN(git_tree_entry *) git_tree_entry_byindex(git_tree *tree, int idx);
  * @param entry a tree entry
  * @return attributes as an integer
  */
-GIT_EXTERN(unsigned int) git_tree_entry_attributes(git_tree_entry *entry);
+GIT_EXTERN(unsigned int) git_tree_entry_attributes(const git_tree_entry *entry);
 
 /**
  * Get the filename of a tree entry
@@ -118,7 +118,7 @@ GIT_EXTERN(unsigned int) git_tree_entry_attributes(git_tree_entry *entry);
  * @param entry a tree entry
  * @return the name of the file
  */
-GIT_EXTERN(const char *) git_tree_entry_name(git_tree_entry *entry);
+GIT_EXTERN(const char *) git_tree_entry_name(const git_tree_entry *entry);
 
 /**
  * Get the id of the object pointed by the entry
@@ -126,7 +126,7 @@ GIT_EXTERN(const char *) git_tree_entry_name(git_tree_entry *entry);
  * @param entry a tree entry
  * @return the oid of the object
  */
-GIT_EXTERN(const git_oid *) git_tree_entry_id(git_tree_entry *entry);
+GIT_EXTERN(const git_oid *) git_tree_entry_id(const git_tree_entry *entry);
 
 /**
  * Convert a tree entry to the git_object it points too.
@@ -134,9 +134,132 @@ GIT_EXTERN(const git_oid *) git_tree_entry_id(git_tree_entry *entry);
  * @param object pointer to the converted object
  * @param repo repository where to lookup the pointed object
  * @param entry a tree entry
- * @return a reference to the pointed object in the repository
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_tree_entry_2object(git_object **object_out, git_repository *repo, const git_tree_entry *entry);
+
+/**
+ * Write a tree to the ODB from the index file
+ *
+ * This method will scan the index and write a representation
+ * of its current state back to disk; it recursively creates
+ * tree objects for each of the subtrees stored in the index,
+ * but only returns the OID of the root tree. This is the OID
+ * that can be used e.g. to create a commit.
+ *
+ * The index instance cannot be bare, and needs to be associated
+ * to an existing repository.
+ *
+ * @param oid Pointer where to store the written tree
+ * @param index Index to write
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_tree_create_fromindex(git_oid *oid, git_index *index);
+
+/**
+ * Create a new tree builder.
+ *
+ * The tree builder can be used to create or modify
+ * trees in memory and write them as tree objects to the
+ * database.
+ *
+ * If the `source` parameter is not NULL, the tree builder
+ * will be initialized with the entries of the given tree.
+ * 
+ * If the `source` parameter is NULL, the tree builder will
+ * have no entries and will have to be filled manually.
+ *
+ * @param builder_p Pointer where to store the tree builder
+ * @param source Source tree to initialize the builder (optional)
+ * @return 0 on sucess; error code otherwise
+ */
+GIT_EXTERN(int) git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source);
+
+/**
+ * Clear all the entires in the builder
+ *
+ * @param bld Builder to clear
+ */
+GIT_EXTERN(void) git_treebuilder_clear(git_treebuilder *bld);
+
+/**
+ * Free a tree builder
+ *
+ * This will clear all the entries and free to builder.
+ * Failing to free the builder after you're done using it
+ * will result in a memory leak
+ *
+ * @param bld Builder to free
+ */
+GIT_EXTERN(void) git_treebuilder_free(git_treebuilder *bld);
+
+/**
+ * Get an entry from the builder from its filename
+ *
+ * The returned entry is owned by the builder and should
+ * not be freed manually.
+ *
+ * @param bld Tree builder
+ * @param filename Name of the entry
+ * @return pointer to the entry; NULL if not found
+ */
+GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, const char *filename);
+
+/**
+ * Add or update an entry to the builder
+ *
+ * Insert a new entry for `filename` in the builder with the
+ * given attributes.
+ *
+ * if an entry named `filename` already exists, its attributes
+ * will be updated with the given ones.
+ *
+ * The optional pointer `entry_out` can be used to retrieve a
+ * pointer to the newly created/updated entry.
+ *
+ * @param entry_out Pointer to store the entry (optional)
+ * @param bld Tree builder
+ * @param filename Filename of the entry
+ * @param id SHA1 oid of the entry
+ * @param attributes Folder attributes of the entry
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes);
+
+/**
+ * Remove an entry from the builder by its filename
+ *
+ * @param bld Tree builder
+ * @param filename Filename of the entry to remove
+ */
+GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filename);
+
+/**
+ * Filter the entries in the tree
+ *
+ * The `filter` callback will be called for each entry
+ * in the tree with a pointer to the entry and the
+ * provided `payload`: if the callback returns 1, the
+ * entry will be filtered (removed from the builder).
+ *
+ * @param bld Tree builder
+ * @param filter Callback to filter entries
+ */
+GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload);
+
+/**
+ * Write the contents of the tree builder as a tree object
+ *
+ * The tree builder will be written to the given `repo`, and
+ * it's identifying SHA1 hash will be stored in the `oid`
+ * pointer.
+ *
+ * @param oid Pointer where to store the written OID
+ * @param repo Repository where to store the object
+ * @param bld Tree builder to write
+ * @return 0 on success; error code otherwise
  */
-GIT_EXTERN(int) git_tree_entry_2object(git_object **object_out, git_repository *repo, git_tree_entry *entry);
+GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld);
 
 /** @} */
 GIT_END_DECL
diff --git a/include/git2/types.h b/include/git2/types.h
index cb5adcc..84e3d5b 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -124,6 +124,9 @@ typedef struct git_tree_entry git_tree_entry;
 /** Representation of a tree object. */
 typedef struct git_tree git_tree;
 
+/** Constructor for in-memory trees */
+typedef struct git_treebuilder git_treebuilder;
+
 /** Memory representation of an index file. */
 typedef struct git_index git_index;
 
diff --git a/src/commit.c b/src/commit.c
index 03b111d..9fc3f07 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -235,9 +235,9 @@ int git_commit_create(
 	return error;
 }
 
-int commit_parse_buffer(git_commit *commit, void *data, size_t len)
+int commit_parse_buffer(git_commit *commit, const void *data, size_t len)
 {
-	char *buffer = (char *)data;
+	const char *buffer = (char *)data;
 	const char *buffer_end = (char *)data + len;
 
 	git_oid parent_oid;
diff --git a/src/odb.c b/src/odb.c
index 33d5468..43d0ef9 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -149,6 +149,69 @@ int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
 	return git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, &raw);
 }
 
+/**
+ * FAKE WSTREAM
+ */
+
+typedef struct {
+	git_odb_stream stream;
+	char *buffer;
+	size_t size, written;
+	git_otype type;
+} fake_wstream;
+
+static int fake_wstream__fwrite(git_oid *oid, git_odb_stream *_stream)
+{
+	fake_wstream *stream = (fake_wstream *)_stream;
+	return _stream->backend->write(oid, _stream->backend, stream->buffer, stream->size, stream->type);
+}
+
+static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len)
+{
+	fake_wstream *stream = (fake_wstream *)_stream;
+
+	if (stream->written + len >= stream->size)
+		return GIT_ENOMEM;
+
+	memcpy(stream->buffer + stream->written, data, len);
+	stream->written += len;
+	return GIT_SUCCESS;
+}
+
+static void fake_wstream__free(git_odb_stream *_stream)
+{
+	fake_wstream *stream = (fake_wstream *)_stream;
+
+	free(stream->buffer);
+	free(stream);
+}
+
+static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type)
+{
+	fake_wstream *stream;
+
+	stream = git__calloc(1, sizeof(fake_wstream));
+	if (stream == NULL)
+		return GIT_ENOMEM;
+
+	stream->size = size;
+	stream->type = type;
+	stream->buffer = git__malloc(size);
+	if (stream->buffer == NULL) {
+		free(stream);
+		return GIT_ENOMEM;
+	}
+
+	stream->stream.backend = backend;
+	stream->stream.read = NULL; /* read only */
+	stream->stream.write = &fake_wstream__write;
+	stream->stream.finalize_write = &fake_wstream__fwrite;
+	stream->stream.free = &fake_wstream__free;
+	stream->stream.mode = GIT_STREAM_WRONLY;
+
+	*stream_p = (git_odb_stream *)stream;
+	return GIT_SUCCESS;
+}
 
 /***********************************************************
  *
@@ -467,6 +530,8 @@ int git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_
 
 		if (b->writestream != NULL)
 			error = b->writestream(stream, b, size, type);
+		else if (b->write != NULL)
+			error = init_fake_wstream(stream, b, size, type);
 	}
 
 	return error;
diff --git a/src/oid.c b/src/oid.c
index eb167a6..86c1e00 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -118,13 +118,13 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
 	return out;
 }
 
-int git__parse_oid(git_oid *oid, char **buffer_out,
+int git__parse_oid(git_oid *oid, const char **buffer_out,
 		const char *buffer_end, const char *header)
 {
 	const size_t sha_len = GIT_OID_HEXSZ;
 	const size_t header_len = strlen(header);
 
-	char *buffer = *buffer_out;
+	const char *buffer = *buffer_out;
 
 	if (buffer + (header_len + sha_len + 1) > buffer_end)
 		return GIT_EOBJCORRUPTED;
diff --git a/src/repository.c b/src/repository.c
index 91b95a8..abbbd12 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -346,7 +346,7 @@ static int repo_init_reinit(repo_init *results)
 {
 	/* TODO: reinit the repository */
 	results->has_been_reinit = 1;
-	return GIT_SUCCESS;
+	return GIT_ENOTIMPLEMENTED;
 }
 
 static int repo_init_createhead(git_repository *repo)
diff --git a/src/repository.h b/src/repository.h
index fef1c7d..813cac9 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -43,7 +43,7 @@ struct git_repository {
  * export */
 void git_object__free(void *object);
 
-int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
+int git__parse_oid(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
 int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid);
 
 #endif
diff --git a/src/signature.c b/src/signature.c
index 4126376..0c99755 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -109,14 +109,14 @@ static int parse_timezone_offset(const char *buffer, int *offset_out)
 }
 
 
-int git_signature__parse(git_signature *sig, char **buffer_out,
+int git_signature__parse(git_signature *sig, const char **buffer_out,
 		const char *buffer_end, const char *header)
 {
 	const size_t header_len = strlen(header);
 
 	int name_length, email_length;
-	char *buffer = *buffer_out;
-	char *line_end, *name_end, *email_end;
+	const char *buffer = *buffer_out;
+	const char *line_end, *name_end, *email_end;
 	int offset = 0;
 
 	memset(sig, 0x0, sizeof(git_signature));
@@ -159,7 +159,7 @@ int git_signature__parse(git_signature *sig, char **buffer_out,
 	if (buffer >= line_end)
 		return GIT_EOBJCORRUPTED;
 
-	sig->when.time = strtol(buffer, &buffer, 10);
+	sig->when.time = strtol(buffer, (char **)&buffer, 10);
 
 	if (sig->when.time == 0)
 		return GIT_EOBJCORRUPTED;
diff --git a/src/signature.h b/src/signature.h
index 3534cb2..feba657 100644
--- a/src/signature.h
+++ b/src/signature.h
@@ -6,7 +6,7 @@
 #include "repository.h"
 #include <time.h>
 
-int git_signature__parse(git_signature *sig, char **buffer_out, const char *buffer_end, const char *header);
+int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header);
 int git_signature__write(char **signature, const char *header, const git_signature *sig);
 
 #endif
diff --git a/src/tag.c b/src/tag.c
index 7baabab..ef58e09 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -79,7 +79,7 @@ const char *git_tag_message(git_tag *t)
 	return t->message;
 }
 
-static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
+static int parse_tag_buffer(git_tag *tag, const char *buffer, const char *buffer_end)
 {
 	static const char *tag_types[] = {
 		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
@@ -130,9 +130,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 
 	text_len = search - buffer;
 
-	if (tag->tag_name != NULL)
-		free(tag->tag_name);
-
 	tag->tag_name = git__malloc(text_len + 1);
 	memcpy(tag->tag_name, buffer, text_len);
 	tag->tag_name[text_len] = '\0';
@@ -141,8 +138,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 
 	tag->tagger = git__malloc(sizeof(git_signature));
 
-	if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0)
+	if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0) {
+		free(tag->tag_name);
+		git_signature_free(tag->tagger);
 		return error;
+	}
 
 	text_len = buffer_end - ++buffer;
 
@@ -153,39 +153,61 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 	return GIT_SUCCESS;
 }
 
-int git_tag_create_o(
-		git_oid *oid,
-		git_repository *repo,
-		const char *tag_name,
-		const git_object *target,
-		const git_signature *tagger,
-		const char *message)
+static int retreive_tag_reference(git_reference **tag_reference_out, char *ref_name_out, git_repository *repo, const char *tag_name)
 {
-	return git_tag_create(
-		oid, repo, tag_name, 
-		git_object_id(target),
-		git_object_type(target),
-		tagger, message);
+	git_reference *tag_ref;
+	int error;
+
+	git__joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name);
+	error = git_reference_lookup(&tag_ref, repo, ref_name_out);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	*tag_reference_out = tag_ref;
+
+	return GIT_SUCCESS;
 }
 
-int git_tag_create(
+static int tag_create(
 		git_oid *oid,
 		git_repository *repo,
 		const char *tag_name,
 		const git_oid *target,
 		git_otype target_type,
 		const git_signature *tagger,
-		const char *message)
+		const char *message,
+		int allow_ref_overwrite)
 {
 	size_t final_size = 0;
 	git_odb_stream *stream;
 
 	const char *type_str;
 	char *tagger_str;
+	git_reference *new_ref;
+
+	char ref_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
 
 	int type_str_len, tag_name_len, tagger_str_len, message_len;
-	int error;
+	int error, should_update_ref = 0;
+
+	/** Ensure the tag name doesn't conflict with an already existing 
+	    reference unless overwriting has explictly been requested **/
+	error = retreive_tag_reference(&new_ref, ref_name, repo, tag_name);
 
+	switch (error) {
+	case GIT_SUCCESS:
+		if (!allow_ref_overwrite)
+			return GIT_EEXISTS;	
+		should_update_ref = 1;
+		
+		/* Fall trough */
+
+	case GIT_ENOTFOUND: 
+		break;
+
+	default:
+		return error;
+	}
 
 	type_str = git_object_type2string(target_type);
 
@@ -223,16 +245,120 @@ int git_tag_create(
 	error = stream->finalize_write(oid, stream);
 	stream->free(stream);
 
-	if (error == GIT_SUCCESS) {
-		char ref_name[512];
-		git_reference *new_ref;
-		git__joinpath(ref_name, GIT_REFS_TAGS_DIR, tag_name);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	if (!should_update_ref)
 		error = git_reference_create_oid(&new_ref, repo, ref_name, oid);
-	}
+	else
+		error = git_reference_set_oid(new_ref, oid);
+	
+	return error;
+}
+
+int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer)
+{
+	git_tag tag;
+	int error;
+	git_object *obj;
+
+	assert(oid && buffer);
+
+	memset(&tag, 0, sizeof(tag));
+
+	if ((error = parse_tag_buffer(&tag, buffer, buffer + strlen(buffer))) < GIT_SUCCESS)
+		return error;
+
+	error = git_object_lookup(&obj, repo, &tag.target, tag.type);
+	if (error < GIT_SUCCESS)
+		goto cleanup;
+
+	error = git_tag_create_o(oid, repo, tag.tag_name, obj, tag.tagger, tag.message);
+
+	git_object_close(obj);
+
+cleanup:
+	git_signature_free(tag.tagger);
+	free(tag.tag_name);
+	free(tag.message);
 
 	return error;
 }
 
+int git_tag_create_o(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_object *target,
+		const git_signature *tagger,
+		const char *message)
+{
+	return tag_create(
+		oid, repo, tag_name, 
+		git_object_id(target),
+		git_object_type(target),
+		tagger, message, 0);
+}
+
+int git_tag_create(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_oid *target,
+		git_otype target_type,
+		const git_signature *tagger,
+		const char *message)
+{
+	return tag_create(
+		oid, repo, tag_name, 
+		target,
+		target_type,
+		tagger, message, 0);
+}
+
+int git_tag_create_fo(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_object *target,
+		const git_signature *tagger,
+		const char *message)
+{
+	return tag_create(
+		oid, repo, tag_name, 
+		git_object_id(target),
+		git_object_type(target),
+		tagger, message, 1);
+}
+
+int git_tag_create_f(
+		git_oid *oid,
+		git_repository *repo,
+		const char *tag_name,
+		const git_oid *target,
+		git_otype target_type,
+		const git_signature *tagger,
+		const char *message)
+{
+	return tag_create(
+		oid, repo, tag_name, 
+		target,
+		target_type,
+		tagger, message, 1);
+}
+
+int git_tag_delete(git_repository *repo, const char *tag_name)
+{
+	int error;
+	git_reference *tag_ref;
+	char ref_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+
+	error = retreive_tag_reference(&tag_ref, ref_name, repo, tag_name);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	return git_reference_delete(tag_ref);
+}
 
 int git_tag__parse(git_tag *tag, git_odb_object *obj)
 {
diff --git a/src/tree.c b/src/tree.c
index 31b286e..693ed1c 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -33,6 +33,10 @@
 #define MAX_FILEMODE 0777777
 #define MAX_FILEMODE_BYTES 6
 
+static int valid_attributes(const int attributes) {
+	return attributes >= 0 && attributes <= MAX_FILEMODE; 
+}
+
 int entry_search_cmp(const void *key, const void *array_member)
 {
 	const char *filename = (const char *)key;
@@ -79,30 +83,30 @@ const git_oid *git_tree_id(git_tree *c)
 	return git_object_id((git_object *)c);
 }
 
-unsigned int git_tree_entry_attributes(git_tree_entry *entry)
+unsigned int git_tree_entry_attributes(const git_tree_entry *entry)
 {
 	return entry->attr;
 }
 
-const char *git_tree_entry_name(git_tree_entry *entry)
+const char *git_tree_entry_name(const git_tree_entry *entry)
 {
 	assert(entry);
 	return entry->filename;
 }
 
-const git_oid *git_tree_entry_id(git_tree_entry *entry)
+const git_oid *git_tree_entry_id(const git_tree_entry *entry)
 {
 	assert(entry);
 	return &entry->oid;
 }
 
-int git_tree_entry_2object(git_object **object_out, git_repository *repo, git_tree_entry *entry)
+int git_tree_entry_2object(git_object **object_out, git_repository *repo, const git_tree_entry *entry)
 {
 	assert(entry && object_out);
 	return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
 }
 
-git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
+const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
 {
 	int idx;
 
@@ -115,7 +119,7 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
 	return git_vector_get(&tree->entries, idx);
 }
 
-git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
+const git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
 {
 	assert(tree);
 	return git_vector_get(&tree->entries, (unsigned int)idx);
@@ -127,7 +131,7 @@ size_t git_tree_entrycount(git_tree *tree)
 	return tree->entries.length;
 }
 
-static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
+static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end)
 {
 	int error = GIT_SUCCESS;
 
@@ -137,7 +141,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
 	while (buffer < buffer_end) {
 		git_tree_entry *entry;
 
-		entry = git__malloc(sizeof(git_tree_entry));
+		entry = git__calloc(1, sizeof(git_tree_entry));
 		if (entry == NULL) {
 			error = GIT_ENOMEM;
 			break;
@@ -146,7 +150,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
 		if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS)
 			return GIT_ENOMEM;
 
-		entry->attr = strtol(buffer, &buffer, 8);
+		entry->attr = strtol(buffer, (char **)&buffer, 8);
 
 		if (*buffer++ != ' ') {
 			error = GIT_EOBJCORRUPTED;
@@ -159,6 +163,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
 		}
 
 		entry->filename = git__strdup(buffer);
+		entry->filename_len = strlen(buffer);
 
 		while (buffer < buffer_end && *buffer != 0)
 			buffer++;
@@ -178,3 +183,304 @@ int git_tree__parse(git_tree *tree, git_odb_object *obj)
 	return tree_parse_buffer(tree, (char *)obj->raw.data, (char *)obj->raw.data + obj->raw.len);
 }
 
+static int write_index_entry(char *buffer, int mode, const char *path, size_t path_len, const git_oid *oid)
+{
+	int written;
+	written = sprintf(buffer, "%o %.*s%c", mode, (int)path_len, path, 0);
+	memcpy(buffer + written, &oid->id, GIT_OID_RAWSZ);
+	return written + GIT_OID_RAWSZ;
+}
+
+static int write_index(git_oid *oid, git_index *index, const char *base, int baselen, int entry_no, int maxentries)
+{
+	size_t size, offset;
+	char *buffer;
+	int nr, error;
+
+	/* Guess at some random initial size */
+	size = maxentries * 40;
+	buffer = git__malloc(size);
+	if (buffer == NULL)
+		return GIT_ENOMEM;
+		
+	offset = 0;
+	
+	for (nr = entry_no; nr < maxentries; ++nr) {
+		git_index_entry *entry = git_index_get(index, nr);
+
+		const char *pathname = entry->path, *filename, *dirname;
+		int pathlen = strlen(pathname), entrylen;
+
+		unsigned int write_mode;
+		git_oid subtree_oid;
+		git_oid *write_oid;
+		
+		/* Did we hit the end of the directory? Return how many we wrote */
+		if (baselen >= pathlen || memcmp(base, pathname, baselen) != 0)
+			break;
+		
+		/* Do we have _further_ subdirectories? */
+		filename = pathname + baselen;
+		dirname = strchr(filename, '/');
+
+		write_oid = &entry->oid;
+		write_mode = entry->mode;
+
+		if (dirname) {
+			int subdir_written;
+
+#if 0
+			if (entry->mode != S_IFDIR) {
+				free(buffer);
+				return GIT_EOBJCORRUPTED;
+			}
+#endif
+			subdir_written = write_index(&subtree_oid, index, pathname, dirname - pathname + 1, nr, maxentries);
+
+			if (subdir_written < GIT_SUCCESS) {
+				free(buffer);
+				return subdir_written;
+			}
+			
+			nr = subdir_written - 1;
+			
+			/* Now we need to write out the directory entry into this tree.. */
+			pathlen = dirname - pathname;
+			write_oid = &subtree_oid;
+			write_mode = S_IFDIR;
+		}
+
+		entrylen = pathlen - baselen;
+		if (offset + entrylen + 32 > size) {
+			size = alloc_nr(offset + entrylen + 32);
+			buffer = git__realloc(buffer, size);
+			
+			if (buffer == NULL)
+				return GIT_ENOMEM;
+		}
+
+		offset += write_index_entry(buffer + offset, write_mode, filename, entrylen, write_oid);
+	}
+	
+	error = git_odb_write(oid, index->repository->db, buffer, offset, GIT_OBJ_TREE);
+	free(buffer);
+
+	return (error == GIT_SUCCESS) ? nr : error;
+}
+
+int git_tree_create_fromindex(git_oid *oid, git_index *index)
+{
+	int error;
+
+	if (index->repository == NULL)
+		return GIT_EBAREINDEX;
+
+	error = write_index(oid, index, "", 0, 0, git_index_entrycount(index));
+	return (error < GIT_SUCCESS) ? error : GIT_SUCCESS;
+}
+
+static void sort_entries(git_treebuilder *bld)
+{
+	git_vector_sort(&bld->entries);
+}
+
+int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
+{
+	git_treebuilder *bld;
+	size_t i, source_entries = DEFAULT_TREE_SIZE;
+
+	assert(builder_p);
+
+	bld = git__calloc(1, sizeof(git_treebuilder));
+	if (bld == NULL)
+		return GIT_ENOMEM;
+
+	if (source != NULL)
+		source_entries = source->entries.length;
+
+	if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < GIT_SUCCESS) {
+		free(bld);
+		return GIT_ENOMEM;
+	}
+
+	if (source != NULL) {
+		bld->entry_count = source_entries;
+		for (i = 0; i < source->entries.length; ++i) {
+			git_tree_entry *entry_src = source->entries.contents[i];
+			git_tree_entry *entry = git__calloc(1, sizeof(git_tree_entry));
+
+			if (entry == NULL) {
+				git_treebuilder_free(bld);
+				return GIT_ENOMEM;
+			}
+
+			entry->filename = git__strdup(entry_src->filename);
+
+			if (entry->filename == NULL) {
+				free(entry);
+				git_treebuilder_free(bld);
+				return GIT_ENOMEM;
+			}
+
+			entry->filename_len = entry_src->filename_len;
+			git_oid_cpy(&entry->oid, &entry_src->oid);
+			entry->attr = entry_src->attr;
+
+			git_vector_insert(&bld->entries, entry);
+		}
+	}
+
+	*builder_p = bld;
+	return GIT_SUCCESS;
+}
+
+int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+{
+	git_tree_entry *entry;
+	int pos;
+
+	assert(bld && id && filename);
+
+	if (!valid_attributes(attributes))
+		return GIT_ERROR;
+
+	if ((pos = git_vector_bsearch2(&bld->entries, entry_search_cmp, filename)) != GIT_ENOTFOUND) {
+		entry = git_vector_get(&bld->entries, pos);
+		if (entry->removed) {
+			entry->removed = 0;
+			bld->entry_count++;
+		}
+	} else {
+		if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
+			return GIT_ENOMEM;
+
+		memset(entry, 0x0, sizeof(git_tree_entry));
+		entry->filename = git__strdup(filename);
+		entry->filename_len = strlen(entry->filename);
+
+		bld->entry_count++;
+	}
+
+	git_oid_cpy(&entry->oid, id);
+	entry->attr = attributes;
+
+	if (pos != GIT_ENOTFOUND) {
+		if (git_vector_insert(&bld->entries, entry) < 0)
+			return GIT_ENOMEM;
+	}
+
+	if (entry_out != NULL)
+		*entry_out = entry;
+
+	return GIT_SUCCESS;
+}
+
+const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename)
+{
+	int idx;
+	git_tree_entry *entry;
+
+	assert(bld && filename);
+
+	sort_entries(bld);
+	idx = git_vector_bsearch2(&bld->entries, entry_search_cmp, filename);
+	if (idx == GIT_ENOTFOUND)
+		return NULL;
+
+	entry = git_vector_get(&bld->entries, idx);
+	if (entry->removed)
+		return NULL;
+
+	return entry;
+}
+
+int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
+{
+	git_tree_entry *remove_ptr = (git_tree_entry *)git_treebuilder_get(bld, filename);
+
+	if (remove_ptr == NULL || remove_ptr->removed)
+		return GIT_ENOTFOUND;
+
+	remove_ptr->removed = 1;
+	bld->entry_count--;
+	return GIT_SUCCESS;
+}
+
+int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld)
+{
+	size_t i, size = 0;
+	char filemode[MAX_FILEMODE_BYTES + 1 + 1];
+	git_odb_stream *stream;
+	int error;
+
+	assert(bld);
+
+	sort_entries(bld);
+
+	for (i = 0; i < bld->entries.length; ++i) {
+		git_tree_entry *entry = bld->entries.contents[i];
+
+		if (entry->removed)
+			continue;
+
+		size += (entry->attr > 0x7FF) ? 7 : 6;
+		size += entry->filename_len + 1;
+		size += GIT_OID_RAWSZ;
+	}
+
+	if ((error = git_odb_open_wstream(&stream, git_repository_database(repo), size, GIT_OBJ_TREE)) < GIT_SUCCESS)
+		return error;
+
+	for (i = 0; i < bld->entries.length; ++i) {
+		git_tree_entry *entry = bld->entries.contents[i];
+
+		if (entry->removed)
+			continue;
+
+		snprintf(filemode, sizeof(filemode), "%o ", entry->attr);
+		stream->write(stream, filemode, strlen(filemode));
+		stream->write(stream, entry->filename, entry->filename_len + 1);
+		stream->write(stream, (char *)entry->oid.id, GIT_OID_RAWSZ);
+	} 
+
+	error = stream->finalize_write(oid, stream);
+	stream->free(stream);
+
+	return error;
+}
+
+void git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload)
+{
+	size_t i;
+
+	assert(bld && filter);
+
+	for (i = 0; i < bld->entries.length; ++i) {
+		git_tree_entry *entry = bld->entries.contents[i];
+		if (!entry->removed && filter(entry, payload))
+			entry->removed = 1;
+	}
+}
+
+void git_treebuilder_clear(git_treebuilder *bld)
+{
+	size_t i;
+	assert(bld);
+
+	for (i = 0; i < bld->entries.length; ++i) {
+		git_tree_entry *e = bld->entries.contents[i];
+		free(e->filename);
+		free(e);
+	}
+
+	git_vector_clear(&bld->entries);
+}
+
+void git_treebuilder_free(git_treebuilder *bld)
+{
+	git_treebuilder_clear(bld);
+	git_vector_free(&bld->entries);
+	free(bld);
+}
+
+
diff --git a/src/tree.h b/src/tree.h
index b4e910a..bff3f8e 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -10,6 +10,8 @@ struct git_tree_entry {
 	unsigned int attr;
 	char *filename;
 	git_oid oid;
+	size_t filename_len;
+	int removed;
 };
 
 struct git_tree {
@@ -17,6 +19,12 @@ struct git_tree {
 	git_vector entries;
 };
 
+struct git_treebuilder {
+	git_vector entries;
+	size_t entry_count;
+};
+
+
 void git_tree__free(git_tree *tree);
 int git_tree__parse(git_tree *tree, git_odb_object *obj);
 
diff --git a/src/util.h b/src/util.h
index 653b34d..f477b64 100644
--- a/src/util.h
+++ b/src/util.h
@@ -14,6 +14,7 @@
  */
 #define git__malloc malloc
 #define git__calloc calloc
+#define git__realloc realloc
 #define git__strdup strdup
 
 extern int git__fmt(char *, size_t, const char *, ...)
diff --git a/tests/resources/testrepo.git/refs/tags/e90810b b/tests/resources/testrepo.git/refs/tags/e90810b
new file mode 100644
index 0000000..584495d
--- /dev/null
+++ b/tests/resources/testrepo.git/refs/tags/e90810b
@@ -0,0 +1 @@
+7b4384978d2493e851f9cca7858815fac9b10980
diff --git a/tests/resources/testrepo.git/refs/tags/very-simple b/tests/resources/testrepo.git/refs/tags/very-simple
deleted file mode 100644
index 584495d..0000000
--- a/tests/resources/testrepo.git/refs/tags/very-simple
+++ /dev/null
@@ -1 +0,0 @@
-7b4384978d2493e851f9cca7858815fac9b10980
diff --git a/tests/t04-commit.c b/tests/t04-commit.c
index e928424..bcc0417 100644
--- a/tests/t04-commit.c
+++ b/tests/t04-commit.c
@@ -114,15 +114,15 @@ BEGIN_TEST(parse0, "parse the OID line in a commit")
 	git_oid oid;
 
 #define TEST_OID_PASS(string, header) { \
-	char *ptr = string;\
-	char *ptr_original = ptr;\
+	const char *ptr = string;\
+	const char *ptr_original = ptr;\
 	size_t len = strlen(ptr);\
 	must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
 	must_be_true(ptr == ptr_original + len);\
 }
 
 #define TEST_OID_FAIL(string, header) { \
-	char *ptr = string;\
+	const char *ptr = string;\
 	size_t len = strlen(ptr);\
 	must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
 }
@@ -154,7 +154,7 @@ END_TEST
 BEGIN_TEST(parse1, "parse the signature line in a commit")
 
 #define TEST_SIGNATURE_PASS(_string, _header, _name, _email, _time, _offset) { \
-	char *ptr = _string; \
+	const char *ptr = _string; \
 	size_t len = strlen(_string);\
 	git_signature person = {NULL, NULL, {0, 0}}; \
 	must_pass(git_signature__parse(&person, &ptr, ptr + len, _header));\
@@ -166,7 +166,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit")
 }
 
 #define TEST_SIGNATURE_FAIL(_string, _header) { \
-	char *ptr = _string; \
+	const char *ptr = _string; \
 	size_t len = strlen(_string);\
 	git_signature person = {NULL, NULL, {0, 0}}; \
 	must_fail(git_signature__parse(&person, &ptr, ptr + len, _header));\
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index 70eeb28..e701648 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -58,6 +58,9 @@ BEGIN_TEST(read0, "read and parse a tag from the repository")
 
 	must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
 
+	git_tag_close(tag1);
+	git_tag_close(tag2);
+	git_commit_close(commit);
 	git_repository_free(repo);
 END_TEST
 
@@ -72,20 +75,19 @@ BEGIN_TEST(write0, "write a tag to the repository and read it again")
 	git_oid target_id, tag_id;
 	const git_signature *tagger;
 	git_reference *ref_tag;
-	/* char hex_oid[41]; */
 
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
 	git_oid_mkstr(&target_id, tagged_commit);
 
-	/* create signatures */
+	/* create signature */
 	tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
 	must_be_true(tagger != NULL);
 
 	must_pass(git_tag_create(
 		&tag_id, /* out id */
 		repo,
-		"the-tag", /* do not update the HEAD */
+		"the-tag",
 		&target_id,
 		GIT_OBJ_COMMIT,
 		tagger,
@@ -94,6 +96,7 @@ BEGIN_TEST(write0, "write a tag to the repository and read it again")
 	git_signature_free((git_signature *)tagger);
 
 	must_pass(git_tag_lookup(&tag, repo, &tag_id));
+	must_be_true(git_oid_cmp(git_tag_target_oid(tag), &target_id) == 0);
 
 	/* Check attributes were set correctly */
 	tagger = git_tag_tagger(tag);
@@ -111,12 +114,136 @@ BEGIN_TEST(write0, "write a tag to the repository and read it again")
 
 	must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag));
 
+	git_tag_close(tag);
 	git_repository_free(repo);
 
 END_TEST
 
+BEGIN_TEST(write1, "write a tag to the repository which points to an unknown oid and read it again")
+	git_repository *repo;
+	git_tag *tag;
+	git_oid target_id, tag_id;
+	const git_signature *tagger;
+	git_reference *ref_tag;
+	git_object *zombie;
+
+	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+	git_oid_mkstr(&target_id, "deadbeef1b46c854b31185ea97743be6a8774479");
+
+	/* create signature */
+	tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
+	must_be_true(tagger != NULL);
+
+	must_pass(git_tag_create(
+		&tag_id, /* out id */
+		repo,
+		"the-zombie-tag",
+		&target_id,
+		GIT_OBJ_COMMIT,
+		tagger,
+		TAGGER_MESSAGE));
+
+	git_signature_free((git_signature *)tagger);
+
+	must_pass(git_tag_lookup(&tag, repo, &tag_id));
+	
+	/* The non existent target can not be looked up */
+	must_fail(git_tag_target(&zombie, tag));
+
+	must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/the-zombie-tag"));
+	
+	must_pass(git_reference_delete(ref_tag));
+	must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag));
+
+	git_tag_close(tag);
+	git_repository_free(repo);
+
+END_TEST
+
+BEGIN_TEST(write2, "Attempt to write a tag bearing the same name than an already existing tag")
+	git_repository *repo;
+	git_oid target_id, tag_id;
+	const git_signature *tagger;
+
+	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+	git_oid_mkstr(&target_id, tagged_commit);
+
+	/* create signature */
+	tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
+	must_be_true(tagger != NULL);
+
+	must_fail(git_tag_create(
+		&tag_id, /* out id */
+		repo,
+		"e90810b",
+		&target_id,
+		GIT_OBJ_COMMIT,
+		tagger,
+		TAGGER_MESSAGE));
+
+	git_signature_free((git_signature *)tagger);
+
+	git_repository_free(repo);
+
+END_TEST
+
+BEGIN_TEST(write3, "Replace an already existing tag")
+	git_repository *repo;
+	git_oid target_id, tag_id, old_tag_id;
+	const git_signature *tagger;
+	git_reference *ref_tag;
+
+	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+
+	git_oid_mkstr(&target_id, tagged_commit);
+
+	must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/e90810b"));
+	git_oid_cpy(&old_tag_id, git_reference_oid(ref_tag));
+
+	/* create signature */
+	tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
+	must_be_true(tagger != NULL);
+
+	must_pass(git_tag_create_f(
+		&tag_id, /* out id */
+		repo,
+		"e90810b",
+		&target_id,
+		GIT_OBJ_COMMIT,
+		tagger,
+		TAGGER_MESSAGE));
+
+	git_signature_free((git_signature *)tagger);
+
+	must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/e90810b"));
+	must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
+	must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &old_tag_id) != 0);
+
+	close_temp_repo(repo);
+
+END_TEST
+
+BEGIN_TEST(write4, "Delete an already existing tag")
+	git_repository *repo;
+	git_reference *ref_tag;
+
+	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+
+	must_pass(git_tag_delete(repo,"e90810b"));
+
+	must_fail(git_reference_lookup(&ref_tag, repo, "refs/tags/e90810b"));
+
+	close_temp_repo(repo);
+
+END_TEST
 
 BEGIN_SUITE(tag)
 	ADD_TEST(read0);
-	ADD_TEST(write0); 
+	ADD_TEST(write0);
+	ADD_TEST(write1);
+	ADD_TEST(write2);
+	ADD_TEST(write3);
+	ADD_TEST(write4);
 END_SUITE
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index 6c1b2e6..e4252db 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -29,6 +29,36 @@
 
 static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
 
+#if 0
+static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
+{
+	static const char *indent = "                              ";
+	git_tree *tree;
+	unsigned int i;
+
+	if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
+		return GIT_ERROR;
+
+	for (i = 0; i < git_tree_entrycount(tree); ++i) {
+		const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
+		char entry_oid[40];
+
+		git_oid_fmt(entry_oid, &entry->oid);
+		printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);
+
+		if (entry->attr == S_IFDIR) {
+			if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
+				git_tree_close(tree);
+				return GIT_ERROR;
+			}
+		}
+	}
+
+	git_tree_close(tree);
+	return GIT_SUCCESS;
+}
+#endif
+
 BEGIN_TEST(read0, "acces randomly the entries on a loaded tree")
 	git_oid id;
 	git_repository *repo;
@@ -55,7 +85,7 @@ BEGIN_TEST(read1, "read a tree from the repository")
 	git_oid id;
 	git_repository *repo;
 	git_tree *tree;
-	git_tree_entry *entry;
+	const git_tree_entry *entry;
 	git_object *obj;
 
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -80,10 +110,27 @@ BEGIN_TEST(read1, "read a tree from the repository")
 	git_repository_free(repo);
 END_TEST
 
+#if 0
+BEGIN_TEST(write0, "write a tree from an index")
+	git_repository *repo;
+	git_index *index;
+	git_oid tree_oid;
+
+	must_pass(git_repository_open(&repo, "/tmp/redtmp/.git"));
+	must_pass(git_repository_index(&index, repo));
+
+	must_pass(git_tree_create_fromindex(&tree_oid, index));
+	must_pass(print_tree(repo, &tree_oid, 0));
+
+	git_repository_free(repo);
+END_TEST
+#endif
+
 BEGIN_SUITE(tree)
+	//ADD_TEST(print0);
 	ADD_TEST(read0);
 	ADD_TEST(read1);
-//	ADD_TEST(write0); /* TODO THREADSAFE */
-//	ADD_TEST(write1);
+	//ADD_TEST(write0);
+	//ADD_TEST(write1);
 END_SUITE
 
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index 413811c..2797cd8 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -27,13 +27,14 @@
 
 #include "repository.h"
 
-static const char *loose_tag_ref_name = "refs/tags/test";
+static const char *loose_tag_ref_name = "refs/tags/e90810b";
 static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
 
 BEGIN_TEST(readtag0, "lookup a loose tag reference")
 	git_repository *repo;
 	git_reference *reference;
 	git_object *object;
+	char ref_name_from_tag_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
 
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
@@ -46,6 +47,10 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
 	must_be_true(object != NULL);
 	must_be_true(git_object_type(object) == GIT_OBJ_TAG);
 
+	/* Ensure the name of the tag matches the name of the reference */
+	git__joinpath(ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object));
+	must_be_true(strcmp(ref_name_from_tag_name, loose_tag_ref_name) == 0);
+
 	git_repository_free(repo);
 END_TEST
 
@@ -227,9 +232,8 @@ BEGIN_TEST(create0, "create a new symbolic reference")
 	must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
 	must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
 
+	git_reference_delete(looked_up_ref);
 	git_repository_free(repo);
-
-	must_pass(gitfo_unlink(ref_path));	/* TODO: replace with git_reference_delete() when available */
 END_TEST
 
 BEGIN_TEST(create1, "create a deep symbolic reference")
@@ -250,9 +254,8 @@ BEGIN_TEST(create1, "create a deep symbolic reference")
 	must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
 	must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
 
+	git_reference_delete(looked_up_ref);
 	git_repository_free(repo);
-
-	must_pass(gitfo_unlink(ref_path));	/* TODO: replace with git_reference_delete() when available */
 END_TEST
 
 BEGIN_TEST(create2, "create a new OID reference")
@@ -290,9 +293,28 @@ BEGIN_TEST(create2, "create a new OID reference")
 	must_pass(git_reference_lookup(&looked_up_ref, repo, new_head));
 	must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
 
+	git_reference_delete(looked_up_ref);
 	git_repository_free(repo);
+END_TEST
 
-	must_pass(gitfo_unlink(ref_path));	/* TODO: replace with git_reference_delete() when available */
+BEGIN_TEST(create3, "Can not create a new OID reference which targets at an unknown id")
+	git_reference *new_reference, *looked_up_ref;
+	git_repository *repo;
+	git_oid id;
+
+	const char *new_head = "refs/heads/new-head";
+
+	git_oid_mkstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644");
+
+	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+	/* Create and write the new object id reference */
+	must_fail(git_reference_create_oid(&new_reference, repo, new_head, &id));
+
+	/* Ensure the reference can't be looked-up... */
+	must_fail(git_reference_lookup(&looked_up_ref, repo, new_head));
+
+	git_repository_free(repo);
 END_TEST
 
 static const char *ref_name = "refs/heads/other";
@@ -892,6 +914,7 @@ BEGIN_SUITE(refs)
 	ADD_TEST(create0);
 	ADD_TEST(create1);
 	ADD_TEST(create2);
+	ADD_TEST(create3);
 
 	ADD_TEST(overwrite0);
 	ADD_TEST(overwrite1);
diff --git a/wscript b/wscript
index f3082ae..dd90406 100644
--- a/wscript
+++ b/wscript
@@ -4,7 +4,7 @@ from waflib.Build import BuildContext, CleanContext, \
 
 # Unix flags
 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
-CFLAGS_UNIX_DBG = ['-g']
+CFLAGS_UNIX_DBG = ['-g', '-O0']
 
 # Windows MSVC flags
 CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']