1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
7 static git_remote
*_remote
;
8 static git_repository
*_repo
;
9 static const git_refspec
*_refspec
;
11 void test_network_remote_remotes__initialize(void)
13 _repo
= cl_git_sandbox_init("testrepo.git");
15 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
17 _refspec
= git_remote_get_refspec(_remote
, 0);
18 cl_assert(_refspec
!= NULL
);
21 void test_network_remote_remotes__cleanup(void)
23 git_remote_free(_remote
);
26 cl_git_sandbox_cleanup();
29 void test_network_remote_remotes__parsing(void)
31 git_remote
*_remote2
= NULL
;
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
);
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");
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");
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");
52 git_remote_free(_remote2
);
55 void test_network_remote_remotes__pushurl(void)
57 const char *name
= git_remote_name(_remote
);
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");
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
);
71 void test_network_remote_remotes__error_when_not_found(void)
74 cl_git_fail_with(git_remote_lookup(&r
, _repo
, "does-not-exist"), GIT_ENOTFOUND
);
76 cl_assert(giterr_last() != NULL
);
77 cl_assert(giterr_last()->klass
== GITERR_CONFIG
);
80 void test_network_remote_remotes__error_when_no_push_available(void)
92 cl_git_pass(git_remote_create_anonymous(&r
, _repo
, cl_fixture("testrepo.git"), NULL
));
94 cl_git_pass(git_remote_set_transport(r
, git_transport_local
, NULL
));
96 cl_git_pass(git_remote_connect(r
, GIT_DIRECTION_PUSH
, NULL
));
98 /* Make sure that push is really not available */
99 r
->transport
->push
= NULL
;
101 cl_git_fail_with(-1, git_remote_upload(r
, &arr
, NULL
));
106 void test_network_remote_remotes__refspec_parsing(void)
108 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/heads/*");
109 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/remotes/test/*");
112 void test_network_remote_remotes__add_fetchspec(void)
116 size
= git_remote_refspec_count(_remote
);
118 cl_git_pass(git_remote_add_fetch(_remote
, "refs/*:refs/*"));
121 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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);
130 void test_network_remote_remotes__dup(void)
135 cl_git_pass(git_remote_dup(&dup
, _remote
));
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
));
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
);
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
);
150 git_remote_free(dup
);
153 void test_network_remote_remotes__add_pushspec(void)
157 size
= git_remote_refspec_count(_remote
);
159 cl_git_pass(git_remote_add_push(_remote
, "refs/*:refs/*"));
161 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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/*");
168 cl_assert_equal_b(_refspec
->push
, true);
171 void test_network_remote_remotes__save(void)
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/*";
179 git_remote_free(_remote
);
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"));
188 git_remote_clear_refspecs(_remote
);
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
);
198 /* Load it from config and make sure everything matches */
199 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "upstream"));
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
);
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
);
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");
216 git_remote_free(_remote
);
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
);
225 void test_network_remote_remotes__fnmatch(void)
227 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
228 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
231 void test_network_remote_remotes__transform(void)
233 git_buf ref
= GIT_BUF_INIT
;
235 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
236 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
240 void test_network_remote_remotes__transform_destination_to_source(void)
242 git_buf ref
= GIT_BUF_INIT
;
244 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
245 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
249 void test_network_remote_remotes__missing_refspecs(void)
253 git_remote_free(_remote
);
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"));
260 git_config_free(cfg
);
263 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
268 "refs/tags/*:refs/tags/*",
270 git_strarray specs
= {
275 cl_git_pass(git_remote_create(&remote
, _repo
, "taggy", git_repository_path(_repo
)));
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
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"));
285 cl_git_pass(git_remote_fetch(remote
, &specs
, NULL
, NULL
));
287 git_remote_free(remote
);
290 void test_network_remote_remotes__list(void)
295 cl_git_pass(git_remote_list(&list
, _repo
));
296 cl_assert(list
.count
== 5);
297 git_strarray_free(&list
);
299 cl_git_pass(git_repository_config(&cfg
, _repo
));
301 /* Create a new remote */
302 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
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"));
307 cl_git_pass(git_remote_list(&list
, _repo
));
308 cl_assert(list
.count
== 7);
309 git_strarray_free(&list
);
311 git_config_free(cfg
);
314 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
316 git_remote_free(_remote
);
319 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_lookup(&_remote
, _repo
, "just-left-few-minutes-ago"));
322 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
324 git_remote_free(_remote
);
327 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_lookup(&_remote
, _repo
, "Inv@{id"));
331 * $ git remote add addtest http://github.com/libgit2/libgit2
336 * url = http://github.com/libgit2/libgit2
337 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
339 void test_network_remote_remotes__add(void)
341 git_remote_free(_remote
);
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
));
347 git_remote_free(_remote
);
350 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "addtest"));
351 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
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");
360 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
366 git_remote_create(&remote
, _repo
, NULL
, "git://github.com/libgit2/libgit2"));
369 void test_network_remote_remotes__cannot_save_an_inmemory_remote(void)
373 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
375 cl_assert_equal_p(NULL
, git_remote_name(remote
));
377 cl_git_fail(git_remote_save(remote
));
378 git_remote_free(remote
);
381 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
383 git_remote
*remote
= NULL
;
387 git_remote_create(&remote
, _repo
, "Inv@{id", "git://github.com/libgit2/libgit2"));
388 cl_assert_equal_p(remote
, NULL
);
392 git_remote_create(&remote
, _repo
, "", "git://github.com/libgit2/libgit2"));
393 cl_assert_equal_p(remote
, NULL
);
396 void test_network_remote_remotes__tagopt(void)
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");
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");
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);
411 void test_network_remote_remotes__can_load_with_an_empty_url(void)
413 git_remote
*remote
= NULL
;
415 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-url"));
417 cl_assert(remote
->url
== NULL
);
418 cl_assert(remote
->pushurl
== NULL
);
420 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
422 cl_assert(giterr_last() != NULL
);
423 cl_assert(giterr_last()->klass
== GITERR_INVALID
);
425 git_remote_free(remote
);
428 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
430 git_remote
*remote
= NULL
;
432 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-pushurl"));
434 cl_assert(remote
->url
== NULL
);
435 cl_assert(remote
->pushurl
== NULL
);
437 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
439 git_remote_free(remote
);
442 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
444 git_remote
*remote
= NULL
;
447 git_remote_lookup(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
450 void assert_cannot_create_remote(const char *name
, int expected_error
)
452 git_remote
*remote
= NULL
;
455 git_remote_create(&remote
, _repo
, name
, "git://github.com/libgit2/libgit2"),
458 cl_assert_equal_p(remote
, NULL
);
461 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
463 assert_cannot_create_remote("test", GIT_EEXISTS
);
466 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
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
);
474 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
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
);
485 static const char *fetch_refspecs
[] = {
486 "+refs/heads/*:refs/remotes/origin/*",
487 "refs/tags/*:refs/tags/*",
488 "+refs/pull/*:refs/pull/*",
491 static const char *push_refspecs
[] = {
492 "refs/heads/*:refs/heads/*",
493 "refs/tags/*:refs/tags/*",
494 "refs/notes/*:refs/notes/*",
497 void test_network_remote_remotes__query_refspecs(void)
503 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
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
]));
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
]);
514 git_strarray_free(&array
);
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
]);
520 git_strarray_free(&array
);
522 git_remote_free(remote
);
525 void test_network_remote_remotes__fetch_from_anonymous(void)
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
);