]>
Commit | Line | Data |
---|---|---|
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 |
16 | static int note_error_notfound(void) |
17 | { | |
18 | giterr_set(GITERR_INVALID, "Note could not be found"); | |
19 | return GIT_ENOTFOUND; | |
20 | } | |
21 | ||
b93688d0 VM |
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) | |
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 | 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; | |
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 | ||
78 | static 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 |
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) | |
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 | 127 | cleanup: |
128 | git_treebuilder_free(tb); | |
129 | return error; | |
130 | } | |
bf477ed4 | 131 | |
b93688d0 VM |
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, | |
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 | 193 | cleanup: |
dca6b228 | 194 | git_tree_free(new); |
a02e7249 | 195 | git_tree_free(subtree); |
196 | return error; | |
197 | } | |
bf477ed4 | 198 | |
b93688d0 VM |
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 | { | |
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 |
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 | { | |
a02e7249 | 223 | GIT_UNUSED(out); |
224 | GIT_UNUSED(repo); | |
225 | GIT_UNUSED(parent); | |
226 | GIT_UNUSED(note_oid); | |
227 | GIT_UNUSED(fanout); | |
bf477ed4 | 228 | |
a02e7249 | 229 | giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); |
230 | return current_error; | |
231 | } | |
bf477ed4 | 232 | |
b93688d0 VM |
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 | { | |
a02e7249 | 241 | GIT_UNUSED(out); |
242 | GIT_UNUSED(repo); | |
243 | GIT_UNUSED(parent); | |
244 | GIT_UNUSED(note_oid); | |
245 | GIT_UNUSED(fanout); | |
bf477ed4 | 246 | |
a02e7249 | 247 | giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); |
248 | return current_error; | |
249 | } | |
bf477ed4 | 250 | |
b93688d0 VM |
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 | { | |
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 | ||
b93688d0 VM |
271 | static int note_write(git_oid *out, |
272 | git_repository *repo, | |
de5596bf BS |
273 | const git_signature *author, |
274 | const git_signature *committer, | |
b93688d0 VM |
275 | const char *notes_ref, |
276 | const char *note, | |
277 | git_tree *commit_tree, | |
278 | const char *target, | |
8716b499 NV |
279 | git_commit **parents, |
280 | int allow_note_overwrite) | |
bf477ed4 | 281 | { |
a02e7249 | 282 | int error; |
bf477ed4 | 283 | git_oid oid; |
a02e7249 | 284 | git_tree *tree = NULL; |
4a44087a | 285 | |
a02e7249 | 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; | |
bf477ed4 | 290 | |
b93688d0 VM |
291 | if ((error = manipulate_note_in_tree_r( |
292 | &tree, repo, commit_tree, &oid, target, 0, | |
8716b499 NV |
293 | allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb, |
294 | insert_note_in_tree_enotfound_cb)) < 0) | |
a02e7249 | 295 | goto cleanup; |
bf477ed4 | 296 | |
a02e7249 | 297 | if (out) |
298 | git_oid_cpy(out, &oid); | |
4aa7de15 | 299 | |
a02e7249 | 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); | |
4aa7de15 | 303 | |
a02e7249 | 304 | cleanup: |
bf477ed4 | 305 | git_tree_free(tree); |
a02e7249 | 306 | return error; |
307 | } | |
bf477ed4 | 308 | |
bad4937e ET |
309 | static int note_new( |
310 | git_note **out, | |
311 | git_oid *note_oid, | |
312 | git_commit *commit, | |
313 | git_blob *blob) | |
a02e7249 | 314 | { |
315 | git_note *note = NULL; | |
bf477ed4 | 316 | |
392702ee | 317 | note = git__malloc(sizeof(git_note)); |
4aa7de15 | 318 | GITERR_CHECK_ALLOC(note); |
bf477ed4 | 319 | |
d0a3de72 | 320 | git_oid_cpy(¬e->id, note_oid); |
bad4937e ET |
321 | |
322 | if (git_signature_dup(¬e->author, git_commit_author(commit)) < 0 || | |
323 | git_signature_dup(¬e->committer, git_commit_committer(commit)) < 0) | |
324 | return -1; | |
325 | ||
b7fb71e3 VM |
326 | note->message = git__strndup(git_blob_rawcontent(blob), git_blob_rawsize(blob)); |
327 | GITERR_CHECK_ALLOC(note->message); | |
bf477ed4 MS |
328 | |
329 | *out = note; | |
a02e7249 | 330 | return 0; |
bf477ed4 MS |
331 | } |
332 | ||
734c6fc1 | 333 | static int note_lookup( |
bad4937e ET |
334 | git_note **out, |
335 | git_repository *repo, | |
336 | git_commit *commit, | |
337 | git_tree *tree, | |
338 | const char *target) | |
bf477ed4 MS |
339 | { |
340 | int error, fanout = 0; | |
341 | git_oid oid; | |
a02e7249 | 342 | git_blob *blob = NULL; |
343 | git_note *note = NULL; | |
344 | git_tree *subtree = NULL; | |
bf477ed4 | 345 | |
a02e7249 | 346 | if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) |
347 | goto cleanup; | |
bf477ed4 | 348 | |
a02e7249 | 349 | if ((error = find_blob(&oid, subtree, target + fanout)) < 0) |
350 | goto cleanup; | |
bf477ed4 | 351 | |
a02e7249 | 352 | if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) |
353 | goto cleanup; | |
bf477ed4 | 354 | |
bad4937e | 355 | if ((error = note_new(¬e, &oid, commit, blob)) < 0) |
a02e7249 | 356 | goto cleanup; |
bf477ed4 | 357 | |
a02e7249 | 358 | *out = note; |
bf477ed4 | 359 | |
a02e7249 | 360 | cleanup: |
361 | git_tree_free(subtree); | |
362 | git_blob_free(blob); | |
363 | return error; | |
364 | } | |
bf477ed4 | 365 | |
a02e7249 | 366 | static int note_remove(git_repository *repo, |
de5596bf BS |
367 | const git_signature *author, const git_signature *committer, |
368 | const char *notes_ref, git_tree *tree, | |
369 | const char *target, git_commit **parents) | |
a02e7249 | 370 | { |
371 | int error; | |
372 | git_tree *tree_after_removal = NULL; | |
373 | git_oid oid; | |
4a44087a | 374 | |
b93688d0 VM |
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) | |
a02e7249 | 378 | goto cleanup; |
bf477ed4 MS |
379 | |
380 | error = git_commit_create(&oid, repo, notes_ref, author, committer, | |
b93688d0 VM |
381 | NULL, GIT_NOTES_DEFAULT_MSG_RM, |
382 | tree_after_removal, | |
383 | *parents == NULL ? 0 : 1, | |
384 | (const git_commit **) parents); | |
bf477ed4 | 385 | |
a02e7249 | 386 | cleanup: |
387 | git_tree_free(tree_after_removal); | |
bf477ed4 MS |
388 | return error; |
389 | } | |
390 | ||
385449b1 | 391 | static int note_get_default_ref(char **out, git_repository *repo) |
caea5e54 | 392 | { |
caea5e54 | 393 | git_config *cfg; |
9f77b3f6 | 394 | int ret = git_repository_config__weakptr(&cfg, repo); |
caea5e54 | 395 | |
9f77b3f6 RB |
396 | *out = (ret != 0) ? NULL : git_config__get_string_force( |
397 | cfg, "core.notesref", GIT_NOTES_DEFAULT_REF); | |
caea5e54 | 398 | |
f95e8cc0 | 399 | return ret; |
caea5e54 MS |
400 | } |
401 | ||
385449b1 | 402 | static int normalize_namespace(char **out, git_repository *repo, const char *notes_ref) |
86ecd844 | 403 | { |
385449b1 CMN |
404 | if (notes_ref) { |
405 | *out = git__strdup(notes_ref); | |
406 | GITERR_CHECK_ALLOC(*out); | |
86ecd844 | 407 | return 0; |
385449b1 | 408 | } |
86ecd844 | 409 | |
385449b1 | 410 | return note_get_default_ref(out, repo); |
86ecd844 | 411 | } |
412 | ||
b93688d0 VM |
413 | static int retrieve_note_tree_and_commit( |
414 | git_tree **tree_out, | |
415 | git_commit **commit_out, | |
385449b1 | 416 | char **notes_ref_out, |
b93688d0 | 417 | git_repository *repo, |
385449b1 | 418 | const char *notes_ref) |
86ecd844 | 419 | { |
a02e7249 | 420 | int error; |
86ecd844 | 421 | git_oid oid; |
422 | ||
385449b1 | 423 | if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0) |
a02e7249 | 424 | return error; |
86ecd844 | 425 | |
385449b1 | 426 | if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0) |
a02e7249 | 427 | return error; |
86ecd844 | 428 | |
a02e7249 | 429 | if (git_commit_lookup(commit_out, repo, &oid) < 0) |
430 | return error; | |
86ecd844 | 431 | |
a02e7249 | 432 | if ((error = git_commit_tree(tree_out, *commit_out)) < 0) |
433 | return error; | |
86ecd844 | 434 | |
a02e7249 | 435 | return 0; |
86ecd844 | 436 | } |
437 | ||
bf477ed4 | 438 | int git_note_read(git_note **out, git_repository *repo, |
385449b1 | 439 | const char *notes_ref_in, const git_oid *oid) |
bf477ed4 MS |
440 | { |
441 | int error; | |
385449b1 | 442 | char *target = NULL, *notes_ref = NULL; |
a02e7249 | 443 | git_tree *tree = NULL; |
444 | git_commit *commit = NULL; | |
bf477ed4 | 445 | |
bf477ed4 | 446 | target = git_oid_allocfmt(oid); |
4aa7de15 | 447 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 448 | |
734c6fc1 | 449 | if (!(error = retrieve_note_tree_and_commit( |
385449b1 | 450 | &tree, &commit, ¬es_ref, repo, notes_ref_in))) |
bad4937e | 451 | error = note_lookup(out, repo, commit, tree, target); |
bf477ed4 | 452 | |
385449b1 | 453 | git__free(notes_ref); |
bf477ed4 | 454 | git__free(target); |
a02e7249 | 455 | git_tree_free(tree); |
456 | git_commit_free(commit); | |
4aa7de15 | 457 | return error; |
bf477ed4 MS |
458 | } |
459 | ||
4aa7de15 | 460 | int git_note_create( |
de5596bf BS |
461 | git_oid *out, |
462 | git_repository *repo, | |
385449b1 | 463 | const char *notes_ref_in, |
de5596bf BS |
464 | const git_signature *author, |
465 | const git_signature *committer, | |
de5596bf | 466 | const git_oid *oid, |
8716b499 NV |
467 | const char *note, |
468 | int allow_note_overwrite) | |
bf477ed4 | 469 | { |
a02e7249 | 470 | int error; |
385449b1 | 471 | char *target = NULL, *notes_ref = NULL; |
bf477ed4 | 472 | git_commit *commit = NULL; |
a02e7249 | 473 | git_tree *tree = NULL; |
bf477ed4 MS |
474 | |
475 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 476 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 477 | |
385449b1 | 478 | error = retrieve_note_tree_and_commit(&tree, &commit, ¬es_ref, repo, notes_ref_in); |
a02e7249 | 479 | |
480 | if (error < 0 && error != GIT_ENOTFOUND) | |
481 | goto cleanup; | |
482 | ||
bf477ed4 | 483 | error = note_write(out, repo, author, committer, notes_ref, |
8716b499 | 484 | note, tree, target, &commit, allow_note_overwrite); |
bf477ed4 | 485 | |
a02e7249 | 486 | cleanup: |
385449b1 | 487 | git__free(notes_ref); |
bf477ed4 MS |
488 | git__free(target); |
489 | git_commit_free(commit); | |
a02e7249 | 490 | git_tree_free(tree); |
4aa7de15 | 491 | return error; |
bf477ed4 MS |
492 | } |
493 | ||
385449b1 | 494 | int git_note_remove(git_repository *repo, const char *notes_ref_in, |
de5596bf BS |
495 | const git_signature *author, const git_signature *committer, |
496 | const git_oid *oid) | |
bf477ed4 MS |
497 | { |
498 | int error; | |
385449b1 | 499 | char *target = NULL, *notes_ref; |
a02e7249 | 500 | git_commit *commit = NULL; |
501 | git_tree *tree = NULL; | |
bf477ed4 MS |
502 | |
503 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 504 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 505 | |
734c6fc1 | 506 | if (!(error = retrieve_note_tree_and_commit( |
385449b1 | 507 | &tree, &commit, ¬es_ref, repo, notes_ref_in))) |
734c6fc1 RB |
508 | error = note_remove( |
509 | repo, author, committer, notes_ref, tree, target, &commit); | |
bf477ed4 | 510 | |
385449b1 | 511 | git__free(notes_ref); |
bf477ed4 MS |
512 | git__free(target); |
513 | git_commit_free(commit); | |
a02e7249 | 514 | git_tree_free(tree); |
4aa7de15 | 515 | return error; |
bf477ed4 MS |
516 | } |
517 | ||
385449b1 | 518 | int git_note_default_ref(git_buf *out, git_repository *repo) |
630c5a4a | 519 | { |
385449b1 CMN |
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; | |
630c5a4a MS |
532 | } |
533 | ||
bad4937e ET |
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 | ||
de5596bf | 546 | const char * git_note_message(const git_note *note) |
bf477ed4 MS |
547 | { |
548 | assert(note); | |
549 | return note->message; | |
550 | } | |
551 | ||
d0a3de72 | 552 | const git_oid * git_note_id(const git_note *note) |
bf477ed4 MS |
553 | { |
554 | assert(note); | |
d0a3de72 | 555 | return ¬e->id; |
bf477ed4 MS |
556 | } |
557 | ||
558 | void git_note_free(git_note *note) | |
559 | { | |
560 | if (note == NULL) | |
561 | return; | |
562 | ||
bad4937e ET |
563 | git_signature_free(note->committer); |
564 | git_signature_free(note->author); | |
bf477ed4 MS |
565 | git__free(note->message); |
566 | git__free(note); | |
567 | } | |
86ecd844 | 568 | |
569 | static int process_entry_path( | |
570 | const char* entry_path, | |
f7b18502 | 571 | git_oid *annotated_object_id) |
86ecd844 | 572 | { |
734c6fc1 | 573 | int error = 0; |
b8457baa | 574 | size_t i = 0, j = 0, len; |
86ecd844 | 575 | git_buf buf = GIT_BUF_INIT; |
576 | ||
5dca2010 | 577 | if ((error = git_buf_puts(&buf, entry_path)) < 0) |
86ecd844 | 578 | goto cleanup; |
5dca2010 | 579 | |
86ecd844 | 580 | len = git_buf_len(&buf); |
581 | ||
582 | while (i < len) { | |
583 | if (buf.ptr[i] == '/') { | |
584 | i++; | |
585 | continue; | |
586 | } | |
5dca2010 | 587 | |
86ecd844 | 588 | if (git__fromhex(buf.ptr[i]) < 0) { |
589 | /* This is not a note entry */ | |
86ecd844 | 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 */ | |
86ecd844 | 605 | goto cleanup; |
606 | } | |
607 | ||
f7b18502 | 608 | error = git_oid_fromstr(annotated_object_id, buf.ptr); |
86ecd844 | 609 | |
86ecd844 | 610 | cleanup: |
611 | git_buf_free(&buf); | |
612 | return error; | |
613 | } | |
614 | ||
615 | int git_note_foreach( | |
0cb16fe9 L |
616 | git_repository *repo, |
617 | const char *notes_ref, | |
618 | git_note_foreach_cb note_cb, | |
619 | void *payload) | |
86ecd844 | 620 | { |
0cb16fe9 L |
621 | int error; |
622 | git_note_iterator *iter = NULL; | |
623 | git_oid note_id, annotated_id; | |
6edb427b | 624 | |
0cb16fe9 L |
625 | if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0) |
626 | return error; | |
86ecd844 | 627 | |
f10d7a36 RB |
628 | while (!(error = git_note_next(¬e_id, &annotated_id, iter))) { |
629 | if ((error = note_cb(¬e_id, &annotated_id, payload)) != 0) { | |
26c1cb91 | 630 | giterr_set_after_callback(error); |
f10d7a36 RB |
631 | break; |
632 | } | |
633 | } | |
86ecd844 | 634 | |
0cb16fe9 L |
635 | if (error == GIT_ITEROVER) |
636 | error = 0; | |
5dca2010 | 637 | |
0cb16fe9 L |
638 | git_note_iterator_free(iter); |
639 | return error; | |
86ecd844 | 640 | } |
6edb427b | 641 | |
f7b18502 | 642 | |
1a90dcf6 NG |
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 | ||
6edb427b | 652 | int git_note_iterator_new( |
1a90dcf6 | 653 | git_note_iterator **it, |
6edb427b | 654 | git_repository *repo, |
385449b1 | 655 | const char *notes_ref_in) |
6edb427b NG |
656 | { |
657 | int error; | |
658 | git_commit *commit = NULL; | |
659 | git_tree *tree = NULL; | |
385449b1 | 660 | char *notes_ref; |
6edb427b | 661 | |
385449b1 | 662 | error = retrieve_note_tree_and_commit(&tree, &commit, ¬es_ref, repo, notes_ref_in); |
f7b18502 NG |
663 | if (error < 0) |
664 | goto cleanup; | |
6edb427b | 665 | |
ed1c6446 | 666 | if ((error = git_iterator_for_tree(it, tree, NULL)) < 0) |
f7b18502 | 667 | git_iterator_free(*it); |
6edb427b | 668 | |
f7b18502 | 669 | cleanup: |
385449b1 | 670 | git__free(notes_ref); |
6edb427b NG |
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, | |
f7b18502 | 680 | git_note_iterator *it) |
6edb427b NG |
681 | { |
682 | int error; | |
683 | const git_index_entry *item; | |
684 | ||
169dc616 | 685 | if ((error = git_iterator_current(&item, it)) < 0) |
cee695ae | 686 | return error; |
6edb427b | 687 | |
d541170c | 688 | git_oid_cpy(note_id, &item->id); |
6edb427b | 689 | |
cee695ae RB |
690 | if (!(error = process_entry_path(item->path, annotated_id))) |
691 | git_iterator_advance(NULL, it); | |
6edb427b NG |
692 | |
693 | return error; | |
694 | } |