notes: honor core.notesRef Setting core.notesRef allows to change the default notes reference used by Git. Check if set before using GIT_NOTES_DEFAULT_REF. Fixes #649.
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
diff --git a/src/notes.c b/src/notes.c
index 05c70c6..9e6722e 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -9,6 +9,7 @@
#include "git2.h"
#include "refs.h"
+#include "config.h"
static int find_subtree(git_tree **subtree, const git_oid *root,
git_repository *repo, const char *target, int *fanout)
@@ -262,6 +263,25 @@ static int note_remove(git_repository *repo,
return error;
}
+static int note_get_default_ref(const char **out, git_repository *repo)
+{
+ int error;
+ git_config *cfg;
+
+ *out = NULL;
+
+ if (git_repository_config__weakptr(&cfg, repo) < 0)
+ return -1;
+
+ error = git_config_get_string(cfg, "core.notesRef", out);
+ if (error == GIT_ENOTFOUND) {
+ *out = GIT_NOTES_DEFAULT_REF;
+ return 0;
+ }
+
+ return error;
+}
+
int git_note_read(git_note **out, git_repository *repo,
const char *notes_ref, const git_oid *oid)
{
@@ -273,8 +293,11 @@ int git_note_read(git_note **out, git_repository *repo,
*out = NULL;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+ if (!notes_ref) {
+ error = note_get_default_ref(¬es_ref, repo);
+ if (error < 0)
+ return error;
+ }
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0)
@@ -314,8 +337,11 @@ int git_note_create(
git_commit *commit = NULL;
git_reference *ref;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+ if (!notes_ref) {
+ error = note_get_default_ref(¬es_ref, repo);
+ if (error < 0)
+ return error;
+ }
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0 && error != GIT_ENOTFOUND)
@@ -359,8 +385,11 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
git_commit *commit;
git_reference *ref;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+ if (!notes_ref) {
+ error = note_get_default_ref(¬es_ref, repo);
+ if (error < 0)
+ return error;
+ }
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0)
diff --git a/tests-clar/notes/notesref.c b/tests-clar/notes/notesref.c
new file mode 100644
index 0000000..f145666
--- /dev/null
+++ b/tests-clar/notes/notesref.c
@@ -0,0 +1,46 @@
+#include "clar_libgit2.h"
+
+static git_repository *_repo;
+static git_note *_note;
+static git_signature *_sig;
+static git_config *_cfg;
+
+void test_notes_notesref__initialize(void)
+{
+ cl_fixture_sandbox("testrepo.git");
+ cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
+}
+
+void test_notes_notesref__cleanup(void)
+{
+ git_note_free(_note);
+ git_signature_free(_sig);
+ git_config_free(_cfg);
+
+ git_repository_free(_repo);
+ cl_fixture_cleanup("testrepo.git");
+}
+
+void test_notes_notesref__config_corenotesref(void)
+{
+ git_oid oid, note_oid;
+
+ cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com"));
+ cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));
+
+ cl_git_pass(git_repository_config(&_cfg, _repo));
+
+ cl_git_pass(git_config_set_string(_cfg, "core.notesRef", "refs/notes/mydefaultnotesref"));
+
+ cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "test123test\n"));
+
+ cl_git_pass(git_note_read(&_note, _repo, NULL, &oid));
+ cl_assert(!strcmp(git_note_message(_note), "test123test\n"));
+ cl_assert(!git_oid_cmp(git_note_oid(_note), ¬e_oid));
+
+ git_note_free(_note);
+
+ cl_git_pass(git_note_read(&_note, _repo, "refs/notes/mydefaultnotesref", &oid));
+ cl_assert(!strcmp(git_note_message(_note), "test123test\n"));
+ cl_assert(!git_oid_cmp(git_note_oid(_note), ¬e_oid));
+}