Hash :
8c8d726e
Author :
Date :
2015-10-21T12:10:30
worktree: implement `git_repository_open_from_worktree` Add function `git_repository_open_from_worktree`, which allows to open a `git_worktree` as repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#include "clar_libgit2.h"
#include "worktree_helpers.h"
#include "repository.h"
#include "worktree.h"
#define COMMON_REPO "testrepo"
#define WORKTREE_REPO "testrepo-worktree"
static worktree_fixture fixture =
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
void test_worktree_worktree__initialize(void)
{
setup_fixture_worktree(&fixture);
}
void test_worktree_worktree__cleanup(void)
{
cleanup_fixture_worktree(&fixture);
}
void test_worktree_worktree__list(void)
{
git_strarray wts;
cl_git_pass(git_worktree_list(&wts, fixture.repo));
cl_assert_equal_i(wts.count, 1);
cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
git_strarray_free(&wts);
}
void test_worktree_worktree__list_with_invalid_worktree_dirs(void)
{
const char *filesets[3][2] = {
{ "gitdir", "commondir" },
{ "gitdir", "HEAD" },
{ "HEAD", "commondir" },
};
git_buf path = GIT_BUF_INIT;
git_strarray wts;
unsigned i, j, len;
cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid",
fixture.repo->commondir));
cl_git_pass(p_mkdir(path.ptr, 0755));
len = path.size;
for (i = 0; i < ARRAY_SIZE(filesets); i++) {
for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
git_buf_truncate(&path, len);
cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
cl_git_pass(p_close(p_creat(path.ptr, 0644)));
}
cl_git_pass(git_worktree_list(&wts, fixture.worktree));
cl_assert_equal_i(wts.count, 1);
cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
git_strarray_free(&wts);
for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
git_buf_truncate(&path, len);
cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
p_unlink(path.ptr);
}
}
git_buf_free(&path);
}
void test_worktree_worktree__list_in_worktree_repo(void)
{
git_strarray wts;
cl_git_pass(git_worktree_list(&wts, fixture.worktree));
cl_assert_equal_i(wts.count, 1);
cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
git_strarray_free(&wts);
}
void test_worktree_worktree__list_bare(void)
{
git_repository *repo;
git_strarray wts;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_worktree_list(&wts, repo));
cl_assert_equal_i(wts.count, 0);
git_repository_free(repo);
}
void test_worktree_worktree__list_without_worktrees(void)
{
git_repository *repo;
git_strarray wts;
repo = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_worktree_list(&wts, repo));
cl_assert_equal_i(wts.count, 0);
git_repository_free(repo);
}
void test_worktree_worktree__lookup(void)
{
git_worktree *wt;
git_buf gitdir_path = GIT_BUF_INIT;
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
git_buf_printf(&gitdir_path, "%s/worktrees/%s", fixture.repo->commondir, "testrepo-worktree");
cl_assert_equal_s(wt->gitdir_path, gitdir_path.ptr);
cl_assert_equal_s(wt->parent_path, fixture.repo->path_repository);
cl_assert_equal_s(wt->gitlink_path, fixture.worktree->path_gitlink);
cl_assert_equal_s(wt->commondir_path, fixture.repo->commondir);
git_buf_free(&gitdir_path);
git_worktree_free(wt);
}
void test_worktree_worktree__lookup_nonexistent_worktree(void)
{
git_worktree *wt;
cl_git_fail(git_worktree_lookup(&wt, fixture.repo, "nonexistent"));
cl_assert_equal_p(wt, NULL);
}
void test_worktree_worktree__open(void)
{
git_worktree *wt;
git_repository *repo;
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_git_pass(git_repository_open_from_worktree(&repo, wt));
cl_assert_equal_s(git_repository_workdir(repo),
git_repository_workdir(fixture.worktree));
git_repository_free(repo);
git_worktree_free(wt);
}
void test_worktree_worktree__open_invalid_commondir(void)
{
git_worktree *wt;
git_repository *repo;
git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/commondir"));
cl_git_pass(git_buf_printf(&path,
"%s/worktrees/testrepo-worktree/commondir",
fixture.repo->commondir));
cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_git_fail(git_repository_open_from_worktree(&repo, wt));
git_buf_free(&buf);
git_buf_free(&path);
git_worktree_free(wt);
}
void test_worktree_worktree__open_invalid_gitdir(void)
{
git_worktree *wt;
git_repository *repo;
git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
cl_git_pass(git_buf_printf(&path,
"%s/worktrees/testrepo-worktree/gitdir",
fixture.repo->commondir));
cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_git_fail(git_repository_open_from_worktree(&repo, wt));
git_buf_free(&buf);
git_buf_free(&path);
git_worktree_free(wt);
}
void test_worktree_worktree__open_invalid_parent(void)
{
git_worktree *wt;
git_repository *repo;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
cl_git_pass(git_futils_writebuffer(&buf,
fixture.worktree->path_gitlink, O_RDWR, 0644));
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_git_fail(git_repository_open_from_worktree(&repo, wt));
git_buf_free(&buf);
git_worktree_free(wt);
}