summaryrefslogtreecommitdiff
path: root/tests/worktree/refs.c
blob: 27dc667eaa869ae254390bc03a6451636d5e3af0 (plain)
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
#include "clar_libgit2.h"
#include "path.h"
#include "refs.h"
#include "worktree.h"
#include "worktree_helpers.h"

#define COMMON_REPO "testrepo"
#define WORKTREE_REPO "testrepo-worktree"

static worktree_fixture fixture =
	WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);

void test_worktree_refs__initialize(void)
{
	setup_fixture_worktree(&fixture);
}

void test_worktree_refs__cleanup(void)
{
	cleanup_fixture_worktree(&fixture);
}

void test_worktree_refs__list(void)
{
	git_strarray refs, wtrefs;
	unsigned i, j;
	int error = 0;

	cl_git_pass(git_reference_list(&refs, fixture.repo));
	cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));

	if (refs.count != wtrefs.count)
	{
		error = GIT_ERROR;
		goto exit;
	}

	for (i = 0; i < refs.count; i++)
	{
		int found = 0;

		for (j = 0; j < wtrefs.count; j++)
		{
			if (!strcmp(refs.strings[i], wtrefs.strings[j]))
			{
				found = 1;
				break;
			}
		}

		if (!found)
		{
			error = GIT_ERROR;
			goto exit;
		}
	}

exit:
	git_strarray_dispose(&refs);
	git_strarray_dispose(&wtrefs);
	cl_git_pass(error);
}

void test_worktree_refs__read_head(void)
{
	git_reference *head;

	cl_git_pass(git_repository_head(&head, fixture.worktree));

	git_reference_free(head);
}

void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void)
{
	git_reference *head;

	cl_git_pass(git_repository_head(&head, fixture.repo));
	cl_git_fail(git_repository_set_head(fixture.worktree, git_reference_name(head)));

	git_reference_free(head);
}

void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void)
{
	git_reference *head;

	cl_git_pass(git_repository_head(&head, fixture.worktree));
	cl_git_fail(git_repository_set_head(fixture.repo, git_reference_name(head)));

	git_reference_free(head);
}

void test_worktree_refs__set_head_works_for_current_HEAD(void)
{
	git_reference *head;

	cl_git_pass(git_repository_head(&head, fixture.repo));
	cl_git_pass(git_repository_set_head(fixture.repo, git_reference_name(head)));

	git_reference_free(head);
}

void test_worktree_refs__set_head_fails_when_already_checked_out(void)
{
	cl_git_fail(git_repository_set_head(fixture.repo, "refs/heads/testrepo-worktree"));
}

void test_worktree_refs__delete_fails_for_checked_out_branch(void)
{
	git_reference *branch;

	cl_git_pass(git_branch_lookup(&branch, fixture.repo,
		    "testrepo-worktree", GIT_BRANCH_LOCAL));
	cl_git_fail(git_branch_delete(branch));

	git_reference_free(branch);
}

void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
{
	git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
	git_reference *branch;
	git_worktree *worktree;

	opts.flags = GIT_WORKTREE_PRUNE_VALID;

	cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename));
	cl_git_pass(git_worktree_prune(worktree, &opts));
	git_worktree_free(worktree);

	cl_git_pass(git_branch_lookup(&branch, fixture.repo,
		    "testrepo-worktree", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
	git_reference_free(branch);
}

void test_worktree_refs__delete_unrelated_branch_on_worktree(void)
{
	git_reference *branch;

	cl_git_pass(git_branch_lookup(&branch, fixture.worktree,
		    "merge-conflict", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));

	git_reference_free(branch);
}

void test_worktree_refs__delete_unrelated_branch_on_parent(void)
{
	git_reference *branch;

	cl_git_pass(git_branch_lookup(&branch, fixture.repo,
		    "merge-conflict", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));

	git_reference_free(branch);
}

void test_worktree_refs__renaming_reference_updates_worktree_heads(void)
{
	git_reference *head, *branch, *renamed;

	cl_git_pass(git_branch_lookup(&branch, fixture.repo,
		    "testrepo-worktree", GIT_BRANCH_LOCAL));
	cl_git_pass(git_reference_rename(&renamed, branch, "refs/heads/renamed", 0, NULL));

	cl_git_pass(git_reference_lookup(&head, fixture.worktree, GIT_HEAD_FILE));
	cl_assert_equal_i(git_reference_type(head), GIT_REFERENCE_SYMBOLIC);
	cl_assert_equal_s(git_reference_symbolic_target(head), "refs/heads/renamed");

	git_reference_free(head);
	git_reference_free(branch);
	git_reference_free(renamed);
}

void test_worktree_refs__creating_refs_uses_commondir(void)
{
	   git_reference *head, *branch, *lookup;
	   git_commit *commit;
	   git_buf refpath = GIT_BUF_INIT;

	   cl_git_pass(git_buf_joinpath(&refpath,
		       git_repository_commondir(fixture.worktree), "refs/heads/testbranch"));
	   cl_assert(!git_path_exists(refpath.ptr));

	   cl_git_pass(git_repository_head(&head, fixture.worktree));
	   cl_git_pass(git_commit_lookup(&commit, fixture.worktree, git_reference_target(head)));
	   cl_git_pass(git_branch_create(&branch, fixture.worktree, "testbranch", commit, 0));
	   cl_git_pass(git_branch_lookup(&lookup, fixture.worktree, "testbranch", GIT_BRANCH_LOCAL));
	   cl_assert(git_reference_cmp(branch, lookup) == 0);
	   cl_assert(git_path_exists(refpath.ptr));

	   git_reference_free(lookup);
	   git_reference_free(branch);
	   git_reference_free(head);
	   git_commit_free(commit);
	   git_buf_dispose(&refpath);
}