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