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_buf url
= GIT_BUF_INIT
;
32 git_remote
*_remote2
= NULL
;
34 cl_assert_equal_s(git_remote_name(_remote
), "test");
35 cl_assert_equal_s(git_remote_url(_remote
), "git://github.com/libgit2/libgit2");
36 cl_assert(git_remote_pushurl(_remote
) == NULL
);
38 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_FETCH
, NULL
));
39 cl_assert_equal_s(url
.ptr
, "git://github.com/libgit2/libgit2");
41 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_PUSH
, NULL
));
42 cl_assert_equal_s(url
.ptr
, "git://github.com/libgit2/libgit2");
44 cl_git_pass(git_remote_lookup(&_remote2
, _repo
, "test_with_pushurl"));
45 cl_assert_equal_s(git_remote_name(_remote2
), "test_with_pushurl");
46 cl_assert_equal_s(git_remote_url(_remote2
), "git://github.com/libgit2/fetchlibgit2");
47 cl_assert_equal_s(git_remote_pushurl(_remote2
), "git://github.com/libgit2/pushlibgit2");
49 cl_git_pass(git_remote__urlfordirection(&url
, _remote2
, GIT_DIRECTION_FETCH
, NULL
));
50 cl_assert_equal_s(url
.ptr
, "git://github.com/libgit2/fetchlibgit2");
52 cl_git_pass(git_remote__urlfordirection(&url
, _remote2
, GIT_DIRECTION_PUSH
, NULL
));
53 cl_assert_equal_s(url
.ptr
, "git://github.com/libgit2/pushlibgit2");
55 git_remote_free(_remote2
);
56 git_buf_dispose(&url
);
59 static int urlresolve_callback(git_buf
*url_resolved
, const char *url
, int direction
, void *payload
)
61 cl_assert(strcmp(url
, "git://github.com/libgit2/libgit2") == 0);
62 cl_assert(strcmp(payload
, "payload") == 0);
63 cl_assert(url_resolved
->size
== 0);
65 if (direction
== GIT_DIRECTION_PUSH
)
66 git_buf_sets(url_resolved
, "pushresolve");
67 if (direction
== GIT_DIRECTION_FETCH
)
68 git_buf_sets(url_resolved
, "fetchresolve");
73 void test_network_remote_remotes__urlresolve(void)
75 git_buf url
= GIT_BUF_INIT
;
77 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
78 callbacks
.resolve_url
= urlresolve_callback
;
79 callbacks
.payload
= "payload";
81 cl_assert_equal_s(git_remote_name(_remote
), "test");
82 cl_assert_equal_s(git_remote_url(_remote
), "git://github.com/libgit2/libgit2");
83 cl_assert(git_remote_pushurl(_remote
) == NULL
);
85 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_FETCH
, &callbacks
));
86 cl_assert_equal_s(url
.ptr
, "fetchresolve");
88 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_PUSH
, &callbacks
));
89 cl_assert_equal_s(url
.ptr
, "pushresolve");
91 git_buf_dispose(&url
);
94 static int urlresolve_passthrough_callback(git_buf
*url_resolved
, const char *url
, int direction
, void *payload
)
96 GIT_UNUSED(url_resolved
);
98 GIT_UNUSED(direction
);
100 return GIT_PASSTHROUGH
;
103 void test_network_remote_remotes__urlresolve_passthrough(void)
105 git_buf url
= GIT_BUF_INIT
;
106 const char *orig_url
= "git://github.com/libgit2/libgit2";
108 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
109 callbacks
.resolve_url
= urlresolve_passthrough_callback
;
111 cl_assert_equal_s(git_remote_name(_remote
), "test");
112 cl_assert_equal_s(git_remote_url(_remote
), orig_url
);
113 cl_assert(git_remote_pushurl(_remote
) == NULL
);
115 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_FETCH
, &callbacks
));
116 cl_assert_equal_s(url
.ptr
, orig_url
);
118 cl_git_pass(git_remote__urlfordirection(&url
, _remote
, GIT_DIRECTION_PUSH
, &callbacks
));
119 cl_assert_equal_s(url
.ptr
, orig_url
);
121 git_buf_dispose(&url
);
124 void test_network_remote_remotes__pushurl(void)
126 const char *name
= git_remote_name(_remote
);
129 cl_git_pass(git_remote_set_pushurl(_repo
, name
, "git://github.com/libgit2/notlibgit2"));
130 cl_git_pass(git_remote_lookup(&mod
, _repo
, name
));
131 cl_assert_equal_s(git_remote_pushurl(mod
), "git://github.com/libgit2/notlibgit2");
132 git_remote_free(mod
);
134 cl_git_pass(git_remote_set_pushurl(_repo
, name
, NULL
));
135 cl_git_pass(git_remote_lookup(&mod
, _repo
, name
));
136 cl_assert(git_remote_pushurl(mod
) == NULL
);
137 git_remote_free(mod
);
140 void test_network_remote_remotes__error_when_not_found(void)
143 cl_git_fail_with(git_remote_lookup(&r
, _repo
, "does-not-exist"), GIT_ENOTFOUND
);
145 cl_assert(git_error_last() != NULL
);
146 cl_assert(git_error_last()->klass
== GIT_ERROR_CONFIG
);
149 void test_network_remote_remotes__error_when_no_push_available(void)
152 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
162 cl_git_pass(git_remote_create_anonymous(&r
, _repo
, cl_fixture("testrepo.git")));
164 callbacks
.transport
= git_transport_local
;
165 cl_git_pass(git_remote_connect(r
, GIT_DIRECTION_PUSH
, &callbacks
, NULL
, NULL
));
167 /* Make sure that push is really not available */
168 r
->transport
->push
= NULL
;
170 cl_git_fail_with(-1, git_remote_upload(r
, &arr
, NULL
));
175 void test_network_remote_remotes__refspec_parsing(void)
177 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/heads/*");
178 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/remotes/test/*");
181 void test_network_remote_remotes__add_fetchspec(void)
185 size
= git_remote_refspec_count(_remote
);
187 cl_git_pass(git_remote_add_fetch(_repo
, "test", "refs/*:refs/*"));
190 git_remote_free(_remote
);
191 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
193 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
195 _refspec
= git_remote_get_refspec(_remote
, size
- 1);
196 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/*");
197 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/*");
198 cl_assert_equal_s(git_refspec_string(_refspec
), "refs/*:refs/*");
199 cl_assert_equal_b(_refspec
->push
, false);
201 cl_git_fail_with(GIT_EINVALIDSPEC
, git_remote_add_fetch(_repo
, "test", "refs/*/foo/*:refs/*"));
204 void test_network_remote_remotes__dup(void)
209 cl_git_pass(git_remote_dup(&dup
, _remote
));
211 cl_assert_equal_s(git_remote_name(dup
), git_remote_name(_remote
));
212 cl_assert_equal_s(git_remote_url(dup
), git_remote_url(_remote
));
213 cl_assert_equal_s(git_remote_pushurl(dup
), git_remote_pushurl(_remote
));
215 cl_git_pass(git_remote_get_fetch_refspecs(&array
, _remote
));
216 cl_assert_equal_i(1, (int)array
.count
);
217 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array
.strings
[0]);
218 git_strarray_dispose(&array
);
220 cl_git_pass(git_remote_get_push_refspecs(&array
, _remote
));
221 cl_assert_equal_i(0, (int)array
.count
);
222 git_strarray_dispose(&array
);
224 git_remote_free(dup
);
227 void test_network_remote_remotes__add_pushspec(void)
231 size
= git_remote_refspec_count(_remote
);
233 cl_git_pass(git_remote_add_push(_repo
, "test", "refs/*:refs/*"));
236 git_remote_free(_remote
);
237 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "test"));
239 cl_assert_equal_i((int)size
, (int)git_remote_refspec_count(_remote
));
241 _refspec
= git_remote_get_refspec(_remote
, size
- 1);
242 cl_assert_equal_s(git_refspec_src(_refspec
), "refs/*");
243 cl_assert_equal_s(git_refspec_dst(_refspec
), "refs/*");
244 cl_assert_equal_s(git_refspec_string(_refspec
), "refs/*:refs/*");
246 cl_assert_equal_b(_refspec
->push
, true);
249 void test_network_remote_remotes__fnmatch(void)
251 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/master"));
252 cl_assert(git_refspec_src_matches(_refspec
, "refs/heads/multi/level/branch"));
255 void test_network_remote_remotes__transform(void)
257 git_buf ref
= GIT_BUF_INIT
;
259 cl_git_pass(git_refspec_transform(&ref
, _refspec
, "refs/heads/master"));
260 cl_assert_equal_s(ref
.ptr
, "refs/remotes/test/master");
261 git_buf_dispose(&ref
);
264 void test_network_remote_remotes__transform_destination_to_source(void)
266 git_buf ref
= GIT_BUF_INIT
;
268 cl_git_pass(git_refspec_rtransform(&ref
, _refspec
, "refs/remotes/test/master"));
269 cl_assert_equal_s(ref
.ptr
, "refs/heads/master");
270 git_buf_dispose(&ref
);
273 void test_network_remote_remotes__missing_refspecs(void)
277 git_remote_free(_remote
);
280 cl_git_pass(git_repository_config(&cfg
, _repo
));
281 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
282 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "specless"));
284 git_config_free(cfg
);
287 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
292 "refs/tags/*:refs/tags/*",
294 git_strarray specs
= {
299 cl_git_pass(git_remote_create(&remote
, _repo
, "taggy", git_repository_path(_repo
)));
302 * Set the current branch's upstream remote to a dummy ref so we call into the code
303 * which tries to check for the current branch's upstream in the refspecs
305 cl_git_pass(git_repository_config(&config
, _repo
));
306 cl_git_pass(git_config_set_string(config
, "branch.master.remote", "taggy"));
307 cl_git_pass(git_config_set_string(config
, "branch.master.merge", "refs/heads/foo"));
309 cl_git_pass(git_remote_fetch(remote
, &specs
, NULL
, NULL
));
311 git_remote_free(remote
);
314 void test_network_remote_remotes__list(void)
319 cl_git_pass(git_remote_list(&list
, _repo
));
320 cl_assert(list
.count
== 5);
321 git_strarray_dispose(&list
);
323 cl_git_pass(git_repository_config(&cfg
, _repo
));
325 /* Create a new remote */
326 cl_git_pass(git_config_set_string(cfg
, "remote.specless.url", "http://example.com"));
328 /* Update a remote (previously without any url/pushurl entry) */
329 cl_git_pass(git_config_set_string(cfg
, "remote.no-remote-url.pushurl", "http://example.com"));
331 cl_git_pass(git_remote_list(&list
, _repo
));
332 cl_assert(list
.count
== 7);
333 git_strarray_dispose(&list
);
335 git_config_free(cfg
);
338 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
340 git_remote_free(_remote
);
343 cl_assert_equal_i(GIT_ENOTFOUND
, git_remote_lookup(&_remote
, _repo
, "just-left-few-minutes-ago"));
346 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
348 git_remote_free(_remote
);
351 cl_assert_equal_i(GIT_EINVALIDSPEC
, git_remote_lookup(&_remote
, _repo
, "Inv@{id"));
355 * $ git remote add addtest http://github.com/libgit2/libgit2
360 * url = http://github.com/libgit2/libgit2
361 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
363 void test_network_remote_remotes__add(void)
365 git_remote_free(_remote
);
368 cl_git_pass(git_remote_create(&_remote
, _repo
, "addtest", "http://github.com/libgit2/libgit2"));
369 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
371 git_remote_free(_remote
);
374 cl_git_pass(git_remote_lookup(&_remote
, _repo
, "addtest"));
375 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, git_remote_autotag(_remote
));
377 _refspec
= git_vector_get(&_remote
->refspecs
, 0);
378 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec
));
379 cl_assert(git_refspec_force(_refspec
) == 1);
380 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec
));
381 cl_assert_equal_s(git_remote_url(_remote
), "http://github.com/libgit2/libgit2");
384 void test_network_remote_remotes__tagopt(void)
386 const char *name
= git_remote_name(_remote
);
388 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_ALL
);
389 assert_config_entry_value(_repo
, "remote.test.tagopt", "--tags");
391 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
392 assert_config_entry_value(_repo
, "remote.test.tagopt", "--no-tags");
394 git_remote_set_autotag(_repo
, name
, GIT_REMOTE_DOWNLOAD_TAGS_AUTO
);
395 assert_config_entry_existence(_repo
, "remote.test.tagopt", false);
398 void test_network_remote_remotes__can_load_with_an_empty_url(void)
400 git_remote
*remote
= NULL
;
402 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-url"));
404 cl_assert(remote
->url
== NULL
);
405 cl_assert(remote
->pushurl
== NULL
);
407 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
409 cl_assert(git_error_last() != NULL
);
410 cl_assert(git_error_last()->klass
== GIT_ERROR_INVALID
);
412 git_remote_free(remote
);
415 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
417 git_remote
*remote
= NULL
;
419 cl_git_pass(git_remote_lookup(&remote
, _repo
, "empty-remote-pushurl"));
421 cl_assert(remote
->url
== NULL
);
422 cl_assert(remote
->pushurl
== NULL
);
424 cl_git_fail(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
426 git_remote_free(remote
);
429 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
431 git_remote
*remote
= NULL
;
434 git_remote_lookup(&remote
, _repo
, "no-remote-url"), GIT_ENOTFOUND
);
437 static const char *fetch_refspecs
[] = {
438 "+refs/heads/*:refs/remotes/origin/*",
439 "refs/tags/*:refs/tags/*",
440 "+refs/pull/*:refs/pull/*",
443 static const char *push_refspecs
[] = {
444 "refs/heads/*:refs/heads/*",
445 "refs/tags/*:refs/tags/*",
446 "refs/notes/*:refs/notes/*",
449 void test_network_remote_remotes__query_refspecs(void)
455 cl_git_pass(git_remote_create_with_fetchspec(&remote
, _repo
, "query", "git://github.com/libgit2/libgit2", NULL
));
456 git_remote_free(remote
);
458 for (i
= 0; i
< 3; i
++) {
459 cl_git_pass(git_remote_add_fetch(_repo
, "query", fetch_refspecs
[i
]));
460 cl_git_pass(git_remote_add_push(_repo
, "query", push_refspecs
[i
]));
463 cl_git_pass(git_remote_lookup(&remote
, _repo
, "query"));
465 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
466 for (i
= 0; i
< 3; i
++) {
467 cl_assert_equal_s(fetch_refspecs
[i
], array
.strings
[i
]);
469 git_strarray_dispose(&array
);
471 cl_git_pass(git_remote_get_push_refspecs(&array
, remote
));
472 for (i
= 0; i
< 3; i
++) {
473 cl_assert_equal_s(push_refspecs
[i
], array
.strings
[i
]);
475 git_strarray_dispose(&array
);
477 git_remote_free(remote
);
478 git_remote_delete(_repo
, "test");