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 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");
60 cl_git_pass(git_remote_set_pushurl(_remote
, NULL
));
61 cl_assert(git_remote_pushurl(_remote
) == NULL
);
64 void test_network_remote_remotes__error_when_not_found(void)
67 cl_git_fail_with(git_remote_lookup(&r
, _repo
, "does-not-exist"), GIT_ENOTFOUND
);
69 cl_assert(giterr_last() != NULL
);
70 cl_assert(giterr_last()->klass
== GITERR_CONFIG
);
73 void test_network_remote_remotes__error_when_no_push_available(void)
85 cl_git_pass(git_remote_create_anonymous(&r
, _repo
, cl_fixture("testrepo.git"), NULL
));
87 cl_git_pass(git_remote_set_transport(r
, git_transport_local
, NULL
));
89 cl_git_pass(git_remote_connect(r
, GIT_DIRECTION_PUSH
));
91 /* Make sure that push is really not available */
92 r
->transport
->push
= NULL
;
94 cl_git_fail_with(-1, git_remote_upload(r
, &arr
, NULL
));
99 void test_network_remote_remotes__refspec_parsing(void)
101 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/heads/*");
102 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/remotes/test/*");
105 void test_network_remote_remotes__add_fetchspec(void)
109 size
= git_remote_refspec_count(_remote
);
111 cl_git_pass(git_remote_add_fetch(_remote
, "refs/*:refs/*"));
114 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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);
123 void test_network_remote_remotes__dup(void)
128 cl_git_pass(git_remote_dup(&dup
, _remote
));
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
));
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
);
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
);
143 git_remote_free(dup
);
146 void test_network_remote_remotes__add_pushspec(void)
150 size
= git_remote_refspec_count(_remote
);
152 cl_git_pass(git_remote_add_push(_remote
, "refs/*:refs/*"));
154 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
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/*");
161 cl_assert_equal_b(_refspec
->push
, true);
164 void test_network_remote_remotes__save(void)
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/*";
172 git_remote_free(_remote
);
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
);
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
);
188 /* Load it from config and make sure everything matches */
189 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "upstream"));
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
);
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
);
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");
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
);
212 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "upstream"));
213 cl_assert(git_remote_pushurl(_remote
) == NULL
);
216 void test_network_remote_remotes__fnmatch(void)
218 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
219 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
222 void test_network_remote_remotes__transform(void)
224 git_buf ref
= GIT_BUF_INIT
;
226 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
227 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
231 void test_network_remote_remotes__transform_destination_to_source(void)
233 git_buf ref
= GIT_BUF_INIT
;
235 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
236 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
240 void test_network_remote_remotes__missing_refspecs(void)
244 git_remote_free(_remote
);
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"));
251 git_config_free(cfg
);
254 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
259 "refs/tags/*:refs/tags/*",
261 git_strarray specs
= {
266 cl_git_pass(git_remote_create(&remote
, _repo
, "taggy", git_repository_path(_repo
)));
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
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"));
276 cl_git_pass(git_remote_fetch(remote
, &specs
, NULL
));
278 git_remote_free(remote
);
281 void test_network_remote_remotes__list(void)
286 cl_git_pass(git_remote_list(&list
, _repo
));
287 cl_assert(list
.count
== 5);
288 git_strarray_free(&list
);
290 cl_git_pass(git_repository_config(&cfg
, _repo
));
292 /* Create a new remote */
293 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
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"));
298 cl_git_pass(git_remote_list(&list
, _repo
));
299 cl_assert(list
.count
== 7);
300 git_strarray_free(&list
);
302 git_config_free(cfg
);
305 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
307 git_remote_free(_remote
);
310 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_lookup(&_remote
, _repo
, "just-left-few-minutes-ago"));
313 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
315 git_remote_free(_remote
);
318 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_lookup(&_remote
, _repo
, "Inv@{id"));
322 * $ git remote add addtest http://github.com/libgit2/libgit2
327 * url = http://github.com/libgit2/libgit2
328 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
330 void test_network_remote_remotes__add(void)
332 git_remote_free(_remote
);
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
));
338 git_remote_free(_remote
);
341 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "addtest"));
342 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
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");
351 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
357 git_remote_create(&remote
, _repo
, NULL
, "git://github.com/libgit2/libgit2"));
360 void test_network_remote_remotes__cannot_save_an_inmemory_remote(void)
364 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
366 cl_assert_equal_p(NULL
, git_remote_name(remote
));
368 cl_git_fail(git_remote_save(remote
));
369 git_remote_free(remote
);
372 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
374 git_remote
*remote
= NULL
;
378 git_remote_create(&remote
, _repo
, "Inv@{id", "git://github.com/libgit2/libgit2"));
379 cl_assert_equal_p(remote
, NULL
);
383 git_remote_create(&remote
, _repo
, "", "git://github.com/libgit2/libgit2"));
384 cl_assert_equal_p(remote
, NULL
);
387 void test_network_remote_remotes__tagopt(void)
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");
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");
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);
402 void test_network_remote_remotes__can_load_with_an_empty_url(void)
404 git_remote
*remote
= NULL
;
406 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-url"));
408 cl_assert(remote
->url
== NULL
);
409 cl_assert(remote
->pushurl
== NULL
);
411 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
413 cl_assert(giterr_last() != NULL
);
414 cl_assert(giterr_last()->klass
== GITERR_INVALID
);
416 git_remote_free(remote
);
419 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
421 git_remote
*remote
= NULL
;
423 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-pushurl"));
425 cl_assert(remote
->url
== NULL
);
426 cl_assert(remote
->pushurl
== NULL
);
428 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
430 git_remote_free(remote
);
433 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
435 git_remote
*remote
= NULL
;
438 git_remote_lookup(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
441 void assert_cannot_create_remote(const char *name
, int expected_error
)
443 git_remote
*remote
= NULL
;
446 git_remote_create(&remote
, _repo
, name
, "git://github.com/libgit2/libgit2"),
449 cl_assert_equal_p(remote
, NULL
);
452 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
454 assert_cannot_create_remote("test", GIT_EEXISTS
);
457 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
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
);
465 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
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
);
476 static const char *fetch_refspecs
[] = {
477 "+refs/heads/*:refs/remotes/origin/*",
478 "refs/tags/*:refs/tags/*",
479 "+refs/pull/*:refs/pull/*",
482 static const char *push_refspecs
[] = {
483 "refs/heads/*:refs/heads/*",
484 "refs/tags/*:refs/tags/*",
485 "refs/notes/*:refs/notes/*",
488 void test_network_remote_remotes__query_refspecs(void)
494 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, "git://github.com/libgit2/libgit2", NULL
));
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
]));
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
]);
505 git_strarray_free(&array
);
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
]);
511 git_strarray_free(&array
);
513 git_remote_free(remote
);
516 static int remote_single_branch(git_remote
**out
, git_repository
*repo
, const char *name
, const char *url
, void *payload
)
518 char *fetch_refspecs
[] = {
519 "refs/heads/first-merge:refs/remotes/origin/first-merge",
521 git_strarray fetch_refspecs_strarray
= {
528 cl_git_pass(git_remote_create(out
, repo
, name
, url
));
529 cl_git_pass(git_remote_set_fetch_refspecs(*out
, &fetch_refspecs_strarray
));
534 void test_network_remote_remotes__fetch_from_anonymous(void)
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
);
544 void test_network_remote_remotes__single_branch(void)
546 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
547 git_repository
*repo
;
551 opts
.remote_cb
= remote_single_branch
;
552 opts
.checkout_branch
= "first-merge";
554 cl_git_pass(git_clone(&repo
, "git://github.com/libgit2/TestGitRepository", "./single-branch", &opts
));
555 cl_git_pass(git_reference_list(&refs
, repo
));
557 for (i
= 0; i
< refs
.count
; i
++) {
558 if (!git__prefixcmp(refs
.strings
[i
], "refs/heads/"))
561 cl_assert_equal_i(1, count
);
563 git_strarray_free(&refs
);
564 git_repository_free(repo
);
567 void test_network_remote_remotes__restricted_refspecs(void)
569 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
570 git_repository
*repo
;
572 opts
.remote_cb
= remote_single_branch
;
574 cl_git_fail_with(GIT_EINVALIDSPEC
, git_clone(&repo
, "git://github.com/libgit2/TestGitRepository", "./restrict-refspec", &opts
));