]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/remotes.c
Merge pull request #3165 from ethomson/downcase
[libgit2.git] / tests / network / remote / remotes.c
1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3 #include "buffer.h"
4 #include "refspec.h"
5 #include "remote.h"
6
7 static git_remote *_remote;
8 static git_repository *_repo;
9 static const git_refspec *_refspec;
10
11 void test_network_remote_remotes__initialize(void)
12 {
13 _repo = cl_git_sandbox_init("testrepo.git");
14
15 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
16
17 _refspec = git_remote_get_refspec(_remote, 0);
18 cl_assert(_refspec != NULL);
19 }
20
21 void test_network_remote_remotes__cleanup(void)
22 {
23 git_remote_free(_remote);
24 _remote = NULL;
25
26 cl_git_sandbox_cleanup();
27 }
28
29 void test_network_remote_remotes__parsing(void)
30 {
31 git_remote *_remote2 = NULL;
32
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);
36
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");
41
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");
46
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");
51
52 git_remote_free(_remote2);
53 }
54
55 void test_network_remote_remotes__pushurl(void)
56 {
57 const char *name = git_remote_name(_remote);
58 git_remote *mod;
59
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");
63 git_remote_free(mod);
64
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);
68 git_remote_free(mod);
69 }
70
71 void test_network_remote_remotes__error_when_not_found(void)
72 {
73 git_remote *r;
74 cl_git_fail_with(git_remote_lookup(&r, _repo, "does-not-exist"), GIT_ENOTFOUND);
75
76 cl_assert(giterr_last() != NULL);
77 cl_assert(giterr_last()->klass == GITERR_CONFIG);
78 }
79
80 void test_network_remote_remotes__error_when_no_push_available(void)
81 {
82 git_remote *r;
83 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
84 char *specs = {
85 "refs/heads/master",
86 };
87 git_strarray arr = {
88 &specs,
89 1,
90 };
91
92
93 cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git")));
94
95 callbacks.transport = git_transport_local;
96 cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, &callbacks));
97
98 /* Make sure that push is really not available */
99 r->transport->push = NULL;
100
101 cl_git_fail_with(-1, git_remote_upload(r, &arr, NULL));
102
103 git_remote_free(r);
104 }
105
106 void test_network_remote_remotes__refspec_parsing(void)
107 {
108 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
109 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
110 }
111
112 void test_network_remote_remotes__add_fetchspec(void)
113 {
114 size_t size;
115
116 size = git_remote_refspec_count(_remote);
117
118 cl_git_pass(git_remote_add_fetch(_repo, "test", "refs/*:refs/*"));
119 size++;
120
121 git_remote_free(_remote);
122 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
123
124 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
125
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);
131
132 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_add_fetch(_repo, "test", "refs/*/foo/*:refs/*"));
133 }
134
135 void test_network_remote_remotes__dup(void)
136 {
137 git_strarray array;
138 git_remote *dup;
139
140 cl_git_pass(git_remote_dup(&dup, _remote));
141
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));
145
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);
150
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);
154
155 git_remote_free(dup);
156 }
157
158 void test_network_remote_remotes__add_pushspec(void)
159 {
160 size_t size;
161
162 size = git_remote_refspec_count(_remote);
163
164 cl_git_pass(git_remote_add_push(_repo, "test", "refs/*:refs/*"));
165 size++;
166
167 git_remote_free(_remote);
168 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
169
170 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
171
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/*");
176
177 cl_assert_equal_b(_refspec->push, true);
178 }
179
180 void test_network_remote_remotes__fnmatch(void)
181 {
182 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
183 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
184 }
185
186 void test_network_remote_remotes__transform(void)
187 {
188 git_buf ref = GIT_BUF_INIT;
189
190 cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master"));
191 cl_assert_equal_s(ref.ptr, "refs/remotes/test/master");
192 git_buf_free(&ref);
193 }
194
195 void test_network_remote_remotes__transform_destination_to_source(void)
196 {
197 git_buf ref = GIT_BUF_INIT;
198
199 cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master"));
200 cl_assert_equal_s(ref.ptr, "refs/heads/master");
201 git_buf_free(&ref);
202 }
203
204 void test_network_remote_remotes__missing_refspecs(void)
205 {
206 git_config *cfg;
207
208 git_remote_free(_remote);
209 _remote = NULL;
210
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"));
214
215 git_config_free(cfg);
216 }
217
218 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
219 {
220 git_config *config;
221 git_remote *remote;
222 char *specstr[] = {
223 "refs/tags/*:refs/tags/*",
224 };
225 git_strarray specs = {
226 specstr,
227 1,
228 };
229
230 cl_git_pass(git_remote_create(&remote, _repo, "taggy", git_repository_path(_repo)));
231
232 /*
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
235 */
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"));
239
240 cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
241
242 git_remote_free(remote);
243 }
244
245 void test_network_remote_remotes__list(void)
246 {
247 git_strarray list;
248 git_config *cfg;
249
250 cl_git_pass(git_remote_list(&list, _repo));
251 cl_assert(list.count == 5);
252 git_strarray_free(&list);
253
254 cl_git_pass(git_repository_config(&cfg, _repo));
255
256 /* Create a new remote */
257 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
258
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"));
261
262 cl_git_pass(git_remote_list(&list, _repo));
263 cl_assert(list.count == 7);
264 git_strarray_free(&list);
265
266 git_config_free(cfg);
267 }
268
269 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
270 {
271 git_remote_free(_remote);
272 _remote = NULL;
273
274 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_lookup(&_remote, _repo, "just-left-few-minutes-ago"));
275 }
276
277 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
278 {
279 git_remote_free(_remote);
280 _remote = NULL;
281
282 cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_lookup(&_remote, _repo, "Inv@{id"));
283 }
284
285 /*
286 * $ git remote add addtest http://github.com/libgit2/libgit2
287 *
288 * $ cat .git/config
289 * [...]
290 * [remote "addtest"]
291 * url = http://github.com/libgit2/libgit2
292 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
293 */
294 void test_network_remote_remotes__add(void)
295 {
296 git_remote_free(_remote);
297 _remote = NULL;
298
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));
301
302 git_remote_free(_remote);
303 _remote = NULL;
304
305 cl_git_pass(git_remote_lookup(&_remote, _repo, "addtest"));
306 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
307
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");
313 }
314
315 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
316 {
317 git_remote *remote;
318
319 cl_assert_equal_i(
320 GIT_EINVALIDSPEC,
321 git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
322 }
323
324 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
325 {
326 git_remote *remote = NULL;
327
328 cl_assert_equal_i(
329 GIT_EINVALIDSPEC,
330 git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
331 cl_assert_equal_p(remote, NULL);
332
333 cl_assert_equal_i(
334 GIT_EINVALIDSPEC,
335 git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
336 cl_assert_equal_p(remote, NULL);
337 }
338
339 void test_network_remote_remotes__tagopt(void)
340 {
341 const char *name = git_remote_name(_remote);
342
343 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
344 assert_config_entry_value(_repo, "remote.test.tagopt", "--tags");
345
346 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
347 assert_config_entry_value(_repo, "remote.test.tagopt", "--no-tags");
348
349 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
350 assert_config_entry_existence(_repo, "remote.test.tagopt", false);
351 }
352
353 void test_network_remote_remotes__can_load_with_an_empty_url(void)
354 {
355 git_remote *remote = NULL;
356
357 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-url"));
358
359 cl_assert(remote->url == NULL);
360 cl_assert(remote->pushurl == NULL);
361
362 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL));
363
364 cl_assert(giterr_last() != NULL);
365 cl_assert(giterr_last()->klass == GITERR_INVALID);
366
367 git_remote_free(remote);
368 }
369
370 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
371 {
372 git_remote *remote = NULL;
373
374 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-pushurl"));
375
376 cl_assert(remote->url == NULL);
377 cl_assert(remote->pushurl == NULL);
378
379 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL));
380
381 git_remote_free(remote);
382 }
383
384 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
385 {
386 git_remote *remote = NULL;
387
388 cl_git_fail_with(
389 git_remote_lookup(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
390 }
391
392 void assert_cannot_create_remote(const char *name, int expected_error)
393 {
394 git_remote *remote = NULL;
395
396 cl_git_fail_with(
397 git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"),
398 expected_error);
399
400 cl_assert_equal_p(remote, NULL);
401 }
402
403 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
404 {
405 assert_cannot_create_remote("test", GIT_EEXISTS);
406 }
407
408 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
409 {
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);
414 }
415
416 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
417 {
418 git_remote *remote;
419 git_strarray array;
420
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);
425 }
426
427 static const char *fetch_refspecs[] = {
428 "+refs/heads/*:refs/remotes/origin/*",
429 "refs/tags/*:refs/tags/*",
430 "+refs/pull/*:refs/pull/*",
431 };
432
433 static const char *push_refspecs[] = {
434 "refs/heads/*:refs/heads/*",
435 "refs/tags/*:refs/tags/*",
436 "refs/notes/*:refs/notes/*",
437 };
438
439 void test_network_remote_remotes__query_refspecs(void)
440 {
441 git_remote *remote;
442 git_strarray array;
443 int i;
444
445 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "query", "git://github.com/libgit2/libgit2", NULL));
446 git_remote_free(remote);
447
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]));
451 }
452
453 cl_git_pass(git_remote_lookup(&remote, _repo, "query"));
454
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]);
458 }
459 git_strarray_free(&array);
460
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]);
464 }
465 git_strarray_free(&array);
466
467 git_remote_free(remote);
468 git_remote_delete(_repo, "test");
469 }