]> git.proxmox.com Git - libgit2.git/blobdiff - src/notes.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / notes.c
index f44c0bd95e9c201a9d73e5b319d521df289f6b4a..d1a2b0f64293780742294f5676933d57be3ff4e5 100644 (file)
@@ -7,15 +7,16 @@
 
 #include "notes.h"
 
-#include "git2.h"
+#include "buf.h"
 #include "refs.h"
 #include "config.h"
 #include "iterator.h"
 #include "signature.h"
+#include "blob.h"
 
 static int note_error_notfound(void)
 {
-       giterr_set(GITERR_INVALID, "Note could not be found");
+       git_error_set(GIT_ERROR_INVALID, "note could not be found");
        return GIT_ENOTFOUND;
 }
 
@@ -226,7 +227,7 @@ static int remove_note_in_tree_enotfound_cb(
        GIT_UNUSED(note_oid);
        GIT_UNUSED(fanout);
 
-       giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
+       git_error_set(GIT_ERROR_REPOSITORY, "object '%s' has no note", annotated_object_sha);
        return current_error;
 }
 
@@ -244,7 +245,7 @@ static int insert_note_in_tree_eexists_cb(git_tree **out,
        GIT_UNUSED(note_oid);
        GIT_UNUSED(fanout);
 
-       giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
+       git_error_set(GIT_ERROR_REPOSITORY, "note for '%s' exists already", annotated_object_sha);
        return current_error;
 }
 
@@ -268,7 +269,9 @@ static int insert_note_in_tree_enotfound_cb(git_tree **out,
                GIT_FILEMODE_BLOB);
 }
 
