Skip to content

Commit baf001e

Browse files
authored
Merge pull request libgit2#6047 from libgit2/ethomson/notes_cleanup
notes: use a buffer internally
2 parents f1b89a2 + cd0fd0f commit baf001e

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/notes.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -407,31 +407,33 @@ static int note_remove(
407407
return error;
408408
}
409409

410-
static int note_get_default_ref(char **out, git_repository *repo)
410+
static int note_get_default_ref(git_buf *out, git_repository *repo)
411411
{
412412
git_config *cfg;
413-
int ret = git_repository_config__weakptr(&cfg, repo);
413+
int error;
414+
415+
if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
416+
return error;
414417

415-
*out = (ret != 0) ? NULL : git_config__get_string_force(
416-
cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
418+
error = git_config_get_string_buf(out, cfg, "core.notesref");
417419

418-
return ret;
420+
if (error == GIT_ENOTFOUND)
421+
error = git_buf_puts(out, GIT_NOTES_DEFAULT_REF);
422+
423+
return error;
419424
}
420425

421-
static int normalize_namespace(char **out, git_repository *repo, const char *notes_ref)
426+
static int normalize_namespace(git_buf *out, git_repository *repo, const char *notes_ref)
422427
{
423-
if (notes_ref) {
424-
*out = git__strdup(notes_ref);
425-
GIT_ERROR_CHECK_ALLOC(*out);
426-
return 0;
427-
}
428+
if (notes_ref)
429+
return git_buf_puts(out, notes_ref);
428430

429431
return note_get_default_ref(out, repo);
430432
}
431433

432434
static int retrieve_note_commit(
433435
git_commit **commit_out,
434-
char **notes_ref_out,
436+
git_buf *notes_ref_out,
435437
git_repository *repo,
436438
const char *notes_ref)
437439
{
@@ -441,7 +443,7 @@ static int retrieve_note_commit(
441443
if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
442444
return error;
443445

444-
if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0)
446+
if ((error = git_reference_name_to_id(&oid, repo, notes_ref_out->ptr)) < 0)
445447
return error;
446448

447449
if (git_commit_lookup(commit_out, repo, &oid) < 0)
@@ -476,7 +478,7 @@ int git_note_read(git_note **out, git_repository *repo,
476478
const char *notes_ref_in, const git_oid *oid)
477479
{
478480
int error;
479-
char *notes_ref = NULL;
481+
git_buf notes_ref = GIT_BUF_INIT;
480482
git_commit *commit = NULL;
481483

482484
error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
@@ -487,7 +489,7 @@ int git_note_read(git_note **out, git_repository *repo,
487489
error = git_note_commit_read(out, repo, commit, oid);
488490

489491
cleanup:
490-
git__free(notes_ref);
492+
git_buf_dispose(&notes_ref);
491493
git_commit_free(commit);
492494
return error;
493495
}
@@ -534,7 +536,7 @@ int git_note_create(
534536
int allow_note_overwrite)
535537
{
536538
int error;
537-
char *notes_ref = NULL;
539+
git_buf notes_ref = GIT_BUF_INIT;
538540
git_commit *existing_notes_commit = NULL;
539541
git_reference *ref = NULL;
540542
git_oid notes_blob_oid, notes_commit_oid;
@@ -553,14 +555,14 @@ int git_note_create(
553555
if (error < 0)
554556
goto cleanup;
555557

556-
error = git_reference_create(&ref, repo, notes_ref,
558+
error = git_reference_create(&ref, repo, notes_ref.ptr,
557559
&notes_commit_oid, 1, NULL);
558560

559561
if (out != NULL)
560562
git_oid_cpy(out, &notes_blob_oid);
561563

562564
cleanup:
563-
git__free(notes_ref);
565+
git_buf_dispose(&notes_ref);
564566
git_commit_free(existing_notes_commit);
565567
git_reference_free(ref);
566568
return error;
@@ -596,7 +598,7 @@ int git_note_remove(git_repository *repo, const char *notes_ref_in,
596598
const git_oid *oid)
597599
{
598600
int error;
599-
char *notes_ref_target = NULL;
601+
git_buf notes_ref_target = GIT_BUF_INIT;
600602
git_commit *existing_notes_commit = NULL;
601603
git_oid new_notes_commit;
602604
git_reference *notes_ref = NULL;
@@ -612,30 +614,28 @@ int git_note_remove(git_repository *repo, const char *notes_ref_in,
612614
if (error < 0)
613615
goto cleanup;
614616

615-
error = git_reference_create(&notes_ref, repo, notes_ref_target,
617+
error = git_reference_create(&notes_ref, repo, notes_ref_target.ptr,
616618
&new_notes_commit, 1, NULL);
617619

618620
cleanup:
619-
git__free(notes_ref_target);
621+
git_buf_dispose(&notes_ref_target);
620622
git_reference_free(notes_ref);
621623
git_commit_free(existing_notes_commit);
622624
return error;
623625
}
624626

625627
int git_note_default_ref(git_buf *out, git_repository *repo)
626628
{
627-
char *default_ref;
628629
int error;
629630

630631
GIT_ASSERT_ARG(out);
631632
GIT_ASSERT_ARG(repo);
632633

633634
if ((error = git_buf_sanitize(out)) < 0 ||
634-
(error = note_get_default_ref(&default_ref, repo)) < 0)
635-
return error;
635+
(error = note_get_default_ref(out, repo)) < 0)
636+
git_buf_dispose(out);
636637

637-
git_buf_attach(out, default_ref, strlen(default_ref));
638-
return 0;
638+
return error;
639639
}
640640

641641
const git_signature *git_note_committer(const git_note *note)
@@ -780,7 +780,7 @@ int git_note_iterator_new(
780780
{
781781
int error;
782782
git_commit *commit = NULL;
783-
char *notes_ref;
783+
git_buf notes_ref = GIT_BUF_INIT;
784784

785785
error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
786786
if (error < 0)
@@ -789,7 +789,7 @@ int git_note_iterator_new(
789789
error = git_note_commit_iterator_new(it, commit);
790790

791791
cleanup:
792-
git__free(notes_ref);
792+
git_buf_dispose(&notes_ref);
793793
git_commit_free(commit);
794794

795795
return error;

0 commit comments

Comments
 (0)