]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/local.c
Merge pull request #4265 from pks-t/pks/read-prefix-tests
[libgit2.git] / tests / network / remote / local.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "path.h"
4 #include "posix.h"
5
6 static git_repository *repo;
7 static git_buf file_path_buf = GIT_BUF_INIT;
8 static git_remote *remote;
9
10 static char *push_refspec_strings[] = {
11 "refs/heads/master",
12 };
13 static git_strarray push_array = {
14 push_refspec_strings,
15 1,
16 };
17
18 void test_network_remote_local__initialize(void)
19 {
20 cl_git_pass(git_repository_init(&repo, "remotelocal/", 0));
21 cl_git_pass(git_repository_set_ident(repo, "Foo Bar", "foo@example.com"));
22 cl_assert(repo != NULL);
23 }
24
25 void test_network_remote_local__cleanup(void)
26 {
27 git_buf_free(&file_path_buf);
28
29 git_remote_free(remote);
30 remote = NULL;
31
32 git_repository_free(repo);
33 repo = NULL;
34
35 cl_fixture_cleanup("remotelocal");
36 }
37
38 static void connect_to_local_repository(const char *local_repository)
39 {
40 git_buf_sets(&file_path_buf, cl_git_path_url(local_repository));
41
42 cl_git_pass(git_remote_create_anonymous(&remote, repo, git_buf_cstr(&file_path_buf)));
43 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
44 }
45
46 void test_network_remote_local__connected(void)
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
55 void test_network_remote_local__retrieve_advertised_references(void)
56 {
57 const git_remote_head **refs;
58 size_t refs_len;
59
60 connect_to_local_repository(cl_fixture("testrepo.git"));
61
62 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
63
64 cl_assert_equal_i(refs_len, 28);
65 }
66
67 void 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
74 cl_git_pass(git_remote_create_anonymous(&remote, repo, git_buf_cstr(&file_path_buf)));
75 cl_git_fail(git_remote_ls(&refs, &refs_len, remote));
76 }
77
78 void test_network_remote_local__retrieve_advertised_references_after_disconnect(void)
79 {
80 const git_remote_head **refs;
81 size_t refs_len;
82
83 connect_to_local_repository(cl_fixture("testrepo.git"));
84 git_remote_disconnect(remote);
85
86 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
87
88 cl_assert_equal_i(refs_len, 28);
89 }
90
91 void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void)
92 {
93 const git_remote_head **refs;
94 size_t refs_len;
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
101 cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
102
103 cl_assert_equal_i(refs_len, 28);
104
105 git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */
106 remote = NULL;
107
108 cl_fixture_cleanup("spaced testrepo.git");
109 }
110
111 void test_network_remote_local__nested_tags_are_completely_peeled(void)
112 {
113 const git_remote_head **refs;
114 size_t refs_len, i;
115
116 connect_to_local_repository(cl_fixture("testrepo.git"));
117
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 }
124 }
125
126 void test_network_remote_local__shorthand_fetch_refspec0(void)
127 {
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 };
136
137 git_reference *ref;
138
139 connect_to_local_repository(cl_fixture("testrepo.git"));
140
141 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
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
150 void test_network_remote_local__shorthand_fetch_refspec1(void)
151 {
152 char *refspec_strings[] = {
153 "master",
154 "hard_tag",
155 };
156 git_strarray array = {
157 refspec_strings,
158 2,
159 };
160
161 git_reference *ref;
162
163 connect_to_local_repository(cl_fixture("testrepo.git"));
164
165 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
166
167 cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/origin/master"));
168 cl_git_fail(git_reference_lookup(&ref, repo, "refs/tags/hard_tag"));
169 }
170
171 void test_network_remote_local__tagopt(void)
172 {
173 git_reference *ref;
174 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
175
176 cl_git_pass(git_remote_create(&remote, repo, "tagopt", cl_git_path_url(cl_fixture("testrepo.git"))));
177 fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
178 cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
179
180 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master"));
181 git_reference_free(ref);
182 cl_git_pass(git_reference_lookup(&ref, repo, "refs/tags/hard_tag"));
183 git_reference_free(ref);
184
185 fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
186 cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
187 cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master"));
188 git_reference_free(ref);
189 }
190
191 void test_network_remote_local__push_to_bare_remote(void)
192 {
193 char *refspec_strings[] = {
194 "master:master",
195 };
196 git_strarray array = {
197 refspec_strings,
198 1,
199 };
200
201 /* Should be able to push to a bare remote */
202 git_remote *localremote;
203
204 /* Get some commits */
205 connect_to_local_repository(cl_fixture("testrepo.git"));
206 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
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 */
216 cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localbare.git"));
217 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
218
219 /* Try to push */
220 cl_git_pass(git_remote_upload(localremote, &push_array, NULL));
221
222 /* Clean up */
223 git_remote_free(localremote);
224 cl_fixture_cleanup("localbare.git");
225 }
226
227 void test_network_remote_local__push_to_bare_remote_with_file_url(void)
228 {
229 char *refspec_strings[] = {
230 "master:master",
231 };
232 git_strarray array = {
233 refspec_strings,
234 1,
235 };
236 /* Should be able to push to a bare remote */
237 git_remote *localremote;
238 const char *url;
239
240 /* Get some commits */
241 connect_to_local_repository(cl_fixture("testrepo.git"));
242 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
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 */
252 url = cl_git_path_url("./localbare.git");
253
254 /* Connect to the bare repo */
255 cl_git_pass(git_remote_create_anonymous(&localremote, repo, url));
256 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
257
258 /* Try to push */
259 cl_git_pass(git_remote_upload(localremote, &push_array, NULL));
260
261 /* Clean up */
262 git_remote_free(localremote);
263 cl_fixture_cleanup("localbare.git");
264 }
265
266
267 void test_network_remote_local__push_to_non_bare_remote(void)
268 {
269 char *refspec_strings[] = {
270 "master:master",
271 };
272 git_strarray array = {
273 refspec_strings,
274 1,
275 };
276 /* Shouldn't be able to push to a non-bare remote */
277 git_remote *localremote;
278 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
279
280 /* Get some commits */
281 connect_to_local_repository(cl_fixture("testrepo.git"));
282 cl_git_pass(git_remote_fetch(remote, &array, &fetch_opts, NULL));
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 */
292 cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localnonbare"));
293 cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL));
294
295 /* Try to push */
296 cl_git_fail_with(GIT_EBAREREPO, git_remote_upload(localremote, &push_array, NULL));
297
298 /* Clean up */
299 git_remote_free(localremote);
300 cl_fixture_cleanup("localbare.git");
301 }
302
303 void test_network_remote_local__fetch(void)
304 {
305 char *refspec_strings[] = {
306 "master:remotes/sloppy/master",
307 };
308 git_strarray array = {
309 refspec_strings,
310 1,
311 };
312
313 git_reflog *log;
314 const git_reflog_entry *entry;
315 git_reference *ref;
316
317 connect_to_local_repository(cl_fixture("testrepo.git"));
318
319 cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!"));
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);
331 }
332
333 void test_network_remote_local__reflog(void)
334 {
335 char *refspec_strings[] = {
336 "master:remotes/sloppy/master",
337 };
338 git_strarray array = {
339 refspec_strings,
340 1,
341 };
342
343 git_reflog *log;
344 const git_reflog_entry *entry;
345
346 connect_to_local_repository(cl_fixture("testrepo.git"));
347
348 cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!"));
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);
357 }
358
359 void test_network_remote_local__fetch_default_reflog_message(void)
360 {
361 char *refspec_strings[] = {
362 "master:remotes/sloppy/master",
363 };
364 git_strarray array = {
365 refspec_strings,
366 1,
367 };
368
369 git_reflog *log;
370 const git_reflog_entry *entry;
371 char expected_reflog_msg[1024];
372
373 connect_to_local_repository(cl_fixture("testrepo.git"));
374
375 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
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);
386 }
387
388 void 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" */
402 cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
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 }
408
409 void 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;
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"));
424 cl_git_pass(git_remote_push(new_remote, &push_array, NULL));
425 /* Make sure remote branch has been created */
426 cl_git_pass(git_branch_lookup(&branch, src_repo, "bare/master", GIT_BRANCH_REMOTE));
427
428 git_reference_free(branch);
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 }
435
436 void 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 */
455 cl_git_pass(git_remote_push(remote, &specs, NULL));
456 cl_git_pass(git_reference_lookup(&ref, dst_repo, "refs/heads/master"));
457 git_reference_free(ref);
458
459 specs.strings = spec_delete;
460 cl_git_pass(git_remote_push(remote, &specs, NULL));
461 cl_git_fail(git_reference_lookup(&ref, dst_repo, "refs/heads/master"));
462
463 git_remote_free(remote);
464 git_repository_free(dst_repo);
465 cl_fixture_cleanup("target.git");
466 cl_git_sandbox_cleanup();
467 }
468
469 void 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 }