Commit b93688d06d4128480ef746856532afa1e53d1e23

Vicent Marti 2012-06-19T02:33:03

Merge remote-tracking branch 'yorah/fix/notes-creation' into development Conflicts: src/notes.c

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
diff --git a/src/notes.c b/src/notes.c
index e87ea65..efbdbab 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -12,50 +12,67 @@
 #include "config.h"
 #include "iterator.h"
 
-static int find_subtree(git_tree **subtree, const git_oid *root,
-			git_repository *repo, const char *target, int *fanout)
+static int find_subtree_in_current_level(
+	git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	const char *annotated_object_sha,
+	int fanout)
 {
-	int error;
 	unsigned int i;
-	git_tree *tree;
 	const git_tree_entry *entry;
 
-	*subtree = NULL;
-
-	error = git_tree_lookup(&tree, repo, root);
-	if (error < 0)
-		return error;
+	*out = NULL;
+	
+	if (parent == NULL)
+		return GIT_ENOTFOUND;
 
-	for (i=0; i<git_tree_entrycount(tree); i++) {
-		entry = git_tree_entry_byindex(tree, i);
+	for (i = 0; i < git_tree_entrycount(parent); i++) {
+		entry = git_tree_entry_byindex(parent, i);
 
 		if (!git__ishex(git_tree_entry_name(entry)))
 			continue;
 
-		/*
-		 * A notes tree follows a strict byte-based progressive fanout
-		 * (i.e. using 2/38, 2/2/36, etc. fanouts, not e.g. 4/36 fanout)
-		 */
-
 		if (S_ISDIR(git_tree_entry_attributes(entry))
-			&& strlen(git_tree_entry_name(entry)) == 2
-			&& !strncmp(git_tree_entry_name(entry), target + *fanout, 2)) {
+			&& strlen(git_tree_entry_name(entry)) == 2 
+			&& !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
+			return git_tree_lookup(out, repo, git_tree_entry_id(entry));
 
-			/* found matching subtree - unpack and resume lookup */
+		/* Not a DIR, so do we have an already existing blob? */
+		if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))				
+			return GIT_EEXISTS;
+	}
+
+	return GIT_ENOTFOUND;
+}
 
-			git_oid subtree_sha;
-			git_oid_cpy(&subtree_sha, git_tree_entry_id(entry));
-			git_tree_free(tree);
+static int find_subtree_r(git_tree **out, git_tree *root,
+			git_repository *repo, const char *target, int *fanout)
+{
+	int error;
+	git_tree *subtree = NULL;
 
-			*fanout += 2;
+	*out = NULL;
 
-			return find_subtree(subtree, &subtree_sha, repo,
-					    target, fanout);
-		}
+	error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
+	if (error == GIT_EEXISTS) {
+		return git_tree_lookup(out, repo, git_object_id((const git_object *)root));
 	}
 
-	*subtree = tree;
-	return 0;
+	if (error < 0)
+		return error;
+
+	*fanout += 2;
+	error = find_subtree_r(out, subtree, repo, target, fanout);
+
+	/*
+	 * root is not ours to free, and the last subtree is the
+	 * one being returned => we only need to free the subtrees in-between
+	 */
+	if (*out != subtree)
+		git_tree_free(subtree);
+
+	return error;
 }
 
 static int find_blob(git_oid *blob, git_tree *tree, const char *target)
@@ -76,191 +93,275 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target)
 	return GIT_ENOTFOUND;
 }
 
