]> git.proxmox.com Git - libgit2.git/blob - src/notes.c
Fixed size_t format string warning
[libgit2.git] / src / notes.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 find_subtree_in_current_level(
17 git_tree **out,
18 git_repository *repo,
19 git_tree *parent,
20 const char *annotated_object_sha,
21 int fanout)
22 {
23 size_t i;
24 const git_tree_entry *entry;
25
26 *out = NULL;
27
28 if (parent == NULL)
29 return GIT_ENOTFOUND;
30
31 for (i = 0; i < git_tree_entrycount(parent); i++) {
32 entry = git_tree_entry_byindex(parent, i);
33
34 if (!git__ishex(git_tree_entry_name(entry)))
35 continue;
36
37 if (S_ISDIR(git_tree_entry_filemode(entry))
38 && strlen(git_tree_entry_name(entry)) == 2
39 && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
40 return git_tree_lookup(out, repo, git_tree_entry_id(entry));
41
42 /* Not a DIR, so do we have an already existing blob? */
43 if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))
44 return GIT_EEXISTS;
45 }
46
47 return GIT_ENOTFOUND;
48 }
49
50 static int find_subtree_r(git_tree **out, git_tree *root,
51 git_repository *repo, const char *target, int *fanout)
52 {
53 int error;
54 git_tree *subtree = NULL;
55
56 *out = NULL;
57
58 error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
59 if (error == GIT_EEXISTS) {
60 return git_tree_lookup(out, repo, git_tree_id(root));
61 }
62
63 if (error < 0)
64 return error;
65
66 *fanout += 2;
67 error = find_subtree_r(out, subtree, repo, target, fanout);
68 git_tree_free(subtree);
69
70 return error;
71 }
72
73 static int find_blob(git_oid *blob, git_tree *tree, const char *target)
74 {
75 size_t i;
76 const git_tree_entry *entry;
77
78 for (i=0; i<git_tree_entrycount(tree); i++) {
79 entry = git_tree_entry_byindex(tree, i);
80
81 if (!strcmp(git_tree_entry_name(entry), target)) {
82 /* found matching note object - return */
83
84 git_oid_cpy(blob, git_tree_entry_id(entry));
85 return 0;
86 }
87 }
88 return GIT_ENOTFOUND;
89 }
90
91 static int tree_write(
92 git_tree **out,
93 git_repository *repo,
94 git_tree *source_tree,
95 const git_oid *object_oid,
96 const char *treeentry_name,
97 unsigned int attributes)
98 {
99 int error;
100 git_treebuilder *tb = NULL;
101 const git_tree_entry *entry;
102 git_oid tree_oid;
103
104 if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
105 goto cleanup;
106
107 if (object_oid) {
108 if ((error = git_treebuilder_insert(
109 &entry, tb, treeentry_name, object_oid, attributes)) < 0)
110 goto cleanup;
111 } else {
112 if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
113 goto cleanup;
114 }
115
116 if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0)
117 goto cleanup;
118
119 error = git_tree_lookup(out, repo, &tree_oid);
120
121 cleanup:
122 git_treebuilder_free(tb);
123 return error;
124 }
125
126 static int manipulate_note_in_tree_r(
127 git_tree **out,
128 git_repository *repo,
129 git_tree *parent,
130 git_oid *note_oid,
131 const char *annotated_object_sha,
132 int fanout,
133 int (*note_exists_cb)(
134 git_tree **out,
135 git_repository *repo,
136 git_tree *parent,
137 git_oid *note_oid,
138 const char *annotated_object_sha,
139 int fanout,
140 int current_error),
141 int (*note_notfound_cb)(
142 git_tree **out,
143 git_repository *repo,
144 git_tree *parent,
145 git_oid *note_oid,
146 const char *annotated_object_sha,
147 int fanout,
148 int current_error))
149 {
150 int error = -1;
151 git_tree *subtree = NULL, *new = NULL;
152 char subtree_name[3];
153
154 error = find_subtree_in_current_level(
155 &subtree, repo, parent, annotated_object_sha, fanout);
156
157 if (error == GIT_EEXISTS) {
158 error = note_exists_cb(
159 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
160 goto cleanup;
161 }
162
163 if (error == GIT_ENOTFOUND) {
164 error = note_notfound_cb(
165 out, repo, parent, note_oid, annotated_object_sha, fanout, error);
166 goto cleanup;
167 }
168
169 if (error < 0)
170 goto cleanup;
171
172 /* An existing fanout has been found, let's dig deeper */
173 error = manipulate_note_in_tree_r(
174 &new, repo, subtree, note_oid, annotated_object_sha,
175 fanout + 2, note_exists_cb, note_notfound_cb);
176
177 if (error < 0)
178 goto cleanup;
179
180 strncpy(subtree_name, annotated_object_sha + fanout, 2);
181 subtree_name[2] = '\0';
182
183 error = tree_write(out, repo, parent, git_tree_id(new),
184 subtree_name, GIT_FILEMODE_TREE);
185
186
187 cleanup:
188 git_tree_free(new);
189 git_tree_free(subtree);
190 return error;
191 }
192
193 static int remove_note_in_tree_eexists_cb(
194 git_tree **out,
195 git_repository *repo,
196 git_tree *parent,
197 git_oid *note_oid,
198 const char *annotated_object_sha,
199 int fanout,
200 int current_error)
201 {
202 GIT_UNUSED(note_oid);
203 GIT_UNUSED(current_error);
204
205 return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
206 }
207
208 static int remove_note_in_tree_enotfound_cb(
209 git_tree **out,
210 git_repository *repo,
211 git_tree *parent,
212 git_oid *note_oid,
213 const char *annotated_object_sha,
214 int fanout,
215 int current_error)
216 {
217 GIT_UNUSED(out);
218 GIT_UNUSED(repo);
219 GIT_UNUSED(parent);
220 GIT_UNUSED(note_oid);
221 GIT_UNUSED(fanout);
222
223 giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
224 return current_error;
225 }
226
227 static int insert_note_in_tree_eexists_cb(git_tree **out,
228 git_repository *repo,
229 git_tree *parent,
230 git_oid *note_oid,
231 const char *annotated_object_sha,
232 int fanout,
233 int current_error)
234 {
235 GIT_UNUSED(out);
236 GIT_UNUSED(repo);
237 GIT_UNUSED(parent);
238 GIT_UNUSED(note_oid);
239 GIT_UNUSED(fanout);
240
241 giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
242 return current_error;
243 }
244
245 static int insert_note_in_tree_enotfound_cb(git_tree **out,
246 git_repository *repo,
247 git_tree *parent,
248 git_oid *note_oid,
249 const char *annotated_object_sha,
250 int fanout,
251 int current_error)
252 {
253 GIT_UNUSED(current_error);
254
255 /* No existing fanout at this level, insert in place */
256 return tree_write(
257 out,
258 repo,
259 parent,
260 note_oid,
261 annotated_object_sha + fanout,
262 GIT_FILEMODE_BLOB);
263 }
264
265 static int note_write(git_oid *out,
266 git_repository *repo,
267 const git_signature *author,
268 const git_signature *committer,
269 const char *notes_ref,
270 const char *note,
271 git_tree *commit_tree,
272 const char *target,
273 git_commit **parents,
274 int allow_note_overwrite)
275 {
276 int error;
277 git_oid oid;
278 git_tree *tree = NULL;
279
280 // TODO: should we apply filters?
281 /* create note object */
282 if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
283 goto cleanup;
284
285 if ((error = manipulate_note_in_tree_r(
286 &tree, repo, commit_tree, &oid, target, 0,
287 allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb,
288 insert_note_in_tree_enotfound_cb)) < 0)
289 goto cleanup;
290
291 if (out)
292 git_oid_cpy(out, &oid);
293
294 error = git_commit_create(&oid, repo, notes_ref, author, committer,
295 NULL, GIT_NOTES_DEFAULT_MSG_ADD,
296 tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
297
298 cleanup:
299 git_tree_free(tree);
300 return error;
301 }
302
303 static int note_new(git_note **out, git_oid *note_oid, git_blob *blob)
304 {
305 git_note *note = NULL;
306
307 note = (git_note *)git__malloc(sizeof(git_note));
308 GITERR_CHECK_ALLOC(note);
309
310 git_oid_cpy(&note->oid, note_oid);
311 note->message = git__strdup((char *)git_blob_rawcontent(blob));
312 GITERR_CHECK_ALLOC(note->message);
313
314 *out = note;
315
316 return 0;
317 }
318
319 static int note_lookup(git_note **out, git_repository *repo,
320 git_tree *tree, const char *target)
321 {
322 int error, fanout = 0;
323 git_oid oid;
324 git_blob *blob = NULL;
325 git_note *note = NULL;
326 git_tree *subtree = NULL;
327
328 if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
329 goto cleanup;
330
331 if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
332 goto cleanup;
333
334 if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
335 goto cleanup;
336
337 if ((error = note_new(&note, &oid, blob)) < 0)
338 goto cleanup;
339
340 *out = note;
341
342 cleanup:
343 git_tree_free(subtree);
344 git_blob_free(blob);
345 return error;
346 }
347
348 static int note_remove(git_repository *repo,
349 const git_signature *author, const git_signature *committer,
350 const char *notes_ref, git_tree *tree,
351 const char *target, git_commit **parents)
352 {
353 int error;
354 git_tree *tree_after_removal = NULL;
355 git_oid oid;
356
357 if ((error = manipulate_note_in_tree_r(
358 &tree_after_removal, repo, tree, NULL, target, 0,
359 remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
360 goto cleanup;
361
362 error = git_commit_create(&oid, repo, notes_ref, author, committer,
363 NULL, GIT_NOTES_DEFAULT_MSG_RM,
364 tree_after_removal,
365 *parents == NULL ? 0 : 1,
366 (const git_commit **) parents);
367
368 cleanup:
369 git_tree_free(tree_after_removal);
370 return error;
371 }
372
373 static int note_get_default_ref(const char **out, git_repository *repo)
374 {
375 int ret;
376 git_config *cfg;
377
378 *out = NULL;
379
380 if (git_repository_config__weakptr(&cfg, repo) < 0)
381 return -1;
382
383 ret = git_config_get_string(out, cfg, "core.notesRef");
384 if (ret == GIT_ENOTFOUND) {
385 *out = GIT_NOTES_DEFAULT_REF;
386 return 0;
387 }
388
389 return ret;
390 }
391
392 static int normalize_namespace(const char **notes_ref, git_repository *repo)
393 {
394 if (*notes_ref)
395 return 0;
396
397 return note_get_default_ref(notes_ref, repo);
398 }
399
400 static int retrieve_note_tree_and_commit(
401 git_tree **tree_out,
402 git_commit **commit_out,
403 git_repository *repo,
404 const char **notes_ref)
405 {
406 int error;
407 git_oid oid;
408
409 if ((error = normalize_namespace(notes_ref, repo)) < 0)
410 return error;
411
412 if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0)
413 return error;
414
415 if (git_commit_lookup(commit_out, repo, &oid) < 0)
416 return error;
417
418 if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
419 return error;
420
421 return 0;
422 }
423
424 int git_note_read(git_note **out, git_repository *repo,
425 const char *notes_ref, const git_oid *oid)
426 {
427 int error;
428 char *target = NULL;
429 git_tree *tree = NULL;
430 git_commit *commit = NULL;
431
432 target = git_oid_allocfmt(oid);
433 GITERR_CHECK_ALLOC(target);
434
435 if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
436 goto cleanup;
437
438 error = note_lookup(out, repo, tree, target);
439
440 cleanup:
441 git__free(target);
442 git_tree_free(tree);
443 git_commit_free(commit);
444 return error;
445 }
446
447 int git_note_create(
448 git_oid *out,
449 git_repository *repo,
450 const git_signature *author,
451 const git_signature *committer,
452 const char *notes_ref,
453 const git_oid *oid,
454 const char *note,
455 int allow_note_overwrite)
456 {
457 int error;
458 char *target = NULL;
459 git_commit *commit = NULL;
460 git_tree *tree = NULL;
461
462 target = git_oid_allocfmt(oid);
463 GITERR_CHECK_ALLOC(target);
464
465 error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
466
467 if (error < 0 && error != GIT_ENOTFOUND)
468 goto cleanup;
469
470 error = note_write(out, repo, author, committer, notes_ref,
471 note, tree, target, &commit, allow_note_overwrite);
472
473 cleanup:
474 git__free(target);
475 git_commit_free(commit);
476 git_tree_free(tree);
477 return error;
478 }
479
480 int git_note_remove(git_repository *repo, const char *notes_ref,
481 const git_signature *author, const git_signature *committer,
482 const git_oid *oid)
483 {
484 int error;
485 char *target = NULL;
486 git_commit *commit = NULL;
487 git_tree *tree = NULL;
488
489 target = git_oid_allocfmt(oid);
490 GITERR_CHECK_ALLOC(target);
491
492 if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
493 goto cleanup;
494
495 error = note_remove(repo, author, committer, notes_ref,
496 tree, target, &commit);
497
498 cleanup:
499 git__free(target);
500 git_commit_free(commit);
501 git_tree_free(tree);
502 return error;
503 }
504
505 int git_note_default_ref(const char **out, git_repository *repo)
506 {
507 assert(repo);
508 return note_get_default_ref(out, repo);
509 }
510
511 const char * git_note_message(const git_note *note)
512 {
513 assert(note);
514 return note->message;
515 }
516
517 const git_oid * git_note_oid(const git_note *note)
518 {
519 assert(note);
520 return &note->oid;
521 }
522
523 void git_note_free(git_note *note)
524 {
525 if (note == NULL)
526 return;
527
528 git__free(note->message);
529 git__free(note);
530 }
531
532 static int process_entry_path(
533 const char* entry_path,
534 const git_oid *note_oid,
535 git_note_foreach_cb note_cb,
536 void *payload)
537 {
538 int error = -1;
539 size_t i = 0, j = 0, len;
540 git_buf buf = GIT_BUF_INIT;
541 git_oid annotated_object_id;
542
543 if ((error = git_buf_puts(&buf, entry_path)) < 0)
544 goto cleanup;
545
546 len = git_buf_len(&buf);
547
548 while (i < len) {
549 if (buf.ptr[i] == '/') {
550 i++;
551 continue;
552 }
553
554 if (git__fromhex(buf.ptr[i]) < 0) {
555 /* This is not a note entry */
556 goto cleanup;
557 }
558
559 if (i != j)
560 buf.ptr[j] = buf.ptr[i];
561
562 i++;
563 j++;
564 }
565
566 buf.ptr[j] = '\0';
567 buf.size = j;
568
569 if (j != GIT_OID_HEXSZ) {
570 /* This is not a note entry */
571 goto cleanup;
572 }
573
574 if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0)
575 goto cleanup;
576
577 if (note_cb(note_oid, &annotated_object_id, payload))
578 error = GIT_EUSER;
579
580 cleanup:
581 git_buf_free(&buf);
582 return error;
583 }
584
585 int git_note_foreach(
586 git_repository *repo,
587 const char *notes_ref,
588 git_note_foreach_cb note_cb,
589 void *payload)
590 {
591 int error;
592 git_iterator *iter = NULL;
593 git_tree *tree = NULL;
594 git_commit *commit = NULL;
595 const git_index_entry *item;
596
597 if (!(error = retrieve_note_tree_and_commit(
598 &tree, &commit, repo, &notes_ref)) &&
599 !(error = git_iterator_for_tree(&iter, tree)))
600 error = git_iterator_current(iter, &item);
601
602 while (!error && item) {
603 error = process_entry_path(item->path, &item->oid, note_cb, payload);
604
605 if (!error)
606 error = git_iterator_advance(iter, &item);
607 }
608
609 git_iterator_free(iter);
610 git_tree_free(tree);
611 git_commit_free(commit);
612
613 return error;
614 }