]> git.proxmox.com Git - libgit2.git/blob - tests/worktree/refs.c
e62149d2e03d5ae82fc9a42ab4c9aebcadbe7d86
[libgit2.git] / tests / worktree / refs.c
1 #include "clar_libgit2.h"
2 #include "path.h"
3 #include "refs.h"
4 #include "worktree.h"
5 #include "worktree_helpers.h"
6
7 #define COMMON_REPO "testrepo"
8 #define WORKTREE_REPO "testrepo-worktree"
9
10 static worktree_fixture fixture =
11 WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
12
13 void test_worktree_refs__initialize(void)
14 {
15 setup_fixture_worktree(&fixture);
16 }
17
18 void test_worktree_refs__cleanup(void)
19 {
20 cleanup_fixture_worktree(&fixture);
21 }
22
23 void test_worktree_refs__list(void)
24 {
25 git_strarray refs, wtrefs;
26 unsigned i, j;
27 int error = 0;
28
29 cl_git_pass(git_reference_list(&refs, fixture.repo));
30 cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));
31
32 if (refs.count != wtrefs.count)
33 {
34 error = GIT_ERROR;
35 goto exit;
36 }
37
38 for (i = 0; i < refs.count; i++)
39 {
40 int found = 0;
41
42 for (j = 0; j < wtrefs.count; j++)
43 {
44 if (!strcmp(refs.strings[i], wtrefs.strings[j]))
45 {
46 found = 1;
47 break;
48 }
49 }
50
51 if (!found)
52 {
53 error = GIT_ERROR;
54 goto exit;
55 }
56 }
57
58 exit:
59 git_strarray_free(&refs);
60 git_strarray_free(&wtrefs);
61 cl_git_pass(error);
62 }
63
64 void test_worktree_refs__read_head(void)
65 {
66 git_reference *head;
67
68 cl_git_pass(git_repository_head(&head, fixture.worktree));
69
70 git_reference_free(head);
71 }
72
73 void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void)
74 {
75 git_reference *head;
76
77 cl_git_pass(git_repository_head(&head, fixture.repo));
78 cl_git_fail(git_repository_set_head(fixture.worktree, git_reference_name(head)));
79
80 git_reference_free(head);
81 }
82
83 void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void)
84 {
85 git_reference *head;
86
87 cl_git_pass(git_repository_head(&head, fixture.worktree));
88 cl_git_fail(git_repository_set_head(fixture.repo, git_reference_name(head)));
89
90 git_reference_free(head);
91 }
92
93 void test_worktree_refs__set_head_works_for_current_HEAD(void)
94 {
95 git_reference *head;
96
97 cl_git_pass(git_repository_head(&head, fixture.repo));
98 cl_git_pass(git_repository_set_head(fixture.repo, git_reference_name(head)));
99
100 git_reference_free(head);
101 }
102
103 void test_worktree_refs__set_head_fails_when_already_checked_out(void)
104 {
105 cl_git_fail(git_repository_set_head(fixture.repo, "refs/heads/testrepo-worktree"));
106 }
107
108 void test_worktree_refs__delete_fails_for_checked_out_branch(void)
109 {
110 git_reference *branch;
111
112 cl_git_pass(git_branch_lookup(&branch, fixture.repo,
113 "testrepo-worktree", GIT_BRANCH_LOCAL));
114 cl_git_fail(git_branch_delete(branch));
115
116 git_reference_free(branch);
117 }
118
119 void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
120 {
121 git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
122 git_reference *branch;
123 git_worktree *worktree;
124
125 opts.flags = GIT_WORKTREE_PRUNE_VALID;
126
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);
130
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);
135 }
136
137 void test_worktree_refs__delete_unrelated_branch_on_worktree(void)
138 {
139 git_reference *branch;
140
141 cl_git_pass(git_branch_lookup(&branch, fixture.worktree,
142 "merge-conflict", GIT_BRANCH_LOCAL));
143 cl_git_pass(git_branch_delete(branch));
144
145 git_reference_free(branch);
146 }
147
148 void test_worktree_refs__delete_unrelated_branch_on_parent(void)
149 {
150 git_reference *branch;
151
152 cl_git_pass(git_branch_lookup(&branch, fixture.repo,
153 "merge-conflict", GIT_BRANCH_LOCAL));
154 cl_git_pass(git_branch_delete(branch));
155
156 git_reference_free(branch);
157 }
158
159 void test_worktree_refs__renaming_reference_updates_worktree_heads(void)
160 {
161 git_reference *head, *branch, *renamed;
162
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));
167
168 git_reference_free(head);
169 git_reference_free(branch);
170 git_reference_free(renamed);
171 }
172
173 void test_worktree_refs__creating_refs_uses_commondir(void)
174 {
175 git_reference *head, *branch, *lookup;
176 git_commit *commit;
177 git_buf refpath = GIT_BUF_INIT;
178
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));
182
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));
189
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);
195 }