-static int note_write(git_oid *out, git_repository *repo,
-		      git_signature *author, git_signature *committer,
-		      const char *notes_ref, const char *note,
-		      const git_oid *tree_sha, const char *target,
-		      int nparents, git_commit **parents)
+static int tree_write(
+	git_tree **out,
+	git_repository *repo,
+	git_tree *source_tree,
+	const git_oid *object_oid,
+	const char *treeentry_name,
+	unsigned int attributes)
 {
-	int error, fanout = 0;
-	git_oid oid;
-	git_tree *tree = NULL;
+	int error;
+	git_treebuilder *tb = NULL;
 	git_tree_entry *entry;
-	git_treebuilder *tb;
-
-	/* check for existing notes tree */
-
-	if (tree_sha) {
-		error = find_subtree(&tree, tree_sha, repo, target, &fanout);
-		if (error < 0)
-			return error;
-
-		error = find_blob(&oid, tree, target + fanout);
-		if (error != GIT_ENOTFOUND) {
-			git_tree_free(tree);
-			if (!error) {
-				giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", target);
-				error = GIT_EEXISTS;
-			}
-			return error;
-		}
-	}
+	git_oid tree_oid;
 
-	/* no matching tree entry - add note object to target tree */
+	if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
+		goto cleanup;
 
-	error = git_treebuilder_create(&tb, tree);
-	git_tree_free(tree);
+	if (object_oid) {
+		if ((error = git_treebuilder_insert(
+				&entry, tb, treeentry_name, object_oid, attributes)) < 0)
+			goto cleanup;
+	} else {
+		if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
+			goto cleanup;
+	}
 
-	if (error < 0)
-		return error;
+	if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0)
+		goto cleanup;
 
-	if (!tree_sha)
-		/* no notes tree yet - create fanout */
-		fanout += 2;
+	error = git_tree_lookup(out, repo, &tree_oid);
 
-	/* create note object */
-	error = git_blob_create_frombuffer(&oid, repo, note, strlen(note));
-	if (error < 0) {
-		git_treebuilder_free(tb);
-		return error;
-	}
+cleanup:
+	git_treebuilder_free(tb);
+	return error;
+}
 
-	error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); //-V536
-	if (error < 0) {
-		/* libgit2 doesn't support object removal (gc) yet */
-		/* we leave an orphaned blob object behind - TODO */
+static int manipulate_note_in_tree_r(
+	git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	git_oid *note_oid,
+	const char *annotated_object_sha,
+	int fanout, 
+	int (*note_exists_cb)(
+		git_tree **out, 
+		git_repository *repo, 
+		git_tree *parent,
+		git_oid *note_oid,
+		const char *annotated_object_sha,
+		int fanout,
+		int current_error),
+	int (*note_notfound_cb)(
+		git_tree **out,
+		git_repository *repo,
+		git_tree *parent,
+		git_oid *note_oid,
+		const char *annotated_object_sha,
+		int fanout,
+		int current_error))
+{
+	int error = -1;	
+	git_tree *subtree = NULL;
+	char subtree_name[3];
 
-		git_treebuilder_free(tb);
-		return error;
-	}
+	error = find_subtree_in_current_level(
+		&subtree, repo, parent, annotated_object_sha, fanout);
 
-	if (out)
-		git_oid_cpy(out, git_tree_entry_id(entry));
+	if (error == GIT_EEXISTS) {
+		error = note_exists_cb(
+			out, repo, parent, note_oid, annotated_object_sha, fanout, error);
+		goto cleanup;
+	}
 
-	error = git_treebuilder_write(&oid, repo, tb);
-	git_treebuilder_free(tb);
+	if (error == GIT_ENOTFOUND) {
+		error = note_notfound_cb(
+			out, repo, parent, note_oid, annotated_object_sha, fanout, error);
+		goto cleanup;
+	}
 
 	if (error < 0)
-		return 0;
-
-	if (!tree_sha) {
-		/* create fanout subtree */
+		goto cleanup;
 
-		char subtree[3];
-		strncpy(subtree, target, 2);
-		subtree[2] = '\0';
+	/* An existing fanout has been found, let's dig deeper */
+	error = manipulate_note_in_tree_r(
+		out, repo, subtree, note_oid, annotated_object_sha,
+		fanout + 2, note_exists_cb, note_notfound_cb);
 
-		error = git_treebuilder_create(&tb, NULL);
-		if (error < 0)
-			return error;
+	if (error < 0)
+		goto cleanup;
 
-		error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); //-V536
-		if (error < 0) {
-			git_treebuilder_free(tb);
-			return error;
-		}
+	strncpy(subtree_name, annotated_object_sha + fanout, 2);
+	subtree_name[2] = '\0';
 
-		error = git_treebuilder_write(&oid, repo, tb);
+	error = tree_write(out, repo, parent,
+		git_object_id((const git_object *)(*out)), subtree_name, 0040000);
 
-		git_treebuilder_free(tb);
+cleanup:
+	git_tree_free(subtree);
+	return error;
+}
 
