]> git.proxmox.com Git - libgit2.git/blob - src/notes.c
Merge pull request #3016 from pks-t/ignore-exclude-fix
[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__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(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(char **out, git_repository *repo, const char *notes_ref)
403 {
404 if (notes_ref) {
405 *out = git__strdup(notes_ref);
406 GITERR_CHECK_ALLOC(*out);
407 return 0;
408 }
409
410 return note_get_default_ref(out, repo);
411 }
412
413 static int retrieve_note_tree_and_commit(
414 git_tree **tree_out,
415 git_commit **commit_out,
416 char **notes_ref_out,
417 git_repository *repo,
418 const char *notes_ref)
419 {
420 int error;
421 git_oid oid;
422
423 if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
424 return error;
425
426 if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0)
427 return error;
428
429 if (git_commit_lookup(commit_out, repo, &oid) < 0)
430 return error;
431
432 if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
433 return error;
434
435 return 0;
436 }
437
438 int git_note_read(git_note **out, git_repository *repo,
439 const char *notes_ref_in, const git_oid *oid)
440 {
441 int error;
442 char *target = NULL, *notes_ref = NULL;
443 git_tree *tree = NULL;
444 git_commit *commit = NULL;
445
446 target = git_oid_allocfmt(oid);
447 GITERR_CHECK_ALLOC(target);
448
449 if (!(error = retrieve_note_tree_and_commit(
450 &tree, &commit, &notes_ref, repo, notes_ref_in)))
451 error = note_lookup(out, repo, commit, tree, target);
452
453 git__free(notes_ref);
454 git__free(target);
455 git_tree_free(tree);
456 git_commit_free(commit);
457 return error;
458 }
459
460 int git_note_create(
461 git_oid *out,
462 git_repository *repo,
463 const char *notes_ref_in,
464 const git_signature *author,
465 const git_signature *committer,
466 const git_oid *oid,
467 const char *note,
468 int allow_note_overwrite)
469 {
470 int error;
471 char *target = NULL, *notes_ref = NULL;
472 git_commit *commit = NULL;
473 git_tree *tree = NULL;
474
475 target = git_oid_allocfmt(oid);
476 GITERR_CHECK_ALLOC(target);
477
478 error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
479
480 if (error < 0 && error != GIT_ENOTFOUND)
481 goto cleanup;
482
483 error = note_write(out, repo, author, committer, notes_ref,
484 note, tree, target, &commit, allow_note_overwrite);
485
486 cleanup:
487 git__free(notes_ref);
488 git__free(target);
489 git_commit_free(commit);
490 git_tree_free(tree);
491 return error;
492 }
493
494 int git_note_remove(git_repository *repo, const char *notes_ref_in,
495 const git_signature *author, const git_signature *committer,
496 const git_oid *oid)
497 {
498 int error;
499 char *target = NULL, *notes_ref;
500 git_commit *commit = NULL;
501 git_tree *tree = NULL;
502
503 target = git_oid_allocfmt(oid);
504 GITERR_CHECK_ALLOC(target);
505
506 if (!(error = retrieve_note_tree_and_commit(
507 &tree, &commit, &notes_ref, repo, notes_ref_in)))
508 error = note_remove(
509 repo, author, committer, notes_ref, tree, target, &commit);
510
511 git__free(notes_ref);
512 git__free(target);
513 git_commit_free(commit);
514 git_tree_free(tree);
515 return error;
516 }
517
518 int git_note_default_ref(git_buf *out, git_repository *repo)
519 {
520 char *default_ref;
521 int error;
522
523 assert(out && repo);
524
525 git_buf_sanitize(out);
526
527 if ((error = note_get_default_ref(&default_ref, repo)) < 0)
528 return error;
529
530 git_buf_attach(out, default_ref, strlen(default_ref));
531 return 0;
532 }
533
534 const git_signature *git_note_committer(const git_note *note)
535 {
536 assert(note);
537 return note->committer;
538 }
539
540 const git_signature *git_note_author(const git_note *note)
541 {
542 assert(note);
543 return note->author;
544 }
545
546 const char * git_note_message(const git_note *note)
547 {
548 assert(note);
549 return note->message;
550 }
551
552 const git_oid * git_note_id(const git_note *note)
553 {
554 assert(note);
555 return &note->id;
556 }
557
558 void git_note_free(git_note *note)
559 {
560 if (note == NULL)
561 return;
562
563 git_signature_free(note->committer);
564 git_signature_free(note->author);
565 git__free(note->message);
566 git__free(note);
567 }
568
569 static int process_entry_path(
570 const char* entry_path,
571 git_oid *annotated_object_id)
572 {
573 int error = 0;
574 size_t i = 0, j = 0, len;
575 git_buf buf = GIT_BUF_INIT;
576
577 if ((error = git_buf_puts(&buf, entry_path)) < 0)
578 goto cleanup;
579
580 len = git_buf_len(&buf);
581
582 while (i < len) {
583 if (buf.ptr[i] == '/') {
584 i++;
585 continue;
586 }
587
588 if (git__fromhex(buf.ptr[i]) < 0) {
589 /* This is not a note entry */
590 goto cleanup;
591 }
592
593 if (i != j)
594 buf.ptr[j] = buf.ptr[i];
595
596 i++;
597 j++;
598 }
599
600 buf.ptr[j] = '\0';
601 buf.size = j;
602
603 if (j != GIT_OID_HEXSZ) {
604 /* This is not a note entry */
605 goto cleanup;
606 }
607
608 error = git_oid_fromstr(annotated_object_id, buf.ptr);
609
610 cleanup:
611 git_buf_free(&buf);
612 return error;
613 }
614
615 int git_note_foreach(
616 git_repository *repo,
617 const char *notes_ref,
618 git_note_foreach_cb note_cb,
619 void *payload)
620 {
621 int error;
622 git_note_iterator *iter = NULL;
623 git_oid note_id, annotated_id;
624
625 if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
626 return error;
627
628 while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
629 if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
630 giterr_set_after_callback(error);
631 break;
632 }
633 }
634
635 if (error == GIT_ITEROVER)
636 error = 0;
637
638 git_note_iterator_free(iter);
639 return error;
640 }
641
642
643 void git_note_iterator_free(git_note_iterator *it)
644 {
645 if (it == NULL)
646 return;
647
648 git_iterator_free(it);
649 }
650
651
652 int git_note_iterator_new(
653 git_note_iterator **it,
654 git_repository *repo,
655 const char *notes_ref_in)
656 {
657 int error;
658 git_commit *commit = NULL;
659 git_tree *tree = NULL;
660 char *notes_ref;
661
662 error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
663 if (error < 0)
664 goto cleanup;
665
666 if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0)
667 git_iterator_free(*it);
668
669 cleanup:
670 git__free(notes_ref);
671 git_tree_free(tree);
672 git_commit_free(commit);
673
674 return error;
675 }
676
677 int git_note_next(
678 git_oid* note_id,
679 git_oid* annotated_id,
680 git_note_iterator *it)
681 {
682 int error;
683 const git_index_entry *item;
684
685 if ((error = git_iterator_current(&item, it)) < 0)
686 return error;
687
688 git_oid_cpy(note_id, &item->id);
689
690 if (!(error = process_entry_path(item->path, annotated_id)))
691 git_iterator_advance(NULL, it);
692
693 return error;
694 }