Commit 79af0676 by Patrick Steinhardt

repository: do not expose grafting mechanism

Currently, we expose the function `git_repository_shallow_roots` to get
all grafted roots of the repository. This already paints us into a
corner, though, as we certainly need to experiment with some
functionality of the grafting mechanism before we can happily expose
some of its functionality. Most importantly, we need to get right when
to refresh grafts and when not.

Thus, this commit removes the public function with no public
replacement. We should first try and see what usecases people come up
with to e.g. expose the `git_grafts` mechanism directly in the future
or do something different altogether. Instead, we provide an internal
interface to get weak pointers to the grafting structs part of the
repository itself.
parent a4803c3c
......@@ -876,18 +876,6 @@ GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo);
GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo);
/**
* Determine the shallow roots of the repository
*
* The resulting OID array needs to be free'd by calling
* `git_oidarray_free`.
*
* @param out An array of shallow oids.
* @param repo The repository
* @return 0 on success, an error otherwise.
*/
GIT_EXTERN(int) git_repository_shallow_roots(git_oidarray *out, git_repository *repo);
/**
* Retrieve the configured identity to use for reflogs
*
* The memory is owned by the repository and must not be freed by the
......
......@@ -1263,6 +1263,20 @@ int git_repository_set_index(git_repository *repo, git_index *index)
return 0;
}
int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo)
{
assert(out && repo && repo->shallow_grafts);
*out = repo->grafts;
return 0;
}
int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo)
{
assert(out && repo && repo->shallow_grafts);
*out = repo->shallow_grafts;
return 0;
}
int git_repository_set_namespace(git_repository *repo, const char *namespace)
{
git__free(repo->namespace);
......@@ -2923,25 +2937,6 @@ int git_repository_state_cleanup(git_repository *repo)
return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
}
int git_repository_shallow_roots(git_oidarray *out, git_repository *repo)
{
git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
int error;
assert(out && repo);
memset(out, 0, sizeof(*out));
if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0 ||
(error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0)
goto error;
error:
git_buf_dispose(&path);
git_buf_dispose(&contents);
return error;
}
int git_repository_is_shallow(git_repository *repo)
{
git_buf path = GIT_BUF_INIT;
......
......@@ -211,6 +211,8 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo);
int git_repository_odb__weakptr(git_odb **out, git_repository *repo);
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo);
int git_repository_index__weakptr(git_index **out, git_repository *repo);
int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo);
int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo);
/*
* Configuration map cache
......
#include "clar_libgit2.h"
#include "futils.h"
#include "grafts.h"
#include "repository.h"
static git_repository *g_repo;
static git_oid g_shallow_oid;
......@@ -42,61 +45,59 @@ void test_grafts_shallow__clears_errors(void)
void test_grafts_shallow__shallow_oids(void)
{
git_oidarray oids;
g_repo = cl_git_sandbox_init("shallow.git");
git_commit_graft *graft;
git_grafts *grafts;
cl_git_pass(git_repository_shallow_roots(&oids, g_repo));
cl_assert_equal_i(1, oids.count);
cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]);
g_repo = cl_git_sandbox_init("shallow.git");
git_oidarray_free(&oids);
cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
cl_assert_equal_i(1, git_grafts_size(grafts));
cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
}
void test_grafts_shallow__cache_clearing(void)
{
git_oidarray oids;
git_commit_graft *graft;
git_grafts *grafts;
git_oid tmp_oid;
cl_git_pass(git_oid_fromstr(&tmp_oid, "0000000000000000000000000000000000000000"));
g_repo = cl_git_sandbox_init("shallow.git");
cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
cl_git_pass(git_repository_shallow_roots(&oids, g_repo));
cl_assert_equal_i(1, oids.count);
cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]);
git_oidarray_free(&oids);
cl_assert_equal_i(1, git_grafts_size(grafts));
cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
cl_git_mkfile("shallow.git/shallow",
"be3563ae3f795b2b4353bcce3a527ad0a4f7f644\n"
"0000000000000000000000000000000000000000\n"
);
cl_git_pass(git_repository_shallow_roots(&oids, g_repo));
cl_assert_equal_i(2, oids.count);
cl_assert((git_oid_equal(&g_shallow_oid, &oids.ids[0]) &&
git_oid_equal(&tmp_oid, &oids.ids[1])) ||
(git_oid_equal(&g_shallow_oid, &oids.ids[1]) &&
git_oid_equal(&tmp_oid, &oids.ids[0])));
git_oidarray_free(&oids);
cl_git_pass(git_grafts_refresh(grafts));
cl_assert_equal_i(2, git_grafts_size(grafts));
cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
cl_git_pass(git_grafts_get(&graft, grafts, &tmp_oid));
cl_git_pass(p_unlink("shallow.git/shallow"));
cl_git_pass(git_repository_shallow_roots(&oids, g_repo));
cl_assert_equal_i(0, oids.count);
git_oidarray_free(&oids);
cl_git_pass(git_grafts_refresh(grafts));
cl_assert_equal_i(0, git_grafts_size(grafts));
}
void test_grafts_shallow__errors_on_borked(void)
{
git_oidarray oids;
git_grafts *grafts;
g_repo = cl_git_sandbox_init("shallow.git");
cl_git_mkfile("shallow.git/shallow", "lolno");
cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo));
cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
cl_git_fail(git_grafts_refresh(grafts));
cl_assert_equal_i(0, git_grafts_size(grafts));
cl_git_mkfile("shallow.git/shallow", "lolno\n");
cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo));
cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
cl_git_fail(git_grafts_refresh(grafts));
cl_assert_equal_i(0, git_grafts_size(grafts));
}
void test_grafts_shallow__revwalk_behavior(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