summaryrefslogtreecommitdiffstats
path: root/tests/libgit2/worktree
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/libgit2/worktree/config.c81
-rw-r--r--tests/libgit2/worktree/refs.c62
-rw-r--r--tests/libgit2/worktree/worktree.c57
3 files changed, 195 insertions, 5 deletions
diff --git a/tests/libgit2/worktree/config.c b/tests/libgit2/worktree/config.c
index 81dcfe1..1fd1f75 100644
--- a/tests/libgit2/worktree/config.c
+++ b/tests/libgit2/worktree/config.c
@@ -6,15 +6,19 @@
static worktree_fixture fixture =
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+static worktree_fixture submodule =
+ WORKTREE_FIXTURE_INIT("submodules", "submodules-worktree-parent");
void test_worktree_config__initialize(void)
{
setup_fixture_worktree(&fixture);
+ setup_fixture_worktree(&submodule);
}
void test_worktree_config__cleanup(void)
{
cleanup_fixture_worktree(&fixture);
+ cleanup_fixture_worktree(&submodule);
}
void test_worktree_config__open(void)
@@ -27,7 +31,7 @@ void test_worktree_config__open(void)
git_config_free(cfg);
}
-void test_worktree_config__set(void)
+void test_worktree_config__set_level_local(void)
{
git_config *cfg;
int32_t val;
@@ -45,3 +49,78 @@ void test_worktree_config__set(void)
cl_assert_equal_i(val, 5);
git_config_free(cfg);
}
+
+void test_worktree_config__requires_extension(void)
+{
+ git_config *cfg;
+ git_config *wtcfg;
+ int extension = 0;
+
+ /*
+ * the "submodules" repo does not have extensions.worktreeconfig
+ * set, the worktree configuration should not be available.
+ */
+ cl_git_pass(git_repository_config(&cfg, submodule.repo));
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_get_bool(&extension, cfg, "extensions.worktreeconfig"));
+ cl_assert_equal_i(0, extension);
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_open_level(&wtcfg, cfg, GIT_CONFIG_LEVEL_WORKTREE));
+ git_config_free(cfg);
+
+ /* the "testrepo" repo does have the configuration set. */
+ cl_git_pass(git_repository_config(&cfg, fixture.repo));
+ cl_git_pass(git_config_get_bool(&extension, cfg, "extensions.worktreeconfig"));
+ cl_assert_equal_i(1, extension);
+ cl_git_pass(git_config_open_level(&wtcfg, cfg, GIT_CONFIG_LEVEL_WORKTREE));
+ git_config_free(wtcfg);
+ git_config_free(cfg);
+}
+
+void test_worktree_config__exists(void)
+{
+ git_config *cfg, *wtcfg, *snap;
+ const char *str;
+
+ cl_git_pass(git_repository_config(&cfg, fixture.repo));
+ cl_git_pass(git_repository_config(&wtcfg, fixture.worktree));
+
+ cl_git_pass(git_config_snapshot(&snap, cfg));
+ cl_git_pass(git_config_get_string(&str, snap, "worktreetest.config"));
+ cl_assert_equal_s("mainrepo", str);
+ git_config_free(snap);
+
+ cl_git_pass(git_config_snapshot(&snap, wtcfg));
+ cl_git_pass(git_config_get_string(&str, snap, "worktreetest.config"));
+ cl_assert_equal_s("worktreerepo", str);
+ git_config_free(snap);
+
+ git_config_free(cfg);
+ git_config_free(wtcfg);
+}
+
+void test_worktree_config__set_level_worktree(void)
+{
+ git_config *cfg;
+ git_config *wtcfg;
+ int32_t val;
+
+ cl_git_pass(git_repository_config(&cfg, fixture.repo));
+ cl_git_pass(git_config_open_level(&wtcfg, cfg, GIT_CONFIG_LEVEL_WORKTREE));
+ cl_git_pass(git_config_set_int32(wtcfg, "worktree.specific", 42));
+
+ cl_git_pass(git_config_get_int32(&val, cfg, "worktree.specific"));
+ cl_assert_equal_i(val, 42);
+
+ /* reopen to verify config has been set */
+ git_config_free(cfg);
+ cl_git_pass(git_repository_config(&cfg, fixture.repo));
+ cl_git_pass(git_config_get_int32(&val, cfg, "worktree.specific"));
+ cl_assert_equal_i(val, 42);
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_delete_entry(cfg, "worktree.specific"));
+
+ cl_git_pass(git_config_delete_entry(wtcfg, "worktree.specific"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_get_int32(&val, cfg, "worktree.specific"));
+
+ git_config_free(cfg);
+ git_config_free(wtcfg);
+}
diff --git a/tests/libgit2/worktree/refs.c b/tests/libgit2/worktree/refs.c
index 557726a..51e7b2b 100644
--- a/tests/libgit2/worktree/refs.c
+++ b/tests/libgit2/worktree/refs.c
@@ -20,7 +20,7 @@ void test_worktree_refs__cleanup(void)
cleanup_fixture_worktree(&fixture);
}
-void test_worktree_refs__list(void)
+void test_worktree_refs__list_no_difference_in_worktree(void)
{
git_strarray refs, wtrefs;
unsigned i, j;
@@ -61,6 +61,66 @@ exit:
cl_git_pass(error);
}
+void test_worktree_refs__list_worktree_specific(void)
+{
+ git_strarray refs, wtrefs;
+ git_reference *ref, *new_branch;
+ int error = 0;
+ git_oid oid;
+
+ cl_git_pass(git_reference_name_to_id(&oid, fixture.repo, "refs/heads/dir"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, fixture.repo, "refs/bisect/a-bisect-ref"));
+ cl_git_pass(git_reference_create(
+ &new_branch, fixture.worktree, "refs/bisect/a-bisect-ref", &oid,
+ 0, "test"));
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, fixture.repo, "refs/bisect/a-bisect-ref"));
+ cl_git_pass(git_reference_lookup(&ref, fixture.worktree, "refs/bisect/a-bisect-ref"));
+
+ cl_git_pass(git_reference_list(&refs, fixture.repo));
+ cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));
+
+ cl_assert_equal_sz(wtrefs.count, refs.count + 1);
+
+ git_reference_free(ref);
+ git_reference_free(new_branch);
+ git_strarray_dispose(&refs);
+ git_strarray_dispose(&wtrefs);
+ cl_git_pass(error);
+}
+
+void test_worktree_refs__list_worktree_specific_hidden_in_main_repo(void)
+{
+ git_strarray refs, wtrefs;
+ git_reference *ref, *new_branch;
+ int error = 0;
+ git_oid oid;
+
+ cl_git_pass(
+ git_reference_name_to_id(&oid, fixture.repo, "refs/heads/dir"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(
+ &ref, fixture.worktree, "refs/bisect/a-bisect-ref"));
+ cl_git_pass(git_reference_create(
+ &new_branch, fixture.repo, "refs/bisect/a-bisect-ref", &oid,
+ 0, "test"));
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(
+ &ref, fixture.worktree, "refs/bisect/a-bisect-ref"));
+ cl_git_pass(git_reference_lookup(
+ &ref, fixture.repo, "refs/bisect/a-bisect-ref"));
+
+ cl_git_pass(git_reference_list(&refs, fixture.repo));
+ cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));
+
+ cl_assert_equal_sz(refs.count, wtrefs.count + 1);
+
+ git_reference_free(ref);
+ git_reference_free(new_branch);
+ git_strarray_dispose(&refs);
+ git_strarray_dispose(&wtrefs);
+ cl_git_pass(error);
+}
+
void test_worktree_refs__read_head(void)
{
git_reference *head;
diff --git a/tests/libgit2/worktree/worktree.c b/tests/libgit2/worktree/worktree.c
index fed5c92..00e3e3f 100644
--- a/tests/libgit2/worktree/worktree.c
+++ b/tests/libgit2/worktree/worktree.c
@@ -217,6 +217,50 @@ void test_worktree_worktree__init(void)
git_repository_free(repo);
}
+void test_worktree_worktree__add_remove_add(void)
+{
+ git_worktree_add_options add_opts = GIT_WORKTREE_ADD_OPTIONS_INIT;
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
+ git_str path = GIT_BUF_INIT;
+ git_reference *branch;
+ git_repository *repo;
+ git_worktree *wt;
+
+ /* Add the worktree */
+ cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-add-remove-add"));
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-add-remove-add", path.ptr, NULL));
+
+ /* Open and verify created repo */
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-add-remove-add/") == 0);
+ cl_git_pass(git_branch_lookup(&branch, repo, "worktree-add-remove-add", GIT_BRANCH_LOCAL));
+ git_reference_free(branch);
+ git_repository_free(repo);
+
+ /* Prune the worktree */
+ opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_WORKING_TREE;
+ cl_git_pass(git_worktree_prune(wt, &opts));
+ cl_assert(!git_fs_path_exists(wt->gitdir_path));
+ cl_assert(!git_fs_path_exists(wt->gitlink_path));
+ git_worktree_free(wt);
+
+ /* Add the worktree back with default options should fail. */
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-add-remove-add", path.ptr, &add_opts));
+ /* If allowing checkout of existing branches, it should succeed. */
+ add_opts.checkout_existing = 1;
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-add-remove-add", path.ptr, &add_opts));
+
+ /* Open and verify created repo */
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-add-remove-add/") == 0);
+ cl_git_pass(git_branch_lookup(&branch, repo, "worktree-add-remove-add", GIT_BRANCH_LOCAL));
+ git_reference_free(branch);
+ git_repository_free(repo);
+
+ git_str_dispose(&path);
+ git_worktree_free(wt);
+}
+
void test_worktree_worktree__add_locked(void)
{
git_worktree *wt;
@@ -244,6 +288,7 @@ void test_worktree_worktree__add_locked(void)
void test_worktree_worktree__init_existing_branch(void)
{
+ git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT;
git_reference *head, *branch;
git_commit *commit;
git_worktree *wt;
@@ -251,12 +296,18 @@ void test_worktree_worktree__init_existing_branch(void)
cl_git_pass(git_repository_head(&head, fixture.repo));
cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid));
- cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new", commit, false));
+ cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new-exist", commit, false));
- cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
- cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
+ cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-new-exist"));
+
+ /* Add the worktree back with default options should fail. */
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new-exist", path.ptr, NULL));
+ /* If allowing checkout of existing branches, it should succeed. */
+ opts.checkout_existing = 1;
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new-exist", path.ptr, &opts));
git_str_dispose(&path);
+ git_worktree_free(wt);
git_commit_free(commit);
git_reference_free(head);
git_reference_free(branch);