-		if (error < 0)
-			return error;
-	}
+static int remove_note_in_tree_eexists_cb(
+	git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	git_oid *note_oid,
+	const char *annotated_object_sha,
+	int fanout,
+	int current_error)
+{
+	GIT_UNUSED(note_oid);
+	GIT_UNUSED(current_error);
 
-	/* create new notes commit */
+	return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
+}
 
-	error = git_tree_lookup(&tree, repo, &oid);
-	if (error < 0)
-		return error;
+static int remove_note_in_tree_enotfound_cb(
+	git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	git_oid *note_oid,
+	const char *annotated_object_sha,
+	int fanout,
+	int current_error)
+{
+	GIT_UNUSED(out);
+	GIT_UNUSED(repo);
+	GIT_UNUSED(parent);
+	GIT_UNUSED(note_oid);
+	GIT_UNUSED(fanout);
+
+	giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
+	return current_error;
+}
 
-	error = git_commit_create(&oid, repo, notes_ref, author, committer,
-				  NULL, GIT_NOTES_DEFAULT_MSG_ADD,
-				  tree, nparents, (const git_commit **) parents);
+static int insert_note_in_tree_eexists_cb(git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	git_oid *note_oid,
+	const char *annotated_object_sha,
+	int fanout,
+	int current_error)
+{
+	GIT_UNUSED(out);
+	GIT_UNUSED(repo);
+	GIT_UNUSED(parent);
+	GIT_UNUSED(note_oid);
+	GIT_UNUSED(fanout);
+
+	giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
+	return current_error;
+}
 
-	git_tree_free(tree);
+static int insert_note_in_tree_enotfound_cb(git_tree **out,
+	git_repository *repo,
+	git_tree *parent,
+	git_oid *note_oid,
+	const char *annotated_object_sha,
+	int fanout,
+	int current_error)
+{
+	GIT_UNUSED(current_error);
 
-	return error;
+	/* No existing fanout at this level, insert in place */
+	return tree_write(out, repo, parent, note_oid, annotated_object_sha + fanout, 0100644);
 }
 
