]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/remotes.c
Merge pull request #3116 from libgit2/cmn/remove-ssh-embed
[libgit2.git] / tests / network / remote / remotes.c
1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3 #include "buffer.h"
4 #include "refspec.h"
5 #include "remote.h"
6
7 static git_remote *_remote;
8 static git_repository *_repo;
9 static const git_refspec *_refspec;
10
11 void test_network_remote_remotes__initialize(void)
12 {
13 _repo = cl_git_sandbox_init("testrepo.git");
14
15 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
16
17 _refspec = git_remote_get_refspec(_remote, 0);
18 cl_assert(_refspec != NULL);
19 }
20
21 void test_network_remote_remotes__cleanup(void)
22 {
23 git_remote_free(_remote);
24 _remote = NULL;
25
26 cl_git_sandbox_cleanup();
27 }
28
29 void test_network_remote_remotes__parsing(void)
30 {
31 git_remote *_remote2 = NULL;
32
33 cl_assert_equal_s(git_remote_name(_remote), "test");
34 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
35 cl_assert(git_remote_pushurl(_remote) == NULL);
36
37 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH),
38 "git://github.com/libgit2/libgit2");
39 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH),
40 "git://github.com/libgit2/libgit2");
41
42 cl_git_pass(git_remote_lookup(&_remote2, _repo, "test_with_pushurl"));
43 cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl");
44 cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2");
45 cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2");
46
47 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH),
48 "git://github.com/libgit2/fetchlibgit2");
49 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH),
50 "git://github.com/libgit2/pushlibgit2");
51
52 git_remote_free(_remote2);
53 }
54
55 void test_network_remote_remotes__pushurl(void)
56 {
57 const char *name = git_remote_name(_remote);
58 git_remote *mod;
59
60 cl_git_pass(git_remote_set_pushurl(_repo, name, "git://github.com/libgit2/notlibgit2"));
61 cl_git_pass(git_remote_lookup(&mod, _repo, name));
62 cl_assert_equal_s(git_remote_pushurl(mod), "git://github.com/libgit2/notlibgit2");
63 git_remote_free(mod);
64
65 cl_git_pass(git_remote_set_pushurl(_repo, name, NULL));
66 cl_git_pass(git_remote_lookup(&mod, _repo, name));
67 cl_assert(git_remote_pushurl(mod) == NULL);
68 git_remote_free(mod);
69 }
70
71 void test_network_remote_remotes__error_when_not_found(void)
72 {
73 git_remote *r;
74 cl_git_fail_with(git_remote_lookup(&r, _repo, "does-not-exist"), GIT_ENOTFOUND);
75
76 cl_assert(giterr_last() != NULL);
77 cl_assert(giterr_last()->klass == GITERR_CONFIG);
78 }
79
80 void test_network_remote_remotes__error_when_no_push_available(void)
81 {
82 git_remote *r;
83 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
84 char *specs = {
85 "refs/heads/master",
86 };
87 git_strarray arr = {
88 &specs,
89 1,
90 };
91
92
93 cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git"), NULL));
94
95 callbacks.transport = git_transport_local;
96 cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, &callbacks));
97
98 /* Make sure that push is really not available */
99 r->transport->push = NULL;
100
101 cl_git_fail_with(-1, git_remote_upload(r, &arr, NULL));
102
103 git_remote_free(r);
104 }
105
106 void test_network_remote_remotes__refspec_parsing(void)
107 {
108 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
109 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
110 }
111
112 void test_network_remote_remotes__add_fetchspec(void)
113 {
114 size_t size;
115
116 size = git_remote_refspec_count(_remote);
117
118 cl_git_pass(git_remote_add_fetch(_repo, "test", "refs/*:refs/*"));
119 size++;
120
121 git_remote_free(_remote);
122 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
123
124 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
125
126 _refspec = git_remote_get_refspec(_remote, size - 1);
127 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
128 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
129 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
130 cl_assert_equal_b(_refspec->push, false);
131 }
132
133 void test_network_remote_remotes__dup(void)
134 {
135 git_strarray array;
136 git_remote *dup;
137
138 cl_git_pass(git_remote_dup(&dup, _remote));
139
140 cl_assert_equal_s(git_remote_name(dup), git_remote_name(_remote));
141 cl_assert_equal_s(git_remote_url(dup), git_remote_url(_remote));
142 cl_assert_equal_s(git_remote_pushurl(dup), git_remote_pushurl(_remote));
143
144 cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote));
145 cl_assert_equal_i(1, (int)array.count);
146 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array.strings[0]);
147 git_strarray_free(&array);
148
149 cl_git_pass(git_remote_get_push_refspecs(&array, _remote));
150 cl_assert_equal_i(0, (int)array.count);
151 git_strarray_free(&array);
152
153 git_remote_free(dup);
154 }
155
156 void test_network_remote_remotes__add_pushspec(void)
157 {
158 size_t size;
159
160 size = git_remote_refspec_count(_remote);
161
162 cl_git_pass(git_remote_add_push(_repo, "test", "refs/*:refs/*"));
163 size++;
164
165 git_remote_free(_remote);
166 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
167
168 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
169
170 _refspec = git_remote_get_refspec(_remote, size - 1);
171 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
172 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
173 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
174
175 cl_assert_equal_b(_refspec->push, true);
176 }
177
178 void test_network_remote_remotes__fnmatch(void)
179 {
180 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
181 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
182 }
183
184 void test_network_remote_remotes__transform(void)
185 {
186 git_buf ref = GIT_BUF_INIT;
187
188 cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master"));
189 cl_assert_equal_s(ref.ptr, "refs/remotes/test/master");
190 git_buf_free(&ref);
191 }
192
193 void test_network_remote_remotes__transform_destination_to_source(void)
194 {
195 git_buf ref = GIT_BUF_INIT;
196
197 cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master"));
198 cl_assert_equal_s(ref.ptr, "refs/heads/master");
199 git_buf_free(&ref);
200 }
201
202 void test_network_remote_remotes__missing_refspecs(void)
203 {
204 git_config *cfg;
205
206 git_remote_free(_remote);
207 _remote = NULL;
208
209 cl_git_pass(git_repository_config(&cfg, _repo));
210 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
211 cl_git_pass(git_remote_lookup(&_remote, _repo, "specless"));
212
213 git_config_free(cfg);
214 }
215
216 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
217 {
218 git_config *config;
219 git_remote *remote;
220 char *specstr[] = {
221 "refs/tags/*:refs/tags/*",
222 };
223 git_strarray specs = {
224 specstr,
225 1,
226 };
227
228 cl_git_pass(git_remote_create(&remote, _repo, "taggy", git_repository_path(_repo)));
229
230 /*
231 * Set the current branch's upstream remote to a dummy ref so we call into the code
232 * which tries to check for the current branch's upstream in the refspecs
233 */
234 cl_git_pass(git_repository_config(&config, _repo));
235 cl_git_pass(git_config_set_string(config, "branch.master.remote", "taggy"));
236 cl_git_pass(git_config_set_string(config, "branch.master.merge", "refs/heads/foo"));
237
238 cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
239
240 git_remote_free(remote);
241 }
242
243 void test_network_remote_remotes__list(void)
244 {
245 git_strarray list;
246 git_config *cfg;
247
248 cl_git_pass(git_remote_list(&list, _repo));
249 cl_assert(list.count == 5);
250 git_strarray_free(&list);
251
252 cl_git_pass(git_repository_config(&cfg, _repo));
253
254 /* Create a new remote */
255 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
256
257 /* Update a remote (previously without any url/pushurl entry) */
258 cl_git_pass(git_config_set_string(cfg, "remote.no-remote-url.pushurl", "http://example.com"));
259
260 cl_git_pass(git_remote_list(&list, _repo));
261 cl_assert(list.count == 7);
262 git_strarray_free(&list);
263
264 git_config_free(cfg);
265 }
266
267 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
268 {
269 git_remote_free(_remote);
270 _remote = NULL;
271
272 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_lookup(&_remote, _repo, "just-left-few-minutes-ago"));
273 }
274
275 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
276 {
277 git_remote_free(_remote);
278 _remote = NULL;
279
280 cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_lookup(&_remote, _repo, "Inv@{id"));
281 }
282
283 /*
284 * $ git remote add addtest http://github.com/libgit2/libgit2
285 *
286 * $ cat .git/config
287 * [...]
288 * [remote "addtest"]
289 * url = http://github.com/libgit2/libgit2
290 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
291 */
292 void test_network_remote_remotes__add(void)
293 {
294 git_remote_free(_remote);
295 _remote = NULL;
296
297 cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2"));
298 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
299
300 git_remote_free(_remote);
301 _remote = NULL;
302
303 cl_git_pass(git_remote_lookup(&_remote, _repo, "addtest"));
304 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
305
306 _refspec = git_vector_get(&_remote->refspecs, 0);
307 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec));
308 cl_assert(git_refspec_force(_refspec) == 1);
309 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec));
310 cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
311 }
312
313 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
314 {
315 git_remote *remote;
316
317 cl_assert_equal_i(
318 GIT_EINVALIDSPEC,
319 git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
320 }
321
322 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
323 {
324 git_remote *remote = NULL;
325
326 cl_assert_equal_i(
327 GIT_EINVALIDSPEC,
328 git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
329 cl_assert_equal_p(remote, NULL);
330
331 cl_assert_equal_i(
332 GIT_EINVALIDSPEC,
333 git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
334 cl_assert_equal_p(remote, NULL);
335 }
336
337 void test_network_remote_remotes__tagopt(void)
338 {
339 const char *name = git_remote_name(_remote);
340
341 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
342 assert_config_entry_value(_repo, "remote.test.tagopt", "--tags");
343
344 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
345 assert_config_entry_value(_repo, "remote.test.tagopt", "--no-tags");
346
347 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
348 assert_config_entry_existence(_repo, "remote.test.tagopt", false);
349 }
350
351 void test_network_remote_remotes__can_load_with_an_empty_url(void)
352 {
353 git_remote *remote = NULL;
354
355 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-url"));
356
357 cl_assert(remote->url == NULL);
358 cl_assert(remote->pushurl == NULL);
359
360 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL));
361
362 cl_assert(giterr_last() != NULL);
363 cl_assert(giterr_last()->klass == GITERR_INVALID);
364
365 git_remote_free(remote);
366 }
367
368 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
369 {
370 git_remote *remote = NULL;
371
372 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-pushurl"));
373
374 cl_assert(remote->url == NULL);
375 cl_assert(remote->pushurl == NULL);
376
377 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL));
378
379 git_remote_free(remote);
380 }
381
382 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
383 {
384 git_remote *remote = NULL;
385
386 cl_git_fail_with(
387 git_remote_lookup(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
388 }
389
390 void assert_cannot_create_remote(const char *name, int expected_error)
391 {
392 git_remote *remote = NULL;
393
394 cl_git_fail_with(
395 git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"),
396 expected_error);
397
398 cl_assert_equal_p(remote, NULL);
399 }
400
401 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
402 {
403 assert_cannot_create_remote("test", GIT_EEXISTS);
404 }
405
406 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
407 {
408 assert_cannot_create_remote("/", GIT_EINVALIDSPEC);
409 assert_cannot_create_remote("//", GIT_EINVALIDSPEC);
410 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC);
411 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC);
412 }
413
414 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
415 {
416 git_remote *remote;
417 git_strarray array;
418
419 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
420 git_remote_get_fetch_refspecs(&array, remote);
421 cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
422 git_remote_free(remote);
423 }
424
425 static const char *fetch_refspecs[] = {
426 "+refs/heads/*:refs/remotes/origin/*",
427 "refs/tags/*:refs/tags/*",
428 "+refs/pull/*:refs/pull/*",
429 };
430
431 static const char *push_refspecs[] = {
432 "refs/heads/*:refs/heads/*",
433 "refs/tags/*:refs/tags/*",
434 "refs/notes/*:refs/notes/*",
435 };
436
437 void test_network_remote_remotes__query_refspecs(void)
438 {
439 git_remote *remote;
440 git_strarray array;
441 int i;
442
443 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "query", "git://github.com/libgit2/libgit2", NULL));
444 git_remote_free(remote);
445
446 for (i = 0; i < 3; i++) {
447 cl_git_pass(git_remote_add_fetch(_repo, "query", fetch_refspecs[i]));
448 cl_git_pass(git_remote_add_push(_repo, "query", push_refspecs[i]));
449 }
450
451 cl_git_pass(git_remote_lookup(&remote, _repo, "query"));
452
453 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
454 for (i = 0; i < 3; i++) {
455 cl_assert_equal_s(fetch_refspecs[i], array.strings[i]);
456 }
457 git_strarray_free(&array);
458
459 cl_git_pass(git_remote_get_push_refspecs(&array, remote));
460 for (i = 0; i < 3; i++) {
461 cl_assert_equal_s(push_refspecs[i], array.strings[i]);
462 }
463 git_strarray_free(&array);
464
465 git_remote_free(remote);
466 git_remote_delete(_repo, "test");
467 }
468
469 void test_network_remote_remotes__fetch_from_anonymous(void)
470 {
471 git_remote *remote;
472
473 cl_git_pass(git_remote_create_anonymous(&remote, _repo, cl_fixture("testrepo.git"),
474 "refs/heads/*:refs/other/*"));
475 cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
476 git_remote_free(remote);
477 }