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)
83 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
93 cl_git_pass(git_remote_create_anonymous(&r
, _repo
, cl_fixture("testrepo.git"), NULL
));
95 callbacks
.transport
= git_transport_local
;
96 cl_git_pass(git_remote_connect(r
, GIT_DIRECTION_PUSH
, &callbacks
));
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(_repo
, "test", "refs/*:refs/*"));
121 git_remote_free(_remote
);
122 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
124 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
126 _refspec
= git_remote_get_refspec(_remote
, size
- 1);
127 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/*");
128 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/*");
129 cl_assert_equal_s(git_refspec_string(_refspec
), "refs/*:refs/*");
130 cl_assert_equal_b(_refspec
->push
, false);
133 void test_network_remote_remotes__dup(void)
138 cl_git_pass(git_remote_dup(&dup
, _remote
));
140 cl_assert_equal_s(git_remote_name(dup
), git_remote_name(_remote
));
141 cl_assert_equal_s(git_remote_url(dup
), git_remote_url(_remote
));
142 cl_assert_equal_s(git_remote_pushurl(dup
), git_remote_pushurl(_remote
));
144 cl_git_pass(git_remote_get_fetch_refspecs(&array
, _remote
));
145 cl_assert_equal_i(1, (int)array
.count
);
146 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array
.strings
[0]);
147 git_strarray_free(&array
);
149 cl_git_pass(git_remote_get_push_refspecs(&array
, _remote
));
150 cl_assert_equal_i(0, (int)array
.count
);
151 git_strarray_free(&array
);
153 git_remote_free(dup
);
156 void test_network_remote_remotes__add_pushspec(void)
160 size
= git_remote_refspec_count(_remote
);
162 cl_git_pass(git_remote_add_push(_repo
, "test", "refs/*:refs/*"));
165 git_remote_free(_remote
);
166 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
168 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
170 _refspec
= git_remote_get_refspec(_remote
, size
- 1);
171 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/*");
172 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/*");
173 cl_assert_equal_s(git_refspec_string(_refspec
), "refs/*:refs/*");
175 cl_assert_equal_b(_refspec
->push
, true);
178 void test_network_remote_remotes__fnmatch(void)
180 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
181 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
184 void test_network_remote_remotes__transform(void)
186 git_buf ref
= GIT_BUF_INIT
;
188 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
189 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
193 void test_network_remote_remotes__transform_destination_to_source(void)
195 git_buf ref
= GIT_BUF_INIT
;
197 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
198 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
202 void test_network_remote_remotes__missing_refspecs(void)
206 git_remote_free(_remote
);
209 cl_git_pass(git_repository_config(&cfg
, _repo
));
210 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
211 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "specless"));
213 git_config_free(cfg
);
216 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
221 "refs/tags/*:refs/tags/*",
223 git_strarray specs
= {
228 cl_git_pass(git_remote_create(&remote
, _repo
, "taggy", git_repository_path(_repo
)));
231 * Set the current branch's upstream remote to a dummy ref so we call into the code
232 * which tries to check for the current branch's upstream in the refspecs
234 cl_git_pass(git_repository_config(&config
, _repo
));
235 cl_git_pass(git_config_set_string(config
, "branch.master.remote", "taggy"));
236 cl_git_pass(git_config_set_string(config
, "branch.master.merge", "refs/heads/foo"));
238 cl_git_pass(git_remote_fetch(remote
, &specs
, NULL
, NULL
));
240 git_remote_free(remote
);
243 void test_network_remote_remotes__list(void)
248 cl_git_pass(git_remote_list(&list
, _repo
));
249 cl_assert(list
.count
== 5);
250 git_strarray_free(&list
);
252 cl_git_pass(git_repository_config(&cfg
, _repo
));
254 /* Create a new remote */
255 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
257 /* Update a remote (previously without any url/pushurl entry) */
258 cl_git_pass(git_config_set_string(cfg
, "remote.no-remote-url.pushurl", "http://example.com"));
260 cl_git_pass(git_remote_list(&list
, _repo
));
261 cl_assert(list
.count
== 7);
262 git_strarray_free(&list
);
264 git_config_free(cfg
);
267 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
269 git_remote_free(_remote
);
272 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_lookup(&_remote
, _repo
, "just-left-few-minutes-ago"));
275 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
277 git_remote_free(_remote
);
280 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_lookup(&_remote
, _repo
, "Inv@{id"));
284 * $ git remote add addtest http://github.com/libgit2/libgit2
289 * url = http://github.com/libgit2/libgit2
290 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
292 void test_network_remote_remotes__add(void)
294 git_remote_free(_remote
);
297 cl_git_pass(git_remote_create(&_remote
, _repo
, "addtest", "http://github.com/libgit2/libgit2"));
298 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
300 git_remote_free(_remote
);
303 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "addtest"));
304 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
306 _refspec
= git_vector_get(&_remote
->refspecs
, 0);
307 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec
));
308 cl_assert(git_refspec_force(_refspec
) == 1);
309 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec
));
310 cl_assert_equal_s(git_remote_url(_remote
), "http://github.com/libgit2/libgit2");
313 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
319 git_remote_create(&remote
, _repo
, NULL
, "git://github.com/libgit2/libgit2"));
322 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
324 git_remote
*remote
= NULL
;
328 git_remote_create(&remote
, _repo
, "Inv@{id", "git://github.com/libgit2/libgit2"));
329 cl_assert_equal_p(remote
, NULL
);
333 git_remote_create(&remote
, _repo
, "", "git://github.com/libgit2/libgit2"));
334 cl_assert_equal_p(remote
, NULL
);
337 void test_network_remote_remotes__tagopt(void)
339 const char *name
= git_remote_name(_remote
);
341 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_ALL
);
342 assert_config_entry_value(_repo
, "remote.test.tagopt", "--tags");
344 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
345 assert_config_entry_value(_repo
, "remote.test.tagopt", "--no-tags");
347 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_AUTO
);
348 assert_config_entry_existence(_repo
, "remote.test.tagopt", false);
351 void test_network_remote_remotes__can_load_with_an_empty_url(void)
353 git_remote
*remote
= NULL
;
355 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-url"));
357 cl_assert(remote
->url
== NULL
);
358 cl_assert(remote
->pushurl
== NULL
);
360 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
362 cl_assert(giterr_last() != NULL
);
363 cl_assert(giterr_last()->klass
== GITERR_INVALID
);
365 git_remote_free(remote
);
368 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
370 git_remote
*remote
= NULL
;
372 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-pushurl"));
374 cl_assert(remote
->url
== NULL
);
375 cl_assert(remote
->pushurl
== NULL
);
377 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
379 git_remote_free(remote
);
382 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
384 git_remote
*remote
= NULL
;
387 git_remote_lookup(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
390 void assert_cannot_create_remote(const char *name
, int expected_error
)
392 git_remote
*remote
= NULL
;
395 git_remote_create(&remote
, _repo
, name
, "git://github.com/libgit2/libgit2"),
398 cl_assert_equal_p(remote
, NULL
);
401 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
403 assert_cannot_create_remote("test", GIT_EEXISTS
);
406 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
408 assert_cannot_create_remote("/", GIT_EINVALIDSPEC
);
409 assert_cannot_create_remote("//", GIT_EINVALIDSPEC
);
410 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC
);
411 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC
);
414 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
419 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
420 git_remote_get_fetch_refspecs(&array
, remote
);
421 cl_assert_equal_s("+refs/*:refs/*", array
.strings
[0]);
422 git_remote_free(remote
);
425 static const char *fetch_refspecs
[] = {
426 "+refs/heads/*:refs/remotes/origin/*",
427 "refs/tags/*:refs/tags/*",
428 "+refs/pull/*:refs/pull/*",
431 static const char *push_refspecs
[] = {
432 "refs/heads/*:refs/heads/*",
433 "refs/tags/*:refs/tags/*",
434 "refs/notes/*:refs/notes/*",
437 void test_network_remote_remotes__query_refspecs(void)
443 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "query", "git://github.com/libgit2/libgit2", NULL
));
444 git_remote_free(remote
);
446 for (i
= 0; i
< 3; i
++) {
447 cl_git_pass(git_remote_add_fetch(_repo
, "query", fetch_refspecs
[i
]));
448 cl_git_pass(git_remote_add_push(_repo
, "query", push_refspecs
[i
]));
451 cl_git_pass(git_remote_lookup(&remote
, _repo
, "query"));
453 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
454 for (i
= 0; i
< 3; i
++) {
455 cl_assert_equal_s(fetch_refspecs
[i
], array
.strings
[i
]);
457 git_strarray_free(&array
);
459 cl_git_pass(git_remote_get_push_refspecs(&array
, remote
));
460 for (i
= 0; i
< 3; i
++) {
461 cl_assert_equal_s(push_refspecs
[i
], array
.strings
[i
]);
463 git_strarray_free(&array
);
465 git_remote_free(remote
);
466 git_remote_delete(_repo
, "test");
469 void test_network_remote_remotes__fetch_from_anonymous(void)
473 cl_git_pass(git_remote_create_anonymous(&remote
, _repo
, cl_fixture("testrepo.git"),
474 "refs/heads/*:refs/other/*"));
475 cl_git_pass(git_remote_fetch(remote
, NULL
, NULL
, NULL
));
476 git_remote_free(remote
);