]>
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, | |
4a44087a | 132 | int fanout, |
b93688d0 | 133 | int (*note_exists_cb)( |
4a44087a NV |
134 | git_tree **out, |
135 | git_repository *repo, | |
b93688d0 VM |
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 | { |
4a44087a | 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, | |
8716b499 NV |
273 | git_commit **parents, |
274 | int allow_note_overwrite) | |
bf477ed4 | 275 | { |
a02e7249 | 276 | int error; |
bf477ed4 | 277 | git_oid oid; |
a02e7249 | 278 | git_tree *tree = NULL; |
4a44087a | 279 | |
a02e7249 | 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; | |
bf477ed4 | 284 | |
b93688d0 VM |
285 | if ((error = manipulate_note_in_tree_r( |
286 | &tree, repo, commit_tree, &oid, target, 0, | |
8716b499 NV |
287 | allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb, |
288 | insert_note_in_tree_enotfound_cb)) < 0) | |
a02e7249 | 289 | goto cleanup; |
bf477ed4 | 290 | |
a02e7249 | 291 | if (out) |
292 | git_oid_cpy(out, &oid); | |
4aa7de15 | 293 | |
a02e7249 | 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); | |
4aa7de15 | 297 | |
a02e7249 | 298 | cleanup: |
bf477ed4 | 299 | git_tree_free(tree); |
a02e7249 | 300 | return error; |
301 | } | |
bf477ed4 | 302 | |
a02e7249 | 303 | static int note_new(git_note **out, git_oid *note_oid, git_blob *blob) |
304 | { | |
305 | git_note *note = NULL; | |
bf477ed4 | 306 | |
a02e7249 | 307 | note = (git_note *)git__malloc(sizeof(git_note)); |
4aa7de15 | 308 | GITERR_CHECK_ALLOC(note); |
bf477ed4 | 309 | |
a02e7249 | 310 | git_oid_cpy(¬e->oid, note_oid); |
311 | note->message = git__strdup((char *)git_blob_rawcontent(blob)); | |
4aa7de15 | 312 | GITERR_CHECK_ALLOC(note->message); |
bf477ed4 MS |
313 | |
314 | *out = note; | |
315 | ||
a02e7249 | 316 | return 0; |
bf477ed4 MS |
317 | } |
318 | ||
a02e7249 | 319 | static int note_lookup(git_note **out, git_repository *repo, |
320 | git_tree *tree, const char *target) | |
bf477ed4 MS |
321 | { |
322 | int error, fanout = 0; | |
323 | git_oid oid; | |
a02e7249 | 324 | git_blob *blob = NULL; |
325 | git_note *note = NULL; | |
326 | git_tree *subtree = NULL; | |
bf477ed4 | 327 | |
a02e7249 | 328 | if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) |
329 | goto cleanup; | |
bf477ed4 | 330 | |
a02e7249 | 331 | if ((error = find_blob(&oid, subtree, target + fanout)) < 0) |
332 | goto cleanup; | |
bf477ed4 | 333 | |
a02e7249 | 334 | if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) |
335 | goto cleanup; | |
bf477ed4 | 336 | |
a02e7249 | 337 | if ((error = note_new(¬e, &oid, blob)) < 0) |
338 | goto cleanup; | |
bf477ed4 | 339 | |
a02e7249 | 340 | *out = note; |
bf477ed4 | 341 | |
a02e7249 | 342 | cleanup: |
343 | git_tree_free(subtree); | |
344 | git_blob_free(blob); | |
345 | return error; | |
346 | } | |
bf477ed4 | 347 | |
a02e7249 | 348 | static int note_remove(git_repository *repo, |
de5596bf BS |
349 | const git_signature *author, const git_signature *committer, |
350 | const char *notes_ref, git_tree *tree, | |
351 | const char *target, git_commit **parents) | |
a02e7249 | 352 | { |
353 | int error; | |
354 | git_tree *tree_after_removal = NULL; | |
355 | git_oid oid; | |
4a44087a | 356 | |
b93688d0 VM |
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) | |
a02e7249 | 360 | goto cleanup; |
bf477ed4 MS |
361 | |
362 | error = git_commit_create(&oid, repo, notes_ref, author, committer, | |
b93688d0 VM |
363 | NULL, GIT_NOTES_DEFAULT_MSG_RM, |
364 | tree_after_removal, | |
365 | *parents == NULL ? 0 : 1, | |
366 | (const git_commit **) parents); | |
bf477ed4 | 367 | |
a02e7249 | 368 | cleanup: |
369 | git_tree_free(tree_after_removal); | |
bf477ed4 MS |
370 | return error; |
371 | } | |
372 | ||
caea5e54 MS |
373 | static int note_get_default_ref(const char **out, git_repository *repo) |
374 | { | |
f95e8cc0 | 375 | int ret; |
caea5e54 MS |
376 | git_config *cfg; |
377 | ||
378 | *out = NULL; | |
379 | ||
380 | if (git_repository_config__weakptr(&cfg, repo) < 0) | |
381 | return -1; | |
382 | ||
29e948de | 383 | ret = git_config_get_string(out, cfg, "core.notesRef"); |
904b67e6 | 384 | if (ret == GIT_ENOTFOUND) { |
caea5e54 MS |
385 | *out = GIT_NOTES_DEFAULT_REF; |
386 | return 0; | |
387 | } | |
388 | ||
f95e8cc0 | 389 | return ret; |
caea5e54 MS |
390 | } |
391 | ||
86ecd844 | 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 | ||
b93688d0 VM |
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) | |
86ecd844 | 405 | { |
a02e7249 | 406 | int error; |
86ecd844 | 407 | git_oid oid; |
408 | ||
a02e7249 | 409 | if ((error = normalize_namespace(notes_ref, repo)) < 0) |
410 | return error; | |
86ecd844 | 411 | |
2508cc66 | 412 | if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0) |
a02e7249 | 413 | return error; |
86ecd844 | 414 | |
a02e7249 | 415 | if (git_commit_lookup(commit_out, repo, &oid) < 0) |
416 | return error; | |
86ecd844 | 417 | |
a02e7249 | 418 | if ((error = git_commit_tree(tree_out, *commit_out)) < 0) |
419 | return error; | |
86ecd844 | 420 | |
a02e7249 | 421 | return 0; |
86ecd844 | 422 | } |
423 | ||
bf477ed4 MS |
424 | int git_note_read(git_note **out, git_repository *repo, |
425 | const char *notes_ref, const git_oid *oid) | |
426 | { | |
427 | int error; | |
a02e7249 | 428 | char *target = NULL; |
429 | git_tree *tree = NULL; | |
430 | git_commit *commit = NULL; | |
bf477ed4 | 431 | |
bf477ed4 | 432 | target = git_oid_allocfmt(oid); |
4aa7de15 | 433 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 434 | |
a02e7249 | 435 | if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) |
436 | goto cleanup; | |
437 | ||
438 | error = note_lookup(out, repo, tree, target); | |
bf477ed4 | 439 | |
a02e7249 | 440 | cleanup: |
bf477ed4 | 441 | git__free(target); |
a02e7249 | 442 | git_tree_free(tree); |
443 | git_commit_free(commit); | |
4aa7de15 | 444 | return error; |
bf477ed4 MS |
445 | } |
446 | ||
4aa7de15 | 447 | int git_note_create( |
de5596bf BS |
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, | |
8716b499 NV |
454 | const char *note, |
455 | int allow_note_overwrite) | |
bf477ed4 | 456 | { |
a02e7249 | 457 | int error; |
458 | char *target = NULL; | |
bf477ed4 | 459 | git_commit *commit = NULL; |
a02e7249 | 460 | git_tree *tree = NULL; |
bf477ed4 MS |
461 | |
462 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 463 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 464 | |
a02e7249 | 465 | error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); |
466 | ||
467 | if (error < 0 && error != GIT_ENOTFOUND) | |
468 | goto cleanup; | |
469 | ||
bf477ed4 | 470 | error = note_write(out, repo, author, committer, notes_ref, |
8716b499 | 471 | note, tree, target, &commit, allow_note_overwrite); |
bf477ed4 | 472 | |
a02e7249 | 473 | cleanup: |
bf477ed4 MS |
474 | git__free(target); |
475 | git_commit_free(commit); | |
a02e7249 | 476 | git_tree_free(tree); |
4aa7de15 | 477 | return error; |
bf477ed4 MS |
478 | } |
479 | ||
480 | int git_note_remove(git_repository *repo, const char *notes_ref, | |
de5596bf BS |
481 | const git_signature *author, const git_signature *committer, |
482 | const git_oid *oid) | |
bf477ed4 MS |
483 | { |
484 | int error; | |
a02e7249 | 485 | char *target = NULL; |
486 | git_commit *commit = NULL; | |
487 | git_tree *tree = NULL; | |
bf477ed4 MS |
488 | |
489 | target = git_oid_allocfmt(oid); | |
4aa7de15 | 490 | GITERR_CHECK_ALLOC(target); |
bf477ed4 | 491 | |
a02e7249 | 492 | if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) |
493 | goto cleanup; | |
494 | ||
bf477ed4 | 495 | error = note_remove(repo, author, committer, notes_ref, |
a02e7249 | 496 | tree, target, &commit); |
bf477ed4 | 497 | |
a02e7249 | 498 | cleanup: |
bf477ed4 MS |
499 | git__free(target); |
500 | git_commit_free(commit); | |
a02e7249 | 501 | git_tree_free(tree); |
4aa7de15 | 502 | return error; |
bf477ed4 MS |
503 | } |
504 | ||
630c5a4a MS |
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 | ||
de5596bf | 511 | const char * git_note_message(const git_note *note) |
bf477ed4 MS |
512 | { |
513 | assert(note); | |
514 | return note->message; | |
515 | } | |
516 | ||
de5596bf | 517 | const git_oid * git_note_oid(const git_note *note) |
bf477ed4 MS |
518 | { |
519 | assert(note); | |
520 | return ¬e->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 | } | |
86ecd844 | 531 | |
532 | static int process_entry_path( | |
533 | const char* entry_path, | |
ee7680d5 | 534 | const git_oid *note_oid, |
de5596bf | 535 | git_note_foreach_cb note_cb, |
86ecd844 | 536 | void *payload) |
537 | { | |
b8457baa | 538 | int error = -1; |
539 | size_t i = 0, j = 0, len; | |
86ecd844 | 540 | git_buf buf = GIT_BUF_INIT; |
2bd5998c | 541 | git_oid annotated_object_id; |
86ecd844 | 542 | |
5dca2010 | 543 | if ((error = git_buf_puts(&buf, entry_path)) < 0) |
86ecd844 | 544 | goto cleanup; |
5dca2010 | 545 | |
86ecd844 | 546 | len = git_buf_len(&buf); |
547 | ||
548 | while (i < len) { | |
549 | if (buf.ptr[i] == '/') { | |
550 | i++; | |
551 | continue; | |
552 | } | |
5dca2010 | 553 | |
86ecd844 | 554 | if (git__fromhex(buf.ptr[i]) < 0) { |
555 | /* This is not a note entry */ | |
86ecd844 | 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 */ | |
86ecd844 | 571 | goto cleanup; |
572 | } | |
573 | ||
2bd5998c | 574 | if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0) |
5dca2010 | 575 | goto cleanup; |
86ecd844 | 576 | |
2bd5998c | 577 | if (note_cb(note_oid, &annotated_object_id, payload)) |
5dca2010 | 578 | error = GIT_EUSER; |
86ecd844 | 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, | |
de5596bf | 588 | git_note_foreach_cb note_cb, |
86ecd844 | 589 | void *payload) |
590 | { | |
5dca2010 | 591 | int error; |
86ecd844 | 592 | git_iterator *iter = NULL; |
593 | git_tree *tree = NULL; | |
a02e7249 | 594 | git_commit *commit = NULL; |
d5ed6348 | 595 | const git_index_entry *item; |
86ecd844 | 596 | |
5dca2010 RB |
597 | if (!(error = retrieve_note_tree_and_commit( |
598 | &tree, &commit, repo, ¬es_ref)) && | |
9950d27a | 599 | !(error = git_iterator_for_tree(&iter, tree))) |
5dca2010 | 600 | error = git_iterator_current(iter, &item); |
86ecd844 | 601 | |
5dca2010 RB |
602 | while (!error && item) { |
603 | error = process_entry_path(item->path, &item->oid, note_cb, payload); | |
86ecd844 | 604 | |
5dca2010 RB |
605 | if (!error) |
606 | error = git_iterator_advance(iter, &item); | |
86ecd844 | 607 | } |
608 | ||
86ecd844 | 609 | git_iterator_free(iter); |
610 | git_tree_free(tree); | |
a02e7249 | 611 | git_commit_free(commit); |
5dca2010 | 612 | |
86ecd844 | 613 | return error; |
614 | } |