]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/remotes.c
New upstream version 1.1.0+dfsg.1
[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_buf url = GIT_BUF_INIT;
32 git_remote *_remote2 = NULL;
33
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);
37
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");
40
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");
43
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");
48
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");
51
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");
54
55 git_remote_free(_remote2);
56 git_buf_dispose(&url);
57 }
58
59 static int urlresolve_callback(git_buf *url_resolved, const char *url, int direction, void *payload)
60 {
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);
64
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");
69
70 return GIT_OK;
71 }
72
73 void test_network_remote_remotes__urlresolve(void)
74 {
75 git_buf url = GIT_BUF_INIT;
76
77 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
78 callbacks.resolve_url = urlresolve_callback;
79 callbacks.payload = "payload";
80
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);
84
85 cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, &callbacks));
86 cl_assert_equal_s(url.ptr, "fetchresolve");
87
88 cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, &callbacks));
89 cl_assert_equal_s(url.ptr, "pushresolve");
90
91 git_buf_dispose(&url);
92 }
93
94 static int urlresolve_passthrough_callback(git_buf *url_resolved, const char *url, int direction, void *payload)
95 {
96 GIT_UNUSED(url_resolved);
97 GIT_UNUSED(url);
98 GIT_UNUSED(direction);
99 GIT_UNUSED(payload);
100 return GIT_PASSTHROUGH;
101 }
102
103 void test_network_remote_remotes__urlresolve_passthrough(void)
104 {
105 git_buf url = GIT_BUF_INIT;
106 const char *orig_url = "git://github.com/libgit2/libgit2";
107
108 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
109 callbacks.resolve_url = urlresolve_passthrough_callback;
110
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);
114
115 cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, &callbacks));
116 cl_assert_equal_s(url.ptr, orig_url);
117
118 cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, &callbacks));
119 cl_assert_equal_s(url.ptr, orig_url);
120
121 git_buf_dispose(&url);
122 }
123
124 void test_network_remote_remotes__pushurl(void)
125 {
126 const char *name = git_remote_name(_remote);
127 git_remote *mod;
128
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);
133
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);
138 }
139
140 void test_network_remote_remotes__error_when_not_found(void)
141 {
142 git_remote *r;
143 cl_git_fail_with(git_remote_lookup(&r, _repo, "does-not-exist"), GIT_ENOTFOUND);
144
145 cl_assert(git_error_last() != NULL);
146 cl_assert(git_error_last()->klass == GIT_ERROR_CONFIG);
147 }
148
149 void test_network_remote_remotes__error_when_no_push_available(void)
150 {
151 git_remote *r;
152 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
153 char *specs = {
154 "refs/heads/master",
155 };
156 git_strarray arr = {
157 &specs,
158 1,
159 };
160
161
162 cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git")));
163
164 callbacks.transport = git_transport_local;
165 cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, &callbacks, NULL, NULL));
166
167 /* Make sure that push is really not available */
168 r->transport->push = NULL;
169
170 cl_git_fail_with(-1, git_remote_upload(r, &arr, NULL));
171
172 git_remote_free(r);
173 }
174
175 void test_network_remote_remotes__refspec_parsing(void)
176 {
177 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
178 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
179 }
180
181 void test_network_remote_remotes__add_fetchspec(void)
182 {
183 size_t size;
184
185 size = git_remote_refspec_count(_remote);
186
187 cl_git_pass(git_remote_add_fetch(_repo, "test", "refs/*:refs/*"));
188 size++;
189
190 git_remote_free(_remote);
191 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
192
193 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
194
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);
200
201 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_add_fetch(_repo, "test", "refs/*/foo/*:refs/*"));
202 }
203
204 void test_network_remote_remotes__dup(void)
205 {
206 git_strarray array;
207 git_remote *dup;
208
209 cl_git_pass(git_remote_dup(&dup, _remote));
210
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));
214
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);
219
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);
223
224 git_remote_free(dup);
225 }
226
227 void test_network_remote_remotes__add_pushspec(void)
228 {
229 size_t size;
230
231 size = git_remote_refspec_count(_remote);
232
233 cl_git_pass(git_remote_add_push(_repo, "test", "refs/*:refs/*"));
234 size++;
235
236 git_remote_free(_remote);
237 cl_git_pass(git_remote_lookup(&_remote, _repo, "test"));
238
239 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
240
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/*");
245
246 cl_assert_equal_b(_refspec->push, true);
247 }
248
249 void test_network_remote_remotes__fnmatch(void)
250 {
251 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
252 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
253 }
254
255 void test_network_remote_remotes__transform(void)
256 {
257 git_buf ref = GIT_BUF_INIT;
258
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);
262 }
263
264 void test_network_remote_remotes__transform_destination_to_source(void)
265 {
266 git_buf ref = GIT_BUF_INIT;
267
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);
271 }
272
273 void test_network_remote_remotes__missing_refspecs(void)
274 {
275 git_config *cfg;
276
277 git_remote_free(_remote);
278 _remote = NULL;
279
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"));
283
284 git_config_free(cfg);
285 }
286
287 void test_network_remote_remotes__nonmatch_upstream_refspec(void)
288 {
289 git_config *config;
290 git_remote *remote;
291 char *specstr[] = {
292 "refs/tags/*:refs/tags/*",
293 };
294 git_strarray specs = {
295 specstr,
296 1,
297 };
298
299 cl_git_pass(git_remote_create(&remote, _repo, "taggy", git_repository_path(_repo)));
300
301 /*
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
304 */
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"));
308
309 cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
310
311 git_remote_free(remote);
312 }
313
314 void test_network_remote_remotes__list(void)
315 {
316 git_strarray list;
317 git_config *cfg;
318
319 cl_git_pass(git_remote_list(&list, _repo));
320 cl_assert(list.count == 5);
321 git_strarray_dispose(&list);
322
323 cl_git_pass(git_repository_config(&cfg, _repo));
324
325 /* Create a new remote */
326 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
327
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"));
330
331 cl_git_pass(git_remote_list(&list, _repo));
332 cl_assert(list.count == 7);
333 git_strarray_dispose(&list);
334
335 git_config_free(cfg);
336 }
337
338 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
339 {
340 git_remote_free(_remote);
341 _remote = NULL;
342
343 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_lookup(&_remote, _repo, "just-left-few-minutes-ago"));
344 }
345
346 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
347 {
348 git_remote_free(_remote);
349 _remote = NULL;
350
351 cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_lookup(&_remote, _repo, "Inv@{id"));
352 }
353
354 /*
355 * $ git remote add addtest http://github.com/libgit2/libgit2
356 *
357 * $ cat .git/config
358 * [...]
359 * [remote "addtest"]
360 * url = http://github.com/libgit2/libgit2
361 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
362 */
363 void test_network_remote_remotes__add(void)
364 {
365 git_remote_free(_remote);
366 _remote = NULL;
367
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));
370
371 git_remote_free(_remote);
372 _remote = NULL;
373
374 cl_git_pass(git_remote_lookup(&_remote, _repo, "addtest"));
375 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
376
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");
382 }
383
384 void test_network_remote_remotes__tagopt(void)
385 {
386 const char *name = git_remote_name(_remote);
387
388 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
389 assert_config_entry_value(_repo, "remote.test.tagopt", "--tags");
390
391 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
392 assert_config_entry_value(_repo, "remote.test.tagopt", "--no-tags");
393
394 git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
395 assert_config_entry_existence(_repo, "remote.test.tagopt", false);
396 }
397
398 void test_network_remote_remotes__can_load_with_an_empty_url(void)
399 {
400 git_remote *remote = NULL;
401
402 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-url"));
403
404 cl_assert(remote->url == NULL);
405 cl_assert(remote->pushurl == NULL);
406
407 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
408
409 cl_assert(git_error_last() != NULL);
410 cl_assert(git_error_last()->klass == GIT_ERROR_INVALID);
411
412 git_remote_free(remote);
413 }
414
415 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
416 {
417 git_remote *remote = NULL;
418
419 cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-pushurl"));
420
421 cl_assert(remote->url == NULL);
422 cl_assert(remote->pushurl == NULL);
423
424 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
425
426 git_remote_free(remote);
427 }
428
429 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
430 {
431 git_remote *remote = NULL;
432
433 cl_git_fail_with(
434 git_remote_lookup(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
435 }
436
437 static const char *fetch_refspecs[] = {
438 "+refs/heads/*:refs/remotes/origin/*",
439 "refs/tags/*:refs/tags/*",
440 "+refs/pull/*:refs/pull/*",
441 };
442
443 static const char *push_refspecs[] = {
444 "refs/heads/*:refs/heads/*",
445 "refs/tags/*:refs/tags/*",
446 "refs/notes/*:refs/notes/*",
447 };
448
449 void test_network_remote_remotes__query_refspecs(void)
450 {
451 git_remote *remote;
452 git_strarray array;
453 int i;
454
455 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "query", "git://github.com/libgit2/libgit2", NULL));
456 git_remote_free(remote);
457
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]));
461 }
462
463 cl_git_pass(git_remote_lookup(&remote, _repo, "query"));
464
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]);
468 }
469 git_strarray_dispose(&array);
470
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]);
474 }
475 git_strarray_dispose(&array);
476
477 git_remote_free(remote);
478 git_remote_delete(_repo, "test");
479 }