]> git.proxmox.com Git - libgit2.git/blob - src/notes.c
treebuilder: rename _create() to _new()
[libgit2.git] / src / notes.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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"
12 #include "config.h"
13 #include "iterator.h"
14 #include "signature.h"
15
16 static int note_error_notfound(void)
17 {
18 giterr_set(GITERR_INVALID, "Note could not be found");
19 return GIT_ENOTFOUND;
20 }
21
22 static 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)
28 {
29 size_t i;
30 const git_tree_entry *entry;
31
32 *out = NULL;
33
34 if (parent == NULL)
35 return note_error_notfound();
36
37 for (i = 0; i < git_tree_entrycount(parent); i++) {
38 entry = git_tree_entry_byindex(parent, i);
39
40 if (!git__ishex(git_tree_entry_name(entry)))
41 continue;
42
43 if (S_ISDIR(git_tree_entry_filemode(entry))
44 && strlen(git_tree_entry_name(entry)) == 2
45 && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
46 return git_tree_lookup(out, repo, git_tree_entry_id(entry));
47
48 /* Not a DIR, so do we have an already existing blob? */
49 if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))
50 return GIT_EEXISTS;
51 }
52
53 return note_error_notfound();
54 }
55
56 static 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;
61
62 *out = NULL;
63
64 error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
65 if (error == GIT_EEXISTS)
66 return git_tree_lookup(out, repo, git_tree_id(root));
67
68 if (error < 0)
69 return error;
70
71 *fanout += 2;
72 error = find_subtree_r(out, subtree, repo, target, fanout);
73 git_tree_free(subtree);
74
75 return error;
76 }
77
78 static int find_blob(git_oid *blob, git_tree *tree, const char *target)
79 {
80 size_t i;
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));
90 return 0;
91 }
92 }
93
94 return note_error_notfound();
95 }
96
97 static 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)
104 {
105 int error;
106 git_treebuilder *tb = NULL;
107 const git_tree_entry *entry;
108 git_oid tree_oid;
109
110 if ((error = git_treebuilder_new(&tb, repo, source_tree)) < 0)
111 goto cleanup;
112
113 if (object_oid) {
114 if ((error = git_treebuilder_insert(
115 &entry, tb, treeentry_name, object_oid, attributes)) < 0)
116 goto cleanup;
117 } else {
118 if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
119 goto cleanup;
120 }
121
122 if ((error = git_treebuilder_write(&tree_oid, tb)) < 0)
123 goto cleanup;
124
125 error = git_tree_lookup(out, repo, &tree_oid);
126
127 cleanup:
128 git_treebuilder_free(tb);
129 return error;
130 }
131
132 static 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,
138 int fanout,
139 int (*note_exists_cb)(
140 git_tree **out,
141 git_repository *repo,
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))
155 {
156 int error;
157 git_tree *subtree = NULL, *new = NULL;
158 char subtree_name[3];
159
160 error = find_subtree_in_current_level(
161 &subtree, repo, parent, annotated_object_sha, fanout);
162
163 if (error == GIT_EEXISTS) {
164 error = note_exists_cb(
165 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
166 goto cleanup;
167 }
168
169 if (error == GIT_ENOTFOUND) {
170 error = note_notfound_cb(
171 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
172 goto cleanup;
173 }
174
175 if (error < 0)
176 goto cleanup;
177
178 /* An existing fanout has been found, let's dig deeper */
179 error = manipulate_note_in_tree_r(
180 &new, repo, subtree, note_oid, annotated_object_sha,
181 fanout + 2, note_exists_cb, note_notfound_cb);
182
183 if (error < 0)
184 goto cleanup;
185
186 strncpy(subtree_name, annotated_object_sha + fanout, 2);
187 subtree_name[2] = '\0';
188
189 error = tree_write(out, repo, parent, git_tree_id(new),
190 subtree_name, GIT_FILEMODE_TREE);
191
192
193 cleanup:
194 git_tree_free(new);
195 git_tree_free(subtree);
196 return error;
197 }
198
199 static 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 {
208 GIT_UNUSED(note_oid);
209 GIT_UNUSED(current_error);
210
211 return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
212 }
213
214 static 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 {
223 GIT_UNUSED(out);
224 GIT_UNUSED(repo);
225 GIT_UNUSED(parent);
226 GIT_UNUSED(note_oid);
227 GIT_UNUSED(fanout);
228
229 giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
230 return current_error;
231 }
232
233 static 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 {
241 GIT_UNUSED(out);
242 GIT_UNUSED(repo);
243 GIT_UNUSED(parent);
244 GIT_UNUSED(note_oid);
245 GIT_UNUSED(fanout);
246
247 giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
248 return current_error;
249 }
250
251 static 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 {
259 GIT_UNUSED(current_error);
260
261 /* No existing fanout at this level, insert in place */
262 return tree_write(
263 out,
264 repo,
265 parent,
266 note_oid,
267 annotated_object_sha + fanout,
268 GIT_FILEMODE_BLOB);
269 }
270
271 static int note_write(git_oid *out,
272 git_repository *repo,
273 const git_signature *author,
274 const git_signature *committer,
275 const char *notes_ref,
276 const char *note,
277 git_tree *commit_tree,
278 const char *target,
279 git_commit **parents,
280 int allow_note_overwrite)
281 {
282 int error;
283 git_oid oid;
284 git_tree *tree = NULL;
285
286 // TODO: should we apply filters?
287 /* create note object */
288 if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
289 goto cleanup;
290
291 if ((error = manipulate_note_in_tree_r(
292 &tree, repo, commit_tree, &oid, target, 0,
293 allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb,
294 insert_note_in_tree_enotfound_cb)) < 0)
295 goto cleanup;
296
297 if (out)
298 git_oid_cpy(out, &oid);
299
300 error = git_commit_create(&oid, repo, notes_ref, author, committer,
301 NULL, GIT_NOTES_DEFAULT_MSG_ADD,
302 tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
303
304 cleanup:
305 git_tree_free(tree);
306 return error;
307 }
308
309 static int note_new(
310 git_note **out,
311 git_oid *note_oid,
312 git_commit *commit,
313 git_blob *blob)
314 {
315 git_note *note = NULL;
316
317 note = (git_note *)git__malloc(sizeof(git_note));
318 GITERR_CHECK_ALLOC(note);
319
320 git_oid_cpy(&note->id, note_oid);
321
322 if (git_signature_dup(&note->author, git_commit_author(commit)) < 0 ||
323 git_signature_dup(&note->committer, git_commit_committer(commit)) < 0)
324 return -1;
325
326 note->message = git__strndup(git_blob_rawcontent(blob), git_blob_rawsize(blob));
327 GITERR_CHECK_ALLOC(note->message);
328
329 *out = note;
330 return 0;
331 }
332
333 static int note_lookup(
334 git_note **out,
335 git_repository *repo,
336 git_commit *commit,
337 git_tree *tree,
338 const char *target)
339 {
340 int error, fanout = 0;
341 git_oid oid;
342 git_blob *blob = NULL;
343 git_note *note = NULL;
344 git_tree *subtree = NULL;
345
346 if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
347 goto cleanup;
348
349 if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
350 goto cleanup;
351
352 if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
353 goto cleanup;
354
355 if ((error = note_new(&note, &oid, commit, blob)) < 0)
356 goto cleanup;
357
358 *out = note;
359
360 cleanup:
361 git_tree_free(subtree);
362 git_blob_free(blob);
363 return error;
364 }
365
366 static int note_remove(git_repository *repo,
367 const git_signature *author, const git_signature *committer,
368 const char *notes_ref, git_tree *tree,
369 const char *target, git_commit **parents)
370 {
371 int error;
372 git_tree *tree_after_removal = NULL;
373 git_oid oid;
374
375 if ((error = manipulate_note_in_tree_r(
376 &tree_after_removal, repo, tree, NULL, target, 0,
377 remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
378 goto cleanup;
379
380 error = git_commit_create(&oid, repo, notes_ref, author, committer,
381 NULL, GIT_NOTES_DEFAULT_MSG_RM,
382 tree_after_removal,
383 *parents == NULL ? 0 : 1,
384 (const git_commit **) parents);
385
386 cleanup:
387 git_tree_free(tree_after_removal);
388 return error;
389 }
390
391 static int note_get_default_ref(const char **out, git_repository *repo)
392 {
393 git_config *cfg;
394 int ret = git_repository_config__weakptr(&cfg, repo);
395
396 *out = (ret != 0) ? NULL : git_config__get_string_force(
397 cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
398
399 return ret;
400 }
401
402 static int normalize_namespace(const char **notes_ref, git_repository *repo)
403 {
404 if (*notes_ref)
405 return 0;
406
407 return note_get_default_ref(notes_ref, repo);
408 }
409
410 static int retrieve_note_tree_and_commit(
411 git_tree **tree_out,
412 git_commit **commit_out,
413 git_repository *repo,
414 const char **notes_ref)
415 {
416 int error;
417 git_oid oid;
418
419 if ((error = normalize_namespace(notes_ref, repo)) < 0)
420 return error;
421
422 if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0)
423 return error;
424
425 if (git_commit_lookup(commit_out, repo, &oid) < 0)
426 return error;
427
428 if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
429 return error;
430
431 return 0;
432 }
433
434 int git_note_read(git_note **out, git_repository *repo,
435 const char *notes_ref, const git_oid *oid)
436 {
437 int error;
438 char *target = NULL;
439 git_tree *tree = NULL;
440 git_commit *commit = NULL;
441
442 target = git_oid_allocfmt(oid);
443 GITERR_CHECK_ALLOC(target);
444
445 if (!(error = retrieve_note_tree_and_commit(
446 &tree, &commit, repo, &notes_ref)))
447 error = note_lookup(out, repo, commit, tree, target);
448
449 git__free(target);
450 git_tree_free(tree);
451 git_commit_free(commit);
452 return error;
453 }
454
455 int git_note_create(
456 git_oid *out,
457 git_repository *repo,
458 const char *notes_ref,
459 const git_signature *author,
460 const git_signature *committer,
461 const git_oid *oid,
462 const char *note,
463 int allow_note_overwrite)
464 {
465 int error;
466 char *target = NULL;
467 git_commit *commit = NULL;
468 git_tree *tree = NULL;
469
470 target = git_oid_allocfmt(oid);
471 GITERR_CHECK_ALLOC(target);
472
473 error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
474
475 if (error < 0 && error != GIT_ENOTFOUND)
476 goto cleanup;
477
478 error = note_write(out, repo, author, committer, notes_ref,
479 note, tree, target, &commit, allow_note_overwrite);
480
481 cleanup:
482 git__free(target);
483 git_commit_free(commit);
484 git_tree_free(tree);
485 return error;
486 }
487
488 int git_note_remove(git_repository *repo, const char *notes_ref,
489 const git_signature *author, const git_signature *committer,
490 const git_oid *oid)
491 {
492 int error;
493 char *target = NULL;
494 git_commit *commit = NULL;
495 git_tree *tree = NULL;
496
497 target = git_oid_allocfmt(oid);
498 GITERR_CHECK_ALLOC(target);
499
500 if (!(error = retrieve_note_tree_and_commit(
501 &tree, &commit, repo, &notes_ref)))
502 error = note_remove(
503 repo, author, committer, notes_ref, tree, target, &commit);
504
505 git__free(target);
506 git_commit_free(commit);
507 git_tree_free(tree);
508 return error;
509 }
510
511 int git_note_default_ref(const char **out, git_repository *repo)
512 {
513 assert(repo);
514 return note_get_default_ref(out, repo);
515 }
516
517 const git_signature *git_note_committer(const git_note *note)
518 {
519 assert(note);
520 return note->committer;
521 }
522
523 const git_signature *git_note_author(const git_note *note)
524 {
525 assert(note);
526 return note->author;
527 }
528
529 const char * git_note_message(const git_note *note)
530 {
531 assert(note);
532 return note->message;
533 }
534
535 const git_oid * git_note_id(const git_note *note)
536 {
537 assert(note);
538 return &note->id;
539 }
540
541 void git_note_free(git_note *note)
542 {
543 if (note == NULL)
544 return;
545
546 git_signature_free(note->committer);
547 git_signature_free(note->author);
548 git__free(note->message);
549 git__free(note);
550 }
551
552 static int process_entry_path(
553 const char* entry_path,
554 git_oid *annotated_object_id)
555 {
556 int error = 0;
557 size_t i = 0, j = 0, len;
558 git_buf buf = GIT_BUF_INIT;
559
560 if ((error = git_buf_puts(&buf, entry_path)) < 0)
561 goto cleanup;
562
563 len = git_buf_len(&buf);
564
565 while (i < len) {
566 if (buf.ptr[i] == '/') {
567 i++;
568 continue;
569 }
570
571 if (git__fromhex(buf.ptr[i]) < 0) {
572 /* This is not a note entry */
573 goto cleanup;
574 }
575
576 if (i != j)
577 buf.ptr[j] = buf.ptr[i];
578
579 i++;
580 j++;
581 }
582
583 buf.ptr[j] = '\0';
584 buf.size = j;
585
586 if (j != GIT_OID_HEXSZ) {
587 /* This is not a note entry */
588 goto cleanup;
589 }
590
591 error = git_oid_fromstr(annotated_object_id, buf.ptr);
592
593 cleanup:
594 git_buf_free(&buf);
595 return error;
596 }
597
598 int git_note_foreach(
599 git_repository *repo,
600 const char *notes_ref,
601 git_note_foreach_cb note_cb,
602 void *payload)
603 {
604 int error;
605 git_note_iterator *iter = NULL;
606 git_oid note_id, annotated_id;
607
608 if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
609 return error;
610
611 while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
612 if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
613 giterr_set_after_callback(error);
614 break;
615 }
616 }
617
618 if (error == GIT_ITEROVER)
619 error = 0;
620
621 git_note_iterator_free(iter);
622 return error;
623 }
624
625
626 void git_note_iterator_free(git_note_iterator *it)
627 {
628 if (it == NULL)
629 return;
630
631 git_iterator_free(it);
632 }
633
634
635 int git_note_iterator_new(
636 git_note_iterator **it,
637 git_repository *repo,
638 const char *notes_ref)
639 {
640 int error;
641 git_commit *commit = NULL;
642 git_tree *tree = NULL;
643
644 error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
645 if (error < 0)
646 goto cleanup;
647
648 if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0)
649 git_iterator_free(*it);
650
651 cleanup:
652 git_tree_free(tree);
653 git_commit_free(commit);
654
655 return error;
656 }
657
658 int git_note_next(
659 git_oid* note_id,
660 git_oid* annotated_id,
661 git_note_iterator *it)
662 {
663 int error;
664 const git_index_entry *item;
665
666 if ((error = git_iterator_current(&item, it)) < 0)
667 return error;
668
669 git_oid_cpy(note_id, &item->id);
670
671 if (!(error = process_entry_path(item->path, annotated_id)))
672 git_iterator_advance(NULL, it);
673
674 return error;
675 }