-static int note_write(git_oid *out,
+static int note_write(
+       git_oid *notes_commit_out,
+       git_oid *notes_blob_out,
        git_repository *repo,
        const git_signature *author,
        const git_signature *committer,
@@ -283,9 +286,9 @@ static int note_write(git_oid *out,
        git_oid oid;
        git_tree *tree = NULL;
 
-       // TODO: should we apply filters?
+       /* TODO: should we apply filters? */
        /* create note object */
-       if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
+       if ((error = git_blob_create_from_buffer(&oid, repo, note, strlen(note))) < 0)
                goto cleanup;
 
        if ((error = manipulate_note_in_tree_r(
@@ -294,13 +297,17 @@ static int note_write(git_oid *out,
                insert_note_in_tree_enotfound_cb)) < 0)
                goto cleanup;
 
-       if (out)
-               git_oid_cpy(out, &oid);
+       if (notes_blob_out)
+               git_oid_cpy(notes_blob_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);
 
+       if (notes_commit_out)
+               git_oid_cpy(notes_commit_out, &oid);
+
 cleanup:
        git_tree_free(tree);
        return error;
@@ -313,9 +320,10 @@ static int note_new(
        git_blob *blob)
 {
        git_note *note = NULL;
+       git_object_size_t blobsize;
 
-       note = (git_note *)git__malloc(sizeof(git_note));
-       GITERR_CHECK_ALLOC(note);
+       note = git__malloc(sizeof(git_note));
+       GIT_ERROR_CHECK_ALLOC(note);
 
        git_oid_cpy(&note->id, note_oid);
 
@@ -323,8 +331,11 @@ static int note_new(
                git_signature_dup(&note->committer, git_commit_committer(commit)) < 0)
                return -1;
 
-       note->message = git__strndup(git_blob_rawcontent(blob), git_blob_rawsize(blob));
-       GITERR_CHECK_ALLOC(note->message);
+       blobsize = git_blob_rawsize(blob);
+       GIT_ERROR_CHECK_BLOBSIZE(blobsize);
+
+       note->message = git__strndup(git_blob_rawcontent(blob), (size_t)blobsize);
+       GIT_ERROR_CHECK_ALLOC(note->message);
 
        *out = note;
        return 0;
@@ -363,7 +374,9 @@ cleanup:
        return error;
 }
 
-static int note_remove(git_repository *repo,
+static int note_remove(
+               git_oid *notes_commit_out,
+               git_repository *repo,
                const git_signature *author, const git_signature *committer,
                const char *notes_ref, git_tree *tree,
                const char *target, git_commit **parents)
@@ -383,79 +396,109 @@ static int note_remove(git_repository *repo,
          *parents == NULL ? 0 : 1,
          (const git_commit **) parents);
 
+       if (error < 0)
+               goto cleanup;
+
+       if (notes_commit_out)
+               git_oid_cpy(notes_commit_out, &oid);
+
 cleanup:
        git_tree_free(tree_after_removal);
        return error;
 }
 
-static int note_get_default_ref(const char **out, git_repository *repo)
+static int note_get_default_ref(git_str *out, git_repository *repo)
 {
        git_config *cfg;
-       int ret = git_repository_config__weakptr(&cfg, repo);
+       int error;
+
+       if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
+               return error;
+
+       error = git_config__get_string_buf(out, cfg, "core.notesref");
 
-       *out = (ret != 0) ? NULL : git_config__get_string_force(
-               cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
+       if (error == GIT_ENOTFOUND)
+               error = git_str_puts(out, GIT_NOTES_DEFAULT_REF);
 
-       return ret;
+       return error;
 }
 
-static int normalize_namespace(const char **notes_ref, git_repository *repo)
+static int normalize_namespace(git_str *out, git_repository *repo, const char *notes_ref)
 {
-       if (*notes_ref)
-               return 0;
+       if (notes_ref)
+               return git_str_puts(out, notes_ref);
 
-       return note_get_default_ref(notes_ref, repo);
+       return note_get_default_ref(out, repo);
 }
 
-static int retrieve_note_tree_and_commit(
-       git_tree **tree_out,
+static int retrieve_note_commit(
        git_commit **commit_out,
+       git_str *notes_ref_out,
        git_repository *repo,
-       const char **notes_ref)
+       const char *notes_ref)
 {
        int error;
        git_oid oid;
 
-       if ((error = normalize_namespace(notes_ref, repo)) < 0)
+       if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
                return error;
 
-       if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0)
+       if ((error = git_reference_name_to_id(&oid, repo, notes_ref_out->ptr)) < 0)
                return error;
 
        if (git_commit_lookup(commit_out, repo, &oid) < 0)
                return error;
 
-       if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
-               return error;
-
        return 0;
 }
 
-int git_note_read(git_note **out, git_repository *repo,
-                 const char *notes_ref, const git_oid *oid)
+int git_note_commit_read(
+       git_note **out,
+       git_repository *repo,
+       git_commit *notes_commit,
+       const git_oid *oid)
 {
        int error;
-       char *target = NULL;
        git_tree *tree = NULL;
-       git_commit *commit = NULL;
+       char target[GIT_OID_HEXSZ + 1];
 
-       target = git_oid_allocfmt(oid);
-       GITERR_CHECK_ALLOC(target);
+       git_oid_tostr(target, sizeof(target), oid);
+
+       if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+               goto cleanup;
 
-       if (!(error = retrieve_note_tree_and_commit(
-                       &tree, &commit, repo, &notes_ref)))
-               error = note_lookup(out, repo, commit, tree, target);
+       error = note_lookup(out, repo, notes_commit, tree, target);
 
-       git__free(target);
+cleanup:
        git_tree_free(tree);
+       return error;
+}
+
+int git_note_read(git_note **out, git_repository *repo,
+                 const char *notes_ref_in, const git_oid *oid)
+{
+       int error;
+       git_str notes_ref = GIT_STR_INIT;
+       git_commit *commit = NULL;
+
+       error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
+
+       if (error < 0)
+               goto cleanup;
+
+       error = git_note_commit_read(out, repo, commit, oid);
+
+cleanup:
+       git_str_dispose(&notes_ref);
        git_commit_free(commit);
        return error;
 }
 
-int git_note_create(
-       git_oid *out,
+int git_note_commit_create(
+       git_oid *notes_commit_out,
+       git_oid *notes_blob_out,
        git_repository *repo,
-       const char *notes_ref,
+       git_commit *parent,
        const git_signature *author,
        const git_signature *committer,
        const git_oid *oid,
@@ -463,78 +506,150 @@ int git_note_create(
        int allow_note_overwrite)
 {
        int error;
-       char *target = NULL;
-       git_commit *commit = NULL;
        git_tree *tree = NULL;
+       char target[GIT_OID_HEXSZ + 1];
+
+       git_oid_tostr(target, sizeof(target), oid);
+
+       if (parent != NULL && (error = git_commit_tree(&tree, parent)) < 0)
+               goto cleanup;
 
-       target = git_oid_allocfmt(oid);
-       GITERR_CHECK_ALLOC(target);
+       error = note_write(notes_commit_out, notes_blob_out, repo, author,
+                       committer, NULL, note, tree, target, &parent, allow_note_overwrite);
+
+       if (error < 0)
+               goto cleanup;
+
+cleanup:
+       git_tree_free(tree);
+       return error;
+}
+
+int git_note_create(
+       git_oid *out,
+       git_repository *repo,
+       const char *notes_ref_in,
+       const git_signature *author,
+       const git_signature *committer,
+       const git_oid *oid,
+       const char *note,
+       int allow_note_overwrite)
+{
+       int error;
+       git_str notes_ref = GIT_STR_INIT;
+       git_commit *existing_notes_commit = NULL;
+       git_reference *ref = NULL;
+       git_oid notes_blob_oid, notes_commit_oid;
 
-       error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+       error = retrieve_note_commit(&existing_notes_commit, &notes_ref,
+                       repo, notes_ref_in);
 
        if (error < 0 && error != GIT_ENOTFOUND)
                goto cleanup;
 
-       error = note_write(out, repo, author, committer, notes_ref,
-                       note, tree, target, &commit, allow_note_overwrite);
+       error = git_note_commit_create(&notes_commit_oid,
+                       &notes_blob_oid,
+                       repo, existing_notes_commit, author,
+                       committer, oid, note,
+                       allow_note_overwrite);
+       if (error < 0)
+               goto cleanup;
+
+       error = git_reference_create(&ref, repo, notes_ref.ptr,
+                               &notes_commit_oid, 1, NULL);
+
+       if (out != NULL)
+               git_oid_cpy(out, &notes_blob_oid);
 
 cleanup:
-       git__free(target);
-       git_commit_free(commit);
-       git_tree_free(tree);
+       git_str_dispose(&notes_ref);
+       git_commit_free(existing_notes_commit);
+       git_reference_free(ref);
        return error;
 }
 
-int git_note_remove(git_repository *repo, const char *notes_ref,
-               const git_signature *author, const git_signature *committer,
+int git_note_commit_remove(
+               git_oid *notes_commit_out,
+               git_repository *repo,
+               git_commit *notes_commit,
+               const git_signature *author,
+               const git_signature *committer,
                const git_oid *oid)
 {
        int error;
-       char *target = NULL;
-       git_commit *commit = NULL;
        git_tree *tree = NULL;
+       char target[GIT_OID_HEXSZ + 1];
+
+       git_oid_tostr(target, sizeof(target), oid);
 
-       target = git_oid_allocfmt(oid);
-       GITERR_CHECK_ALLOC(target);
+       if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+               goto cleanup;
 
-       if (!(error = retrieve_note_tree_and_commit(
-                       &tree, &commit, repo, &notes_ref)))
-               error = note_remove(
-                       repo, author, committer, notes_ref, tree, target, &commit);
+       error = note_remove(notes_commit_out,
+               repo, author, committer, NULL, tree, target, &notes_commit);
 
-       git__free(target);
-       git_commit_free(commit);
+cleanup:
        git_tree_free(tree);
        return error;
 }
 
-int git_note_default_ref(const char **out, git_repository *repo)
+int git_note_remove(git_repository *repo, const char *notes_ref_in,
+               const git_signature *author, const git_signature *committer,
+               const git_oid *oid)
 {
-       assert(repo);
-       return note_get_default_ref(out, repo);
+       int error;
+       git_str notes_ref_target = GIT_STR_INIT;
+       git_commit *existing_notes_commit = NULL;
+       git_oid new_notes_commit;
+       git_reference *notes_ref = NULL;
+
+       error = retrieve_note_commit(&existing_notes_commit, &notes_ref_target,
+                       repo, notes_ref_in);
+
+       if (error < 0)
+               goto cleanup;
+
+       error = git_note_commit_remove(&new_notes_commit, repo,
+                       existing_notes_commit, author, committer, oid);
+       if (error < 0)
+               goto cleanup;
+
+       error = git_reference_create(&notes_ref, repo, notes_ref_target.ptr,
+                       &new_notes_commit, 1, NULL);
+
+cleanup:
+       git_str_dispose(&notes_ref_target);
+       git_reference_free(notes_ref);
+       git_commit_free(existing_notes_commit);
+       return error;
+}
+
+int git_note_default_ref(git_buf *out, git_repository *repo)
+{
+       GIT_BUF_WRAP_PRIVATE(out, note_get_default_ref, repo);
 }
 
 const git_signature *git_note_committer(const git_note *note)
 {
-       assert(note);
+       GIT_ASSERT_ARG_WITH_RETVAL(note, NULL);
        return note->committer;
 }
 
 const git_signature *git_note_author(const git_note *note)
 {
-       assert(note);
+       GIT_ASSERT_ARG_WITH_RETVAL(note, NULL);
        return note->author;
 }
 
-const char * git_note_message(const git_note *note)
+const char *git_note_message(const git_note *note)
 {
-       assert(note);
+       GIT_ASSERT_ARG_WITH_RETVAL(note, NULL);
        return note->message;
 }
 
-const git_oid * git_note_id(const git_note *note)
+const git_oid *git_note_id(const git_note *note)
 {
-       assert(note);
+       GIT_ASSERT_ARG_WITH_RETVAL(note, NULL);
        return &note->id;
 }
 
@@ -550,17 +665,17 @@ void git_note_free(git_note *note)
 }
 
 static int process_entry_path(
-       const charentry_path,
+       const char *entry_path,
        git_oid *annotated_object_id)
 {
        int error = 0;
        size_t i = 0, j = 0, len;
-       git_buf buf = GIT_BUF_INIT;
+       git_str buf = GIT_STR_INIT;
 
-       if ((error = git_buf_puts(&buf, entry_path)) < 0)
+       if ((error = git_str_puts(&buf, entry_path)) < 0)
                goto cleanup;
 
-       len = git_buf_len(&buf);
+       len = git_str_len(&buf);
 
        while (i < len) {
                if (buf.ptr[i] == '/') {
@@ -591,7 +706,7 @@ static int process_entry_path(
        error = git_oid_fromstr(annotated_object_id, buf.ptr);
 
 cleanup:
-       git_buf_free(&buf);
+       git_str_dispose(&buf);
        return error;
 }
 
@@ -610,7 +725,7 @@ int git_note_foreach(
 
        while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
                if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
-                       giterr_set_after_callback(error);
+                       git_error_set_after_callback(error);
                        break;
                }
        }
@@ -622,7 +737,6 @@ int git_note_foreach(
        return error;
 }
 
-
 void git_note_iterator_free(git_note_iterator *it)
 {
        if (it == NULL)
@@ -631,33 +745,50 @@ void git_note_iterator_free(git_note_iterator *it)
        git_iterator_free(it);
 }
 
+int git_note_commit_iterator_new(
+       git_note_iterator **it,
+       git_commit *notes_commit)
+{
+       int error;
+       git_tree *tree;
+
+       if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+               goto cleanup;
+
+       if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
+               git_iterator_free(*it);
+
+cleanup:
+       git_tree_free(tree);
+
+       return error;
+}
 
 int git_note_iterator_new(
        git_note_iterator **it,
        git_repository *repo,
-       const char *notes_ref)
+       const char *notes_ref_in)
 {
        int error;
        git_commit *commit = NULL;
-       git_tree *tree = NULL;
+       git_str notes_ref = GIT_STR_INIT;
 
-       error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+       error = retrieve_note_commit(&commit, &notes_ref, repo, notes_ref_in);
        if (error < 0)
                goto cleanup;
 
-       if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0)
-               git_iterator_free(*it);
+       error = git_note_commit_iterator_new(it, commit);
 
 cleanup:
-       git_tree_free(tree);
+       git_str_dispose(&notes_ref);
        git_commit_free(commit);
 
        return error;
 }
 
 int git_note_next(
-       git_oidnote_id,
-       git_oidannotated_id,
+       git_oid *note_id,
+       git_oid *annotated_id,
        git_note_iterator *it)
 {
        int error;
@@ -668,8 +799,11 @@ int git_note_next(
 
        git_oid_cpy(note_id, &item->id);
 
-       if (!(error = process_entry_path(item->path, annotated_id)))
-               git_iterator_advance(NULL, it);
+       if ((error = process_entry_path(item->path, annotated_id)) < 0)
+               return error;
 
-       return error;
+       if ((error = git_iterator_advance(NULL, it)) < 0 && error != GIT_ITEROVER)
+               return error;
+
+       return 0;
 }