Skip to content

Commit d788f42

Browse files
committed
notes: Rewrite funcs in terms of note_commit funcs
1 parent 60bee89 commit d788f42

File tree

1 file changed

+86
-81
lines changed

1 file changed

+86
-81
lines changed

src/notes.c

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ static int normalize_namespace(char **out, git_repository *repo, const char *not
424424
return note_get_default_ref(out, repo);
425425
}
426426

427-
static int retrieve_note_tree_and_commit(
428-
git_tree **tree_out,
427+
static int retrieve_note_commit(
429428
git_commit **commit_out,
430429
char **notes_ref_out,
431430
git_repository *repo,
@@ -443,34 +442,9 @@ static int retrieve_note_tree_and_commit(
443442
if (git_commit_lookup(commit_out, repo, &oid) < 0)
444443
return error;
445444

446-
if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
447-
return error;
448-
449445
return 0;
450446
}
451447

452-
int git_note_read(git_note **out, git_repository *repo,
453-
const char *notes_ref_in, const git_oid *oid)
454-
{
455-
int error;
456-
char *target = NULL, *notes_ref = NULL;
457-
git_tree *tree = NULL;
458-
git_commit *commit = NULL;
459-
460-
target = git_oid_allocfmt(oid);
461-
GITERR_CHECK_ALLOC(target);
462-
463-
if (!(error = retrieve_note_tree_and_commit(
464-
&tree, &commit, &notes_ref, repo, notes_ref_in)))
465-
error = note_lookup(out, repo, commit, tree, target);
466-
467-
git__free(notes_ref);
468-
git__free(target);
469-
git_tree_free(tree);
470-
git_commit_free(commit);
471-
return error;
472-
}
473-
474448
int git_note_commit_read(
475449
git_note **out,
476450
git_repository *repo,
@@ -493,37 +467,23 @@ int git_note_commit_read(
493467
return error;
494468
}
495469

496-
int git_note_create(
497-
git_oid *out,
498-
git_repository *repo,
499-
const char *notes_ref_in,
500-
const git_signature *author,
501-
const git_signature *committer,
502-
const git_oid *oid,
503-
const char *note,
504-
int allow_note_overwrite)
470+
int git_note_read(git_note **out, git_repository *repo,
471+
const char *notes_ref_in, const git_oid *oid)
505472
{
506473
int error;
507-
char *target = NULL, *notes_ref = NULL;
474+
char *notes_ref = NULL;
508475
git_commit *commit = NULL;
509-
git_tree *tree = NULL;
510476

511-
target = git_oid_allocfmt(oid);
512-
GITERR_CHECK_ALLOC(target);
477+
error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
513478

514-
error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
515-
516-
if (error < 0 && error != GIT_ENOTFOUND)
479+
if (error < 0)
517480
goto cleanup;
518481

519-
error = note_write(NULL, out, repo, author, committer, notes_ref,
520-
note, tree, target, &commit, allow_note_overwrite);
482+
error = git_note_commit_read(out, repo, commit, oid);
521483

522484
cleanup:
523485
git__free(notes_ref);
524-
git__free(target);
525486
git_commit_free(commit);
526-
git_tree_free(tree);
527487
return error;
528488
}
529489

@@ -558,27 +518,46 @@ int git_note_commit_create(
558518
return error;
559519
}
560520

561-
int git_note_remove(git_repository *repo, const char *notes_ref_in,
562-
const git_signature *author, const git_signature *committer,
563-
const git_oid *oid)
521+
int git_note_create(
522+
git_oid *out,
523+
git_repository *repo,
524+
const char *notes_ref_in,
525+
const git_signature *author,
526+
const git_signature *committer,
527+
const git_oid *oid,
528+
const char *note,
529+
int allow_note_overwrite)
564530
{
565531
int error;
566-
char *target = NULL, *notes_ref;
567-
git_commit *commit = NULL;
568-
git_tree *tree = NULL;
532+
char *notes_ref = NULL;
533+
git_commit *existing_notes_commit = NULL;
534+
git_reference *ref = NULL;
535+
git_oid notes_blob_oid, notes_commit_oid;
569536

570-
target = git_oid_allocfmt(oid);
571-
GITERR_CHECK_ALLOC(target);
537+
error = retrieve_note_commit(&existing_notes_commit, &notes_ref,
538+
repo, notes_ref_in);
539+
540+
if (error < 0 && error != GIT_ENOTFOUND)
541+
goto cleanup;
542+
543+
error = git_note_commit_create(&notes_commit_oid,
544+
&notes_blob_oid,
545+
repo, existing_notes_commit, author,
546+
committer, oid, note,
547+
allow_note_overwrite);
548+
if (error < 0)
549+
goto cleanup;
572550

573-
if (!(error = retrieve_note_tree_and_commit(
574-
&tree, &commit, &notes_ref, repo, notes_ref_in)))
575-
error = note_remove(NULL,
576-
repo, author, committer, notes_ref, tree, target, &commit);
551+
error = git_reference_create(&ref, repo, notes_ref,
552+
&notes_commit_oid, 1, NULL);
577553

554+
if (out != NULL)
555+
git_oid_cpy(out, &notes_blob_oid);
556+
557+
cleanup:
578558
git__free(notes_ref);
579-
git__free(target);
580-
git_commit_free(commit);
581-
git_tree_free(tree);
559+
git_commit_free(existing_notes_commit);
560+
git_reference_free(ref);
582561
return error;
583562
}
584563

@@ -607,6 +586,37 @@ int git_note_commit_remove(
607586
return error;
608587
}
609588

589+
int git_note_remove(git_repository *repo, const char *notes_ref_in,
590+
const git_signature *author, const git_signature *committer,
591+
const git_oid *oid)
592+
{
593+
int error;
594+
char *notes_ref_target = NULL;
595+
git_commit *existing_notes_commit = NULL;
596+
git_oid new_notes_commit;
597+
git_reference *notes_ref = NULL;
598+
599+
error = retrieve_note_commit(&existing_notes_commit, &notes_ref_target,
600+
repo, notes_ref_in);
601+
602+
if (error < 0)
603+
goto cleanup;
604+
605+
error = git_note_commit_remove(&new_notes_commit, repo,
606+
existing_notes_commit, author, committer, oid);
607+
if (error < 0)
608+
goto cleanup;
609+
610+
error = git_reference_create(&notes_ref, repo, notes_ref_target,
611+
&new_notes_commit, 1, NULL);
612+
613+
cleanup:
614+
git__free(notes_ref_target);
615+
git_reference_free(notes_ref);
616+
git_commit_free(existing_notes_commit);
617+
return error;
618+
}
619+
610620
int git_note_default_ref(git_buf *out, git_repository *repo)
611621
{
612622
char *default_ref;
@@ -731,7 +741,6 @@ int git_note_foreach(
731741
return error;
732742
}
733743

734-
735744
void git_note_iterator_free(git_note_iterator *it)
736745
{
737746
if (it == NULL)
@@ -740,47 +749,43 @@ void git_note_iterator_free(git_note_iterator *it)
740749
git_iterator_free(it);
741750
}
742751

743-
744-
int git_note_iterator_new(
752+
int git_note_commit_iterator_new(
745753
git_note_iterator **it,
746-
git_repository *repo,
747-
const char *notes_ref_in)
754+
git_commit *notes_commit)
748755
{
749756
int error;
750-
git_commit *commit = NULL;
751-
git_tree *tree = NULL;
752-
char *notes_ref;
757+
git_tree *tree;
753758

754-
error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
755-
if (error < 0)
759+
if ((error = git_commit_tree(&tree, notes_commit)) < 0)
756760
goto cleanup;
757761

758762
if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
759763
git_iterator_free(*it);
760764

761765
cleanup:
762-
git__free(notes_ref);
763766
git_tree_free(tree);
764-
git_commit_free(commit);
765767

766768
return error;
767769
}
768770

769-
int git_note_commit_iterator_new(
771+
int git_note_iterator_new(
770772
git_note_iterator **it,
771-
git_commit *notes_commit)
773+
git_repository *repo,
774+
const char *notes_ref_in)
772775
{
773776
int error;
774-
git_tree *tree;
777+
git_commit *commit = NULL;
778+
char *notes_ref;
775779

776-
if ((error = git_commit_tree(&tree, notes_commit)) < 0)
780+
error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
781+
if (error < 0)
777782
goto cleanup;
778783

779-
if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
780-
git_iterator_free(*it);
784+
error = git_note_commit_iterator_new(it, commit);
781785

782786
cleanup:
783-
git_tree_free(tree);
787+
git__free(notes_ref);
788+
git_commit_free(commit);
784789

785790
return error;
786791
}

0 commit comments

Comments
 (0)