Commit 22f201b1 by Patrick Steinhardt

grafts: make the structure self-contained and opaque

In order to increase maintainability in the future, we should try to
make structures as self-contained and opaque to its users as possible.
Thus it is probably not a good idea to just typedef `git_graftmap` to
`git_oidmap`, as that will make it a lot harder in the future to extend
the API in the future, if this need ever arises.

Refactor the code to instead declare a real structure `git_grafts`,
which is completely opaque to its callers.
parent d54c0081
......@@ -22,7 +22,7 @@
#include "object.h"
#include "array.h"
#include "oidarray.h"
#include "graft.h"
#include "grafts.h"
void git_commit__free(void *_commit)
{
......@@ -504,7 +504,7 @@ int git_commit__parse_ext(git_commit *commit, git_odb_object *odb_obj, unsigned
return error;
/* Perform necessary grafts */
if (git__graft_for_oid(&graft, repo->grafts, git_odb_object_id(odb_obj)) != GIT_ENOTFOUND) {
if (git_grafts_get(&graft, repo->grafts, git_odb_object_id(odb_obj)) != GIT_ENOTFOUND) {
size_t idx;
git_oid *oid;
git_array_clear(commit->parent_ids);
......
......@@ -5,9 +5,53 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "graft.h"
#include "grafts.h"
int git__graft_register(git_graftmap *grafts, const git_oid *oid, git_array_oid_t parents)
struct git_grafts {
/* Map of `git_commit_graft`s */
git_oidmap *commits;
};
int git_grafts_new(git_grafts **out)
{
git_grafts *grafts;
grafts = git__calloc(1, sizeof(*grafts));
GIT_ERROR_CHECK_ALLOC(grafts);
if ((git_oidmap_new(&grafts->commits)) < 0) {
git__free(grafts);
return -1;
}
*out = grafts;
return 0;
}
void git_grafts_free(git_grafts *grafts)
{
if (!grafts)
return;
git_grafts_clear(grafts);
git_oidmap_free(grafts->commits);
git__free(grafts);
}
void git_grafts_clear(git_grafts *grafts)
{
git_commit_graft *graft;
assert(grafts);
git_oidmap_foreach_value(grafts->commits, graft, {
git__free(graft->parents.ptr);
git__free(graft);
});
git_oidmap_clear(grafts->commits);
}
int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents)
{
git_commit_graft *graft;
git_oid *parent_oid;
......@@ -28,7 +72,7 @@ int git__graft_register(git_graftmap *grafts, const git_oid *oid, git_array_oid_
}
git_oid_cpy(&graft->oid, oid);
if ((error = git_oidmap_set(grafts, &graft->oid, graft)) < 0)
if ((error = git_oidmap_set(grafts->commits, &graft->oid, graft)) < 0)
goto cleanup;
return 0;
......@@ -39,17 +83,17 @@ cleanup:
return error;
}
int git__graft_unregister(git_graftmap *grafts, const git_oid *oid)
int git_grafts_remove(git_grafts *grafts, const git_oid *oid)
{
git_commit_graft *graft;
int error;
assert(grafts && oid);
if ((graft = git_oidmap_get(grafts, oid)) == NULL)
if ((graft = git_oidmap_get(grafts->commits, oid)) == NULL)
return GIT_ENOTFOUND;
if ((error = git_oidmap_delete(grafts, oid)) < 0)
if ((error = git_oidmap_delete(grafts->commits, oid)) < 0)
return error;
git__free(graft);
......@@ -57,24 +101,15 @@ int git__graft_unregister(git_graftmap *grafts, const git_oid *oid)
return 0;
}
void git__graft_clear(git_graftmap *grafts)
{
git_commit_graft *graft;
assert(grafts);
git_oidmap_foreach_value(grafts, graft, {
git__free(graft->parents.ptr);
git__free(graft);
});
git_oidmap_clear(grafts);
}
int git__graft_for_oid(git_commit_graft **out, git_graftmap *grafts, const git_oid *oid)
int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid)
{
assert(out && grafts && oid);
if ((*out = git_oidmap_get(grafts, oid)) == NULL)
if ((*out = git_oidmap_get(grafts->commits, oid)) == NULL)
return GIT_ENOTFOUND;
return 0;
}
size_t git_grafts_size(git_grafts *grafts)
{
return git_oidmap_size(grafts->commits);
}
......@@ -17,13 +17,15 @@ typedef struct {
git_array_oid_t parents;
} git_commit_graft;
/* A special type of git_oidmap with git_commit_grafts as values */
typedef git_oidmap git_graftmap;
typedef struct git_grafts git_grafts;
int git__graft_register(git_graftmap *grafts, const git_oid *oid, git_array_oid_t parents);
int git__graft_unregister(git_graftmap *grafts, const git_oid *oid);
void git__graft_clear(git_graftmap *grafts);
int git_grafts_new(git_grafts **out);
void git_grafts_free(git_grafts *grafts);
void git_grafts_clear(git_grafts *grafts);
int git__graft_for_oid(git_commit_graft **out, git_graftmap *grafts, const git_oid *oid);
int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents);
int git_grafts_remove(git_grafts *grafts, const git_oid *oid);
int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid);
size_t git_grafts_size(git_grafts *grafts);
#endif
......@@ -147,10 +147,7 @@ int git_repository__cleanup(git_repository *repo)
git_repository_submodule_cache_clear(repo);
git_cache_clear(&repo->objects);
git_attr_cache_flush(repo);
git__graft_clear(repo->grafts);
git_oidmap_free(repo->grafts);
git_grafts_free(repo->grafts);
git_array_clear(repo->shallow_oids);
set_config(repo, NULL);
......@@ -259,7 +256,7 @@ static git_repository *repository_alloc(void)
/* set all the entries in the configmap cache to `unset` */
git_repository__configmap_lookup_cache_clear(repo);
if (git_oidmap_new(&repo->grafts) < 0)
if (git_grafts_new(&repo->grafts) < 0)
goto on_error;
return repo;
......@@ -610,9 +607,8 @@ static int load_grafts(git_repository *repo)
if (error < 0)
goto cleanup;
if (updated) {
git__graft_clear(repo->grafts);
}
if (updated)
git_grafts_clear(repo->grafts);
dup_contents.ptr = contents.ptr;
git_buf_foreach_line(line_start, line_end, line_num, &dup_contents) {
......@@ -638,7 +634,7 @@ static int load_grafts(git_repository *repo)
}
}
if (git__graft_register(repo->grafts, &graft_oid, parents) < 0) {
if (git_grafts_add(repo->grafts, &graft_oid, parents) < 0) {
git_error_set(GIT_ERROR_REPOSITORY, "Invalid graft at line %d", line_num);
error = -1;
goto cleanup;
......@@ -668,9 +664,8 @@ static int load_shallow(git_repository *repo)
}
git_array_foreach(roots, i, graft_oid) {
if ((error = git__graft_register(repo->grafts, graft_oid, parents)) < 0) {
if ((error = git_grafts_add(repo->grafts, graft_oid, parents)) < 0)
return error;
}
}
return 0;
}
......
......@@ -24,7 +24,7 @@
#include "attrcache.h"
#include "submodule.h"
#include "diff_driver.h"
#include "graft.h"
#include "grafts.h"
#include "oidarray.h"
#define DOT_GIT ".git"
......@@ -154,7 +154,7 @@ struct git_repository {
unsigned int lru_counter;
git_graftmap *grafts;
git_grafts *grafts;
git_oid graft_checksum;
git_oid shallow_checksum;
......
#include "clar_libgit2.h"
#include "futils.h"
#include "graft.h"
#include "grafts.h"
static git_repository *g_repo;
......@@ -17,29 +17,28 @@ void test_grafts_basic__cleanup(void)
void test_grafts_basic__graft_add(void)
{
git_array_oid_t parents = GIT_ARRAY_INIT;
git_oid oid_src, *oid1;
git_commit_graft *graft;
git_graftmap *grafts;
git_array_oid_t parents = GIT_ARRAY_INIT;
git_grafts *grafts;
cl_git_pass(git_oidmap_new(&grafts));
cl_git_pass(git_grafts_new(&grafts));
cl_assert(oid1 = git_array_alloc(parents));
cl_git_pass(git_oid_fromstr(&oid_src, "2f3053cbff8a4ca2f0666de364ddb734a28a31a9"));
git_oid_cpy(oid1, &oid_src);
git_oid_fromstr(&oid_src, "f503807ffa920e407a600cfaee96b7152259acc7");
cl_git_pass(git__graft_register(grafts, &oid_src, parents));
cl_git_pass(git_grafts_add(grafts, &oid_src, parents));
git_array_clear(parents);
cl_assert_equal_i(1, git_oidmap_size(grafts));
cl_git_pass(git__graft_for_oid(&graft, grafts, &oid_src));
cl_assert_equal_i(1, git_grafts_size(grafts));
cl_git_pass(git_grafts_get(&graft, grafts, &oid_src));
cl_assert_equal_s("f503807ffa920e407a600cfaee96b7152259acc7", git_oid_tostr_s(&graft->oid));
cl_assert_equal_i(1, git_array_size(graft->parents));
cl_assert_equal_s("2f3053cbff8a4ca2f0666de364ddb734a28a31a9", git_oid_tostr_s(git_array_get(graft->parents, 0)));
git__graft_clear(grafts);
git_oidmap_free(grafts);
git_grafts_free(grafts);
}
void test_grafts_basic__grafted_revwalk(void)
......
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