]> git.proxmox.com Git - libgit2.git/blame - tests/network/remote/local.c
Merge pull request #4265 from pks-t/pks/read-prefix-tests
[libgit2.git] / tests / network / remote / local.c
CommitLineData
3fd1520c 1#include "clar_libgit2.h"
db1f7e59 2#include "buffer.h"
3#include "path.h"
e2580375 4#include "posix.h"
db1f7e59 5
6static git_repository *repo;
7static git_buf file_path_buf = GIT_BUF_INIT;
8static git_remote *remote;
9
fe794b2e
CMN
10static char *push_refspec_strings[] = {
11 "refs/heads/master",
12};
13static git_strarray push_array = {
14 push_refspec_strings,
15 1,
16};
17
624924e8 18void test_network_remote_local__initialize(void)
db1f7e59 19{
db1f7e59 20 cl_git_pass(git_repository_init(&repo, "remotelocal/", 0));
659cf202 21 cl_git_pass(git_repository_set_ident(repo, "Foo Bar", "foo@example.com"));
db1f7e59 22 cl_assert(repo != NULL);
db1f7e59 23}
24
624924e8 25void test_network_remote_local__cleanup(void)
db1f7e59 26{
db1f7e59 27 git_buf_free(&file_path_buf);
9094d30b
SC
28
29 git_remote_free(remote);
30 remote = NULL;
31
db1f7e59 32 git_repository_free(repo);
9094d30b
SC
33 repo = NULL;
34
db1f7e59 35 cl_fixture_cleanup("remotelocal");
36}
37
e2580375 38static void connect_to_local_repository(const char *local_repository)
39{
2ff1a0d0 40 git_buf_sets(&file_path_buf, cl_git_path_url(local_repository));
e2580375 41
ae5b9362 42 cl_git_pass(git_remote_create_anonymous(&remote, repo, git_buf_cstr(&file_path_buf)));
07bd3e57 43 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
e2580375 44}
45
624924e8 46void test_network_remote_local__connected(void)
36c88422
AS
47{
48 connect_to_local_repository(cl_fixture("testrepo.git"));
49 cl_assert(git_remote_connected(remote));
50
51 git_remote_disconnect(remote);
52 cl_assert(!git_remote_connected(remote));
53}
54
624924e8 55void test_network_remote_local__retrieve_advertised_references(void)
db1f7e59 56{
359dce72
CMN
57 const git_remote_head **refs;
58 size_t refs_len;
db1f7e59 59
e2580375 60 connect_to_local_repository(cl_fixture("testrepo.git"));
61
359dce72 62 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
db1f7e59 63
359dce72 64 cl_assert_equal_i(refs_len, 28);
db1f7e59 65}
e2580375 66
dc8adda4
JG
67void test_network_remote_local__retrieve_advertised_before_connect(void)
68{
69 const git_remote_head **refs;
70 size_t refs_len = 0;
71
72 git_buf_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git")));
73
ae5b9362 74 cl_git_pass(git_remote_create_anonymous(&remote, repo, git_buf_cstr(&file_path_buf)));
dc8adda4
JG
75 cl_git_fail(git_remote_ls(&refs, &refs_len, remote));
76}
77
edbaa63a
AS
78void test_network_remote_local__retrieve_advertised_references_after_disconnect(void)
79{
359dce72
CMN
80 const git_remote_head **refs;
81 size_t refs_len;
edbaa63a
AS
82
83 connect_to_local_repository(cl_fixture("testrepo.git"));
84 git_remote_disconnect(remote);
85
359dce72 86 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
edbaa63a 87
359dce72 88 cl_assert_equal_i(refs_len, 28);
edbaa63a
AS
89}
90
624924e8 91void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void)
e2580375 92{
359dce72
CMN
93 const git_remote_head **refs;
94 size_t refs_len;
e2580375 95
96 cl_fixture_sandbox("testrepo.git");
97 cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git"));
98
99 connect_to_local_repository("spaced testrepo.git");
100
359dce72 101 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
e2580375 102
359dce72 103 cl_assert_equal_i(refs_len, 28);
e2580375 104
79fd4230 105 git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */
106 remote = NULL;
107
e2580375 108 cl_fixture_cleanup("spaced testrepo.git");
109}
79fd4230 110
624924e8 111void test_network_remote_local__nested_tags_are_completely_peeled(void)
79fd4230 112{
359dce72
CMN
113 const git_remote_head **refs;
114 size_t refs_len, i;
115
79fd4230 116 connect_to_local_repository(cl_fixture("testrepo.git"));
117
359dce72
CMN
118 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
119
120 for (i = 0; i < refs_len; i++) {
121 if (!strcmp(refs[i]->name, "refs/tags/test^{}"))
122 cl_git_pass(git_oid_streq(&refs[i]->oid, "e90810b8df3e80c413d903f631643c716887138d"));
123 }
79fd4230 124}
d8488457
CMN
125
126void test_network_remote_local__shorthand_fetch_refspec0(void)
127{
9c206a22
CMN
128 char *refspec_strings[] = {
129 "master:remotes/sloppy/master",
130 "master:boh/sloppy/master",
131 };
132 git_strarray array = {
133 refspec_strings,
134 2,
135 };
d8488457
CMN
136
137 git_reference *ref;
138
139 connect_to_local_repository(cl_fixture("testrepo.git"));
d8488457 140
8f0104ec 141 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
d8488457
CMN
142
143 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
144 git_reference_free(ref);
145
146 cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/boh/sloppy/master"));
147 git_reference_free(ref);
148}
149
150void test_network_remote_local__shorthand_fetch_refspec1(void)
151{
9c206a22
CMN
152 char *refspec_strings[] = {
153 "master",
154 "hard_tag",
155 };
156 git_strarray array = {
157 refspec_strings,
158 2,
159 };
d8488457
CMN
160
161 git_reference *ref;
162
163 connect_to_local_repository(cl_fixture("testrepo.git"));
d8488457 164
8f0104ec 165 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
d8488457 166
35a8a8c5 167 cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/origin/master"));
d8488457
CMN
168 cl_git_fail(git_reference_lookup(&ref, repo, "refs/tags/hard_tag"));
169}
505b5d0c
CMN
170
171void test_network_remote_local__tagopt(void)
172{
173 git_reference *ref;
35a8a8c5 174 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
505b5d0c 175
ea8dedc9 176 cl_git_pass(git_remote_create(&remote, repo, "tagopt", cl_git_path_url(cl_fixture("testrepo.git"))));
35a8a8c5
CMN
177 fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
178 cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
505b5d0c 179
e284c451 180 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master"));
f7fcb18f 181 git_reference_free(ref);
505b5d0c
CMN
182 cl_git_pass(git_reference_lookup(&ref, repo, "refs/tags/hard_tag"));
183 git_reference_free(ref);
ea8dedc9 184
35a8a8c5
CMN
185 fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
186 cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
ea8dedc9 187 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master"));
f7fcb18f 188 git_reference_free(ref);
505b5d0c 189}
61d57b7a
BS
190
191void test_network_remote_local__push_to_bare_remote(void)
192{
9c206a22
CMN
193 char *refspec_strings[] = {
194 "master:master",
195 };
196 git_strarray array = {
197 refspec_strings,
198 1,
199 };
fe794b2e 200
61d57b7a
BS
201 /* Should be able to push to a bare remote */
202 git_remote *localremote;
61d57b7a
BS
203
204 /* Get some commits */
205 connect_to_local_repository(cl_fixture("testrepo.git"));
8f0104ec 206 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
61d57b7a
BS
207
208 /* Set up an empty bare repo to push into */
209 {
210 git_repository *localbarerepo;
211 cl_git_pass(git_repository_init(&localbarerepo, "./localbare.git", 1));
212 git_repository_free(localbarerepo);
213 }
214
215 /* Connect to the bare repo */
ae5b9362 216 cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localbare.git"));
07bd3e57 217 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
61d57b7a
BS
218
219 /* Try to push */
0bc3d56d 220 cl_git_pass(git_remote_upload(localremote, &push_array, NULL));
61d57b7a
BS
221
222 /* Clean up */
223 git_remote_free(localremote);
224 cl_fixture_cleanup("localbare.git");
225}
226
194d077c
GD
227void test_network_remote_local__push_to_bare_remote_with_file_url(void)
228{
9c206a22
CMN
229 char *refspec_strings[] = {
230 "master:master",
231 };
232 git_strarray array = {
233 refspec_strings,
234 1,
235 };
194d077c
GD
236 /* Should be able to push to a bare remote */
237 git_remote *localremote;
66d585c6 238 const char *url;
194d077c
GD
239
240 /* Get some commits */
241 connect_to_local_repository(cl_fixture("testrepo.git"));
35a8a8c5 242 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
194d077c
GD
243
244 /* Set up an empty bare repo to push into */
245 {
246 git_repository *localbarerepo;
247 cl_git_pass(git_repository_init(&localbarerepo, "./localbare.git", 1));
248 git_repository_free(localbarerepo);
249 }
250
251 /* Create a file URL */
66d585c6 252 url = cl_git_path_url("./localbare.git");
194d077c
GD
253
254 /* Connect to the bare repo */
ae5b9362 255 cl_git_pass(git_remote_create_anonymous(&localremote, repo, url));
07bd3e57 256 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
194d077c
GD
257
258 /* Try to push */
0bc3d56d 259 cl_git_pass(git_remote_upload(localremote, &push_array, NULL));
194d077c
GD
260
261 /* Clean up */
194d077c
GD
262 git_remote_free(localremote);
263 cl_fixture_cleanup("localbare.git");
264}
265
266
61d57b7a
BS
267void test_network_remote_local__push_to_non_bare_remote(void)
268{
9c206a22
CMN
269 char *refspec_strings[] = {
270 "master:master",
271 };
272 git_strarray array = {
273 refspec_strings,
274 1,
275 };
61d57b7a
BS
276 /* Shouldn't be able to push to a non-bare remote */
277 git_remote *localremote;
35a8a8c5 278 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
61d57b7a
BS
279
280 /* Get some commits */
281 connect_to_local_repository(cl_fixture("testrepo.git"));
35a8a8c5 282 cl_git_pass(git_remote_fetch(remote, &array, &fetch_opts, NULL));
61d57b7a
BS
283
284 /* Set up an empty non-bare repo to push into */
285 {
286 git_repository *remoterepo = NULL;
287 cl_git_pass(git_repository_init(&remoterepo, "localnonbare", 0));
288 git_repository_free(remoterepo);
289 }
290
291 /* Connect to the bare repo */
ae5b9362 292 cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localnonbare"));
07bd3e57 293 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
61d57b7a
BS
294
295 /* Try to push */
fe794b2e 296 cl_git_fail_with(GIT_EBAREREPO, git_remote_upload(localremote, &push_array, NULL));
61d57b7a
BS
297
298 /* Clean up */
299 git_remote_free(localremote);
300 cl_fixture_cleanup("localbare.git");
301}
c3ab1e5a
BS
302
303void test_network_remote_local__fetch(void)
304{
9c206a22
CMN
305 char *refspec_strings[] = {
306 "master:remotes/sloppy/master",
307 };
308 git_strarray array = {
309 refspec_strings,
310 1,
311 };
c3ab1e5a
BS
312
313 git_reflog *log;
314 const git_reflog_entry *entry;
c3ab1e5a
BS
315 git_reference *ref;
316
c3ab1e5a 317 connect_to_local_repository(cl_fixture("testrepo.git"));
c3ab1e5a 318
8f0104ec 319 cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!"));
c3ab1e5a
BS
320
321 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
322 git_reference_free(ref);
323
324 cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
325 cl_assert_equal_i(1, git_reflog_entrycount(log));
326 entry = git_reflog_entry_byindex(log, 0);
327 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
328 cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry));
329
330 git_reflog_free(log);
c3ab1e5a
BS
331}
332
333void test_network_remote_local__reflog(void)
334{
9c206a22
CMN
335 char *refspec_strings[] = {
336 "master:remotes/sloppy/master",
337 };
338 git_strarray array = {
339 refspec_strings,
340 1,
341 };
c3ab1e5a
BS
342
343 git_reflog *log;
344 const git_reflog_entry *entry;
c3ab1e5a
BS
345
346 connect_to_local_repository(cl_fixture("testrepo.git"));
c3ab1e5a 347
35a8a8c5 348 cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!"));
c3ab1e5a
BS
349
350 cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
351 cl_assert_equal_i(1, git_reflog_entrycount(log));
352 entry = git_reflog_entry_byindex(log, 0);
353 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
354 cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry));
355
356 git_reflog_free(log);
c3ab1e5a 357}
db55bb73
BS
358
359void test_network_remote_local__fetch_default_reflog_message(void)
360{
9c206a22
CMN
361 char *refspec_strings[] = {
362 "master:remotes/sloppy/master",
363 };
364 git_strarray array = {
365 refspec_strings,
366 1,
367 };
db55bb73
BS
368
369 git_reflog *log;
370 const git_reflog_entry *entry;
db55bb73
BS
371 char expected_reflog_msg[1024];
372
db55bb73 373 connect_to_local_repository(cl_fixture("testrepo.git"));
db55bb73 374
8f0104ec 375 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
db55bb73
BS
376
377 cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
378 cl_assert_equal_i(1, git_reflog_entrycount(log));
379 entry = git_reflog_entry_byindex(log, 0);
380 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
381
382 sprintf(expected_reflog_msg, "fetch %s", git_remote_url(remote));
383 cl_assert_equal_s(expected_reflog_msg, git_reflog_entry_message(entry));
384
385 git_reflog_free(log);
db55bb73 386}
c5837cad
CMN
387
388void test_network_remote_local__opportunistic_update(void)
389{
390 git_reference *ref;
391 char *refspec_strings[] = {
392 "master",
393 };
394 git_strarray array = {
395 refspec_strings,
396 1,
397 };
398
399 /* this remote has a passive refspec of "refs/heads/<star>:refs/remotes/origin/<star>" */
400 cl_git_pass(git_remote_create(&remote, repo, "origin", cl_git_fixture_url("testrepo.git")));
401 /* and we pass the active refspec "master" */
8f0104ec 402 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
c5837cad
CMN
403
404 /* and we expect that to update our copy of origin's master */
405 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/origin/master"));
406 git_reference_free(ref);
407}
d3cd7da5
POL
408
409void test_network_remote_local__update_tips_for_new_remote(void) {
410 git_repository *src_repo;
411 git_repository *dst_repo;
412 git_remote *new_remote;
d3cd7da5
POL
413 git_reference* branch;
414
415 /* Copy test repo */
416 cl_fixture_sandbox("testrepo.git");
417 cl_git_pass(git_repository_open(&src_repo, "testrepo.git"));
418
419 /* Set up an empty bare repo to push into */
420 cl_git_pass(git_repository_init(&dst_repo, "./localbare.git", 1));
421
422 /* Push to bare repo */
423 cl_git_pass(git_remote_create(&new_remote, src_repo, "bare", "./localbare.git"));
8f0104ec
CMN
424 cl_git_pass(git_remote_push(new_remote, &push_array, NULL));
425 /* Make sure remote branch has been created */
d3cd7da5
POL
426 cl_git_pass(git_branch_lookup(&branch, src_repo, "bare/master", GIT_BRANCH_REMOTE));
427
428 git_reference_free(branch);
d3cd7da5
POL
429 git_remote_free(new_remote);
430 git_repository_free(dst_repo);
431 cl_fixture_cleanup("localbare.git");
432 git_repository_free(src_repo);
433 cl_fixture_cleanup("testrepo.git");
434}
3fade40e
CMN
435
436void test_network_remote_local__push_delete(void)
437{
438 git_repository *src_repo;
439 git_repository *dst_repo;
440 git_remote *remote;
441 git_reference *ref;
442 char *spec_push[] = { "refs/heads/master" };
443 char *spec_delete[] = { ":refs/heads/master" };
444 git_strarray specs = {
445 spec_push,
446 1,
447 };
448
449 src_repo = cl_git_sandbox_init("testrepo.git");
450 cl_git_pass(git_repository_init(&dst_repo, "target.git", 1));
451
452 cl_git_pass(git_remote_create(&remote, src_repo, "origin", "./target.git"));
453
454 /* Push the master branch and verify it's there */
412a3808 455 cl_git_pass(git_remote_push(remote, &specs, NULL));
3fade40e
CMN
456 cl_git_pass(git_reference_lookup(&ref, dst_repo, "refs/heads/master"));
457 git_reference_free(ref);
458
459 specs.strings = spec_delete;
412a3808 460 cl_git_pass(git_remote_push(remote, &specs, NULL));
3fade40e
CMN
461 cl_git_fail(git_reference_lookup(&ref, dst_repo, "refs/heads/master"));
462
3fade40e
CMN
463 git_remote_free(remote);
464 git_repository_free(dst_repo);
d226fbf1
L
465 cl_fixture_cleanup("target.git");
466 cl_git_sandbox_cleanup();
3fade40e 467}
fe9a5dd3
ET
468
469void test_network_remote_local__anonymous_remote_inmemory_repo(void)
470{
471 git_repository *inmemory;
472 git_remote *remote;
473
474 git_buf_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git")));
475
476 cl_git_pass(git_repository_new(&inmemory));
477 cl_git_pass(git_remote_create_anonymous(&remote, inmemory, git_buf_cstr(&file_path_buf)));
478 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
479 cl_assert(git_remote_connected(remote));
480 git_remote_disconnect(remote);
481 git_remote_free(remote);
482 git_repository_free(inmemory);
483}