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")));
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);
132 cl_git_fail_with(GIT_EINVALIDSPEC
, git_remote_add_fetch(_repo
, "test", "refs/*/foo/*:refs/*"));
135 void test_network_remote_remotes__dup(void)
140 cl_git_pass(git_remote_dup(&dup
, _remote
));
142 cl_assert_equal_s(git_remote_name(dup
), git_remote_name(_remote
));
143 cl_assert_equal_s(git_remote_url(dup
), git_remote_url(_remote
));
144 cl_assert_equal_s(git_remote_pushurl(dup
), git_remote_pushurl(_remote
));
146 cl_git_pass(git_remote_get_fetch_refspecs(&array
, _remote
));
147 cl_assert_equal_i(1, (int)array
.count
);
148 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array
.strings
[0]);
149 git_strarray_free(&array
);
151 cl_git_pass(git_remote_get_push_refspecs(&array
, _remote
));
152 cl_assert_equal_i(0, (int)array
.count
);
153 git_strarray_free(&array
);
155 git_remote_free(dup
);
158 void test_network_remote_remotes__add_pushspec(void)
162 size
= git_remote_refspec_count(_remote
);
164 cl_git_pass(git_remote_add_push(_repo
, "test", "refs/*:refs/*"));
167 git_remote_free(_remote
);
168 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
170 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
172 _refspec
= git_remote_get_refspec(_remote
, size
- 1);
173 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/*");
174 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/*");
175 cl_assert_equal_s(git_refspec_string(_refspec
), "refs/*:refs/*");
177 cl_assert_equal_b(_refspec
->push
, true);
180 void test_network_remote_remotes__fnmatch(void)
182 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
183 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
186 void test_network_remote_remotes__transform(void)
188 git_buf ref
= GIT_BUF_INIT
;
190 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
191 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
195 void test_network_remote_remotes__transform_destination_to_source(void)
197 git_buf ref
= GIT_BUF_INIT
;
199 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
200 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
204 void test_network_remote_remotes__missing_refspecs(void)
208 git_remote_free(_remote
);
211 cl_git_pass(git_repository_config(&cfg
, _repo
));
212 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
213 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "specless"));
215 git_config_free(cfg
);
218 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
223 "refs/tags/*:refs/tags/*",
225 git_strarray specs
= {
230 cl_git_pass(git_remote_create(&remote
, _repo
, "taggy", git_repository_path(_repo
)));
233 * Set the current branch's upstream remote to a dummy ref so we call into the code
234 * which tries to check for the current branch's upstream in the refspecs
236 cl_git_pass(git_repository_config(&config
, _repo
));
237 cl_git_pass(git_config_set_string(config
, "branch.master.remote", "taggy"));
238 cl_git_pass(git_config_set_string(config
, "branch.master.merge", "refs/heads/foo"));
240 cl_git_pass(git_remote_fetch(remote
, &specs
, NULL
, NULL
));
242 git_remote_free(remote
);
245 void test_network_remote_remotes__list(void)
250 cl_git_pass(git_remote_list(&list
, _repo
));
251 cl_assert(list
.count
== 5);
252 git_strarray_free(&list
);
254 cl_git_pass(git_repository_config(&cfg
, _repo
));
256 /* Create a new remote */
257 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
259 /* Update a remote (previously without any url/pushurl entry) */
260 cl_git_pass(git_config_set_string(cfg
, "remote.no-remote-url.pushurl", "http://example.com"));
262 cl_git_pass(git_remote_list(&list
, _repo
));
263 cl_assert(list
.count
== 7);
264 git_strarray_free(&list
);
266 git_config_free(cfg
);
269 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
271 git_remote_free(_remote
);
274 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_lookup(&_remote
, _repo
, "just-left-few-minutes-ago"));
277 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
279 git_remote_free(_remote
);
282 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_lookup(&_remote
, _repo
, "Inv@{id"));
286 * $ git remote add addtest http://github.com/libgit2/libgit2
291 * url = http://github.com/libgit2/libgit2
292 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
294 void test_network_remote_remotes__add(void)
296 git_remote_free(_remote
);
299 cl_git_pass(git_remote_create(&_remote
, _repo
, "addtest", "http://github.com/libgit2/libgit2"));
300 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
302 git_remote_free(_remote
);
305 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "addtest"));
306 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
308 _refspec
= git_vector_get(&_remote
->refspecs
, 0);
309 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec
));
310 cl_assert(git_refspec_force(_refspec
) == 1);
311 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec
));
312 cl_assert_equal_s(git_remote_url(_remote
), "http://github.com/libgit2/libgit2");
315 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
321 git_remote_create(&remote
, _repo
, NULL
, "git://github.com/libgit2/libgit2"));
324 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
326 git_remote
*remote
= NULL
;
330 git_remote_create(&remote
, _repo
, "Inv@{id", "git://github.com/libgit2/libgit2"));
331 cl_assert_equal_p(remote
, NULL
);
335 git_remote_create(&remote
, _repo
, "", "git://github.com/libgit2/libgit2"));
336 cl_assert_equal_p(remote
, NULL
);
339 void test_network_remote_remotes__tagopt(void)
341 const char *name
= git_remote_name(_remote
);
343 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_ALL
);
344 assert_config_entry_value(_repo
, "remote.test.tagopt", "--tags");
346 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
347 assert_config_entry_value(_repo
, "remote.test.tagopt", "--no-tags");
349 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_AUTO
);
350 assert_config_entry_existence(_repo
, "remote.test.tagopt", false);
353 void test_network_remote_remotes__can_load_with_an_empty_url(void)
355 git_remote
*remote
= NULL
;
357 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-url"));
359 cl_assert(remote
->url
== NULL
);
360 cl_assert(remote
->pushurl
== NULL
);
362 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
364 cl_assert(giterr_last() != NULL
);
365 cl_assert(giterr_last()->klass
== GITERR_INVALID
);
367 git_remote_free(remote
);
370 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
372 git_remote
*remote
= NULL
;
374 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-pushurl"));
376 cl_assert(remote
->url
== NULL
);
377 cl_assert(remote
->pushurl
== NULL
);
379 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
));
381 git_remote_free(remote
);
384 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
386 git_remote
*remote
= NULL
;
389 git_remote_lookup(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
392 void assert_cannot_create_remote(const char *name
, int expected_error
)
394 git_remote
*remote
= NULL
;
397 git_remote_create(&remote
, _repo
, name
, "git://github.com/libgit2/libgit2"),
400 cl_assert_equal_p(remote
, NULL
);
403 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
405 assert_cannot_create_remote("test", GIT_EEXISTS
);
408 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
410 assert_cannot_create_remote("/", GIT_EINVALIDSPEC
);
411 assert_cannot_create_remote("//", GIT_EINVALIDSPEC
);
412 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC
);
413 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC
);
416 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
421 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
422 git_remote_get_fetch_refspecs(&array
, remote
);
423 cl_assert_equal_s("+refs/*:refs/*", array
.strings
[0]);
424 git_remote_free(remote
);
427 static const char *fetch_refspecs
[] = {
428 "+refs/heads/*:refs/remotes/origin/*",
429 "refs/tags/*:refs/tags/*",
430 "+refs/pull/*:refs/pull/*",
433 static const char *push_refspecs
[] = {
434 "refs/heads/*:refs/heads/*",
435 "refs/tags/*:refs/tags/*",
436 "refs/notes/*:refs/notes/*",
439 void test_network_remote_remotes__query_refspecs(void)
445 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "query", "git://github.com/libgit2/libgit2", NULL
));
446 git_remote_free(remote
);
448 for (i
= 0; i
< 3; i
++) {
449 cl_git_pass(git_remote_add_fetch(_repo
, "query", fetch_refspecs
[i
]));
450 cl_git_pass(git_remote_add_push(_repo
, "query", push_refspecs
[i
]));
453 cl_git_pass(git_remote_lookup(&remote
, _repo
, "query"));
455 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
456 for (i
= 0; i
< 3; i
++) {
457 cl_assert_equal_s(fetch_refspecs
[i
], array
.strings
[i
]);
459 git_strarray_free(&array
);
461 cl_git_pass(git_remote_get_push_refspecs(&array
, remote
));
462 for (i
= 0; i
< 3; i
++) {
463 cl_assert_equal_s(push_refspecs
[i
], array
.strings
[i
]);
465 git_strarray_free(&array
);
467 git_remote_free(remote
);
468 git_remote_delete(_repo
, "test");