Commit a02e7249 by yorah

notes: simplify the handling of fanouts

 - Do not create new levels of fanout when creating notes from libgit2
 - Insert a note in an existing matching fanout
 - Remove a note from an existing fanout
 - Cleanup git_note_read, git_note_remove, git_note_foreach, git_note_create methods in order use tree structures instead of tree_oids
parent b0b3b4e3
...@@ -12,50 +12,62 @@ ...@@ -12,50 +12,62 @@
#include "config.h" #include "config.h"
#include "iterator.h" #include "iterator.h"
static int find_subtree(git_tree **subtree, const git_oid *root, static int find_subtree_in_current_level(git_tree **out, git_repository *repo, git_tree *parent, const char *annotated_object_sha, int fanout)
git_repository *repo, const char *target, int *fanout)
{ {
int error;
unsigned int i; unsigned int i;
git_tree *tree;
const git_tree_entry *entry; const git_tree_entry *entry;
*subtree = NULL; *out = NULL;
error = git_tree_lookup(&tree, repo, root); if (parent == NULL)
if (error < 0) return GIT_ENOTFOUND;
return error;
for (i=0; i<git_tree_entrycount(tree); i++) { for (i = 0; i < git_tree_entrycount(parent); i++) {
entry = git_tree_entry_byindex(tree, i); entry = git_tree_entry_byindex(parent, i);
if (!git__ishex(git_tree_entry_name(entry))) if (!git__ishex(git_tree_entry_name(entry)))
continue; 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_attributes(entry))
&& strlen(git_tree_entry_name(entry)) == 2 && 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; return GIT_ENOTFOUND;
git_oid_cpy(&subtree_sha, git_tree_entry_id(entry)); }
git_tree_free(tree);
*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, *out = NULL;
target, fanout);
} error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
if (error == GIT_EEXISTS)
{
error = git_tree_lookup(out, repo, git_object_id((const git_object *)root));
goto cleanup;
} }
*subtree = tree; if (error < 0)
return 0; goto cleanup;
*fanout += 2;
error = find_subtree_r(out, subtree, repo, target, fanout);
// *root is not ours to free, and the last subtree is the one being returned => we only need to free the subtrees in-between
if (*out != subtree)
git_tree_free(subtree);
cleanup:
return error;
} }
static int find_blob(git_oid *blob, git_tree *tree, const char *target) static int find_blob(git_oid *blob, git_tree *tree, const char *target)
...@@ -76,191 +88,201 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target) ...@@ -76,191 +88,201 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target)
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
} }
static int note_write(git_oid *out, git_repository *repo, 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)
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)
{ {
int error, fanout = 0; int error;
git_oid oid; git_treebuilder *tb = NULL;
git_tree *tree = NULL;
git_tree_entry *entry; git_tree_entry *entry;
git_treebuilder *tb; git_oid tree_oid;
/* 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;
}
}
/* 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); if (object_oid) {
git_tree_free(tree); 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) if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0)
return error; goto cleanup;
if (!tree_sha) error = git_tree_lookup(out, repo, &tree_oid);
/* no notes tree yet - create fanout */
fanout += 2;
/* create note object */ cleanup:
error = git_blob_create_frombuffer(&oid, repo, note, strlen(note)); git_treebuilder_free(tb);
if (error < 0) { return error;
git_treebuilder_free(tb); }
return error;
}
error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); 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,
if (error < 0) { 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),
/* libgit2 doesn't support object removal (gc) yet */ 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))
/* we leave an orphaned blob object behind - TODO */ {
int error = -1;
git_tree *subtree = NULL;
char subtree_name[3];
git_treebuilder_free(tb); error = find_subtree_in_current_level(&subtree, repo, parent, annotated_object_sha, fanout);
return error;
}
if (out) if (error == GIT_EEXISTS) {
git_oid_cpy(out, git_tree_entry_id(entry)); error = note_exists_cb(out, repo, parent, note_oid, annotated_object_sha, fanout, error);
goto cleanup;
}
error = git_treebuilder_write(&oid, repo, tb); if (error == GIT_ENOTFOUND) {
git_treebuilder_free(tb); error = note_notfound_cb(out, repo, parent, note_oid, annotated_object_sha, fanout, error);
goto cleanup;
}
if (error < 0) if (error < 0)
return 0; goto cleanup;
if (!tree_sha) { /* An existing fanout has been found, let's dig deeper */
/* create fanout subtree */ error = manipulate_note_in_tree_r(out, repo, subtree, note_oid, annotated_object_sha, fanout + 2, note_exists_cb, note_notfound_cb);
if (error < 0)
goto cleanup;
char subtree[3]; strncpy(subtree_name, annotated_object_sha + fanout, 2);
strncpy(subtree, target, 2); subtree_name[2] = '\0';
subtree[2] = '\0';
error = git_treebuilder_create(&tb, NULL); error = tree_write(out, repo, parent, git_object_id((const git_object *)(*out)), subtree_name, 0040000);
if (error < 0)
return error;
error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); cleanup:
if (error < 0) { git_tree_free(subtree);
git_treebuilder_free(tb); return error;
return error; }
}
error = git_treebuilder_write(&oid, repo, tb); 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);
git_treebuilder_free(tb); return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
}
if (error < 0) 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) {
return error; GIT_UNUSED(out);
} GIT_UNUSED(repo);
GIT_UNUSED(parent);
GIT_UNUSED(note_oid);
GIT_UNUSED(fanout);
/* create new notes commit */ giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
return current_error;
}
error = git_tree_lookup(&tree, repo, &oid); 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) {
if (error < 0) GIT_UNUSED(out);
return error; GIT_UNUSED(repo);
GIT_UNUSED(parent);
GIT_UNUSED(note_oid);
GIT_UNUSED(fanout);
error = git_commit_create(&oid, repo, notes_ref, author, committer, giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
NULL, GIT_NOTES_DEFAULT_MSG_ADD, return current_error;
tree, nparents, (const git_commit **) parents); }
git_tree_free(tree); 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);
return error; /* No existing fanout at this level, insert in place */
return tree_write(out, repo, parent, note_oid, annotated_object_sha + fanout, 0100644);
} }
static int note_lookup(git_note **out, git_repository *repo, static int note_write(git_oid *out, git_repository *repo,
const git_oid *tree_sha, const char *target) git_signature *author, git_signature *committer,
const char *notes_ref, const char *note,
git_tree *commit_tree, const char *target,
git_commit **parents)
{ {
int error, fanout = 0; int error;
git_oid oid; git_oid oid;
git_blob *blob; git_tree *tree = NULL;
git_tree *tree;
git_note *note; // TODO: should we apply filters?
/* create note object */
if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
goto cleanup;
error = find_subtree(&tree, tree_sha, repo, target, &fanout); if ((error = manipulate_note_in_tree_r(&tree, repo, commit_tree, &oid, target, 0, insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0)
if (error < 0) goto cleanup;
return error;
error = find_blob(&oid, tree, target + fanout); 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); git_tree_free(tree);
if (error < 0) return error;
return error; }
error = git_blob_lookup(&blob, repo, &oid); static int note_new(git_note **out, git_oid *note_oid, git_blob *blob)
if (error < 0) {
return error; git_note *note = NULL;
note = git__malloc(sizeof(git_note)); note = (git_note *)git__malloc(sizeof(git_note));
GITERR_CHECK_ALLOC(note); GITERR_CHECK_ALLOC(note);
git_oid_cpy(&note->oid, &oid); git_oid_cpy(&note->oid, note_oid);
note->message = git__strdup(git_blob_rawcontent(blob)); note->message = git__strdup((char *)git_blob_rawcontent(blob));
GITERR_CHECK_ALLOC(note->message); GITERR_CHECK_ALLOC(note->message);
*out = note; *out = note;
git_blob_free(blob); return 0;
return error;
} }
static int note_remove(git_repository *repo, static int note_lookup(git_note **out, git_repository *repo,
git_signature *author, git_signature *committer, git_tree *tree, const char *target)
const char *notes_ref, const git_oid *tree_sha,
const char *target, int nparents, git_commit **parents)
{ {
int error, fanout = 0; int error, fanout = 0;
git_oid oid; git_oid oid;
git_tree *tree; git_blob *blob = NULL;
git_treebuilder *tb; git_note *note = NULL;
git_tree *subtree = NULL;
error = find_subtree(&tree, tree_sha, repo, target, &fanout); if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
if (error < 0) goto cleanup;
return error;
error = find_blob(&oid, tree, target + fanout); if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
if (!error) goto cleanup;
error = git_treebuilder_create(&tb, tree);
git_tree_free(tree); if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
if (error < 0) goto cleanup;
return error;
error = git_treebuilder_remove(tb, target + fanout); if ((error = note_new(&note, &oid, blob)) < 0)
if (!error) goto cleanup;
error = git_treebuilder_write(&oid, repo, tb);
git_treebuilder_free(tb); *out = note;
if (error < 0)
return error;
/* create new notes commit */ cleanup:
git_tree_free(subtree);
git_blob_free(blob);
return error;
}
error = git_tree_lookup(&tree, repo, &oid); static int note_remove(git_repository *repo,
if (error < 0) git_signature *author, git_signature *committer,
return error; const char *notes_ref, git_tree *tree,
const char *target, git_commit **parents)
{
int error;
git_tree *tree_after_removal = NULL;
git_oid oid;
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;
error = git_commit_create(&oid, repo, notes_ref, author, committer, error = git_commit_create(&oid, repo, notes_ref, author, committer,
NULL, GIT_NOTES_DEFAULT_MSG_RM, NULL, GIT_NOTES_DEFAULT_MSG_RM,
tree, nparents, (const git_commit **) parents); tree_after_removal, *parents == NULL ? 0 : 1, (const git_commit **) parents);
git_tree_free(tree);
cleanup:
git_tree_free(tree_after_removal);
return error; return error;
} }
...@@ -291,48 +313,46 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo) ...@@ -291,48 +313,46 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo)
return note_get_default_ref(notes_ref, 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; int error;
git_commit *commit = NULL;
git_oid oid; git_oid oid;
if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0) if ((error = normalize_namespace(notes_ref, repo)) < 0)
goto cleanup; return error;
if (git_commit_lookup(&commit, repo, &oid) < 0) if ((error = git_reference_name_to_oid(&oid, repo, *notes_ref)) < 0)
goto cleanup; 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: return 0;
git_commit_free(commit);
return error;
} }
int git_note_read(git_note **out, git_repository *repo, int git_note_read(git_note **out, git_repository *repo,
const char *notes_ref, const git_oid *oid) const char *notes_ref, const git_oid *oid)
{ {
int error; int error;
char *target; char *target = NULL;
git_oid sha; git_tree *tree = NULL;
git_commit *commit = NULL;
*out = NULL;
if (normalize_namespace(&notes_ref, repo) < 0)
return -1;
if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0)
return error;
target = git_oid_allocfmt(oid); target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target); GITERR_CHECK_ALLOC(target);
error = note_lookup(out, repo, &sha, target); if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
goto cleanup;
error = note_lookup(out, repo, tree, target);
cleanup:
git__free(target); git__free(target);
git_tree_free(tree);
git_commit_free(commit);
return error; return error;
} }
...@@ -342,44 +362,26 @@ int git_note_create( ...@@ -342,44 +362,26 @@ int git_note_create(
const char *notes_ref, const git_oid *oid, const char *notes_ref, const git_oid *oid,
const char *note) const char *note)
{ {
int error, nparents = 0; int error;
char *target; char *target = NULL;
git_oid sha;
git_commit *commit = NULL; git_commit *commit = NULL;
git_reference *ref; git_tree *tree = NULL;
if (normalize_namespace(&notes_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++;
}
target = git_oid_allocfmt(oid); target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target); GITERR_CHECK_ALLOC(target);
error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
error = note_write(out, repo, author, committer, notes_ref, error = note_write(out, repo, author, committer, notes_ref,
note, nparents ? &sha : NULL, target, note, tree, target, &commit);
nparents, &commit);
cleanup:
git__free(target); git__free(target);
git_commit_free(commit); git_commit_free(commit);
git_tree_free(tree);
return error; return error;
} }
...@@ -388,37 +390,23 @@ int git_note_remove(git_repository *repo, const char *notes_ref, ...@@ -388,37 +390,23 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
const git_oid *oid) const git_oid *oid)
{ {
int error; int error;
char *target; char *target = NULL;
git_oid sha; git_commit *commit = NULL;
git_commit *commit; git_tree *tree = NULL;
git_reference *ref;
if (normalize_namespace(&notes_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));
target = git_oid_allocfmt(oid); target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target); GITERR_CHECK_ALLOC(target);
if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
goto cleanup;
error = note_remove(repo, author, committer, notes_ref, error = note_remove(repo, author, committer, notes_ref,
&sha, target, 1, &commit); tree, target, &commit);
cleanup:
git__free(target); git__free(target);
git_commit_free(commit); git_commit_free(commit);
git_tree_free(tree);
return error; return error;
} }
...@@ -511,18 +499,12 @@ int git_note_foreach( ...@@ -511,18 +499,12 @@ int git_note_foreach(
void *payload) void *payload)
{ {
int error = -1; int error = -1;
git_oid tree_oid;
git_iterator *iter = NULL; git_iterator *iter = NULL;
git_tree *tree = NULL; git_tree *tree = NULL;
git_commit *commit = NULL;
const git_index_entry *item; const git_index_entry *item;
if (normalize_namespace(&notes_ref, repo) < 0) if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
return -1;
if ((error = retrieve_note_tree_oid(&tree_oid, repo, notes_ref)) < 0)
goto cleanup;
if (git_tree_lookup(&tree, repo, &tree_oid) < 0)
goto cleanup; goto cleanup;
if (git_iterator_for_tree(&iter, repo, tree) < 0) if (git_iterator_for_tree(&iter, repo, tree) < 0)
...@@ -544,5 +526,6 @@ int git_note_foreach( ...@@ -544,5 +526,6 @@ int git_note_foreach(
cleanup: cleanup:
git_iterator_free(iter); git_iterator_free(iter);
git_tree_free(tree); git_tree_free(tree);
git_commit_free(commit);
return error; return error;
} }
...@@ -179,3 +179,36 @@ void test_notes_notes__can_correctly_insert_a_note_in_an_existing_fanout(void) ...@@ -179,3 +179,36 @@ void test_notes_notes__can_correctly_insert_a_note_in_an_existing_fanout(void)
git_oid_cpy(&target_oid, &note_oid); git_oid_cpy(&target_oid, &note_oid);
} }
} }
/*
* $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
* 08b041783f40edfe12bb406c9c9a8a040177c125
*/
void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
{
git_oid note_oid, target_oid;
git_note *note;
cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
cl_git_pass(git_oid_fromstr(&note_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
cl_assert(!git_oid_cmp(git_note_oid(note), &note_oid));
git_note_free(note);
}
/*
* $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
* 08b041783f40edfe12bb406c9c9a8a040177c125
*/
void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
{
git_oid target_oid;
git_note *note;
cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
cl_git_fail(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment