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