notes: add git_notes_foreach()
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
diff --git a/include/git2/notes.h b/include/git2/notes.h
index ecb37f3..7b2ac1f 100644
--- a/include/git2/notes.h
+++ b/include/git2/notes.h
@@ -102,6 +102,27 @@ GIT_EXTERN(void) git_note_free(git_note *note);
*/
GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo);
+/**
+ * Loop over all the notes within a specified namespace
+ * and issue a callback for each one.
+ *
+ * @param repo Repository where to find the notes.
+ *
+ * @param notes_ref OID reference to read from (optional); defaults to "refs/notes/commits".
+ *
+ * @param note_cb Callback to invoke per found annotation.
+ *
+ * @param payload Extra parameter to callback function.
+ *
+ * @return GIT_SUCCESS or an error code.
+ */
+GIT_EXTERN(int) git_note_foreach(
+ git_repository *repo,
+ const char *notes_ref,
+ int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
+ void *payload
+);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/notes.c b/src/notes.c
index 4afdac0..e3fb91f 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -10,6 +10,7 @@
#include "git2.h"
#include "refs.h"
#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)
@@ -282,41 +283,54 @@ static int note_get_default_ref(const char **out, git_repository *repo)
return ret;
}
+static int normalize_namespace(const char **notes_ref, git_repository *repo)
+{
+ if (*notes_ref)
+ return 0;
+
+ 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)
+{
+ int error = -1;
+ git_commit *commit = NULL;
+ git_oid oid;
+
+ if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0)
+ goto cleanup;
+
+ if (git_commit_lookup(&commit, repo, &oid) < 0)
+ goto cleanup;
+
+ git_oid_cpy(tree_oid_out, git_commit_tree_oid(commit));
+
+ error = 0;
+
+cleanup:
+ git_commit_free(commit);
+ return error;
+}
+
int git_note_read(git_note **out, git_repository *repo,
const char *notes_ref, const git_oid *oid)
{
int error;
char *target;
- git_reference *ref;
- git_commit *commit;
- const git_oid *sha;
+ git_oid sha;
*out = NULL;
- if (!notes_ref && note_get_default_ref(¬es_ref, repo) < 0)
+ if (normalize_namespace(¬es_ref, repo) < 0)
return -1;
- error = git_reference_lookup(&ref, repo, notes_ref);
- if (error < 0)
+ if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0)
return error;
- assert(git_reference_type(ref) == GIT_REF_OID);
-
- sha = git_reference_oid(ref);
- error = git_commit_lookup(&commit, repo, sha);
-
- git_reference_free(ref);
-
- if (error < 0)
- return error;
-
- sha = git_commit_tree_oid(commit);
- git_commit_free(commit);
-
target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target);
- error = note_lookup(out, repo, sha, target);
+ error = note_lookup(out, repo, &sha, target);
git__free(target);
return error;
@@ -334,7 +348,7 @@ int git_note_create(
git_commit *commit = NULL;
git_reference *ref;
- if (!notes_ref && note_get_default_ref(¬es_ref, repo) < 0)
+ if (normalize_namespace(¬es_ref, repo) < 0)
return -1;
error = git_reference_lookup(&ref, repo, notes_ref);
@@ -379,8 +393,7 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
git_commit *commit;
git_reference *ref;
-
- if (!notes_ref && note_get_default_ref(¬es_ref, repo) < 0)
+ if (normalize_namespace(¬es_ref, repo) < 0)
return -1;
error = git_reference_lookup(&ref, repo, notes_ref);
@@ -435,3 +448,102 @@ void git_note_free(git_note *note)
git__free(note->message);
git__free(note);
}
+
+static int process_entry_path(
+ const char* entry_path,
+ git_oid note_oid,
+ int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
+ void *payload)
+{
+ int i = 0, j = 0, error = -1, len;
+ bool is_hex_only = true;
+ git_oid target_oid;
+ git_buf buf = GIT_BUF_INIT;
+
+ if (git_buf_puts(&buf, entry_path) < 0)
+ goto cleanup;
+
+ len = git_buf_len(&buf);
+
+ while (i < len) {
+ if (buf.ptr[i] == '/') {
+ i++;
+ continue;
+ }
+
+ if (git__fromhex(buf.ptr[i]) < 0) {
+ /* This is not a note entry */
+ error = 0;
+ goto cleanup;
+ }
+
+ if (i != j)
+ buf.ptr[j] = buf.ptr[i];
+
+ i++;
+ j++;
+ }
+
+ buf.ptr[j] = '\0';
+ buf.size = j;
+
+ if (j != GIT_OID_HEXSZ) {
+ /* This is not a note entry */
+ error = 0;
+ goto cleanup;
+ }
+
+ if (git_oid_fromstr(&target_oid, buf.ptr) < 0)
+ return -1;
+
+ error = note_cb(¬e_oid, &target_oid, payload);
+
+cleanup:
+ git_buf_free(&buf);
+ return error;
+}
+
+int git_note_foreach(
+ git_repository *repo,
+ const char *notes_ref,
+ int (*note_cb)(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload),
+ void *payload)
+{
+ int error = -1;
+ unsigned int i;
+ char *note;
+ git_oid tree_oid;
+ git_iterator *iter = NULL;
+ git_tree *tree = NULL;
+ git_index_entry *item;
+
+ if (normalize_namespace(¬es_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)
+ goto cleanup;
+
+ if (git_iterator_for_tree(repo, tree, &iter) < 0)
+ goto cleanup;
+
+ if (git_iterator_current(iter, &item) < 0)
+ goto cleanup;
+
+ while (item) {
+ if (process_entry_path(item->path, item->oid, note_cb, payload) < 0)
+ goto cleanup;
+
+ if (git_iterator_advance(iter, &item) < 0)
+ goto cleanup;
+ }
+
+ error = 0;
+
+cleanup:
+ git_iterator_free(iter);
+ git_tree_free(tree);
+ return error;
+}
diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c
index ca82ab2..228e414 100644
--- a/tests-clar/notes/notes.c
+++ b/tests-clar/notes/notes.c
@@ -1,43 +1,46 @@
#include "clar_libgit2.h"
static git_repository *_repo;
-static git_note *_note;
-static git_blob *_blob;
static git_signature *_sig;
void test_notes_notes__initialize(void)
{
- cl_fixture_sandbox("testrepo.git");
- cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
+ _repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com"));
}
void test_notes_notes__cleanup(void)
{
- git_note_free(_note);
- git_blob_free(_blob);
git_signature_free(_sig);
+ cl_git_sandbox_cleanup();
+}
+
+static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message)
+{
+ git_oid oid;
- git_repository_free(_repo);
- cl_fixture_cleanup("testrepo.git");
+ cl_git_pass(git_oid_fromstr(&oid, target_sha));
+ 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_signature_now(&_sig, "alice", "alice@example.com"));
cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
- cl_git_pass(git_note_read(&_note, _repo, NULL, &oid));
+ cl_git_pass(git_note_read(¬e, _repo, NULL, &oid));
- cl_assert_equal_s(git_note_message(_note), "hello world\n");
- cl_assert(!git_oid_cmp(git_note_oid(_note), ¬e_oid));
+ cl_assert_equal_s(git_note_message(note), "hello world\n");
+ cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid));
- cl_git_pass(git_blob_lookup(&_blob, _repo, ¬e_oid));
- cl_assert_equal_s(git_note_message(_note), git_blob_rawcontent(_blob));
+ cl_git_pass(git_blob_lookup(&blob, _repo, ¬e_oid));
+ cl_assert_equal_s(git_note_message(note), git_blob_rawcontent(blob));
cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
@@ -47,4 +50,83 @@ void test_notes_notes__1(void)
cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, ¬e_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;
+} list_expectations[] = {
+ { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
+ { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "9fd738e8f7967c078dceed8190330fc8648ee56a" },
+ { "257b43746b6b46caa4aa788376c647cce0a33e2b", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750" },
+ { "1ec1c8e03f461f4f5d3f3702172483662e7223f3", "c47800c7266a2be04c571c04d5a6614691ea99bd" },
+ { NULL, NULL }
+};
+
+#define EXPECTATIONS_COUNT (sizeof(list_expectations)/sizeof(list_expectations[0])) - 1
+
+static int note_list_cb(const git_oid *note_oid, const git_oid *annotated_object_oid, void *payload)
+{
+ git_oid expected_note_oid, expected_target_oid;
+
+ int *count = (int *)payload;
+
+ cl_assert(*count < EXPECTATIONS_COUNT);
+
+ cl_git_pass(git_oid_fromstr(&expected_note_oid, list_expectations[*count].note_sha));
+ cl_assert(git_oid_cmp(&expected_note_oid, note_oid) == 0);
+
+ cl_git_pass(git_oid_fromstr(&expected_target_oid, list_expectations[*count].annotated_object_sha));
+ cl_assert(git_oid_cmp(&expected_target_oid, annotated_object_oid) == 0);
+
+ (*count)++;
+
+ return 0;
+}
+
+/*
+ * $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
+ * $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
+ * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 9fd738e8f7967c078dceed8190330fc8648ee56a
+ * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
+ *
+ * $ git notes --ref i-can-see-dead-notes list
+ * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
+ * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
+ * 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
+ * 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
+ *
+ * $ git ls-tree refs/notes/i-can-see-dead-notes
+ * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
+ * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
+ * 100644 blob 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
+ * 100644 blob 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
+*/
+void test_notes_notes__can_retrieve_a_list_of_notes_for_a_given_namespace(void)
+{
+ git_oid note_oid1, note_oid2, note_oid3, note_oid4;
+ int retrieved_notes = 0;
+
+ create_note(¬e_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
+ create_note(¬e_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
+ create_note(¬e_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
+ create_note(¬e_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
+
+ cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_cb, &retrieved_notes));
+
+ cl_assert_equal_i(4, retrieved_notes);
+}
+
+void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_returns_ENOTFOUND(void)
+{
+ int error, retrieved_notes = 0;
+
+ error = git_note_foreach(_repo, "refs/notes/i-am-not", note_list_cb, &retrieved_notes);
+ cl_git_fail(error);
+ cl_assert_equal_i(GIT_ENOTFOUND, error);
+
+ cl_assert_equal_i(0, retrieved_notes);
}