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