-static int note_lookup(git_note **out, git_repository *repo,
-		       const git_oid *tree_sha, const char *target)
+static int note_write(git_oid *out,
+	git_repository *repo,
+	git_signature *author,
+	git_signature *committer,
+	const char *notes_ref,
+	const char *note,
+	git_tree *commit_tree,
+	const char *target,
+	git_commit **parents)
 {
-	int error, fanout = 0;
+	int error;
 	git_oid oid;
-	git_blob *blob;
-	git_tree *tree;
-	git_note *note;
+	git_tree *tree = NULL;
+	
+	// TODO: should we apply filters?
+	/* create note object */
+	if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
+		goto cleanup;
 
-	error = find_subtree(&tree, tree_sha, repo, target, &fanout);
-	if (error < 0)
-		return error;
+	if ((error = manipulate_note_in_tree_r(
+		&tree, repo, commit_tree, &oid, target, 0,
+		insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0)
+		goto cleanup;
 
-	error = find_blob(&oid, tree, target + fanout);
+	if (out)
+		git_oid_cpy(out, &oid);
+
+	error = git_commit_create(&oid, repo, notes_ref, author, committer,
+				  NULL, GIT_NOTES_DEFAULT_MSG_ADD,
+				  tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
 
+cleanup:
 	git_tree_free(tree);
-	if (error < 0)
-		return error;
+	return error;
+}
 
-	error = git_blob_lookup(&blob, repo, &oid);
-	if (error < 0)
-		return error;
+static int note_new(git_note **out, git_oid *note_oid, git_blob *blob)
+{
+	git_note *note = NULL;
 
-	note = git__malloc(sizeof(git_note));
+	note = (git_note *)git__malloc(sizeof(git_note));
 	GITERR_CHECK_ALLOC(note);
 
-	git_oid_cpy(&note->oid, &oid);
-	note->message = git__strdup(git_blob_rawcontent(blob));
+	git_oid_cpy(&note->oid, note_oid);
+	note->message = git__strdup((char *)git_blob_rawcontent(blob));
 	GITERR_CHECK_ALLOC(note->message);
 
 	*out = note;
 
-	git_blob_free(blob);
-	return error;
+	return 0;
 }
 
-static int note_remove(git_repository *repo,
-		       git_signature *author, git_signature *committer,
-		       const char *notes_ref, const git_oid *tree_sha,
-		       const char *target, int nparents, git_commit **parents)
+static int note_lookup(git_note **out, git_repository *repo,
+		       git_tree *tree, const char *target)
 {
 	int error, fanout = 0;
 	git_oid oid;
-	git_tree *tree;
-	git_treebuilder *tb;
+	git_blob *blob = NULL;
+	git_note *note = NULL;
+	git_tree *subtree = NULL;
 
-	error = find_subtree(&tree, tree_sha, repo, target, &fanout);
-	if (error < 0)
-		return error;
+	if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
+		goto cleanup;
 
-	error = find_blob(&oid, tree, target + fanout);
-	if (!error)
-		error = git_treebuilder_create(&tb, tree);
+	if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
+		goto cleanup;
 
-	git_tree_free(tree);
-	if (error < 0)
-		return error;
+	if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
+		goto cleanup;
 
-	error = git_treebuilder_remove(tb, target + fanout);
-	if (!error)
-		error = git_treebuilder_write(&oid, repo, tb);
+	if ((error = note_new(&note, &oid, blob)) < 0)
+		goto cleanup;
 
-	git_treebuilder_free(tb);
-	if (error < 0)
-		return error;
+	*out = note;
 
-	/* create new notes commit */
+cleanup:
+	git_tree_free(subtree);
+	git_blob_free(blob);
+	return error;
+}
 
-	error = git_tree_lookup(&tree, repo, &oid);
-	if (error < 0)
-		return error;
+static int note_remove(git_repository *repo,
+		       git_signature *author, git_signature *committer,
+		       const char *notes_ref, git_tree *tree,
+		       const char *target, git_commit **parents)
+{
+	int error;
+	git_tree *tree_after_removal = NULL;
+	git_oid oid;
+		
+	if ((error = manipulate_note_in_tree_r(
+		&tree_after_removal, repo, tree, NULL, target, 0,
+		remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
+		goto cleanup;
 
 	error = git_commit_create(&oid, repo, notes_ref, author, committer,
-				  NULL, GIT_NOTES_DEFAULT_MSG_RM,
-				  tree, nparents, (const git_commit **) parents);
-
-	git_tree_free(tree);
+	  NULL, GIT_NOTES_DEFAULT_MSG_RM,
+	  tree_after_removal,
+	  *parents == NULL ? 0 : 1,
+	  (const git_commit **) parents);
 
+cleanup:
+	git_tree_free(tree_after_removal);
 	return error;
 }
 
@@ -291,48 +392,50 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo)
 	return note_get_default_ref(notes_ref, repo);
 }
 
-static int retrieve_note_tree_oid(git_oid *tree_oid_out, git_repository *repo, const char *notes_ref)
+static int retrieve_note_tree_and_commit(
+	git_tree **tree_out,
+	git_commit **commit_out,
+	git_repository *repo,
+	const char **notes_ref)
 {
-	int error = -1;
-	git_commit *commit = NULL;
+	int error;
 	git_oid oid;
 
-	if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0)
-		goto cleanup;
+	if ((error = normalize_namespace(notes_ref, repo)) < 0)
+		return error;
 
-	if (git_commit_lookup(&commit, repo, &oid) < 0)
-		goto cleanup;
+	if ((error = git_reference_name_to_oid(&oid, repo, *notes_ref)) < 0)
+		return error;
 
-	git_oid_cpy(tree_oid_out, git_commit_tree_oid(commit));
+	if (git_commit_lookup(commit_out, repo, &oid) < 0)
+		return error;
 
-	error = 0;
+	if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
+		return error;
 
-cleanup:
-	git_commit_free(commit);
-	return error;
+	return 0;
 }
 
 int git_note_read(git_note **out, git_repository *repo,
 		  const char *notes_ref, const git_oid *oid)
 {
 	int error;
-	char *target;
-	git_oid sha;
-
-	*out = NULL;
-
-	if (normalize_namespace(&notes_ref, repo) < 0)
-		return -1;
-
-	if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0)
-		return error;
+	char *target = NULL;
+	git_tree *tree = NULL;
+	git_commit *commit = NULL;
 
 	target = git_oid_allocfmt(oid);
 	GITERR_CHECK_ALLOC(target);
 
-	error = note_lookup(out, repo, &sha, target);
+	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
+		goto cleanup;
+
+	error = note_lookup(out, repo, tree, target);
 
+cleanup:
 	git__free(target);
+	git_tree_free(tree);
+	git_commit_free(commit);
 	return error;
 }
 
@@ -342,44 +445,26 @@ int git_note_create(
 	const char *notes_ref, const git_oid *oid,
 	const char *note)
 {
-	int error, nparents = 0;
-	char *target;
-	git_oid sha;
+	int error;
+	char *target = NULL;
 	git_commit *commit = NULL;
-	git_reference *ref;
-
-	if (normalize_namespace(&notes_ref, repo) < 0)
-		return -1;
-
-	error = git_reference_lookup(&ref, repo, notes_ref);
-	if (error < 0 && error != GIT_ENOTFOUND)
-		return error;
-
-	if (!error) {
-		assert(git_reference_type(ref) == GIT_REF_OID);
-
-		/* lookup existing notes tree oid */
-
-		git_oid_cpy(&sha, git_reference_oid(ref));
-		git_reference_free(ref);
-
-		error = git_commit_lookup(&commit, repo, &sha);
-		if (error < 0)
-			return error;
-
-		git_oid_cpy(&sha, git_commit_tree_oid(commit));
-		nparents++;
-	}
+	git_tree *tree = NULL;
 
 	target = git_oid_allocfmt(oid);
 	GITERR_CHECK_ALLOC(target);
 
+	error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+
+	if (error < 0 && error != GIT_ENOTFOUND)
+		goto cleanup;
+
 	error = note_write(out, repo, author, committer, notes_ref,
-			   note, nparents ? &sha : NULL, target,
-			   nparents, &commit);
+			   note, tree, target, &commit);
 
+cleanup:
 	git__free(target);
 	git_commit_free(commit);
+	git_tree_free(tree);
 	return error;
 }
 
