]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/remotes.c
remote: don't check for upstream on an anonymous remote
[libgit2.git] / tests / network / remote / remotes.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "refspec.h"
4 #include "remote.h"
5
6 static git_remote *_remote;
7 static git_repository *_repo;
8 static const git_refspec *_refspec;
9
10 void test_network_remote_remotes__initialize(void)
11 {
12 _repo = cl_git_sandbox_init("testrepo.git");
13
14 cl_git_pass(git_remote_load(&_remote, _repo, "test"));
15
16 _refspec = git_remote_get_refspec(_remote, 0);
17 cl_assert(_refspec != NULL);
18 }
19
20 void test_network_remote_remotes__cleanup(void)
21 {
22 git_remote_free(_remote);
23 _remote = NULL;
24
25 cl_git_sandbox_cleanup();
26 }
27
28 void test_network_remote_remotes__parsing(void)
29 {
30 git_remote *_remote2 = NULL;
31
32 cl_assert_equal_s(git_remote_name(_remote), "test");
33 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
34 cl_assert(git_remote_pushurl(_remote) == NULL);
35
36 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH),
37 "git://github.com/libgit2/libgit2");
38 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH),
39 "git://github.com/libgit2/libgit2");
40
41 cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl"));
42 cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl");
43 cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2");
44 cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2");
45
46 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH),
47 "git://github.com/libgit2/fetchlibgit2");
48 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH),
49 "git://github.com/libgit2/pushlibgit2");
50
51 git_remote_free(_remote2);
52 }
53
54 void test_network_remote_remotes__pushurl(void)
55 {
56 cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2"));
57 cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2");
58
59 cl_git_pass(git_remote_set_pushurl(_remote, NULL));
60 cl_assert(git_remote_pushurl(_remote) == NULL);
61 }
62
63 void test_network_remote_remotes__error_when_not_found(void)
64 {
65 git_remote *r;
66 cl_git_fail_with(git_remote_load(&r, _repo, "does-not-exist"), GIT_ENOTFOUND);
67
68 cl_assert(giterr_last() != NULL);
69 cl_assert(giterr_last()->klass == GITERR_CONFIG);
70 }
71
72 void test_network_remote_remotes__error_when_no_push_available(void)
73 {
74 git_remote *r;
75 git_push *p;
76
77 cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git"), NULL));
78
79 cl_git_pass(git_remote_set_transport(r, git_transport_local, NULL));
80
81 cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH));
82
83 /* Make sure that push is really not available */
84 r->transport->push = NULL;
85
86 cl_git_pass(git_push_new(&p, r));
87 cl_git_pass(git_push_add_refspec(p, "refs/heads/master"));
88 cl_git_fail_with(git_push_finish(p), GIT_ERROR);
89
90 git_push_free(p);
91 git_remote_free(r);
92 }
93
94 void test_network_remote_remotes__refspec_parsing(void)
95 {
96 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
97 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
98 }
99
100 void test_network_remote_remotes__add_fetchspec(void)
101 {
102 size_t size;
103
104 size = git_remote_refspec_count(_remote);
105
106 cl_git_pass(git_remote_add_fetch(_remote, "refs/*:refs/*"));
107
108 size++;
109 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
110
111 _refspec = git_remote_get_refspec(_remote, size - 1);
112 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
113 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
114 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
115 cl_assert_equal_b(_refspec->push, false);
116 }
117
118 void test_network_remote_remotes__dup(void)
119 {
120 git_strarray array;
121 git_remote *dup;
122
123 cl_git_pass(git_remote_dup(&dup, _remote));
124
125 cl_assert_equal_s(git_remote_name(dup), git_remote_name(_remote));
126 cl_assert_equal_s(git_remote_url(dup), git_remote_url(_remote));
127 cl_assert_equal_s(git_remote_pushurl(dup), git_remote_pushurl(_remote));
128
129 cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote));
130 cl_assert_equal_i(1, (int)array.count);
131 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array.strings[0]);
132 git_strarray_free(&array);
133
134 cl_git_pass(git_remote_get_push_refspecs(&array, _remote));
135 cl_assert_equal_i(0, (int)array.count);
136 git_strarray_free(&array);
137
138 git_remote_free(dup);
139 }
140
141 void test_network_remote_remotes__add_pushspec(void)
142 {
143 size_t size;
144
145 size = git_remote_refspec_count(_remote);
146
147 cl_git_pass(git_remote_add_push(_remote, "refs/*:refs/*"));
148 size++;
149 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
150
151 _refspec = git_remote_get_refspec(_remote, size - 1);
152 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
153 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
154 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
155
156 cl_assert_equal_b(_refspec->push, true);
157 }
158
159 void test_network_remote_remotes__save(void)
160 {
161 git_strarray array;
162 const char *fetch_refspec1 = "refs/heads/ns1/*:refs/remotes/upstream/ns1/*";
163 const char *fetch_refspec2 = "refs/heads/ns2/*:refs/remotes/upstream/ns2/*";
164 const char *push_refspec1 = "refs/heads/ns1/*:refs/heads/ns1/*";
165 const char *push_refspec2 = "refs/heads/ns2/*:refs/heads/ns2/*";
166
167 git_remote_free(_remote);
168 _remote = NULL;
169
170 /* Set up the remote and save it to config */
171 cl_git_pass(git_remote_create(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2"));
172 git_remote_clear_refspecs(_remote);
173
174 cl_git_pass(git_remote_add_fetch(_remote, fetch_refspec1));
175 cl_git_pass(git_remote_add_fetch(_remote, fetch_refspec2));
176 cl_git_pass(git_remote_add_push(_remote, push_refspec1));
177 cl_git_pass(git_remote_add_push(_remote, push_refspec2));
178 cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push"));
179 cl_git_pass(git_remote_save(_remote));
180 git_remote_free(_remote);
181 _remote = NULL;
182
183 /* Load it from config and make sure everything matches */
184 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
185
186 cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote));
187 cl_assert_equal_i(2, (int)array.count);
188 cl_assert_equal_s(fetch_refspec1, array.strings[0]);
189 cl_assert_equal_s(fetch_refspec2, array.strings[1]);
190 git_strarray_free(&array);
191
192 cl_git_pass(git_remote_get_push_refspecs(&array, _remote));
193 cl_assert_equal_i(2, (int)array.count);
194 cl_assert_equal_s(push_refspec1, array.strings[0]);
195 cl_assert_equal_s(push_refspec2, array.strings[1]);
196 git_strarray_free(&array);
197
198 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
199 cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push");
200
201 /* remove the pushurl again and see if we can save that too */
202 cl_git_pass(git_remote_set_pushurl(_remote, NULL));
203 cl_git_pass(git_remote_save(_remote));
204 git_remote_free(_remote);
205 _remote = NULL;
206
207 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
208 cl_assert(git_remote_pushurl(_remote) == NULL);
209 }
210
211 void test_network_remote_remotes__fnmatch(void)
212 {
213 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
214 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
215 }
216
217 void test_network_remote_remotes__transform(void)
218 {
219 git_buf ref = GIT_BUF_INIT;
220
221 cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master"));
222 cl_assert_equal_s(ref.ptr, "refs/remotes/test/master");
223 git_buf_free(&ref);
224 }
225
226 void test_network_remote_remotes__transform_destination_to_source(void)
227 {
228 git_buf ref = GIT_BUF_INIT;
229
230 cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master"));
231 cl_assert_equal_s(ref.ptr, "refs/heads/master");
232 git_buf_free(&ref);
233 }
234
235 void test_network_remote_remotes__missing_refspecs(void)
236 {
237 git_config *cfg;
238
239 git_remote_free(_remote);
240 _remote = NULL;
241
242 cl_git_pass(git_repository_config(&cfg, _repo));
243 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
244 cl_git_pass(git_remote_load(&_remote, _repo, "specless"));
245
246 git_config_free(cfg);
247 }
248
249 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
250 {
251 git_config *config;
252 git_remote *remote;
253 char *specstr[] = {
254 "refs/tags/*:refs/tags/*",
255 };
256 git_strarray specs = {
257 specstr,
258 1,
259 };
260
261 cl_git_pass(git_remote_create(&remote, _repo, "taggy", git_repository_path(_repo)));
262
263 /*
264 * Set the current branch's upstream remote to a dummy ref so we call into the code
265 * which tries to check for the current branch's upstream in the refspecs
266 */
267 cl_git_pass(git_repository_config(&config, _repo));
268 cl_git_pass(git_config_set_string(config, "branch.master.remote", "taggy"));
269 cl_git_pass(git_config_set_string(config, "branch.master.merge", "refs/heads/foo"));
270
271 cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
272
273 git_remote_free(remote);
274 }
275
276 void test_network_remote_remotes__list(void)
277 {
278 git_strarray list;
279 git_config *cfg;
280
281 cl_git_pass(git_remote_list(&list, _repo));
282 cl_assert(list.count == 5);
283 git_strarray_free(&list);
284
285 cl_git_pass(git_repository_config(&cfg, _repo));
286
287 /* Create a new remote */
288 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
289
290 /* Update a remote (previously without any url/pushurl entry) */
291 cl_git_pass(git_config_set_string(cfg, "remote.no-remote-url.pushurl", "http://example.com"));
292
293 cl_git_pass(git_remote_list(&list, _repo));
294 cl_assert(list.count == 7);
295 git_strarray_free(&list);
296
297 git_config_free(cfg);
298 }
299
300 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
301 {
302 git_remote_free(_remote);
303 _remote = NULL;
304
305 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago"));
306 }
307
308 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
309 {
310 git_remote_free(_remote);
311 _remote = NULL;
312
313 cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id"));
314 }
315
316 /*
317 * $ git remote add addtest http://github.com/libgit2/libgit2
318 *
319 * $ cat .git/config
320 * [...]
321 * [remote "addtest"]
322 * url = http://github.com/libgit2/libgit2
323 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
324 */
325 void test_network_remote_remotes__add(void)
326 {
327 git_remote_free(_remote);
328 _remote = NULL;
329
330 cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2"));
331 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
332
333 git_remote_free(_remote);
334 _remote = NULL;
335
336 cl_git_pass(git_remote_load(&_remote, _repo, "addtest"));
337 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
338
339 _refspec = git_vector_get(&_remote->refspecs, 0);
340 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec));
341 cl_assert(git_refspec_force(_refspec) == 1);
342 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec));
343 cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
344 }
345
346 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
347 {
348 git_remote *remote;
349
350 cl_assert_equal_i(
351 GIT_EINVALIDSPEC,
352 git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
353 }
354
355 void test_network_remote_remotes__cannot_save_an_inmemory_remote(void)
356 {
357 git_remote *remote;
358
359 cl_git_pass(git_remote_create_anonymous(&remote, _repo, "git://github.com/libgit2/libgit2", NULL));
360
361 cl_assert_equal_p(NULL, git_remote_name(remote));
362
363 cl_git_fail(git_remote_save(remote));
364 git_remote_free(remote);
365 }
366
367 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
368 {
369 git_remote *remote = NULL;
370
371 cl_assert_equal_i(
372 GIT_EINVALIDSPEC,
373 git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
374 cl_assert_equal_p(remote, NULL);
375
376 cl_assert_equal_i(
377 GIT_EINVALIDSPEC,
378 git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
379 cl_assert_equal_p(remote, NULL);
380 }
381
382 void test_network_remote_remotes__tagopt(void)
383 {
384 const char *opt;
385 git_config *cfg;
386
387 cl_git_pass(git_repository_config(&cfg, _repo));
388
389 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
390 cl_git_pass(git_remote_save(_remote));
391 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
392 cl_assert_equal_s("--tags", opt);
393
394 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
395 cl_git_pass(git_remote_save(_remote));
396 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
397 cl_assert_equal_s("--no-tags", opt);
398
399 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
400 cl_git_pass(git_remote_save(_remote));
401 cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND);
402
403 git_config_free(cfg);
404 }
405
406 void test_network_remote_remotes__can_load_with_an_empty_url(void)
407 {
408 git_remote *remote = NULL;
409
410 cl_git_pass(git_remote_load(&remote, _repo, "empty-remote-url"));
411
412 cl_assert(remote->url == NULL);
413 cl_assert(remote->pushurl == NULL);
414
415 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH));
416
417 cl_assert(giterr_last() != NULL);
418 cl_assert(giterr_last()->klass == GITERR_INVALID);
419
420 git_remote_free(remote);
421 }
422
423 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
424 {
425 git_remote *remote = NULL;
426
427 cl_git_pass(git_remote_load(&remote, _repo, "empty-remote-pushurl"));
428
429 cl_assert(remote->url == NULL);
430 cl_assert(remote->pushurl == NULL);
431
432 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH));
433
434 git_remote_free(remote);
435 }
436
437 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
438 {
439 git_remote *remote = NULL;
440
441 cl_git_fail_with(
442 git_remote_load(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
443 }
444
445 void assert_cannot_create_remote(const char *name, int expected_error)
446 {
447 git_remote *remote = NULL;
448
449 cl_git_fail_with(
450 git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"),
451 expected_error);
452
453 cl_assert_equal_p(remote, NULL);
454 }
455
456 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
457 {
458 assert_cannot_create_remote("test", GIT_EEXISTS);
459 }
460
461 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
462 {
463 assert_cannot_create_remote("/", GIT_EINVALIDSPEC);
464 assert_cannot_create_remote("//", GIT_EINVALIDSPEC);
465 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC);
466 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC);
467 }
468
469 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
470 {
471 git_remote *remote;
472 git_strarray array;
473
474 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
475 git_remote_get_fetch_refspecs(&array, remote);
476 cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
477 git_remote_free(remote);
478 }
479
480 static const char *fetch_refspecs[] = {
481 "+refs/heads/*:refs/remotes/origin/*",
482 "refs/tags/*:refs/tags/*",
483 "+refs/pull/*:refs/pull/*",
484 };
485
486 static const char *push_refspecs[] = {
487 "refs/heads/*:refs/heads/*",
488 "refs/tags/*:refs/tags/*",
489 "refs/notes/*:refs/notes/*",
490 };
491
492 void test_network_remote_remotes__query_refspecs(void)
493 {
494 git_remote *remote;
495 git_strarray array;
496 int i;
497
498 cl_git_pass(git_remote_create_anonymous(&remote, _repo, "git://github.com/libgit2/libgit2", NULL));
499
500 for (i = 0; i < 3; i++) {
501 cl_git_pass(git_remote_add_fetch(remote, fetch_refspecs[i]));
502 cl_git_pass(git_remote_add_push(remote, push_refspecs[i]));
503 }
504
505 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
506 for (i = 0; i < 3; i++) {
507 cl_assert_equal_s(fetch_refspecs[i], array.strings[i]);
508 }
509 git_strarray_free(&array);
510
511 cl_git_pass(git_remote_get_push_refspecs(&array, remote));
512 for (i = 0; i < 3; i++) {
513 cl_assert_equal_s(push_refspecs[i], array.strings[i]);
514 }
515 git_strarray_free(&array);
516
517 git_remote_free(remote);
518 }
519
520 static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
521 {
522 char *fetch_refspecs[] = {
523 "refs/heads/first-merge:refs/remotes/origin/first-merge",
524 };
525 git_strarray fetch_refspecs_strarray = {
526 fetch_refspecs,
527 1,
528 };
529
530 GIT_UNUSED(payload);
531
532 cl_git_pass(git_remote_create(out, repo, name, url));
533 cl_git_pass(git_remote_set_fetch_refspecs(*out, &fetch_refspecs_strarray));
534
535 return 0;
536 }
537
538 void test_network_remote_remotes__fetch_from_anonymous(void)
539 {
540 git_remote *remote;
541
542 cl_git_pass(git_remote_create_anonymous(&remote, _repo, cl_fixture("testrepo.git"),
543 "refs/heads/*:refs/other/*"));
544 cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
545 git_remote_free(remote);
546 }
547
548 void test_network_remote_remotes__single_branch(void)
549 {
550 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
551 git_repository *repo;
552 git_strarray refs;
553 size_t i, count = 0;
554
555 opts.remote_cb = remote_single_branch;
556 opts.checkout_branch = "first-merge";
557
558 cl_git_pass(git_clone(&repo, "git://github.com/libgit2/TestGitRepository", "./single-branch", &opts));
559 cl_git_pass(git_reference_list(&refs, repo));
560
561 for (i = 0; i < refs.count; i++) {
562 if (!git__prefixcmp(refs.strings[i], "refs/heads/"))
563 count++;
564 }
565 cl_assert_equal_i(1, count);
566
567 git_strarray_free(&refs);
568 git_repository_free(repo);
569 }
570
571 void test_network_remote_remotes__restricted_refspecs(void)
572 {
573 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
574 git_repository *repo;
575
576 opts.remote_cb = remote_single_branch;
577
578 cl_git_fail_with(GIT_EINVALIDSPEC, git_clone(&repo, "git://github.com/libgit2/TestGitRepository", "./restrict-refspec", &opts));
579 }