Commit d788f42a9c5f7840ff6ff86e0b9b06a785f42bd2

Richard Ipsum 2017-04-09T14:06:23

notes: Rewrite funcs in terms of note_commit funcs

diff --git a/src/notes.c b/src/notes.c
index 68486c9..f63ef36 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -424,8 +424,7 @@ static int normalize_namespace(char **out, git_repository *repo, const char *not
 	return note_get_default_ref(out, repo);
 }
 
-static int retrieve_note_tree_and_commit(
-	git_tree **tree_out,
+static int retrieve_note_commit(
 	git_commit **commit_out,
 	char **notes_ref_out,
 	git_repository *repo,
@@ -443,34 +442,9 @@ static int retrieve_note_tree_and_commit(
 	if (git_commit_lookup(commit_out, repo, &oid) < 0)
 		return error;
 
-	if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
-		return error;
-
 	return 0;
 }
 
-int git_note_read(git_note **out, git_repository *repo,
-		  const char *notes_ref_in, const git_oid *oid)
-{
-	int error;
-	char *target = NULL, *notes_ref = NULL;
-	git_tree *tree = NULL;
-	git_commit *commit = NULL;
-
-	target = git_oid_allocfmt(oid);
-	GITERR_CHECK_ALLOC(target);
-
-	if (!(error = retrieve_note_tree_and_commit(
-		      &tree, &commit, &notes_ref, repo, notes_ref_in)))
-		error = note_lookup(out, repo, commit, tree, target);
-
-	git__free(notes_ref);
-	git__free(target);
-	git_tree_free(tree);
-	git_commit_free(commit);
-	return error;
-}
-
 int git_note_commit_read(
 	git_note **out,
 	git_repository *repo,
@@ -493,37 +467,23 @@ cleanup:
 	return error;
 }
 
-int git_note_create(
-	git_oid *out,
-	git_repository *repo,
-	const char *notes_ref_in,
-	const git_signature *author,
-	const git_signature *committer,
-	const git_oid *oid,
-	const char *note,
-	int allow_note_overwrite)
+int git_note_read(git_note **out, git_repository *repo,
+		  const char *notes_ref_in, const git_oid *oid)
 {
 	int error;
-	char *target = NULL, *notes_ref = NULL;
+	char *notes_ref = NULL;
 	git_commit *commit = NULL;
-	git_tree *tree = NULL;
 
-	target = git_oid_allocfmt(oid);
-	GITERR_CHECK_ALLOC(target);
+	error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
 
-	error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
-
-	if (error < 0 && error != GIT_ENOTFOUND)
+	if (error < 0)
 		goto cleanup;
 
-	error = note_write(NULL, out, repo, author, committer, notes_ref,
-			note, tree, target, &commit, allow_note_overwrite);
+	error = git_note_commit_read(out, repo, commit, oid);
 
 cleanup:
 	git__free(notes_ref);
-	git__free(target);
 	git_commit_free(commit);
-	git_tree_free(tree);
 	return error;
 }
 
@@ -558,27 +518,46 @@ cleanup:
 	return error;
 }
 
-int git_note_remove(git_repository *repo, const char *notes_ref_in,
-		const git_signature *author, const git_signature *committer,
-		const git_oid *oid)
+int git_note_create(
+	git_oid *out,
+	git_repository *repo,
+	const char *notes_ref_in,
+	const git_signature *author,
+	const git_signature *committer,
+	const git_oid *oid,
+	const char *note,
+	int allow_note_overwrite)
 {
 	int error;
-	char *target = NULL, *notes_ref;
-	git_commit *commit = NULL;
-	git_tree *tree = NULL;
+	char *notes_ref = NULL;
+	git_commit *existing_notes_commit = NULL;
+	git_reference *ref = NULL;
+	git_oid notes_blob_oid, notes_commit_oid;
 
-	target = git_oid_allocfmt(oid);
-	GITERR_CHECK_ALLOC(target);
+	error = retrieve_note_commit(&existing_notes_commit, &notes_ref,
+			repo, notes_ref_in);
+
+	if (error < 0 && error != GIT_ENOTFOUND)
+		goto cleanup;
+
+	error = git_note_commit_create(&notes_commit_oid,
+			&notes_blob_oid,
+			repo, existing_notes_commit, author,
+			committer, oid, note,
+			allow_note_overwrite);
+	if (error < 0)
+		goto cleanup;
 
-	if (!(error = retrieve_note_tree_and_commit(
-		      &tree, &commit, &notes_ref, repo, notes_ref_in)))
-		error = note_remove(NULL,
-			repo, author, committer, notes_ref, tree, target, &commit);
+	error = git_reference_create(&ref, repo, notes_ref,
+				&notes_commit_oid, 1, NULL);
 
+	if (out != NULL)
+		git_oid_cpy(out, &notes_blob_oid);
+
+cleanup:
 	git__free(notes_ref);
-	git__free(target);
-	git_commit_free(commit);
-	git_tree_free(tree);
+	git_commit_free(existing_notes_commit);
+	git_reference_free(ref);
 	return error;
 }
 
@@ -607,6 +586,37 @@ cleanup:
 	return error;
 }
 
