#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;
}
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;
}
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;
}
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,
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(
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;
git_blob *blob)
{
git_note *note = NULL;
+ git_object_size_t blobsize;
note = git__malloc(sizeof(git_note));
- GITERR_CHECK_ALLOC(note);
+ GIT_ERROR_CHECK_ALLOC(note);
git_oid_cpy(¬e->id, note_oid);
git_signature_dup(¬e->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;
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)
*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(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(char **out, git_repository *repo, const char *notes_ref)
+static int normalize_namespace(git_str *out, git_repository *repo, const char *notes_ref)
{
- if (notes_ref) {
- *out = git__strdup(notes_ref);
- GITERR_CHECK_ALLOC(*out);
- return 0;
- }
+ if (notes_ref)
+ return git_str_puts(out, notes_ref);
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,
- char **notes_ref_out,
+ git_str *notes_ref_out,
git_repository *repo,
const char *notes_ref)
{
if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
return error;
- if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 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_commit_read(
+ git_note **out,
+ git_repository *repo,
+ git_commit *notes_commit,
+ const git_oid *oid)
+{
+ int error;
+ git_tree *tree = NULL;
+ char target[GIT_OID_HEXSZ + 1];
+
+ git_oid_tostr(target, sizeof(target), oid);
+
+ if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+ goto cleanup;
+
+ error = note_lookup(out, repo, notes_commit, tree, 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;
- char *target = NULL, *notes_ref = NULL;
- git_tree *tree = NULL;
+ git_str notes_ref = GIT_STR_INIT;
git_commit *commit = NULL;
- target = git_oid_allocfmt(oid);
- GITERR_CHECK_ALLOC(target);
+ error = retrieve_note_commit(&commit, ¬es_ref, repo, notes_ref_in);
+
+ if (error < 0)
+ goto cleanup;
- if (!(error = retrieve_note_tree_and_commit(
- &tree, &commit, ¬es_ref, repo, notes_ref_in)))
- error = note_lookup(out, repo, commit, tree, target);
+ error = git_note_commit_read(out, repo, commit, oid);
- git__free(notes_ref);
- git__free(target);
- git_tree_free(tree);
+cleanup:
+ git_str_dispose(¬es_ref);
git_commit_free(commit);
return error;
}
+int git_note_commit_create(
+ git_oid *notes_commit_out,
+ git_oid *notes_blob_out,
+ git_repository *repo,
+ git_commit *parent,
+ const git_signature *author,
+ const git_signature *committer,
+ const git_oid *oid,
+ const char *note,
+ int allow_note_overwrite)
+{
+ int error;
+ 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;
+
+ 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,
int allow_note_overwrite)
{
int error;
- char *target = NULL, *notes_ref = NULL;
- git_commit *commit = NULL;
- git_tree *tree = NULL;
-
- target = git_oid_allocfmt(oid);
- GITERR_CHECK_ALLOC(target);
+ 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, ¬es_ref, repo, notes_ref_in);
+ error = retrieve_note_commit(&existing_notes_commit, ¬es_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(¬es_commit_oid,
+ ¬es_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,
+ ¬es_commit_oid, 1, NULL);
+
+ if (out != NULL)
+ git_oid_cpy(out, ¬es_blob_oid);
cleanup:
- git__free(notes_ref);
- git__free(target);
- git_commit_free(commit);
- git_tree_free(tree);
+ git_str_dispose(¬es_ref);
+ git_commit_free(existing_notes_commit);
+ git_reference_free(ref);
return error;
}
-int git_note_remove(git_repository *repo, const char *notes_ref_in,
- 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, *notes_ref;
- git_commit *commit = NULL;
git_tree *tree = 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 = retrieve_note_tree_and_commit(
- &tree, &commit, ¬es_ref, repo, notes_ref_in)))
- error = note_remove(
- repo, author, committer, notes_ref, tree, target, &commit);
+ if ((error = git_commit_tree(&tree, notes_commit)) < 0)
+ goto cleanup;
- git__free(notes_ref);
- git__free(target);
- git_commit_free(commit);
+ error = note_remove(notes_commit_out,
+ repo, author, committer, NULL, tree, target, ¬es_commit);
+
+cleanup:
git_tree_free(tree);
return error;
}
-int git_note_default_ref(git_buf *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)
{
- char *default_ref;
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;
- assert(out && repo);
+ error = retrieve_note_commit(&existing_notes_commit, ¬es_ref_target,
+ repo, notes_ref_in);
- git_buf_sanitize(out);
+ if (error < 0)
+ goto cleanup;
- if ((error = note_get_default_ref(&default_ref, repo)) < 0)
- return error;
+ error = git_note_commit_remove(&new_notes_commit, repo,
+ existing_notes_commit, author, committer, oid);
+ if (error < 0)
+ goto cleanup;
- git_buf_attach(out, default_ref, strlen(default_ref));
- return 0;
+ error = git_reference_create(¬es_ref, repo, notes_ref_target.ptr,
+ &new_notes_commit, 1, NULL);
+
+cleanup:
+ git_str_dispose(¬es_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 ¬e->id;
}
}
static int process_entry_path(
- const char* entry_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] == '/') {
error = git_oid_fromstr(annotated_object_id, buf.ptr);
cleanup:
- git_buf_free(&buf);
+ git_str_dispose(&buf);
return error;
}
while (!(error = git_note_next(¬e_id, &annotated_id, iter))) {
if ((error = note_cb(¬e_id, &annotated_id, payload)) != 0) {
- giterr_set_after_callback(error);
+ git_error_set_after_callback(error);
break;
}
}
return error;
}
-
void git_note_iterator_free(git_note_iterator *it)
{
if (it == NULL)
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,
{
int error;
git_commit *commit = NULL;
- git_tree *tree = NULL;
- char *notes_ref;
+ git_str notes_ref = GIT_STR_INIT;
- error = retrieve_note_tree_and_commit(&tree, &commit, ¬es_ref, repo, notes_ref_in);
+ error = retrieve_note_commit(&commit, ¬es_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__free(notes_ref);
- git_tree_free(tree);
+ git_str_dispose(¬es_ref);
git_commit_free(commit);
return error;
}
int git_note_next(
- git_oid* note_id,
- git_oid* annotated_id,
+ git_oid *note_id,
+ git_oid *annotated_id,
git_note_iterator *it)
{
int error;
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;
}