@@ -388,37 +473,23 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
 		    const git_oid *oid)
 {
 	int error;
-	char *target;
-	git_oid sha;
-	git_commit *commit;
-	git_reference *ref;
-
-	if (normalize_namespace(&notes_ref, repo) < 0)
-		return -1;
-
-	error = git_reference_lookup(&ref, repo, notes_ref);
-	if (error < 0)
-		return error;
-
-	assert(git_reference_type(ref) == GIT_REF_OID);
-
-	git_oid_cpy(&sha, git_reference_oid(ref));
-	git_reference_free(ref);
-
-	error = git_commit_lookup(&commit, repo, &sha);
-	if (error < 0)
-		return error;
-
-	git_oid_cpy(&sha, git_commit_tree_oid(commit));
+	char *target = NULL;
+	git_commit *commit = NULL;
+	git_tree *tree = NULL;
 
 	target = git_oid_allocfmt(oid);
 	GITERR_CHECK_ALLOC(target);
 
+	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
+		goto cleanup;
+
 	error = note_remove(repo, author, committer, notes_ref,
-			    &sha, target, 1, &commit);
+			    tree, target, &commit);
 
+cleanup:
 	git__free(target);
 	git_commit_free(commit);
+	git_tree_free(tree);
 	return error;
 }
 
@@ -511,18 +582,12 @@ int git_note_foreach(
 	void *payload)
 {
 	int error = -1;
-	git_oid tree_oid;
 	git_iterator *iter = NULL;
 	git_tree *tree = NULL;
+	git_commit *commit = NULL;
 	const git_index_entry *item;
 
-	if (normalize_namespace(&notes_ref, repo) < 0)
-		return -1;
-
-	if ((error = retrieve_note_tree_oid(&tree_oid, repo, notes_ref)) < 0)
-		goto cleanup;
-
-	if (git_tree_lookup(&tree, repo, &tree_oid) < 0)
+	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
 		goto cleanup;
 
 	if (git_iterator_for_tree(&iter, repo, tree) < 0)
@@ -544,5 +609,6 @@ int git_note_foreach(
 cleanup:
 	git_iterator_free(iter);
 	git_tree_free(tree);
+	git_commit_free(commit);
 	return error;
 }
diff --git a/src/tree.c b/src/tree.c
index d575dc8..9bdc218 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -634,6 +634,9 @@ void git_treebuilder_clear(git_treebuilder *bld)
 
 void git_treebuilder_free(git_treebuilder *bld)
 {
+	if (bld == NULL)
+		return;
+
 	git_treebuilder_clear(bld);
 	git_vector_free(&bld->entries);
 	git__free(bld);
diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c
index e66ea11..4192297 100644
--- a/tests-clar/network/remotelocal.c
+++ b/tests-clar/network/remotelocal.c
@@ -107,7 +107,7 @@ void test_network_remotelocal__retrieve_advertised_references(void)
 
 	cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs));
 
