]> git.proxmox.com Git - libgit2.git/blob - src/notes.c
notes: Do not assume blob contents are NULL-terminated
[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_create(&tb, 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, repo, 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 git_buf note_contents = GIT_BUF_INIT;
317
318 note = (git_note *)git__malloc(sizeof(git_note));
319 GITERR_CHECK_ALLOC(note);
320
321 git_oid_cpy(&note->id, note_oid);
322
323 if (git_signature_dup(&note->author, git_commit_author(commit)) < 0 ||
324 git_signature_dup(&note->committer, git_commit_committer(commit)) < 0)
325 return -1;
326
327 git_buf_put(&note_contents, git_blob_rawcontent(blob), git_blob_rawsize(blob));
328 note->message = git_buf_detach(&note_contents);
329
330 *out = note;
331 return 0;
332 }
333
334 static int note_lookup(
335 git_note **out,
336 git_repository *repo,
337 git_commit *commit,
338 git_tree *tree,
339 const char *target)
340 {
341 int error, fanout = 0;
342 git_oid oid;
343 git_blob *blob = NULL;
344 git_note *note = NULL;
345 git_tree *subtree = NULL;
346
347 if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
348 goto cleanup;
349
350 if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
351 goto cleanup;
352
353 if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
354 goto cleanup;
355
356 if ((error = note_new(&note, &oid, commit, blob)) < 0)
357 goto cleanup;
358
359 *out = note;
360
361 cleanup:
362 git_tree_free(subtree);
363 git_blob_free(blob);
364 return error;
365 }
366
367 static int note_remove(git_repository *repo,
368 const git_signature *author, const git_signature *committer,
369 const char *notes_ref, git_tree *tree,
370 const char *target, git_commit **parents)
371 {
372 int error;
373 git_tree *tree_after_removal = NULL;
374 git_oid oid;
375
376 if ((error = manipulate_note_in_tree_r(
377 &tree_after_removal, repo, tree, NULL, target, 0,
378 remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
379 goto cleanup;
380
381 error = git_commit_create(&oid, repo, notes_ref, author, committer,
382 NULL, GIT_NOTES_DEFAULT_MSG_RM,
383 tree_after_removal,
384 *parents == NULL ? 0 : 1,
385 (const git_commit **) parents);
386
387 cleanup:
388 git_tree_free(tree_after_removal);
389 return error;
390 }
391
392 static int note_get_default_ref(const char **out, git_repository *repo)
393 {
394 git_config *cfg;
395 int ret = git_repository_config__weakptr(&cfg, repo);
396
397 *out = (ret != 0) ? NULL : git_config__get_string_force(
398 cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
399
400 return ret;
401 }
402
403 static int normalize_namespace(const char **notes_ref, git_repository *repo)
404 {
405 if (*notes_ref)
406 return 0;
407
408 return note_get_default_ref(notes_ref, repo);
409 }
410
411 static int retrieve_note_tree_and_commit(
412 git_tree **tree_out,
413 git_commit **commit_out,
414 git_repository *repo,
415 const char **notes_ref)
416 {
417 int error;
418 git_oid oid;
419
420 if ((error = normalize_namespace(notes_ref, repo)) < 0)
421 return error;
422
423 if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0)
424 return error;
425
426 if (git_commit_lookup(commit_out, repo, &oid) < 0)
427 return error;
428
429 if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
430 return error;
431
432 return 0;
433 }
434
435 int git_note_read(git_note **out, git_repository *repo,
436 const char *notes_ref, const git_oid *oid)
437 {
438 int error;
439 char *target = NULL;
440 git_tree *tree = NULL;
441 git_commit *commit = NULL;
442
443 target = git_oid_allocfmt(oid);
444 GITERR_CHECK_ALLOC(target);
445
446 if (!(error = retrieve_note_tree_and_commit(
447 &tree, &commit, repo, &notes_ref)))
448 error = note_lookup(out, repo, commit, tree, target);
449
450 git__free(target);
451 git_tree_free(tree);
452 git_commit_free(commit);
453 return error;
454 }
455
456 int git_note_create(
457 git_oid *out,
458 git_repository *repo,
459 const git_signature *author,
460 const git_signature *committer,
461 const char *notes_ref,
462 const git_oid *oid,
463 const char *note,
464 int allow_note_overwrite)
465 {
466 int error;
467 char *target = NULL;
468 git_commit *commit = NULL;
469 git_tree *tree = NULL;
470
471 target = git_oid_allocfmt(oid);
472 GITERR_CHECK_ALLOC(target);
473
474 error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
475
476 if (error < 0 && error != GIT_ENOTFOUND)
477 goto cleanup;
478
479 error = note_write(out, repo, author, committer, notes_ref,
480 note, tree, target, &commit, allow_note_overwrite);
481
482 cleanup:
483 git__free(target);
484 git_commit_free(commit);
485 git_tree_free(tree);
486 return error;
487 }
488
489 int git_note_remove(git_repository *repo, const char *notes_ref,
490 const git_signature *author, const git_signature *committer,
491 const git_oid *oid)
492 {
493 int error;
494 char *target = NULL;
495 git_commit *commit = NULL;
496 git_tree *tree = NULL;
497
498 target = git_oid_allocfmt(oid);
499 GITERR_CHECK_ALLOC(target);
500
501 if (!(error = retrieve_note_tree_and_commit(
502 &tree, &commit, repo, &notes_ref)))
503 error = note_remove(
504 repo, author, committer, notes_ref, tree, target, &commit);
505
506 git__free(target);
507 git_commit_free(commit);
508 git_tree_free(tree);
509 return error;
510 }
511
512 int git_note_default_ref(const char **out, git_repository *repo)
513 {
514 assert(repo);
515 return note_get_default_ref(out, repo);
516 }
517
518 const git_signature *git_note_committer(const git_note *note)
519 {
520 assert(note);
521 return note->committer;
522 }
523
524 const git_signature *git_note_author(const git_note *note)
525 {
526 assert(note);
527 return note->author;
528 }
529
530 const char * git_note_message(const git_note *note)
531 {
532 assert(note);
533 return note->message;
534 }
535
536 const git_oid * git_note_id(const git_note *note)
537 {
538 assert(note);
539 return &note->id;
540 }
541
542 void git_note_free(git_note *note)
543 {
544 if (note == NULL)
545 return;
546
547 git_signature_free(note->committer);
548 git_signature_free(note->author);
549 git__free(note->message);
550 git__free(note);
551 }
552
553 static int process_entry_path(
554 const char* entry_path,
555 git_oid *annotated_object_id)
556 {
557 int error = 0;
558 size_t i = 0, j = 0, len;
559 git_buf buf = GIT_BUF_INIT;
560
561 if ((error = git_buf_puts(&buf, entry_path)) < 0)
562 goto cleanup;
563
564 len = git_buf_len(&buf);
565
566 while (i < len) {
567 if (buf.ptr[i] == '/') {
568 i++;
569 continue;
570 }
571
572 if (git__fromhex(buf.ptr[i]) < 0) {
573 /* This is not a note entry */
574 goto cleanup;
575 }
576
577 if (i != j)
578 buf.ptr[j] = buf.ptr[i];
579
580 i++;
581 j++;
582 }
583
584 buf.ptr[j] = '\0';
585 buf.size = j;
586
587 if (j != GIT_OID_HEXSZ) {
588 /* This is not a note entry */
589 goto cleanup;
590 }
591
592 error = git_oid_fromstr(annotated_object_id, buf.ptr);
593
594 cleanup:
595 git_buf_free(&buf);
596 return error;
597 }
598
599 int git_note_foreach(
600 git_repository *repo,
601 const char *notes_ref,
602 git_note_foreach_cb note_cb,
603 void *payload)
604 {
605 int error;
606 git_note_iterator *iter = NULL;
607 git_oid note_id, annotated_id;
608
609 if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
610 return error;
611
612 while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
613 if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
614 giterr_set_after_callback(error);
615 break;
616 }
617 }
618
619 if (error == GIT_ITEROVER)
620 error = 0;
621
622 git_note_iterator_free(iter);
623 return error;
624 }
625
626
627 void git_note_iterator_free(git_note_iterator *it)
628 {
629 if (it == NULL)
630 return;
631
632 git_iterator_free(it);
633 }
634
635
636 int git_note_iterator_new(
637 git_note_iterator **it,
638 git_repository *repo,
639 const char *notes_ref)
640 {
641 int error;
642 git_commit *commit = NULL;
643 git_tree *tree = NULL;
644
645 error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
646 if (error < 0)
647 goto cleanup;
648
649 if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0)
650 git_iterator_free(*it);
651
652 cleanup:
653 git_tree_free(tree);
654 git_commit_free(commit);
655
656 return error;
657 }
658
659 int git_note_next(
660 git_oid* note_id,
661 git_oid* annotated_id,
662 git_note_iterator *it)
663 {
664 int error;
665 const git_index_entry *item;
666
667 if ((error = git_iterator_current(&item, it)) < 0)
668 return error;
669
670 git_oid_cpy(note_id, &item->id);
671
672 if (!(error = process_entry_path(item->path, annotated_id)))
673 git_iterator_advance(NULL, it);
674
675 return error;
676 }