]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/rename.c
Merge pull request #2695 from libgit2/cmn/remote-lookup
[libgit2.git] / tests / network / remote / rename.c
1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3
4 #include "repository.h"
5
6 static git_repository *_repo;
7 static const char *_remote_name = "test";
8
9 void test_network_remote_rename__initialize(void)
10 {
11 _repo = cl_git_sandbox_init("testrepo.git");
12 }
13
14 void test_network_remote_rename__cleanup(void)
15 {
16 cl_git_sandbox_cleanup();
17 }
18
19 static int dont_call_me_cb(const char *fetch_refspec, void *payload)
20 {
21 GIT_UNUSED(fetch_refspec);
22 GIT_UNUSED(payload);
23
24 cl_assert(false);
25
26 return -1;
27 }
28
29 void test_network_remote_rename__renaming_a_remote_moves_related_configuration_section(void)
30 {
31 git_strarray problems = {0};
32
33 assert_config_entry_existence(_repo, "remote.test.fetch", true);
34 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
35
36 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
37 cl_assert_equal_i(0, problems.count);
38 git_strarray_free(&problems);
39
40 assert_config_entry_existence(_repo, "remote.test.fetch", false);
41 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true);
42 }
43
44 void test_network_remote_rename__renaming_a_remote_updates_branch_related_configuration_entries(void)
45 {
46 git_strarray problems = {0};
47
48 assert_config_entry_value(_repo, "branch.master.remote", "test");
49
50 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
51 cl_assert_equal_i(0, problems.count);
52 git_strarray_free(&problems);
53
54 assert_config_entry_value(_repo, "branch.master.remote", "just/renamed");
55 }
56
57 void test_network_remote_rename__renaming_a_remote_updates_default_fetchrefspec(void)
58 {
59 git_strarray problems = {0};
60
61 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
62 cl_assert_equal_i(0, problems.count);
63 git_strarray_free(&problems);
64
65 assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*");
66 }
67
68 void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void)
69 {
70 git_config *config;
71 git_remote *remote;
72 git_strarray problems = {0};
73
74 cl_git_pass(git_repository_config__weakptr(&config, _repo));
75 cl_git_pass(git_config_delete_entry(config, "remote.test.fetch"));
76
77 cl_git_pass(git_remote_lookup(&remote, _repo, "test"));
78 git_remote_free(remote);
79
80 assert_config_entry_existence(_repo, "remote.test.fetch", false);
81
82 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
83 cl_assert_equal_i(0, problems.count);
84 git_strarray_free(&problems);
85
86 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
87 }
88
89 void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void)
90 {
91 git_config *config;
92 git_remote *remote;
93 git_strarray problems = {0};
94
95 cl_git_pass(git_repository_config__weakptr(&config, _repo));
96 cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*"));
97 cl_git_pass(git_remote_lookup(&remote, _repo, "test"));
98 git_remote_free(remote);
99
100 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
101 cl_assert_equal_i(1, problems.count);
102 cl_assert_equal_s("+refs/*:refs/*", problems.strings[0]);
103 git_strarray_free(&problems);
104
105 assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*");
106
107 git_strarray_free(&problems);
108 }
109
110 void test_network_remote_rename__new_name_can_contain_dots(void)
111 {
112 git_strarray problems = {0};
113
114 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just.renamed"));
115 cl_assert_equal_i(0, problems.count);
116 git_strarray_free(&problems);
117 assert_config_entry_existence(_repo, "remote.just.renamed.fetch", true);
118 }
119
120 void test_network_remote_rename__new_name_must_conform_to_reference_naming_conventions(void)
121 {
122 git_strarray problems = {0};
123
124 cl_assert_equal_i(
125 GIT_EINVALIDSPEC,
126 git_remote_rename(&problems, _repo, _remote_name, "new@{name"));
127 }
128
129 void test_network_remote_rename__renamed_name_is_persisted(void)
130 {
131 git_remote *renamed;
132 git_repository *another_repo;
133 git_strarray problems = {0};
134
135 cl_git_fail(git_remote_lookup(&renamed, _repo, "just/renamed"));
136
137 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
138 cl_assert_equal_i(0, problems.count);
139 git_strarray_free(&problems);
140
141 cl_git_pass(git_repository_open(&another_repo, "testrepo.git"));
142 cl_git_pass(git_remote_lookup(&renamed, _repo, "just/renamed"));
143
144 git_remote_free(renamed);
145 git_repository_free(another_repo);
146 }
147
148 void test_network_remote_rename__cannot_overwrite_an_existing_remote(void)
149 {
150 git_strarray problems = {0};
151
152 cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _repo, _remote_name, "test"));
153 cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _repo, _remote_name, "test_with_pushurl"));
154 }
155
156 void test_network_remote_rename__renaming_a_remote_moves_the_underlying_reference(void)
157 {
158 git_reference *underlying;
159 git_strarray problems = {0};
160
161 cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed"));
162 cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
163 git_reference_free(underlying);
164
165 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed"));
166 cl_assert_equal_i(0, problems.count);
167 git_strarray_free(&problems);
168
169 cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
170 cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master"));
171 git_reference_free(underlying);
172 }
173
174 void test_network_remote_rename__overwrite_ref_in_target(void)
175 {
176 git_oid id;
177 char idstr[GIT_OID_HEXSZ + 1] = {0};
178 git_reference *ref;
179 git_branch_t btype;
180 git_branch_iterator *iter;
181 git_strarray problems = {0};
182
183 cl_git_pass(git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
184 cl_git_pass(git_reference_create(&ref, _repo, "refs/remotes/renamed/master", &id, 1, NULL, NULL));
185 git_reference_free(ref);
186
187 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "renamed"));
188 cl_assert_equal_i(0, problems.count);
189 git_strarray_free(&problems);
190
191 /* make sure there's only one remote-tracking branch */
192 cl_git_pass(git_branch_iterator_new(&iter, _repo, GIT_BRANCH_REMOTE));
193 cl_git_pass(git_branch_next(&ref, &btype, iter));
194 cl_assert_equal_s("refs/remotes/renamed/master", git_reference_name(ref));
195 git_oid_fmt(idstr, git_reference_target(ref));
196 cl_assert_equal_s("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", idstr);
197 git_reference_free(ref);
198
199 cl_git_fail_with(GIT_ITEROVER, git_branch_next(&ref, &btype, iter));
200 git_branch_iterator_free(iter);
201 }
202
203 void test_network_remote_rename__nonexistent_returns_enotfound(void)
204 {
205 git_strarray problems = {0};
206
207 int err = git_remote_rename(&problems, _repo, "nonexistent", "renamed");
208
209 cl_assert_equal_i(GIT_ENOTFOUND, err);
210 }
211
212 void test_network_remote_rename__symref_head(void)
213 {
214 int error;
215 git_reference *ref;
216 git_branch_t btype;
217 git_branch_iterator *iter;
218 git_strarray problems = {0};
219 char idstr[GIT_OID_HEXSZ + 1] = {0};
220 git_vector refs;
221
222 cl_git_pass(git_reference_symbolic_create(&ref, _repo, "refs/remotes/test/HEAD", "refs/remotes/test/master", 0, NULL, NULL));
223 git_reference_free(ref);
224
225 cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "renamed"));
226 cl_assert_equal_i(0, problems.count);
227 git_strarray_free(&problems);
228
229 cl_git_pass(git_vector_init(&refs, 2, (git_vector_cmp) git_reference_cmp));
230 cl_git_pass(git_branch_iterator_new(&iter, _repo, GIT_BRANCH_REMOTE));
231
232 while ((error = git_branch_next(&ref, &btype, iter)) == 0) {
233 cl_git_pass(git_vector_insert(&refs, ref));
234 }
235 cl_assert_equal_i(GIT_ITEROVER, error);
236 git_vector_sort(&refs);
237
238 cl_assert_equal_i(2, refs.length);
239
240 ref = git_vector_get(&refs, 0);
241 cl_assert_equal_s("refs/remotes/renamed/HEAD", git_reference_name(ref));
242 cl_assert_equal_s("refs/remotes/renamed/master", git_reference_symbolic_target(ref));
243 git_reference_free(ref);
244
245 ref = git_vector_get(&refs, 1);
246 cl_assert_equal_s("refs/remotes/renamed/master", git_reference_name(ref));
247 git_oid_fmt(idstr, git_reference_target(ref));
248 cl_assert_equal_s("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", idstr);
249 git_reference_free(ref);
250
251 git_vector_free(&refs);
252
253 cl_git_fail_with(GIT_ITEROVER, git_branch_next(&ref, &btype, iter));
254 git_branch_iterator_free(iter);
255 }