-	cl_assert_equal_i(how_many_refs, 20);
+	cl_assert_equal_i(how_many_refs, 21);
 }
 
 void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void)
@@ -121,7 +121,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi
 
 	cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs));
 
-	cl_assert_equal_i(how_many_refs, 20);
+	cl_assert_equal_i(how_many_refs, 21);
 
 	git_remote_free(remote);	/* Disconnect from the "spaced repo" before the cleanup */
 	remote = NULL;
diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c
index 5185f25..5f7f5a9 100644
--- a/tests-clar/notes/notes.c
+++ b/tests-clar/notes/notes.c
@@ -15,6 +15,16 @@ void test_notes_notes__cleanup(void)
 	cl_git_sandbox_cleanup();
 }
 
+static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) {
+	git_blob *blob;
+
+	cl_assert_equal_s(git_note_message(note), message);
+	cl_assert(!git_oid_cmp(git_note_oid(note), note_oid));
+
+	cl_git_pass(git_blob_lookup(&blob, _repo, note_oid));
+	cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob));
+}
+
 static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message)
 {
 	git_oid oid;
@@ -23,38 +33,6 @@ static void create_note(git_oid *note_oid, const char *canonical_namespace, cons
 	cl_git_pass(git_note_create(note_oid, _repo, _sig, _sig, canonical_namespace, &oid, message));
 }
 
-void test_notes_notes__1(void)
-{
-	git_oid oid, note_oid;
-	static git_note *note;
-	static git_blob *blob;
-
-	cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));
-
-	cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
-	cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
-
-	cl_git_pass(git_note_read(&note, _repo, NULL, &oid));
-
-	cl_assert_equal_s(git_note_message(note), "hello world\n");
-	cl_assert(!git_oid_cmp(git_note_oid(note), &note_oid));
-
-	cl_git_pass(git_blob_lookup(&blob, _repo, &note_oid));
-	cl_assert_equal_s(git_note_message(note), git_blob_rawcontent(blob));
-
-	cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
-	cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
-
-	cl_git_pass(git_note_remove(_repo, NULL, _sig, _sig, &oid));
-	cl_git_pass(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));
-
-	cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, &note_oid));
-	cl_git_fail(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));
-
-	git_note_free(note);
-	git_blob_free(blob);
-}
-
 static struct {
 	const char *note_sha;
 	const char *annotated_object_sha;
@@ -131,3 +109,152 @@ void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_retur
 
 	cl_assert_equal_i(0, retrieved_notes);
 }
