diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
commit | a50086d174658914d4d6462afbc83b02825b1f5b (patch) | |
tree | e8daa1c7bf678222cf351445179837bed7db3a72 /src/notes.c | |
parent | 5b9fac39d8a76b9139667c26a63e6b3f204b3977 (diff) | |
parent | f124ebd457bfbf43de3516629aaba5a279636e04 (diff) |
Merge branch 'development'v0.18.0
Diffstat (limited to 'src/notes.c')
-rw-r--r-- | src/notes.c | 687 |
1 files changed, 400 insertions, 287 deletions
diff --git a/src/notes.c b/src/notes.c index 84ad94087..ef48ac88e 100644 --- a/src/notes.c +++ b/src/notes.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2012 the libgit2 contributors + * Copyright (C) the libgit2 contributors. All rights reserved. * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. @@ -11,56 +11,68 @@ #include "refs.h" #include "config.h" #include "iterator.h" +#include "signature.h" -static int find_subtree(git_tree **subtree, const git_oid *root, - git_repository *repo, const char *target, int *fanout) +static int find_subtree_in_current_level( + git_tree **out, + git_repository *repo, + git_tree *parent, + const char *annotated_object_sha, + int fanout) { - int error; - unsigned int i; - git_tree *tree; + size_t i; const git_tree_entry *entry; - *subtree = NULL; + *out = NULL; - error = git_tree_lookup(&tree, repo, root); - if (error < 0) - return error; + if (parent == NULL) + return GIT_ENOTFOUND; - for (i=0; i<git_tree_entrycount(tree); i++) { - entry = git_tree_entry_byindex(tree, i); + for (i = 0; i < git_tree_entrycount(parent); i++) { + entry = git_tree_entry_byindex(parent, i); if (!git__ishex(git_tree_entry_name(entry))) continue; - /* - * A notes tree follows a strict byte-based progressive fanout - * (i.e. using 2/38, 2/2/36, etc. fanouts, not e.g. 4/36 fanout) - */ - - if (S_ISDIR(git_tree_entry_attributes(entry)) + if (S_ISDIR(git_tree_entry_filemode(entry)) && strlen(git_tree_entry_name(entry)) == 2 - && !strncmp(git_tree_entry_name(entry), target + *fanout, 2)) { + && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) + return git_tree_lookup(out, repo, git_tree_entry_id(entry)); - /* found matching subtree - unpack and resume lookup */ + /* Not a DIR, so do we have an already existing blob? */ + if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) + return GIT_EEXISTS; + } - git_oid subtree_sha; - git_oid_cpy(&subtree_sha, git_tree_entry_id(entry)); - git_tree_free(tree); + return GIT_ENOTFOUND; +} - *fanout += 2; +static int find_subtree_r(git_tree **out, git_tree *root, + git_repository *repo, const char *target, int *fanout) +{ + int error; + git_tree *subtree = NULL; - return find_subtree(subtree, &subtree_sha, repo, - target, fanout); - } + *out = NULL; + + error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); + if (error == GIT_EEXISTS) { + return git_tree_lookup(out, repo, git_tree_id(root)); } - *subtree = tree; - return 0; + if (error < 0) + return error; + + *fanout += 2; + error = find_subtree_r(out, subtree, repo, target, fanout); + git_tree_free(subtree); + + return error; } static int find_blob(git_oid *blob, git_tree *tree, const char *target) { - unsigned int i; + size_t i; const git_tree_entry *entry; for (i=0; i<git_tree_entrycount(tree); i++) { @@ -76,191 +88,285 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target) return GIT_ENOTFOUND; } -static int note_write(git_oid *out, git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const char *note, - const git_oid *tree_sha, const char *target, - int nparents, git_commit **parents) +static int tree_write( + git_tree **out, + git_repository *repo, + git_tree *source_tree, + const git_oid *object_oid, + const char *treeentry_name, + unsigned int attributes) { - int error, fanout = 0; - git_oid oid; - git_tree *tree = NULL; - git_tree_entry *entry; - git_treebuilder *tb; - - /* check for existing notes tree */ - - if (tree_sha) { - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; - - error = find_blob(&oid, tree, target + fanout); - if (error != GIT_ENOTFOUND) { - git_tree_free(tree); - if (!error) { - giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", target); - error = GIT_EEXISTS; - } - return error; - } - } + int error; + git_treebuilder *tb = NULL; + const git_tree_entry *entry; + git_oid tree_oid; - /* no matching tree entry - add note object to target tree */ + if ((error = git_treebuilder_create(&tb, source_tree)) < 0) + goto cleanup; - error = git_treebuilder_create(&tb, tree); - git_tree_free(tree); + if (object_oid) { + if ((error = git_treebuilder_insert( + &entry, tb, treeentry_name, object_oid, attributes)) < 0) + goto cleanup; + } else { + if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0) + goto cleanup; + } - if (error < 0) - return error; + if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0) + goto cleanup; - if (!tree_sha) - /* no notes tree yet - create fanout */ - fanout += 2; + error = git_tree_lookup(out, repo, &tree_oid); - /* create note object */ - error = git_blob_create_frombuffer(&oid, repo, note, strlen(note)); - if (error < 0) { - git_treebuilder_free(tb); - return error; - } +cleanup: + git_treebuilder_free(tb); + return error; +} - error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); - if (error < 0) { - /* libgit2 doesn't support object removal (gc) yet */ - /* we leave an orphaned blob object behind - TODO */ +static int manipulate_note_in_tree_r( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int (*note_exists_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error), + int (*note_notfound_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error)) +{ + int error; + git_tree *subtree = NULL, *new = NULL; + char subtree_name[3]; - git_treebuilder_free(tb); - return error; - } + error = find_subtree_in_current_level( + &subtree, repo, parent, annotated_object_sha, fanout); - if (out) - git_oid_cpy(out, git_tree_entry_id(entry)); + if (error == GIT_EEXISTS) { + error = note_exists_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } - error = git_treebuilder_write(&oid, repo, tb); - git_treebuilder_free(tb); + if (error == GIT_ENOTFOUND) { + error = note_notfound_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } if (error < 0) - return 0; - - if (!tree_sha) { - /* create fanout subtree */ + goto cleanup; - char subtree[3]; - strncpy(subtree, target, 2); - subtree[2] = '\0'; + /* An existing fanout has been found, let's dig deeper */ + error = manipulate_note_in_tree_r( + &new, repo, subtree, note_oid, annotated_object_sha, + fanout + 2, note_exists_cb, note_notfound_cb); - error = git_treebuilder_create(&tb, NULL); - if (error < 0) - return error; + if (error < 0) + goto cleanup; - error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); - if (error < 0) { - git_treebuilder_free(tb); - return error; - } + strncpy(subtree_name, annotated_object_sha + fanout, 2); + subtree_name[2] = '\0'; - error = git_treebuilder_write(&oid, repo, tb); + error = tree_write(out, repo, parent, git_tree_id(new), + subtree_name, GIT_FILEMODE_TREE); - git_treebuilder_free(tb); - if (error < 0) - return error; - } +cleanup: + git_tree_free(new); + git_tree_free(subtree); + return error; +} - /* create new notes commit */ +static int remove_note_in_tree_eexists_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(note_oid); + GIT_UNUSED(current_error); - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; + return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0); +} - error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_ADD, - tree, nparents, (const git_commit **) parents); +static int remove_note_in_tree_enotfound_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); + return current_error; +} - git_tree_free(tree); +static int insert_note_in_tree_eexists_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); + return current_error; +} - return error; +static int insert_note_in_tree_enotfound_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(current_error); + + /* No existing fanout at this level, insert in place */ + return tree_write( + out, + repo, + parent, + note_oid, + annotated_object_sha + fanout, + GIT_FILEMODE_BLOB); } -static int note_lookup(git_note **out, git_repository *repo, - const git_oid *tree_sha, const char *target) +static int note_write(git_oid *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *notes_ref, + const char *note, + git_tree *commit_tree, + const char *target, + git_commit **parents, + int allow_note_overwrite) { - int error, fanout = 0; + int error; git_oid oid; - git_blob *blob; - git_tree *tree; - git_note *note; + git_tree *tree = NULL; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + // TODO: should we apply filters? + /* create note object */ + if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); + if ((error = manipulate_note_in_tree_r( + &tree, repo, commit_tree, &oid, target, 0, + allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb, + insert_note_in_tree_enotfound_cb)) < 0) + goto cleanup; + + if (out) + git_oid_cpy(out, &oid); + + error = git_commit_create(&oid, repo, notes_ref, author, committer, + NULL, GIT_NOTES_DEFAULT_MSG_ADD, + tree, *parents == NULL ? 0 : 1, (const git_commit **) parents); +cleanup: git_tree_free(tree); - if (error < 0) - return error; + return error; +} - error = git_blob_lookup(&blob, repo, &oid); - if (error < 0) - return error; +static int note_new(git_note **out, git_oid *note_oid, git_blob *blob) +{ + git_note *note = NULL; - note = git__malloc(sizeof(git_note)); + note = (git_note *)git__malloc(sizeof(git_note)); GITERR_CHECK_ALLOC(note); - git_oid_cpy(¬e->oid, &oid); - note->message = git__strdup(git_blob_rawcontent(blob)); + git_oid_cpy(¬e->oid, note_oid); + note->message = git__strdup((char *)git_blob_rawcontent(blob)); GITERR_CHECK_ALLOC(note->message); *out = note; - git_blob_free(blob); - return error; + return 0; } -static int note_remove(git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const git_oid *tree_sha, - const char *target, int nparents, git_commit **parents) +static int note_lookup(git_note **out, git_repository *repo, + git_tree *tree, const char *target) { int error, fanout = 0; git_oid oid; - git_tree *tree; - git_treebuilder *tb; + git_blob *blob = NULL; + git_note *note = NULL; + git_tree *subtree = NULL; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); - if (!error) - error = git_treebuilder_create(&tb, tree); + if ((error = find_blob(&oid, subtree, target + fanout)) < 0) + goto cleanup; - git_tree_free(tree); - if (error < 0) - return error; + if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) + goto cleanup; - error = git_treebuilder_remove(tb, target + fanout); - if (!error) - error = git_treebuilder_write(&oid, repo, tb); + if ((error = note_new(¬e, &oid, blob)) < 0) + goto cleanup; - git_treebuilder_free(tb); - if (error < 0) - return error; + *out = note; - /* create new notes commit */ +cleanup: + git_tree_free(subtree); + git_blob_free(blob); + return error; +} - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; +static int note_remove(git_repository *repo, + const git_signature *author, const git_signature *committer, + const char *notes_ref, git_tree *tree, + const char *target, git_commit **parents) +{ + int error; + git_tree *tree_after_removal = NULL; + git_oid oid; - error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_RM, - tree, nparents, (const git_commit **) parents); + if ((error = manipulate_note_in_tree_r( + &tree_after_removal, repo, tree, NULL, target, 0, + remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) + goto cleanup; - git_tree_free(tree); + error = git_commit_create(&oid, repo, notes_ref, author, committer, + NULL, GIT_NOTES_DEFAULT_MSG_RM, + tree_after_removal, + *parents == NULL ? 0 : 1, + (const git_commit **) parents); +cleanup: + git_tree_free(tree_after_removal); return error; } @@ -291,134 +397,108 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo) return note_get_default_ref(notes_ref, repo); } -static int retrieve_note_tree_oid(git_oid *tree_oid_out, git_repository *repo, const char *notes_ref) +static int retrieve_note_tree_and_commit( + git_tree **tree_out, + git_commit **commit_out, + git_repository *repo, + const char **notes_ref) { - int error = -1; - git_commit *commit = NULL; + int error; git_oid oid; - if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0) - goto cleanup; + if ((error = normalize_namespace(notes_ref, repo)) < 0) + return error; - if (git_commit_lookup(&commit, repo, &oid) < 0) - goto cleanup; + if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0) + return error; - git_oid_cpy(tree_oid_out, git_commit_tree_oid(commit)); + if (git_commit_lookup(commit_out, repo, &oid) < 0) + return error; - error = 0; + if ((error = git_commit_tree(tree_out, *commit_out)) < 0) + return error; -cleanup: - git_commit_free(commit); - return error; + return 0; } int git_note_read(git_note **out, git_repository *repo, const char *notes_ref, const git_oid *oid) { int error; - char *target; - git_oid sha; - - *out = NULL; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0) - return error; + char *target = NULL; + git_tree *tree = NULL; + git_commit *commit = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); - error = note_lookup(out, repo, &sha, target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + + error = note_lookup(out, repo, tree, target); +cleanup: git__free(target); + git_tree_free(tree); + git_commit_free(commit); return error; } int git_note_create( - git_oid *out, git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const git_oid *oid, - const char *note) + git_oid *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *notes_ref, + const git_oid *oid, + const char *note, + int allow_note_overwrite) { - int error, nparents = 0; - char *target; - git_oid sha; + int error; + char *target = NULL; git_commit *commit = NULL; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - if (!error) { - assert(git_reference_type(ref) == GIT_REF_OID); - - /* lookup existing notes tree oid */ - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); - nparents++; - } + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + error = note_write(out, repo, author, committer, notes_ref, - note, nparents ? &sha : NULL, target, - nparents, &commit); + note, tree, target, &commit, allow_note_overwrite); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } int git_note_remove(git_repository *repo, const char *notes_ref, - git_signature *author, git_signature *committer, - const git_oid *oid) + const git_signature *author, const git_signature *committer, + const git_oid *oid) { int error; - char *target; - git_oid sha; - git_commit *commit; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0) - return error; - - assert(git_reference_type(ref) == GIT_REF_OID); - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); + char *target = NULL; + git_commit *commit = NULL; + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + error = note_remove(repo, author, committer, notes_ref, - &sha, target, 1, &commit); + tree, target, &commit); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } @@ -428,13 +508,13 @@ int git_note_default_ref(const char **out, git_repository *repo) return note_get_default_ref(out, repo); } -const char * git_note_message(git_note *note) +const char * git_note_message(const git_note *note) { assert(note); return note->message; } -const git_oid * git_note_oid(git_note *note) +const git_oid * git_note_oid(const git_note *note) { assert(note); return ¬e->oid; @@ -451,17 +531,15 @@ void git_note_free(git_note *note) static int process_entry_path( const char* entry_path, - const git_oid *note_oid, - int (*note_cb)(git_note_data *note_data, void *payload), - void *payload) + git_oid *annotated_object_id) { - int i = 0, j = 0, error = -1, len; + int error = -1; + size_t i = 0, j = 0, len; git_buf buf = GIT_BUF_INIT; - git_note_data note_data; - if (git_buf_puts(&buf, entry_path) < 0) + if ((error = git_buf_puts(&buf, entry_path)) < 0) goto cleanup; - + len = git_buf_len(&buf); while (i < len) { @@ -469,10 +547,9 @@ static int process_entry_path( i++; continue; } - + if (git__fromhex(buf.ptr[i]) < 0) { /* This is not a note entry */ - error = 0; goto cleanup; } @@ -488,16 +565,10 @@ static int process_entry_path( if (j != GIT_OID_HEXSZ) { /* This is not a note entry */ - error = 0; goto cleanup; } - if (git_oid_fromstr(¬e_data.annotated_object_oid, buf.ptr) < 0) - return -1; - - git_oid_cpy(¬e_data.blob_oid, note_oid); - - error = note_cb(¬e_data, payload); + error = git_oid_fromstr(annotated_object_id, buf.ptr); cleanup: git_buf_free(&buf); @@ -505,44 +576,86 @@ cleanup: } int git_note_foreach( - git_repository *repo, - const char *notes_ref, - int (*note_cb)(git_note_data *note_data, void *payload), - void *payload) + git_repository *repo, + const char *notes_ref, + git_note_foreach_cb note_cb, + void *payload) { - int error = -1; - git_oid tree_oid; - git_iterator *iter = NULL; - git_tree *tree = NULL; - const git_index_entry *item; + int error; + git_note_iterator *iter = NULL; + git_oid note_id, annotated_id; - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; + if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0) + return error; - if ((error = retrieve_note_tree_oid(&tree_oid, repo, notes_ref)) < 0) - goto cleanup; + while (!(error = git_note_next(¬e_id, &annotated_id, iter))) { + if (note_cb(¬e_id, &annotated_id, payload)) { + error = GIT_EUSER; + break; + } + } - if (git_tree_lookup(&tree, repo, &tree_oid) < 0) - goto cleanup; + if (error == GIT_ITEROVER) + error = 0; - if (git_iterator_for_tree(&iter, repo, tree) < 0) - goto cleanup; + git_note_iterator_free(iter); + return error; +} - if (git_iterator_current(iter, &item) < 0) - goto cleanup; - while (item) { - if (process_entry_path(item->path, &item->oid, note_cb, payload) < 0) - goto cleanup; +void git_note_iterator_free(git_note_iterator *it) +{ + if (it == NULL) + return; - if (git_iterator_advance(iter, &item) < 0) - goto cleanup; - } + git_iterator_free(it); +} - error = 0; + +int git_note_iterator_new( + git_note_iterator **it, + git_repository *repo, + const char *notes_ref) +{ + int error; + git_commit *commit = NULL; + git_tree *tree = NULL; + + error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); + if (error < 0) + goto cleanup; + + if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0) + git_iterator_free(*it); cleanup: - git_iterator_free(iter); git_tree_free(tree); + git_commit_free(commit); + + return error; +} + +int git_note_next( + git_oid* note_id, + git_oid* annotated_id, + git_note_iterator *it) +{ + int error; + const git_index_entry *item; + + if ((error = git_iterator_current(&item, it)) < 0) + goto exit; + + if (item != NULL) { + git_oid_cpy(note_id, &item->oid); + error = process_entry_path(item->path, annotated_id); + + if (error >= 0) + error = git_iterator_advance(NULL, it); + } else { + error = GIT_ITEROVER; + } + +exit: return error; } |