]> git.proxmox.com Git - libgit2.git/blame - src/notes.c
New upstream version 0.27.7+dfsg.1
[libgit2.git] / src / notes.c
CommitLineData
bf477ed4 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
bf477ed4
MS
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include "notes.h"
9
10#include "git2.h"
11#include "refs.h"
caea5e54 12#include "config.h"
86ecd844 13#include "iterator.h"
4ec197f3 14#include "signature.h"
bf477ed4 15
734c6fc1
RB
16static int note_error_notfound(void)
17{
909d5494 18 giterr_set(GITERR_INVALID, "note could not be found");
734c6fc1
RB
19 return GIT_ENOTFOUND;
20}
21
b93688d0
VM
22static int find_subtree_in_current_level(
23 git_tree **out,
24 git_repository *repo,
25 git_tree *parent,
26 const char *annotated_object_sha,
27 int fanout)
bf477ed4 28{
e120123e 29 size_t i;
bf477ed4
MS
30 const git_tree_entry *entry;
31
a02e7249 32 *out = NULL;
e120123e 33
a02e7249 34 if (parent == NULL)
734c6fc1 35 return note_error_notfound();
bf477ed4 36
a02e7249 37 for (i = 0; i < git_tree_entrycount(parent); i++) {
38 entry = git_tree_entry_byindex(parent, i);
bf477ed4
MS
39
40 if (!git__ishex(git_tree_entry_name(entry)))
41 continue;
42
9d7ac675 43 if (S_ISDIR(git_tree_entry_filemode(entry))
e120123e 44 && strlen(git_tree_entry_name(entry)) == 2
a02e7249 45 && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
46 return git_tree_lookup(out, repo, git_tree_entry_id(entry));
bf477ed4 47
a02e7249 48 /* Not a DIR, so do we have an already existing blob? */
e120123e 49 if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))
a02e7249 50 return GIT_EEXISTS;
51 }
bf477ed4 52
734c6fc1 53 return note_error_notfound();
a02e7249 54}
bf477ed4 55
a02e7249 56static int find_subtree_r(git_tree **out, git_tree *root,
57 git_repository *repo, const char *target, int *fanout)
58{
59 int error;
60 git_tree *subtree = NULL;
bf477ed4 61
a02e7249 62 *out = NULL;
bf477ed4 63
a02e7249 64 error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
734c6fc1 65 if (error == GIT_EEXISTS)
dca6b228 66 return git_tree_lookup(out, repo, git_tree_id(root));
bf477ed4 67
a02e7249 68 if (error < 0)
b93688d0 69 return error;
a02e7249 70
71 *fanout += 2;
72 error = find_subtree_r(out, subtree, repo, target, fanout);
dca6b228 73 git_tree_free(subtree);
a02e7249 74
a02e7249 75 return error;
bf477ed4
MS
76}
77
78static int find_blob(git_oid *blob, git_tree *tree, const char *target)
79{
e120123e 80 size_t i;
bf477ed4
MS
81 const git_tree_entry *entry;
82
83 for (i=0; i<git_tree_entrycount(tree); i++) {
84 entry = git_tree_entry_byindex(tree, i);
85
86 if (!strcmp(git_tree_entry_name(entry), target)) {
87 /* found matching note object - return */
88
89 git_oid_cpy(blob, git_tree_entry_id(entry));
4aa7de15 90 return 0;
bf477ed4
MS
91 }
92 }
734c6fc1
RB
93
94 return note_error_notfound();
bf477ed4
MS
95}
96
b93688d0
VM
97static int tree_write(
98 git_tree **out,
99 git_repository *repo,
100 git_tree *source_tree,
101 const git_oid *object_oid,
102 const char *treeentry_name,
103 unsigned int attributes)
bf477ed4 104{
a02e7249 105 int error;
106 git_treebuilder *tb = NULL;
0e2fcca8 107 const git_tree_entry *entry;
a02e7249 108 git_oid tree_oid;
bf477ed4 109
208a2c8a 110 if ((error = git_treebuilder_new(&tb, repo, source_tree)) < 0)
a02e7249 111 goto cleanup;
bf477ed4 112
a02e7249 113 if (object_oid) {
b93688d0
VM
114 if ((error = git_treebuilder_insert(
115 &entry, tb, treeentry_name, object_oid, attributes)) < 0)
a02e7249 116 goto cleanup;
117 } else {
118 if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
119 goto cleanup;
120 }
bf477ed4 121
dce7b1a4 122 if ((error = git_treebuilder_write(&tree_oid, tb)) < 0)
a02e7249 123 goto cleanup;
bf477ed4 124
a02e7249 125 error = git_tree_lookup(out, repo, &tree_oid);
bf477ed4 126
a02e7249 127cleanup:
128 git_treebuilder_free(tb);
129 return error;
130}
bf477ed4 131
b93688d0
VM
132static int manipulate_note_in_tree_r(
133 git_tree **out,
134 git_repository *repo,
135 git_tree *parent,
136 git_oid *note_oid,
137 const char *annotated_object_sha,
4a44087a 138 int fanout,
b93688d0 139 int (*note_exists_cb)(
4a44087a
NV
140 git_tree **out,
141 git_repository *repo,
b93688d0
VM
142 git_tree *parent,
143 git_oid *note_oid,
144 const char *annotated_object_sha,
145 int fanout,
146 int current_error),
147 int (*note_notfound_cb)(
148 git_tree **out,
149 git_repository *repo,
150 git_tree *parent,
151 git_oid *note_oid,
152 const char *annotated_object_sha,
153 int fanout,
154 int current_error))
a02e7249 155{
56543a60 156 int error;
dca6b228 157 git_tree *subtree = NULL, *new = NULL;
a02e7249 158 char subtree_name[3];
bf477ed4 159
b93688d0
VM
160 error = find_subtree_in_current_level(
161 &subtree, repo, parent, annotated_object_sha, fanout);
bf477ed4 162
a02e7249 163 if (error == GIT_EEXISTS) {
b93688d0
VM
164 error = note_exists_cb(
165 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
a02e7249 166 goto cleanup;
167 }
bf477ed4 168
a02e7249 169 if (error == GIT_ENOTFOUND) {
b93688d0
VM
170 error = note_notfound_cb(
171 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
a02e7249 172 goto cleanup;
173 }
bf477ed4 174
4aa7de15 175 if (error < 0)
a02e7249 176 goto cleanup;
bf477ed4 177
a02e7249 178 /* An existing fanout has been found, let's dig deeper */
b93688d0 179 error = manipulate_note_in_tree_r(
dca6b228 180 &new, repo, subtree, note_oid, annotated_object_sha,
b93688d0 181 fanout + 2, note_exists_cb, note_notfound_cb);
bf477ed4 182
a02e7249 183 if (error < 0)
184 goto cleanup;
bf477ed4 185
a02e7249 186 strncpy(subtree_name, annotated_object_sha + fanout, 2);
187 subtree_name[2] = '\0';
bf477ed4 188
dca6b228 189 error = tree_write(out, repo, parent, git_tree_id(new),
a7dbac0b 190 subtree_name, GIT_FILEMODE_TREE);
dca6b228 191
4aa7de15 192
a02e7249 193cleanup:
dca6b228 194 git_tree_free(new);
a02e7249 195 git_tree_free(subtree);
196 return error;
197}
bf477ed4 198
b93688d0
VM
199static int remove_note_in_tree_eexists_cb(
200 git_tree **out,
201 git_repository *repo,
202 git_tree *parent,
203 git_oid *note_oid,
204 const char *annotated_object_sha,
205 int fanout,
206 int current_error)
207{
a02e7249 208 GIT_UNUSED(note_oid);
209 GIT_UNUSED(current_error);
bf477ed4 210
a02e7249 211 return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
212}
bf477ed4 213
b93688d0
VM
214static int remove_note_in_tree_enotfound_cb(
215 git_tree **out,
216 git_repository *repo,
217 git_tree *parent,
218 git_oid *note_oid,
219 const char *annotated_object_sha,
220 int fanout,
221 int current_error)
222{
a02e7249 223 GIT_UNUSED(out);
224 GIT_UNUSED(repo);
225 GIT_UNUSED(parent);
226 GIT_UNUSED(note_oid);
227 GIT_UNUSED(fanout);
bf477ed4 228
909d5494 229 giterr_set(GITERR_REPOSITORY, "object '%s' has no note", annotated_object_sha);
a02e7249 230 return current_error;
231}
bf477ed4 232
b93688d0
VM
233static int insert_note_in_tree_eexists_cb(git_tree **out,
234 git_repository *repo,
235 git_tree *parent,
236 git_oid *note_oid,
237 const char *annotated_object_sha,
238 int fanout,
239 int current_error)
240{
a02e7249 241 GIT_UNUSED(out);
242 GIT_UNUSED(repo);
243 GIT_UNUSED(parent);
244 GIT_UNUSED(note_oid);
245 GIT_UNUSED(fanout);
bf477ed4 246
909d5494 247 giterr_set(GITERR_REPOSITORY, "note for '%s' exists already", annotated_object_sha);
a02e7249 248 return current_error;
249}
bf477ed4 250
b93688d0
VM
251static int insert_note_in_tree_enotfound_cb(git_tree **out,
252 git_repository *repo,
253 git_tree *parent,
254 git_oid *note_oid,
255 const char *annotated_object_sha,
256 int fanout,
257 int current_error)
258{
a02e7249 259 GIT_UNUSED(current_error);
bf477ed4 260
a02e7249 261 /* No existing fanout at this level, insert in place */
a7dbac0b 262 return tree_write(
263 out,
264 repo,
265 parent,
266 note_oid,
267 annotated_object_sha + fanout,
268 GIT_FILEMODE_BLOB);
bf477ed4
MS
269}
270
eae0bfdc
PP
271static int note_write(
272 git_oid *notes_commit_out,
273 git_oid *notes_blob_out,
b93688d0 274 git_repository *repo,
de5596bf
BS
275 const git_signature *author,
276 const git_signature *committer,
b93688d0
VM
277 const char *notes_ref,
278 const char *note,
279 git_tree *commit_tree,
280 const char *target,
8716b499
NV
281 git_commit **parents,
282 int allow_note_overwrite)
bf477ed4 283{
a02e7249 284 int error;
bf477ed4 285 git_oid oid;
a02e7249 286 git_tree *tree = NULL;
4a44087a 287
a02e7249 288 // TODO: should we apply filters?
289 /* create note object */
290 if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
291 goto cleanup;
bf477ed4 292
b93688d0
VM
293 if ((error = manipulate_note_in_tree_r(
294 &tree, repo, commit_tree, &oid, target, 0,
8716b499
NV
295 allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb,
296 insert_note_in_tree_enotfound_cb)) < 0)
a02e7249 297 goto cleanup;
bf477ed4 298
eae0bfdc
PP
299 if (notes_blob_out)
300 git_oid_cpy(notes_blob_out, &oid);
301
4aa7de15 302
a02e7249 303 error = git_commit_create(&oid, repo, notes_ref, author, committer,
304 NULL, GIT_NOTES_DEFAULT_MSG_ADD,
305 tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
4aa7de15 306
eae0bfdc
PP
307 if (notes_commit_out)
308 git_oid_cpy(notes_commit_out, &oid);
309
a02e7249 310cleanup:
bf477ed4 311 git_tree_free(tree);
a02e7249 312 return error;
313}
bf477ed4 314
bad4937e
ET
315static int note_new(
316 git_note **out,
317 git_oid *note_oid,
318 git_commit *commit,
319 git_blob *blob)
a02e7249 320{
321 git_note *note = NULL;
bf477ed4 322
392702ee 323 note = git__malloc(sizeof(git_note));
4aa7de15 324 GITERR_CHECK_ALLOC(note);
bf477ed4 325
d0a3de72 326 git_oid_cpy(&note->id, note_oid);
bad4937e
ET
327
328 if (git_signature_dup(&note->author, git_commit_author(commit)) < 0 ||
329 git_signature_dup(&note->committer, git_commit_committer(commit)) < 0)
330 return -1;
331
b7fb71e3
VM
332 note->message = git__strndup(git_blob_rawcontent(blob), git_blob_rawsize(blob));
333 GITERR_CHECK_ALLOC(note->message);
bf477ed4
MS
334
335 *out = note;
a02e7249 336 return 0;
bf477ed4
MS
337}
338
734c6fc1 339static int note_lookup(
bad4937e
ET
340 git_note **out,
341 git_repository *repo,
342 git_commit *commit,
343 git_tree *tree,
344 const char *target)
bf477ed4
MS
345{
346 int error, fanout = 0;
347 git_oid oid;
a02e7249 348 git_blob *blob = NULL;
349 git_note *note = NULL;
350 git_tree *subtree = NULL;
bf477ed4 351
a02e7249 352 if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
353 goto cleanup;
bf477ed4 354
a02e7249 355 if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
356 goto cleanup;
bf477ed4 357
a02e7249 358 if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
359 goto cleanup;
bf477ed4 360
bad4937e 361 if ((error = note_new(&note, &oid, commit, blob)) < 0)
a02e7249 362 goto cleanup;
bf477ed4 363
a02e7249 364 *out = note;
bf477ed4 365
a02e7249 366cleanup:
367 git_tree_free(subtree);
368 git_blob_free(blob);
369 return error;
370}
bf477ed4 371
eae0bfdc
PP
372static int note_remove(
373 git_oid *notes_commit_out,
374 git_repository *repo,
de5596bf
BS
375 const git_signature *author, const git_signature *committer,
376 const char *notes_ref, git_tree *tree,
377 const char *target, git_commit **parents)
a02e7249 378{
379 int error;
380 git_tree *tree_after_removal = NULL;
381 git_oid oid;
4a44087a 382
b93688d0
VM
383 if ((error = manipulate_note_in_tree_r(
384 &tree_after_removal, repo, tree, NULL, target, 0,
385 remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
a02e7249 386 goto cleanup;
bf477ed4
MS
387
388 error = git_commit_create(&oid, repo, notes_ref, author, committer,
b93688d0
VM
389 NULL, GIT_NOTES_DEFAULT_MSG_RM,
390 tree_after_removal,
391 *parents == NULL ? 0 : 1,
392 (const git_commit **) parents);
bf477ed4 393
eae0bfdc
PP
394 if (error < 0)
395 goto cleanup;
396
397 if (notes_commit_out)
398 git_oid_cpy(notes_commit_out, &oid);
399
a02e7249 400cleanup:
401 git_tree_free(tree_after_removal);
bf477ed4
MS
402 return error;
403}
404
385449b1 405static int note_get_default_ref(char **out, git_repository *repo)
caea5e54 406{
caea5e54 407 git_config *cfg;
9f77b3f6 408 int ret = git_repository_config__weakptr(&cfg, repo);
caea5e54 409
9f77b3f6
RB
410 *out = (ret != 0) ? NULL : git_config__get_string_force(
411 cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
caea5e54 412
f95e8cc0 413 return ret;
caea5e54
MS
414}
415
385449b1 416static int normalize_namespace(char **out, git_repository *repo, const char *notes_ref)
86ecd844 417{
385449b1
CMN
418 if (notes_ref) {
419 *out = git__strdup(notes_ref);
420 GITERR_CHECK_ALLOC(*out);
86ecd844 421 return 0;
385449b1 422 }
86ecd844 423
385449b1 424 return note_get_default_ref(out, repo);
86ecd844 425}
426
eae0bfdc 427static int retrieve_note_commit(
b93688d0 428 git_commit **commit_out,
385449b1 429 char **notes_ref_out,
b93688d0 430 git_repository *repo,
385449b1 431 const char *notes_ref)
86ecd844 432{
a02e7249 433 int error;
86ecd844 434 git_oid oid;
435
385449b1 436 if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
a02e7249 437 return error;
86ecd844 438
385449b1 439 if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0)
a02e7249 440 return error;
86ecd844 441
a02e7249 442 if (git_commit_lookup(commit_out, repo, &oid) < 0)
443 return error;
86ecd844 444
a02e7249 445 return 0;
86ecd844 446}
447
eae0bfdc
PP
448int git_note_commit_read(
449 git_note **out,
450 git_repository *repo,
451 git_commit *notes_commit,
452 const git_oid *oid)
453{
454 int error;
455 git_tree *tree = NULL;
456 char target[GIT_OID_HEXSZ + 1];
457
458 git_oid_tostr(target, sizeof(target), oid);
459
460 if ((error = git_commit_tree(&tree, notes_commit)) < 0)
461 goto cleanup;
462
463 error = note_lookup(out, repo, notes_commit, tree, target);
464
465cleanup:
466 git_tree_free(tree);
467 return error;
468}
469
bf477ed4 470int git_note_read(git_note **out, git_repository *repo,
385449b1 471 const char *notes_ref_in, const git_oid *oid)
bf477ed4
MS
472{
473 int error;
eae0bfdc 474 char *notes_ref = NULL;
a02e7249 475 git_commit *commit = NULL;
bf477ed4 476
eae0bfdc 477 error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
bf477ed4 478
eae0bfdc
PP
479 if (error < 0)
480 goto cleanup;
bf477ed4 481
eae0bfdc
PP
482 error = git_note_commit_read(out, repo, commit, oid);
483
484cleanup:
385449b1 485 git__free(notes_ref);
a02e7249 486 git_commit_free(commit);
4aa7de15 487 return error;
bf477ed4
MS
488}
489
eae0bfdc
PP
490int git_note_commit_create(
491 git_oid *notes_commit_out,
492 git_oid *notes_blob_out,
493 git_repository *repo,
494 git_commit *parent,
495 const git_signature *author,
496 const git_signature *committer,
497 const git_oid *oid,
498 const char *note,
499 int allow_note_overwrite)
500{
501 int error;
502 git_tree *tree = NULL;
503 char target[GIT_OID_HEXSZ + 1];
504
505 git_oid_tostr(target, sizeof(target), oid);
506
507 if (parent != NULL && (error = git_commit_tree(&tree, parent)) < 0)
508 goto cleanup;
509
510 error = note_write(notes_commit_out, notes_blob_out, repo, author,
511 committer, NULL, note, tree, target, &parent, allow_note_overwrite);
512
513 if (error < 0)
514 goto cleanup;
515
516cleanup:
517 git_tree_free(tree);
518 return error;
519}
520
4aa7de15 521int git_note_create(
de5596bf
BS
522 git_oid *out,
523 git_repository *repo,
385449b1 524 const char *notes_ref_in,
de5596bf
BS
525 const git_signature *author,
526 const git_signature *committer,
de5596bf 527 const git_oid *oid,
8716b499
NV
528 const char *note,
529 int allow_note_overwrite)
bf477ed4 530{
a02e7249 531 int error;
eae0bfdc
PP
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;
bf477ed4 536
eae0bfdc
PP
537 error = retrieve_note_commit(&existing_notes_commit, &notes_ref,
538 repo, notes_ref_in);
a02e7249 539
540 if (error < 0 && error != GIT_ENOTFOUND)
541 goto cleanup;
542
eae0bfdc
PP
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;
550
551 error = git_reference_create(&ref, repo, notes_ref,
552 &notes_commit_oid, 1, NULL);
553
554 if (out != NULL)
555 git_oid_cpy(out, &notes_blob_oid);
bf477ed4 556
a02e7249 557cleanup:
385449b1 558 git__free(notes_ref);
eae0bfdc
PP
559 git_commit_free(existing_notes_commit);
560 git_reference_free(ref);
561 return error;
562}
563
564int git_note_commit_remove(
565 git_oid *notes_commit_out,
566 git_repository *repo,
567 git_commit *notes_commit,
568 const git_signature *author,
569 const git_signature *committer,
570 const git_oid *oid)
571{
572 int error;
573 git_tree *tree = NULL;
574 char target[GIT_OID_HEXSZ + 1];
575
576 git_oid_tostr(target, sizeof(target), oid);
577
578 if ((error = git_commit_tree(&tree, notes_commit)) < 0)
579 goto cleanup;
580
581 error = note_remove(notes_commit_out,
582 repo, author, committer, NULL, tree, target, &notes_commit);
583
584cleanup:
a02e7249 585 git_tree_free(tree);
4aa7de15 586 return error;
bf477ed4
MS
587}
588
385449b1 589int git_note_remove(git_repository *repo, const char *notes_ref_in,
de5596bf
BS
590 const git_signature *author, const git_signature *committer,
591 const git_oid *oid)
bf477ed4
MS
592{
593 int error;
eae0bfdc
PP
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;
bf477ed4 598
eae0bfdc
PP
599 error = retrieve_note_commit(&existing_notes_commit, &notes_ref_target,
600 repo, notes_ref_in);
bf477ed4 601
eae0bfdc
PP
602 if (error < 0)
603 goto cleanup;
bf477ed4 604
eae0bfdc
PP
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
613cleanup:
614 git__free(notes_ref_target);
615 git_reference_free(notes_ref);
616 git_commit_free(existing_notes_commit);
4aa7de15 617 return error;
bf477ed4
MS
618}
619
385449b1 620int git_note_default_ref(git_buf *out, git_repository *repo)
630c5a4a 621{
385449b1
CMN
622 char *default_ref;
623 int error;
624
625 assert(out && repo);
626
627 git_buf_sanitize(out);
628
629 if ((error = note_get_default_ref(&default_ref, repo)) < 0)
630 return error;
631
632 git_buf_attach(out, default_ref, strlen(default_ref));
633 return 0;
630c5a4a
MS
634}
635
bad4937e
ET
636const git_signature *git_note_committer(const git_note *note)
637{
638 assert(note);
639 return note->committer;
640}
641
642const git_signature *git_note_author(const git_note *note)
643{
644 assert(note);
645 return note->author;
646}
647
de5596bf 648const char * git_note_message(const git_note *note)
bf477ed4
MS
649{
650 assert(note);
651 return note->message;
652}
653
d0a3de72 654const git_oid * git_note_id(const git_note *note)
bf477ed4
MS
655{
656 assert(note);
d0a3de72 657 return &note->id;
bf477ed4
MS
658}
659
660void git_note_free(git_note *note)
661{
662 if (note == NULL)
663 return;
664
bad4937e
ET
665 git_signature_free(note->committer);
666 git_signature_free(note->author);
bf477ed4
MS
667 git__free(note->message);
668 git__free(note);
669}
86ecd844 670
671static int process_entry_path(
672 const char* entry_path,
f7b18502 673 git_oid *annotated_object_id)
86ecd844 674{
734c6fc1 675 int error = 0;
b8457baa 676 size_t i = 0, j = 0, len;
86ecd844 677 git_buf buf = GIT_BUF_INIT;
678
5dca2010 679 if ((error = git_buf_puts(&buf, entry_path)) < 0)
86ecd844 680 goto cleanup;
5dca2010 681
86ecd844 682 len = git_buf_len(&buf);
683
684 while (i < len) {
685 if (buf.ptr[i] == '/') {
686 i++;
687 continue;
688 }
5dca2010 689
86ecd844 690 if (git__fromhex(buf.ptr[i]) < 0) {
691 /* This is not a note entry */
86ecd844 692 goto cleanup;
693 }
694
695 if (i != j)
696 buf.ptr[j] = buf.ptr[i];
697
698 i++;
699 j++;
700 }
701
702 buf.ptr[j] = '\0';
703 buf.size = j;
704
705 if (j != GIT_OID_HEXSZ) {
706 /* This is not a note entry */
86ecd844 707 goto cleanup;
708 }
709
f7b18502 710 error = git_oid_fromstr(annotated_object_id, buf.ptr);
86ecd844 711
86ecd844 712cleanup:
713 git_buf_free(&buf);
714 return error;
715}
716
717int git_note_foreach(
0cb16fe9
L
718 git_repository *repo,
719 const char *notes_ref,
720 git_note_foreach_cb note_cb,
721 void *payload)
86ecd844 722{
0cb16fe9
L
723 int error;
724 git_note_iterator *iter = NULL;
725 git_oid note_id, annotated_id;
6edb427b 726
0cb16fe9
L
727 if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
728 return error;
86ecd844 729
f10d7a36
RB
730 while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
731 if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
26c1cb91 732 giterr_set_after_callback(error);
f10d7a36
RB
733 break;
734 }
735 }
86ecd844 736
0cb16fe9
L
737 if (error == GIT_ITEROVER)
738 error = 0;
5dca2010 739
0cb16fe9
L
740 git_note_iterator_free(iter);
741 return error;
86ecd844 742}
6edb427b 743
1a90dcf6
NG
744void git_note_iterator_free(git_note_iterator *it)
745{
746 if (it == NULL)
747 return;
748
749 git_iterator_free(it);
750}
751
eae0bfdc
PP
752int git_note_commit_iterator_new(
753 git_note_iterator **it,
754 git_commit *notes_commit)
755{
756 int error;
757 git_tree *tree;
758
759 if ((error = git_commit_tree(&tree, notes_commit)) < 0)
760 goto cleanup;
761
762 if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
763 git_iterator_free(*it);
764
765cleanup:
766 git_tree_free(tree);
767
768 return error;
769}
1a90dcf6 770
6edb427b 771int git_note_iterator_new(
1a90dcf6 772 git_note_iterator **it,
6edb427b 773 git_repository *repo,
385449b1 774 const char *notes_ref_in)
6edb427b
NG
775{
776 int error;
777 git_commit *commit = NULL;
385449b1 778 char *notes_ref;
6edb427b 779
eae0bfdc 780 error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
f7b18502
NG
781 if (error < 0)
782 goto cleanup;
6edb427b 783
eae0bfdc 784 error = git_note_commit_iterator_new(it, commit);
6edb427b 785
f7b18502 786cleanup:
385449b1 787 git__free(notes_ref);
6edb427b
NG
788 git_commit_free(commit);
789
790 return error;
791}
792
793int git_note_next(
794 git_oid* note_id,
795 git_oid* annotated_id,
f7b18502 796 git_note_iterator *it)
6edb427b
NG
797{
798 int error;
799 const git_index_entry *item;
800
169dc616 801 if ((error = git_iterator_current(&item, it)) < 0)
cee695ae 802 return error;
6edb427b 803
d541170c 804 git_oid_cpy(note_id, &item->id);
6edb427b 805
cee695ae
RB
806 if (!(error = process_entry_path(item->path, annotated_id)))
807 git_iterator_advance(NULL, it);
6edb427b
NG
808
809 return error;
810}