+
+void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void)
+{
+	git_oid note_oid, target_oid;
+	git_note *note, *default_namespace_note;
+	const char *default_ref;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
+	cl_git_pass(git_note_default_ref(&default_ref, _repo));
+
+	create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
+
+	cl_git_pass(git_note_read(&note, _repo, NULL, &target_oid));
+	cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref, &target_oid));
+
+	assert_note_equal(note, "hello world\n", &note_oid);
+	assert_note_equal(default_namespace_note, "hello world\n", &note_oid);
+
+	git_note_free(note);
+	git_note_free(default_namespace_note);
+}
+
+void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void)
+{
+	git_oid note_oid, target_oid;
+	git_note *note;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
+
+	create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n");
+
+	cl_git_pass(git_note_read(&note, _repo, "refs/notes/some/namespace", &target_oid));
+
+	assert_note_equal(note, "hello world on a custom namespace\n", &note_oid);
+
+	git_note_free(note);
+}
+
+/*
+ * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
+ * 08b041783f40edfe12bb406c9c9a8a040177c125
+ */
+void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void)
+{
+	int error;
+	git_oid note_oid, target_oid;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
+
+	create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
+	error = git_note_create(&note_oid, _repo, _sig, _sig, NULL, &target_oid, "hello world\n");
+	cl_git_fail(error);
+	cl_assert_equal_i(GIT_EEXISTS, error);
+
+	create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
+	error = git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello world\n");
+	cl_git_fail(error);
+	cl_assert_equal_i(GIT_EEXISTS, error);
+}
+
+static char *messages[] = {
+	"08c041783f40edfe12bb406c9c9a8a040177c125",
+	"96c45fbe09ab7445fc7c60fd8d17f32494399343",
+	"48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1",
+	"24c3eaafb681c3df668f9df96f58e7b8c756eb04",
+	"96ca1b6ccc7858ae94684777f85ac0e7447f7040",
+	"7ac2db4378a08bb244a427c357e0082ee0d57ac6",
+	"e6cba23dbf4ef84fe35e884f017f4e24dc228572",
+	"c8cf3462c7d8feba716deeb2ebe6583bd54589e2",
+	"39c16b9834c2d665ac5f68ad91dc5b933bad8549",
+	"f3c582b1397df6a664224ebbaf9d4cc952706597",
+	"29cec67037fe8e89977474988219016ae7f342a6",
+	"36c4cd238bf8e82e27b740e0741b025f2e8c79ab",
+	"f1c45a47c02e01d5a9a326f1d9f7f756373387f8",
+	"4aca84406f5daee34ab513a60717c8d7b1763ead",
+	"84ce167da452552f63ed8407b55d5ece4901845f",
+	NULL
+};
+
+#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
+
+/*
+ * $ git ls-tree refs/notes/fanout
+ * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea    84
+ *
+ * $ git ls-tree 4b22b35
+ * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759    96
+ *
+ * $ git ls-tree d71aab4
+ * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125    071c1b46c854b31185ea97743be6a8774479
+ */
+void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void)
+{
+	size_t i;
+	git_oid note_oid, target_oid;
+	git_note *_note;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
+	
+	for (i = 0; i <  MESSAGES_COUNT; i++) {
+		cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/fanout", &target_oid, messages[i]));
+		cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid));
+		git_note_free(_note);
+
+		git_oid_cpy(&target_oid, &note_oid);
+	}
+}
+
+/*
+ * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
+ * 08b041783f40edfe12bb406c9c9a8a040177c125
+ */
+void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
+{
+	git_oid note_oid, target_oid;
+	git_note *note;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
+	cl_git_pass(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
+
+	cl_git_pass(git_oid_fromstr(&note_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
+	cl_assert(!git_oid_cmp(git_note_oid(note), &note_oid));
+
+	git_note_free(note);
+}
+
+void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
+{
+	git_oid target_oid;
+	git_note *note;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
+	cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
+
+	cl_git_fail(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
+}
+
+void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void)
+{
+	int error;
+	git_oid target_oid;
+
+	cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
+	cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
+	
+	error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid);
+	cl_git_fail(error);
+	cl_assert_equal_i(GIT_ENOTFOUND, error);
+}
diff --git a/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125
new file mode 100644
index 0000000..d1c032f
Binary files /dev/null and b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 differ
diff --git a/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7
new file mode 100644
index 0000000..0a1500a
Binary files /dev/null and b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 differ
diff --git a/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea
new file mode 100644
index 0000000..b4e5aa1
Binary files /dev/null and b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea differ
diff --git a/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659
new file mode 100644
index 0000000..f3b46b3
Binary files /dev/null and b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 differ
diff --git a/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759
new file mode 100644
index 0000000..2d47e6f
Binary files /dev/null and b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 differ
diff --git a/tests-clar/resources/testrepo.git/refs/notes/fanout b/tests-clar/resources/testrepo.git/refs/notes/fanout
new file mode 100644
index 0000000..1f17036
--- /dev/null
+++ b/tests-clar/resources/testrepo.git/refs/notes/fanout
@@ -0,0 +1 @@
+d07b0f9a8c89f1d9e74dc4fce6421dec5ef8a659