]>
Commit | Line | Data |
---|---|---|
bf477ed4 MS |
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" | |
caea5e54 | 12 | #include "config.h" |
86ecd844 | 13 | #include "iterator.h" |
4ec197f3 | 14 | #include "signature.h" |
bf477ed4 | 15 | |
b93688d0 VM |
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) | |
bf477ed4 | 22 | { |
e120123e | 23 | size_t i; |
bf477ed4 MS |
24 | const git_tree_entry *entry; |
25 | ||
a02e7249 | 26 | *out = NULL; |
e120123e | 27 | |
a02e7249 | 28 | if (parent == NULL) |
29 | return GIT_ENOTFOUND; | |
bf477ed4 | 30 | |
a02e7249 | 31 | for (i = 0; i < git_tree_entrycount(parent); i++) { |
32 | entry = git_tree_entry_byindex(parent, i); | |
bf477ed4 MS |
33 | |
34 | if (!git__ishex(git_tree_entry_name(entry))) | |
35 | continue; | |
36 | ||
9d7ac675 | 37 | if (S_ISDIR(git_tree_entry_filemode(entry)) |
e120123e | 38 | && strlen(git_tree_entry_name(entry)) == 2 |
a02e7249 | 39 | && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) |
40 | return git_tree_lookup(out, repo, git_tree_entry_id(entry)); | |
bf477ed4 | 41 | |
a02e7249 | 42 | /* Not a DIR, so do we have an already existing blob? */ |
e120123e | 43 | if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) |
a02e7249 | 44 | return GIT_EEXISTS; |
45 | } | |
bf477ed4 | 46 | |
a02e7249 | 47 | return GIT_ENOTFOUND; |
48 | } | |
bf477ed4 | 49 | |
a02e7249 | 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; | |
bf477ed4 | 55 | |
a02e7249 | 56 | *out = NULL; |
bf477ed4 | 57 | |
a02e7249 | 58 | error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); |
b93688d0 | 59 | if (error == GIT_EEXISTS) { |
dca6b228 | 60 | return git_tree_lookup(out, repo, git_tree_id(root)); |
bf477ed4 MS |
61 | } |
62 | ||
a02e7249 | 63 | if (error < 0) |
b93688d0 | 64 | return error; |
a02e7249 | 65 | |
66 | *fanout += 2; | |
67 | error = find_subtree_r(out, subtree, repo, target, fanout); | |
dca6b228 | 68 | git_tree_free(subtree); |
a02e7249 | 69 | |
a02e7249 | 70 | return error; |
bf477ed4 MS |
71 | } |
72 | ||
73 | static int find_blob(git_oid *blob, git_tree *tree, const char *target) | |
74 | { | |
e120123e | 75 | size_t i; |
bf477ed4 MS |
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)); | |
4aa7de15 | 85 | return 0; |
bf477ed4 MS |
86 | } |
87 | } | |
904b67e6 | 88 | return GIT_ENOTFOUND; |
bf477ed4 MS |
89 | } |
90 | ||
b93688d0 VM |
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) | |
bf477ed4 | 98 | { |
a02e7249 | 99 | int error; |
100 | git_treebuilder *tb = NULL; | |
0e2fcca8 | 101 | const git_tree_entry *entry; |
a02e7249 | 102 | git_oid tree_oid; |
bf477ed4 | 103 | |
a02e7249 | 104 | if ((error = git_treebuilder_create(&tb, source_tree)) < 0) |
105 | goto cleanup; | |
bf477ed4 | 106 | |
a02e7249 | 107 | if (object_oid) { |
b93688d0 VM |
108 | if ((error = git_treebuilder_insert( |
109 | &entry, tb, treeentry_name, object_oid, attributes)) < 0) | |
a02e7249 | 110 | goto cleanup; |
111 | } else { | |
112 | if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0) | |
113 | goto cleanup; | |
114 | } | |
bf477ed4 | 115 | |
a02e7249 | 116 | if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0) |
117 | goto cleanup; | |
bf477ed4 | 118 | |
a02e7249 | 119 | error = git_tree_lookup(out, repo, &tree_oid); |
bf477ed4 | 120 | |
a02e7249 | 121 | cleanup: |
122 | git_treebuilder_free(tb); | |
123 | return error; | |
124 | } | |
bf477ed4 | 125 | |
b93688d0 VM |
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)) | |
a02e7249 | 149 | { |
150 | int error = -1; | |
dca6b228 | 151 | git_tree *subtree = NULL, *new = NULL; |
a02e7249 | 152 | char subtree_name[3]; |
bf477ed4 | 153 | |
b93688d0 VM |
154 | error = find_subtree_in_current_level( |
155 | &subtree, repo, parent, annotated_object_sha, fanout); | |
bf477ed4 | 156 | |
a02e7249 | 157 | if (error == GIT_EEXISTS) { |
b93688d0 VM |
158 | error = note_exists_cb( |
159 | out, repo, parent, note_oid, annotated_object_sha, fanout, error); | |
a02e7249 | 160 | goto cleanup; |
161 | } | |
bf477ed4 | 162 | |
a02e7249 | 163 | if (error == GIT_ENOTFOUND) { |
b93688d0 VM |
164 | error = note_notfound_cb( |
165 | out, repo, parent, note_oid, annotated_object_sha, fanout, error); | |
a02e7249 | 166 | goto cleanup; |
167 | } | |
bf477ed4 | 168 | |
4aa7de15 | 169 | if (error < 0) |
a02e7249 | 170 | goto cleanup; |
bf477ed4 | 171 | |
a02e7249 | 172 | /* An existing fanout has been found, let's dig deeper */ |
b93688d0 | 173 | error = manipulate_note_in_tree_r( |
dca6b228 | 174 | &new, repo, subtree, note_oid, annotated_object_sha, |
b93688d0 | 175 | fanout + 2, note_exists_cb, note_notfound_cb); |
bf477ed4 | 176 | |
a02e7249 | 177 | if (error < 0) |
178 | goto cleanup; | |
bf477ed4 | 179 | |
a02e7249 | 180 | strncpy(subtree_name, annotated_object_sha + fanout, 2); |
181 | subtree_name[2] = '\0'; | |
bf477ed4 | 182 | |
dca6b228 | 183 | error = tree_write(out, repo, parent, git_tree_id(new), |
a7dbac0b | 184 | subtree_name, GIT_FILEMODE_TREE); |
dca6b228 | 185 | |
4aa7de15 | 186 | |
a02e7249 | 187 | cleanup: |
dca6b228 | 188 | git_tree_free(new); |
a02e7249 | 189 | git_tree_free(subtree); |
190 | return error; | |
191 | } | |
bf477ed4 | 192 | |
b93688d0 VM |
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 | { | |
a02e7249 | 202 | GIT_UNUSED(note_oid); |
203 | GIT_UNUSED(current_error); | |
bf477ed4 | 204 | |
a02e7249 | 205 | return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0); |
206 | } | |
bf477ed4 | 207 | |
b93688d0 VM |
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 | { | |
a02e7249 | 217 | GIT_UNUSED(out); |
218 | GIT_UNUSED(repo); | |
219 | GIT_UNUSED(parent); | |
220 | GIT_UNUSED(note_oid); | |
221 | GIT_UNUSED(fanout); | |
bf477ed4 | 222 | |
a02e7249 | 223 | giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); |
224 | return current_error; | |
225 | } | |
bf477ed4 | 226 | |
b93688d0 VM |
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 | { | |
a02e7249 | 235 | GIT_UNUSED(out); |
236 | GIT_UNUSED(repo); | |
237 | GIT_UNUSED(parent); | |
238 | GIT_UNUSED(note_oid); | |
239 | GIT_UNUSED(fanout); | |
bf477ed4 | 240 | |
a02e7249 | 241 | giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); |
242 | return current_error; | |
243 | } | |
bf477ed4 | 244 | |
b93688d0 VM |
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 | { | |
a02e7249 | 253 | GIT_UNUSED(current_error); |
bf477ed4 | 254 | |
a02e7249 | 255 | /* No existing fanout at this level, insert in place */ |
a7dbac0b | 256 | return tree_write( |
257 | out, | |
258 | repo, | |
259 | parent, | |
260 | note_oid, | |
261 | annotated_object_sha + fanout, | |
262 | GIT_FILEMODE_BLOB); | |
bf477ed4 MS |
263 | } |
264 | ||
b93688d0 VM |
265 | static int note_write(git_oid *out, |
266 | git_repository *repo, | |
de5596bf BS |
267 | const git_signature *author, |
268 | const git_signature *committer, | |
b93688d0 VM |
269 | const char *notes_ref, |
270 | const char *note, | |
271 | git_tree *commit_tree, | |
272 | const char *target, | |
273 | git_commit **parents) | |
bf477ed4 | 274 | { |
a02e7249 | 275 | int error; |
bf477ed4 | 276 | git_oid oid; |
a02e7249 | 277 | git_tree *tree = NULL; |
278 | ||
279 | // TODO: should we apply filters? | |
280 | /* create note object */ | |
281 | if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0) | |
282 | goto cleanup; | |
bf477ed4 | 283 | |
b93688d0 VM |
284 | if ((error = manipulate_note_in_tree_r( |
285 | &tree, repo, commit_tree, &oid, target, 0, | |
286 | insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0) | |
a02e7249 | 287 | goto cleanup; |
bf477ed4 | 288 | |
a02e7249 | 289 | if (out) |
290 | git_oid_cpy(out, &oid); | |
4aa7de15 | 291 | |
a02e7249 | 292 | error = git_commit_create(&oid, repo, notes_ref, author, committer, |
293 | NULL, GIT_NOTES_DEFAULT_MSG_ADD, | |
294 | tree, *parents == NULL ? 0 : 1, (const git_commit **) parents); | |
4aa7de15 | 295 | |
a02e7249 | 296 | cleanup: |
bf477ed4 | 297 | git_tree_free(tree); |
a02e7249 | 298 | return error; |
299 | } | |
bf477ed4 | 300 | |
a02e7249 | 301 | static int note_new(git_note **out, git_oid *note_oid, git_blob *blob) |
302 | { | |
303 | git_note *note = NULL; | |
bf477ed4 | 304 | |
a02e7249 | 305 | note = (git_note *)git__malloc(sizeof(git_note)); |
4aa7de15 | 306 | GITERR_CHECK_ALLOC(note); |
bf477ed4 | 307 | |
a02e7249 | 308 | git_oid_cpy(¬e->oid, note_oid); |
309 | note->message = git__strdup((char *)git_blob_rawcontent(blob)); | |
4aa7de15 | 310 | GITERR_CHECK_ALLOC(note->message); |
bf477ed4 MS |
311 | |
312 | *out = note; | |
313 | ||
a02e7249 | 314 | return 0; |
bf477ed4 MS |
315 | } |
316 | ||
a02e7249 | 317 | static int note_lookup(git_note **out, git_repository *repo, |
318 | git_tree *tree, const char *target) | |
bf477ed4 MS |
319 | { |
320 | int error, fanout = 0; | |
321 | git_oid oid; | |
a02e7249 | 322 | git_blob *blob = NULL; |
323 | git_note *note = NULL; | |
324 | git_tree *subtree = NULL; | |
bf477ed4 | 325 | |
a02e7249 | 326 | if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) |
327 | goto cleanup; | |
bf477ed4 | 328 | |
a02e7249 | 329 | if ((error = find_blob(&oid, subtree, target + fanout)) < 0) |
330 | goto cleanup; | |
bf477ed4 | 331 | |
a02e7249 | 332 | if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) |
333 | goto cleanup; | |
bf477ed4 | 334 | |
a02e7249 | 335 | if ((error = note_new(¬e, &oid, blob)) < 0) |
336 | goto cleanup; | |
bf477ed4 | 337 | |
a02e7249 | 338 | *out = note; |
bf477ed4 | 339 | |
a02e7249 | 340 | cleanup: |
341 | git_tree_free(subtree); | |
342 | git_blob_free(blob); | |
343 | return error; | |
344 | } | |
bf477ed4 | 345 | |
a02e7249 | 346 | static int note_remove(git_repository *repo, |
de5596bf BS |
347 | const git_signature *author, const git_signature *committer, |
348 | const char *notes_ref, git_tree *tree, | |
349 | const char *target, git_commit **parents) | |
a02e7249 | 350 | { |
351 | int error; | |
352 | git_tree *tree_after_removal = NULL; | |
353 | git_oid oid; | |
354 | ||
b93688d0 VM |
355 | if ((error = manipulate_note_in_tree_r( |
356 | &tree_after_removal, repo, tree, NULL, target, 0, | |
357 | remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) | |
a02e7249 | 358 | goto cleanup; |
bf477ed4 MS |
359 | |
360 | error = git_commit_create(&oid, repo, notes_ref, author, committer, | |
b93688d0 VM |
361 | NULL, GIT_NOTES_DEFAULT_MSG_RM, |
362 | tree_after_removal, | |
363 | *parents == NULL ? 0 : 1, | |
364 | (const git_commit **) parents); | |
bf477ed4 | 365 | |
a02e7249 | 366 | cleanup: |
367 | git_tree_free(tree_after_removal); | |
bf477ed4 MS |
368 | return error; |
369 | } | |
370 | ||
caea5e54 MS |
371 | static int note_get_default_ref(const char **out, git_repository *repo) |
372 | { | |
f95e8cc0 | 373 | int ret; |
caea5e54 MS |
374 | git_config *cfg; |
375 | ||
376 | *out = NULL; | |
377 | ||
378 | if (git_repository_config__weakptr(&cfg, repo) < 0) | |
379 | return -1; | |
380 | ||
29e948de | 381 | ret = git_config_get_string(out, cfg, "core.notesRef"); |
904b67e6 | 382 | if (ret == GIT_ENOTFOUND) { |
caea5e54 MS |
383 | *out = GIT_NOTES_DEFAULT_REF; |
384 | return 0; | |
385 | } | |
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 | |
a02e7249 | 433 | if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) |
434 | goto cleanup; | |
435 | ||
436 | error = note_lookup(out, repo, tree, target); | |
bf477ed4 | 437 | |
a02e7249 | 438 | cleanup: |
bf477ed4 | 439 | git__free(target); |
a02e7249 | 440 | git_tree_free(tree); |
441 | git_commit_free(commit); | |
4aa7de15 | 442 | return error; |
bf477ed4 MS |
443 | } |
444 | ||
4aa7de15 | 445 | int git_note_create( |
de5596bf BS |
446 | git_oid *out, |
447 | git_repository *repo, | |
448 | const git_signature *author, | |
449 | const git_signature *committer, | |
450 | const char *notes_ref, | |
451 | const git_oid *oid, | |
4aa7de15 | 452 | const char *note) |
bf477ed4 | 453 | { |
a02e7249 | 454 | int error; |
455 | char *target = NULL; | |
bf477ed4 | 456 | git_commit *commit = NULL; |
a02e7249 | 457 | git_tree *tree = NULL; |
bf477ed4 MS |
458 | |
459 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 460 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 461 | |
a02e7249 | 462 | error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); |
463 | ||
464 | if (error < 0 && error != GIT_ENOTFOUND) | |
465 | goto cleanup; | |
466 | ||
bf477ed4 | 467 | error = note_write(out, repo, author, committer, notes_ref, |
de5596bf | 468 | note, tree, target, &commit); |
bf477ed4 | 469 | |
a02e7249 | 470 | cleanup: |
bf477ed4 MS |
471 | git__free(target); |
472 | git_commit_free(commit); | |
a02e7249 | 473 | git_tree_free(tree); |
4aa7de15 | 474 | return error; |
bf477ed4 MS |
475 | } |
476 | ||
477 | int git_note_remove(git_repository *repo, const char *notes_ref, | |
de5596bf BS |
478 | const git_signature *author, const git_signature *committer, |
479 | const git_oid *oid) | |
bf477ed4 MS |
480 | { |
481 | int error; | |
a02e7249 | 482 | char *target = NULL; |
483 | git_commit *commit = NULL; | |
484 | git_tree *tree = NULL; | |
bf477ed4 MS |
485 | |
486 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 487 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 488 | |
a02e7249 | 489 | if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) |
490 | goto cleanup; | |
491 | ||
bf477ed4 | 492 | error = note_remove(repo, author, committer, notes_ref, |
a02e7249 | 493 | tree, target, &commit); |
bf477ed4 | 494 | |
a02e7249 | 495 | cleanup: |
bf477ed4 MS |
496 | git__free(target); |
497 | git_commit_free(commit); | |
a02e7249 | 498 | git_tree_free(tree); |
4aa7de15 | 499 | return error; |
bf477ed4 MS |
500 | } |
501 | ||
630c5a4a MS |
502 | int git_note_default_ref(const char **out, git_repository *repo) |
503 | { | |
504 | assert(repo); | |
505 | return note_get_default_ref(out, repo); | |
506 | } | |
507 | ||
de5596bf | 508 | const char * git_note_message(const git_note *note) |
bf477ed4 MS |
509 | { |
510 | assert(note); | |
511 | return note->message; | |
512 | } | |
513 | ||
de5596bf | 514 | const git_oid * git_note_oid(const git_note *note) |
bf477ed4 MS |
515 | { |
516 | assert(note); | |
517 | return ¬e->oid; | |
518 | } | |
519 | ||
520 | void git_note_free(git_note *note) | |
521 | { | |
522 | if (note == NULL) | |
523 | return; | |
524 | ||
525 | git__free(note->message); | |
526 | git__free(note); | |
527 | } | |
86ecd844 | 528 | |
529 | static int process_entry_path( | |
530 | const char* entry_path, | |
ee7680d5 | 531 | const git_oid *note_oid, |
de5596bf | 532 | git_note_foreach_cb note_cb, |
86ecd844 | 533 | void *payload) |
534 | { | |
b8457baa | 535 | int error = -1; |
536 | size_t i = 0, j = 0, len; | |
86ecd844 | 537 | git_buf buf = GIT_BUF_INIT; |
2bd5998c | 538 | git_oid annotated_object_id; |
86ecd844 | 539 | |
5dca2010 | 540 | if ((error = git_buf_puts(&buf, entry_path)) < 0) |
86ecd844 | 541 | goto cleanup; |
5dca2010 | 542 | |
86ecd844 | 543 | len = git_buf_len(&buf); |
544 | ||
545 | while (i < len) { | |
546 | if (buf.ptr[i] == '/') { | |
547 | i++; | |
548 | continue; | |
549 | } | |
5dca2010 | 550 | |
86ecd844 | 551 | if (git__fromhex(buf.ptr[i]) < 0) { |
552 | /* This is not a note entry */ | |
86ecd844 | 553 | goto cleanup; |
554 | } | |
555 | ||
556 | if (i != j) | |
557 | buf.ptr[j] = buf.ptr[i]; | |
558 | ||
559 | i++; | |
560 | j++; | |
561 | } | |
562 | ||
563 | buf.ptr[j] = '\0'; | |
564 | buf.size = j; | |
565 | ||
566 | if (j != GIT_OID_HEXSZ) { | |
567 | /* This is not a note entry */ | |
86ecd844 | 568 | goto cleanup; |
569 | } | |
570 | ||
2bd5998c | 571 | if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0) |
5dca2010 | 572 | goto cleanup; |
86ecd844 | 573 | |
2bd5998c | 574 | if (note_cb(note_oid, &annotated_object_id, payload)) |
5dca2010 | 575 | error = GIT_EUSER; |
86ecd844 | 576 | |
577 | cleanup: | |
578 | git_buf_free(&buf); | |
579 | return error; | |
580 | } | |
581 | ||
582 | int git_note_foreach( | |
583 | git_repository *repo, | |
584 | const char *notes_ref, | |
de5596bf | 585 | git_note_foreach_cb note_cb, |
86ecd844 | 586 | void *payload) |
587 | { | |
5dca2010 | 588 | int error; |
86ecd844 | 589 | git_iterator *iter = NULL; |
590 | git_tree *tree = NULL; | |
a02e7249 | 591 | git_commit *commit = NULL; |
d5ed6348 | 592 | const git_index_entry *item; |
86ecd844 | 593 | |
5dca2010 RB |
594 | if (!(error = retrieve_note_tree_and_commit( |
595 | &tree, &commit, repo, ¬es_ref)) && | |
9950d27a | 596 | !(error = git_iterator_for_tree(&iter, tree))) |
5dca2010 | 597 | error = git_iterator_current(iter, &item); |
86ecd844 | 598 | |
5dca2010 RB |
599 | while (!error && item) { |
600 | error = process_entry_path(item->path, &item->oid, note_cb, payload); | |
86ecd844 | 601 | |
5dca2010 RB |
602 | if (!error) |
603 | error = git_iterator_advance(iter, &item); | |
86ecd844 | 604 | } |
605 | ||
86ecd844 | 606 | git_iterator_free(iter); |
607 | git_tree_free(tree); | |
a02e7249 | 608 | git_commit_free(commit); |
5dca2010 | 609 | |
86ecd844 | 610 | return error; |
611 | } |