+int git_note_remove(git_repository *repo, const char *notes_ref_in,
+		const git_signature *author, const git_signature *committer,
+		const git_oid *oid)
+{
+	int error;
+	char *notes_ref_target = NULL;
+	git_commit *existing_notes_commit = NULL;
+	git_oid new_notes_commit;
+	git_reference *notes_ref = NULL;
+
+	error = retrieve_note_commit(&existing_notes_commit, &notes_ref_target,
+			repo, notes_ref_in);
+
+	if (error < 0)
+		goto cleanup;
+
+	error = git_note_commit_remove(&new_notes_commit, repo,
+			existing_notes_commit, author, committer, oid);
+	if (error < 0)
+		goto cleanup;
+
+	error = git_reference_create(&notes_ref, repo, notes_ref_target,
+			&new_notes_commit, 1, NULL);
+
+cleanup:
+	git__free(notes_ref_target);
+	git_reference_free(notes_ref);
+	git_commit_free(existing_notes_commit);
+	return error;
+}
+
 int git_note_default_ref(git_buf *out, git_repository *repo)
 {
 	char *default_ref;
@@ -731,7 +741,6 @@ int git_note_foreach(
 	return error;
 }
 
-
 void git_note_iterator_free(git_note_iterator *it)
 {
 	if (it == NULL)
@@ -740,47 +749,43 @@ void git_note_iterator_free(git_note_iterator *it)
 	git_iterator_free(it);
 }
 
-
-int git_note_iterator_new(
+int git_note_commit_iterator_new(
 	git_note_iterator **it,
-	git_repository *repo,
-	const char *notes_ref_in)
+	git_commit *notes_commit)
 {
 	int error;
-	git_commit *commit = NULL;
-	git_tree *tree = NULL;
-	char *notes_ref;
+	git_tree *tree;
 
-	error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
-	if (error < 0)
+	if ((error = git_commit_tree(&tree, notes_commit)) < 0)
 		goto cleanup;
 
 	if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
 		git_iterator_free(*it);
 
 cleanup:
-	git__free(notes_ref);
 	git_tree_free(tree);
-	git_commit_free(commit);
 
 	return error;
 }
 
-int git_note_commit_iterator_new(
+int git_note_iterator_new(
 	git_note_iterator **it,
-	git_commit *notes_commit)
+	git_repository *repo,
+	const char *notes_ref_in)
 {
 	int error;
-	git_tree *tree;
+	git_commit *commit = NULL;
+	char *notes_ref;
 
-	if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+	error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
+	if (error < 0)
 		goto cleanup;
 
-	if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
-		git_iterator_free(*it);
+	error = git_note_commit_iterator_new(it, commit);
 
 cleanup:
-	git_tree_free(tree);
+	git__free(notes_ref);
+	git_commit_free(commit);
 
 	return error;
 }