1 #include "clar_libgit2.h"
5 #include "worktree_helpers.h"
7 #define COMMON_REPO "testrepo"
8 #define WORKTREE_REPO "testrepo-worktree"
10 static worktree_fixture fixture
=
11 WORKTREE_FIXTURE_INIT(COMMON_REPO
, WORKTREE_REPO
);
13 void test_worktree_refs__initialize(void)
15 setup_fixture_worktree(&fixture
);
18 void test_worktree_refs__cleanup(void)
20 cleanup_fixture_worktree(&fixture
);
23 void test_worktree_refs__list(void)
25 git_strarray refs
, wtrefs
;
29 cl_git_pass(git_reference_list(&refs
, fixture
.repo
));
30 cl_git_pass(git_reference_list(&wtrefs
, fixture
.worktree
));
32 if (refs
.count
!= wtrefs
.count
)
38 for (i
= 0; i
< refs
.count
; i
++)
42 for (j
= 0; j
< wtrefs
.count
; j
++)
44 if (!strcmp(refs
.strings
[i
], wtrefs
.strings
[j
]))
59 git_strarray_free(&refs
);
60 git_strarray_free(&wtrefs
);
64 void test_worktree_refs__read_head(void)
68 cl_git_pass(git_repository_head(&head
, fixture
.worktree
));
70 git_reference_free(head
);
73 void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void)
77 cl_git_pass(git_repository_head(&head
, fixture
.repo
));
78 cl_git_fail(git_repository_set_head(fixture
.worktree
, git_reference_name(head
)));
80 git_reference_free(head
);
83 void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void)
87 cl_git_pass(git_repository_head(&head
, fixture
.worktree
));
88 cl_git_fail(git_repository_set_head(fixture
.repo
, git_reference_name(head
)));
90 git_reference_free(head
);
93 void test_worktree_refs__set_head_works_for_current_HEAD(void)
97 cl_git_pass(git_repository_head(&head
, fixture
.repo
));
98 cl_git_pass(git_repository_set_head(fixture
.repo
, git_reference_name(head
)));
100 git_reference_free(head
);
103 void test_worktree_refs__set_head_fails_when_already_checked_out(void)
105 cl_git_fail(git_repository_set_head(fixture
.repo
, "refs/heads/testrepo-worktree"));
108 void test_worktree_refs__delete_fails_for_checked_out_branch(void)
110 git_reference
*branch
;
112 cl_git_pass(git_branch_lookup(&branch
, fixture
.repo
,
113 "testrepo-worktree", GIT_BRANCH_LOCAL
));
114 cl_git_fail(git_branch_delete(branch
));
116 git_reference_free(branch
);
119 void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
121 git_worktree_prune_options opts
= GIT_WORKTREE_PRUNE_OPTIONS_INIT
;
122 git_reference
*branch
;
123 git_worktree
*worktree
;
125 opts
.flags
= GIT_WORKTREE_PRUNE_VALID
;
127 cl_git_pass(git_worktree_lookup(&worktree
, fixture
.repo
, fixture
.worktreename
));
128 cl_git_pass(git_worktree_prune(worktree
, &opts
));
129 git_worktree_free(worktree
);
131 cl_git_pass(git_branch_lookup(&branch
, fixture
.repo
,
132 "testrepo-worktree", GIT_BRANCH_LOCAL
));
133 cl_git_pass(git_branch_delete(branch
));
134 git_reference_free(branch
);
137 void test_worktree_refs__delete_unrelated_branch_on_worktree(void)
139 git_reference
*branch
;
141 cl_git_pass(git_branch_lookup(&branch
, fixture
.worktree
,
142 "merge-conflict", GIT_BRANCH_LOCAL
));
143 cl_git_pass(git_branch_delete(branch
));
145 git_reference_free(branch
);
148 void test_worktree_refs__delete_unrelated_branch_on_parent(void)
150 git_reference
*branch
;
152 cl_git_pass(git_branch_lookup(&branch
, fixture
.repo
,
153 "merge-conflict", GIT_BRANCH_LOCAL
));
154 cl_git_pass(git_branch_delete(branch
));
156 git_reference_free(branch
);
159 void test_worktree_refs__renaming_reference_updates_worktree_heads(void)
161 git_reference
*head
, *branch
, *renamed
;
163 cl_git_pass(git_branch_lookup(&branch
, fixture
.repo
,
164 "testrepo-worktree", GIT_BRANCH_LOCAL
));
165 cl_git_pass(git_reference_rename(&renamed
, branch
, "refs/heads/renamed", 0, NULL
));
166 cl_git_pass(git_repository_head(&head
, fixture
.worktree
));
168 git_reference_free(head
);
169 git_reference_free(branch
);
170 git_reference_free(renamed
);
173 void test_worktree_refs__creating_refs_uses_commondir(void)
175 git_reference
*head
, *branch
, *lookup
;
177 git_buf refpath
= GIT_BUF_INIT
;
179 cl_git_pass(git_buf_joinpath(&refpath
,
180 git_repository_commondir(fixture
.worktree
), "refs/heads/testbranch"));
181 cl_assert(!git_path_exists(refpath
.ptr
));
183 cl_git_pass(git_repository_head(&head
, fixture
.worktree
));
184 cl_git_pass(git_commit_lookup(&commit
, fixture
.worktree
, git_reference_target(head
)));
185 cl_git_pass(git_branch_create(&branch
, fixture
.worktree
, "testbranch", commit
, 0));
186 cl_git_pass(git_branch_lookup(&lookup
, fixture
.worktree
, "testbranch", GIT_BRANCH_LOCAL
));
187 cl_assert(git_reference_cmp(branch
, lookup
) == 0);
188 cl_assert(git_path_exists(refpath
.ptr
));
190 git_reference_free(lookup
);
191 git_reference_free(branch
);
192 git_reference_free(head
);
193 git_commit_free(commit
);
194 git_buf_dispose(&refpath
);