1 #include "clar_libgit2.h"
6 static git_remote
*_remote
;
7 static git_repository
*_repo
;
8 static const git_refspec
*_refspec
;
10 void test_network_remote_remotes__initialize(void)
12 _repo
= cl_git_sandbox_init("testrepo.git");
14 cl_git_pass(git_remote_load(&_remote
, _repo
, "test"));
16 _refspec
= git_remote_get_refspec(_remote
, 0);
17 cl_assert(_refspec
!= NULL
);
20 void test_network_remote_remotes__cleanup(void)
22 git_remote_free(_remote
);
25 cl_git_sandbox_cleanup();
28 void test_network_remote_remotes__parsing(void)
30 git_remote
*_remote2
= NULL
;
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
);
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");
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");
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");
51 git_remote_free(_remote2
);
54 void test_network_remote_remotes__pushurl(void)
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");
59 cl_git_pass(git_remote_set_pushurl(_remote
, NULL
));
60 cl_assert(git_remote_pushurl(_remote
) == NULL
);
63 void test_network_remote_remotes__error_when_not_found(void)
66 cl_git_fail_with(git_remote_load(&r
, _repo
, "does-not-exist"), GIT_ENOTFOUND
);
68 cl_assert(giterr_last() != NULL
);
69 cl_assert(giterr_last()->klass
== GITERR_CONFIG
);
72 void test_network_remote_remotes__error_when_no_push_available(void)
77 cl_git_pass(git_remote_create_anonymous(&r
, _repo
, cl_fixture("testrepo.git"), NULL
));
79 cl_git_pass(git_remote_set_transport(r
, git_transport_local
, NULL
));
81 cl_git_pass(git_remote_connect(r
, GIT_DIRECTION_PUSH
));
83 /* Make sure that push is really not available */
84 r
->transport
->push
= NULL
;
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
);
94 void test_network_remote_remotes__refspec_parsing(void)
96 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/heads/*");
97 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/remotes/test/*");
100 void test_network_remote_remotes__add_fetchspec(void)
104 size
= git_remote_refspec_count(_remote
);
106 cl_git_pass(git_remote_add_fetch(_remote
, "refs/*:refs/*"));
109 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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);
118 void test_network_remote_remotes__dup(void)
123 cl_git_pass(git_remote_dup(&dup
, _remote
));
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
));
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
);
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
);
138 git_remote_free(dup
);
141 void test_network_remote_remotes__add_pushspec(void)
145 size
= git_remote_refspec_count(_remote
);
147 cl_git_pass(git_remote_add_push(_remote
, "refs/*:refs/*"));
149 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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/*");
156 cl_assert_equal_b(_refspec
->push
, true);
159 void test_network_remote_remotes__save(void)
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/*";
167 git_remote_free(_remote
);
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
);
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
);
183 /* Load it from config and make sure everything matches */
184 cl_git_pass(git_remote_load(&_remote
, _repo
, "upstream"));
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
);
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
);
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");
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
);
207 cl_git_pass(git_remote_load(&_remote
, _repo
, "upstream"));
208 cl_assert(git_remote_pushurl(_remote
) == NULL
);
211 void test_network_remote_remotes__fnmatch(void)
213 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
214 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
217 void test_network_remote_remotes__transform(void)
219 git_buf ref
= GIT_BUF_INIT
;
221 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
222 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
226 void test_network_remote_remotes__transform_destination_to_source(void)
228 git_buf ref
= GIT_BUF_INIT
;
230 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
231 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
235 void test_network_remote_remotes__missing_refspecs(void)
239 git_remote_free(_remote
);
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"));
246 git_config_free(cfg
);
249 void test_network_remote_remotes__list(void)
254 cl_git_pass(git_remote_list(&list
, _repo
));
255 cl_assert(list
.count
== 5);
256 git_strarray_free(&list
);
258 cl_git_pass(git_repository_config(&cfg
, _repo
));
260 /* Create a new remote */
261 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
263 /* Update a remote (previously without any url/pushurl entry) */
264 cl_git_pass(git_config_set_string(cfg
, "remote.no-remote-url.pushurl", "http://example.com"));
266 cl_git_pass(git_remote_list(&list
, _repo
));
267 cl_assert(list
.count
== 7);
268 git_strarray_free(&list
);
270 git_config_free(cfg
);
273 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
275 git_remote_free(_remote
);
278 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_load(&_remote
, _repo
, "just-left-few-minutes-ago"));
281 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
283 git_remote_free(_remote
);
286 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_load(&_remote
, _repo
, "Inv@{id"));
290 * $ git remote add addtest http://github.com/libgit2/libgit2
295 * url = http://github.com/libgit2/libgit2
296 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
298 void test_network_remote_remotes__add(void)
300 git_remote_free(_remote
);
303 cl_git_pass(git_remote_create(&_remote
, _repo
, "addtest", "http://github.com/libgit2/libgit2"));
304 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
306 git_remote_free(_remote
);
309 cl_git_pass(git_remote_load(&_remote
, _repo
, "addtest"));
310 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
312 _refspec
= git_vector_get(&_remote
->refspecs
, 0);
313 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec
));
314 cl_assert(git_refspec_force(_refspec
) == 1);
315 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec
));
316 cl_assert_equal_s(git_remote_url(_remote
), "http://github.com/libgit2/libgit2");
319 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
325 git_remote_create(&remote
, _repo
, NULL
, "git://github.com/libgit2/libgit2"));
328 void test_network_remote_remotes__cannot_save_an_inmemory_remote(void)
332 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
334 cl_assert_equal_p(NULL
, git_remote_name(remote
));
336 cl_git_fail(git_remote_save(remote
));
337 git_remote_free(remote
);
340 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
342 git_remote
*remote
= NULL
;
346 git_remote_create(&remote
, _repo
, "Inv@{id", "git://github.com/libgit2/libgit2"));
347 cl_assert_equal_p(remote
, NULL
);
351 git_remote_create(&remote
, _repo
, "", "git://github.com/libgit2/libgit2"));
352 cl_assert_equal_p(remote
, NULL
);
355 void test_network_remote_remotes__tagopt(void)
360 cl_git_pass(git_repository_config(&cfg
, _repo
));
362 git_remote_set_autotag(_remote
, GIT_REMOTE_DOWNLOAD_TAGS_ALL
);
363 cl_git_pass(git_remote_save(_remote
));
364 cl_git_pass(git_config_get_string(&opt
, cfg
, "remote.test.tagopt"));
365 cl_assert_equal_s("--tags", opt
);
367 git_remote_set_autotag(_remote
, GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
368 cl_git_pass(git_remote_save(_remote
));
369 cl_git_pass(git_config_get_string(&opt
, cfg
, "remote.test.tagopt"));
370 cl_assert_equal_s("--no-tags", opt
);
372 git_remote_set_autotag(_remote
, GIT_REMOTE_DOWNLOAD_TAGS_AUTO
);
373 cl_git_pass(git_remote_save(_remote
));
374 cl_assert(git_config_get_string(&opt
, cfg
, "remote.test.tagopt") == GIT_ENOTFOUND
);
376 git_config_free(cfg
);
379 void test_network_remote_remotes__can_load_with_an_empty_url(void)
381 git_remote
*remote
= NULL
;
383 cl_git_pass(git_remote_load(&remote
, _repo
, "empty-remote-url"));
385 cl_assert(remote
->url
== NULL
);
386 cl_assert(remote
->pushurl
== NULL
);
388 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
390 cl_assert(giterr_last() != NULL
);
391 cl_assert(giterr_last()->klass
== GITERR_INVALID
);
393 git_remote_free(remote
);
396 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
398 git_remote
*remote
= NULL
;
400 cl_git_pass(git_remote_load(&remote
, _repo
, "empty-remote-pushurl"));
402 cl_assert(remote
->url
== NULL
);
403 cl_assert(remote
->pushurl
== NULL
);
405 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
407 git_remote_free(remote
);
410 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
412 git_remote
*remote
= NULL
;
415 git_remote_load(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
418 void assert_cannot_create_remote(const char *name
, int expected_error
)
420 git_remote
*remote
= NULL
;
423 git_remote_create(&remote
, _repo
, name
, "git://github.com/libgit2/libgit2"),
426 cl_assert_equal_p(remote
, NULL
);
429 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
431 assert_cannot_create_remote("test", GIT_EEXISTS
);
434 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
436 assert_cannot_create_remote("/", GIT_EINVALIDSPEC
);
437 assert_cannot_create_remote("//", GIT_EINVALIDSPEC
);
438 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC
);
439 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC
);
442 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
447 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
448 git_remote_get_fetch_refspecs(&array
, remote
);
449 cl_assert_equal_s("+refs/*:refs/*", array
.strings
[0]);
450 git_remote_free(remote
);
453 static const char *fetch_refspecs
[] = {
454 "+refs/heads/*:refs/remotes/origin/*",
455 "refs/tags/*:refs/tags/*",
456 "+refs/pull/*:refs/pull/*",
459 static const char *push_refspecs
[] = {
460 "refs/heads/*:refs/heads/*",
461 "refs/tags/*:refs/tags/*",
462 "refs/notes/*:refs/notes/*",
465 void test_network_remote_remotes__query_refspecs(void)
471 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
473 for (i
= 0; i
< 3; i
++) {
474 cl_git_pass(git_remote_add_fetch(remote
, fetch_refspecs
[i
]));
475 cl_git_pass(git_remote_add_push(remote
, push_refspecs
[i
]));
478 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
479 for (i
= 0; i
< 3; i
++) {
480 cl_assert_equal_s(fetch_refspecs
[i
], array
.strings
[i
]);
482 git_strarray_free(&array
);
484 cl_git_pass(git_remote_get_push_refspecs(&array
, remote
));
485 for (i
= 0; i
< 3; i
++) {
486 cl_assert_equal_s(push_refspecs
[i
], array
.strings
[i
]);
488 git_strarray_free(&array
);
490 git_remote_free(remote
);
493 static int remote_single_branch(git_remote
**out
, git_repository
*repo
, const char *name
, const char *url
, void *payload
)
495 char *fetch_refspecs
[] = {
496 "refs/heads/first-merge:refs/remotes/origin/first-merge",
498 git_strarray fetch_refspecs_strarray
= {
505 cl_git_pass(git_remote_create(out
, repo
, name
, url
));
506 cl_git_pass(git_remote_set_fetch_refspecs(*out
, &fetch_refspecs_strarray
));
511 void test_network_remote_remotes__single_branch(void)
513 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
514 git_repository
*repo
;
518 opts
.remote_cb
= remote_single_branch
;
519 opts
.checkout_branch
= "first-merge";
521 cl_git_pass(git_clone(&repo
, "git://github.com/libgit2/TestGitRepository", "./single-branch", &opts
));
522 cl_git_pass(git_reference_list(&refs
, repo
));
524 for (i
= 0; i
< refs
.count
; i
++) {
525 if (!git__prefixcmp(refs
.strings
[i
], "refs/heads/"))
528 cl_assert_equal_i(1, count
);
530 git_strarray_free(&refs
);
531 git_repository_free(repo
);
534 void test_network_remote_remotes__restricted_refspecs(void)
536 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
537 git_repository
*repo
;
539 opts
.remote_cb
= remote_single_branch
;
541 cl_git_fail_with(GIT_EINVALIDSPEC
, git_clone(&repo
, "git://github.com/libgit2/TestGitRepository", "./restrict-refspec", &opts
));