1 #include "clar_libgit2.h"
5 #include "git2/sys/repository.h"
7 static git_repository
*repo
;
8 static git_buf file_path_buf
= GIT_BUF_INIT
;
9 static git_remote
*remote
;
11 static char *push_refspec_strings
[] = {
14 static git_strarray push_array
= {
19 void test_network_remote_local__initialize(void)
21 cl_git_pass(git_repository_init(&repo
, "remotelocal/", 0));
22 cl_git_pass(git_repository_set_ident(repo
, "Foo Bar", "foo@example.com"));
23 cl_assert(repo
!= NULL
);
26 void test_network_remote_local__cleanup(void)
28 git_buf_dispose(&file_path_buf
);
30 git_remote_free(remote
);
33 git_repository_free(repo
);
36 cl_fixture_cleanup("remotelocal");
39 static void connect_to_local_repository(const char *local_repository
)
41 git_buf_sets(&file_path_buf
, cl_git_path_url(local_repository
));
43 cl_git_pass(git_remote_create_anonymous(&remote
, repo
, git_buf_cstr(&file_path_buf
)));
44 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
47 void test_network_remote_local__connected(void)
49 connect_to_local_repository(cl_fixture("testrepo.git"));
50 cl_assert(git_remote_connected(remote
));
52 git_remote_disconnect(remote
);
53 cl_assert(!git_remote_connected(remote
));
56 void test_network_remote_local__retrieve_advertised_references(void)
58 const git_remote_head
**refs
;
61 connect_to_local_repository(cl_fixture("testrepo.git"));
63 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
65 cl_assert_equal_i(refs_len
, 28);
68 void test_network_remote_local__retrieve_advertised_before_connect(void)
70 const git_remote_head
**refs
;
73 git_buf_sets(&file_path_buf
, cl_git_path_url(cl_fixture("testrepo.git")));
75 cl_git_pass(git_remote_create_anonymous(&remote
, repo
, git_buf_cstr(&file_path_buf
)));
76 cl_git_fail(git_remote_ls(&refs
, &refs_len
, remote
));
79 void test_network_remote_local__retrieve_advertised_references_after_disconnect(void)
81 const git_remote_head
**refs
;
84 connect_to_local_repository(cl_fixture("testrepo.git"));
85 git_remote_disconnect(remote
);
87 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
89 cl_assert_equal_i(refs_len
, 28);
92 void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void)
94 const git_remote_head
**refs
;
97 cl_fixture_sandbox("testrepo.git");
98 cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git"));
100 connect_to_local_repository("spaced testrepo.git");
102 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
104 cl_assert_equal_i(refs_len
, 28);
106 git_remote_free(remote
); /* Disconnect from the "spaced repo" before the cleanup */
109 cl_fixture_cleanup("spaced testrepo.git");
112 void test_network_remote_local__nested_tags_are_completely_peeled(void)
114 const git_remote_head
**refs
;
117 connect_to_local_repository(cl_fixture("testrepo.git"));
119 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
121 for (i
= 0; i
< refs_len
; i
++) {
122 if (!strcmp(refs
[i
]->name
, "refs/tags/test^{}"))
123 cl_git_pass(git_oid_streq(&refs
[i
]->oid
, "e90810b8df3e80c413d903f631643c716887138d"));
127 void test_network_remote_local__shorthand_fetch_refspec0(void)
129 char *refspec_strings
[] = {
130 "master:remotes/sloppy/master",
131 "master:boh/sloppy/master",
133 git_strarray array
= {
140 connect_to_local_repository(cl_fixture("testrepo.git"));
142 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
144 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/sloppy/master"));
145 git_reference_free(ref
);
147 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/heads/boh/sloppy/master"));
148 git_reference_free(ref
);
151 void test_network_remote_local__shorthand_fetch_refspec1(void)
153 char *refspec_strings
[] = {
157 git_strarray array
= {
164 connect_to_local_repository(cl_fixture("testrepo.git"));
166 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
168 cl_git_fail(git_reference_lookup(&ref
, repo
, "refs/remotes/origin/master"));
169 cl_git_fail(git_reference_lookup(&ref
, repo
, "refs/tags/hard_tag"));
172 void test_network_remote_local__tagopt(void)
175 git_fetch_options fetch_opts
= GIT_FETCH_OPTIONS_INIT
;
177 cl_git_pass(git_remote_create(&remote
, repo
, "tagopt", cl_git_path_url(cl_fixture("testrepo.git"))));
178 fetch_opts
.download_tags
= GIT_REMOTE_DOWNLOAD_TAGS_ALL
;
179 cl_git_pass(git_remote_fetch(remote
, NULL
, &fetch_opts
, NULL
));
181 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/tagopt/master"));
182 git_reference_free(ref
);
183 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/tags/hard_tag"));
184 git_reference_free(ref
);
186 fetch_opts
.download_tags
= GIT_REMOTE_DOWNLOAD_TAGS_AUTO
;
187 cl_git_pass(git_remote_fetch(remote
, NULL
, &fetch_opts
, NULL
));
188 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/tagopt/master"));
189 git_reference_free(ref
);
192 void test_network_remote_local__push_to_bare_remote(void)
194 char *refspec_strings
[] = {
197 git_strarray array
= {
202 /* Should be able to push to a bare remote */
203 git_remote
*localremote
;
205 /* Get some commits */
206 connect_to_local_repository(cl_fixture("testrepo.git"));
207 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
209 /* Set up an empty bare repo to push into */
211 git_repository
*localbarerepo
;
212 cl_git_pass(git_repository_init(&localbarerepo
, "./localbare.git", 1));
213 git_repository_free(localbarerepo
);
216 /* Connect to the bare repo */
217 cl_git_pass(git_remote_create_anonymous(&localremote
, repo
, "./localbare.git"));
218 cl_git_pass(git_remote_connect(localremote
, GIT_DIRECTION_PUSH
, NULL
, NULL
, NULL
));
221 cl_git_pass(git_remote_upload(localremote
, &push_array
, NULL
));
224 git_remote_free(localremote
);
225 cl_fixture_cleanup("localbare.git");
228 void test_network_remote_local__push_to_bare_remote_with_file_url(void)
230 char *refspec_strings
[] = {
233 git_strarray array
= {
237 /* Should be able to push to a bare remote */
238 git_remote
*localremote
;
241 /* Get some commits */
242 connect_to_local_repository(cl_fixture("testrepo.git"));
243 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
245 /* Set up an empty bare repo to push into */
247 git_repository
*localbarerepo
;
248 cl_git_pass(git_repository_init(&localbarerepo
, "./localbare.git", 1));
249 git_repository_free(localbarerepo
);
252 /* Create a file URL */
253 url
= cl_git_path_url("./localbare.git");
255 /* Connect to the bare repo */
256 cl_git_pass(git_remote_create_anonymous(&localremote
, repo
, url
));
257 cl_git_pass(git_remote_connect(localremote
, GIT_DIRECTION_PUSH
, NULL
, NULL
, NULL
));
260 cl_git_pass(git_remote_upload(localremote
, &push_array
, NULL
));
263 git_remote_free(localremote
);
264 cl_fixture_cleanup("localbare.git");
268 void test_network_remote_local__push_to_non_bare_remote(void)
270 char *refspec_strings
[] = {
273 git_strarray array
= {
277 /* Shouldn't be able to push to a non-bare remote */
278 git_remote
*localremote
;
279 git_fetch_options fetch_opts
= GIT_FETCH_OPTIONS_INIT
;
281 /* Get some commits */
282 connect_to_local_repository(cl_fixture("testrepo.git"));
283 cl_git_pass(git_remote_fetch(remote
, &array
, &fetch_opts
, NULL
));
285 /* Set up an empty non-bare repo to push into */
287 git_repository
*remoterepo
= NULL
;
288 cl_git_pass(git_repository_init(&remoterepo
, "localnonbare", 0));
289 git_repository_free(remoterepo
);
292 /* Connect to the bare repo */
293 cl_git_pass(git_remote_create_anonymous(&localremote
, repo
, "./localnonbare"));
294 cl_git_pass(git_remote_connect(localremote
, GIT_DIRECTION_PUSH
, NULL
, NULL
, NULL
));
297 cl_git_fail_with(GIT_EBAREREPO
, git_remote_upload(localremote
, &push_array
, NULL
));
300 git_remote_free(localremote
);
301 cl_fixture_cleanup("localbare.git");
304 void test_network_remote_local__fetch(void)
306 char *refspec_strings
[] = {
307 "master:remotes/sloppy/master",
309 git_strarray array
= {
315 const git_reflog_entry
*entry
;
318 connect_to_local_repository(cl_fixture("testrepo.git"));
320 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, "UPDAAAAAATE!!"));
322 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/sloppy/master"));
323 git_reference_free(ref
);
325 cl_git_pass(git_reflog_read(&log
, repo
, "refs/remotes/sloppy/master"));
326 cl_assert_equal_i(1, git_reflog_entrycount(log
));
327 entry
= git_reflog_entry_byindex(log
, 0);
328 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry
)->email
);
329 cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry
));
331 git_reflog_free(log
);
334 void test_network_remote_local__reflog(void)
336 char *refspec_strings
[] = {
337 "master:remotes/sloppy/master",
339 git_strarray array
= {
345 const git_reflog_entry
*entry
;
347 connect_to_local_repository(cl_fixture("testrepo.git"));
349 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, "UPDAAAAAATE!!"));
351 cl_git_pass(git_reflog_read(&log
, repo
, "refs/remotes/sloppy/master"));
352 cl_assert_equal_i(1, git_reflog_entrycount(log
));
353 entry
= git_reflog_entry_byindex(log
, 0);
354 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry
)->email
);
355 cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry
));
357 git_reflog_free(log
);
360 void test_network_remote_local__fetch_default_reflog_message(void)
362 char *refspec_strings
[] = {
363 "master:remotes/sloppy/master",
365 git_strarray array
= {
371 const git_reflog_entry
*entry
;
372 char expected_reflog_msg
[1024];
374 connect_to_local_repository(cl_fixture("testrepo.git"));
376 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
378 cl_git_pass(git_reflog_read(&log
, repo
, "refs/remotes/sloppy/master"));
379 cl_assert_equal_i(1, git_reflog_entrycount(log
));
380 entry
= git_reflog_entry_byindex(log
, 0);
381 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry
)->email
);
383 sprintf(expected_reflog_msg
, "fetch %s", git_remote_url(remote
));
384 cl_assert_equal_s(expected_reflog_msg
, git_reflog_entry_message(entry
));
386 git_reflog_free(log
);
389 void test_network_remote_local__opportunistic_update(void)
392 char *refspec_strings
[] = {
395 git_strarray array
= {
400 /* this remote has a passive refspec of "refs/heads/<star>:refs/remotes/origin/<star>" */
401 cl_git_pass(git_remote_create(&remote
, repo
, "origin", cl_git_fixture_url("testrepo.git")));
402 /* and we pass the active refspec "master" */
403 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
405 /* and we expect that to update our copy of origin's master */
406 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/origin/master"));
407 git_reference_free(ref
);
410 void test_network_remote_local__update_tips_for_new_remote(void) {
411 git_repository
*src_repo
;
412 git_repository
*dst_repo
;
413 git_remote
*new_remote
;
414 git_reference
* branch
;
417 cl_fixture_sandbox("testrepo.git");
418 cl_git_pass(git_repository_open(&src_repo
, "testrepo.git"));
420 /* Set up an empty bare repo to push into */
421 cl_git_pass(git_repository_init(&dst_repo
, "./localbare.git", 1));
423 /* Push to bare repo */
424 cl_git_pass(git_remote_create(&new_remote
, src_repo
, "bare", "./localbare.git"));
425 cl_git_pass(git_remote_push(new_remote
, &push_array
, NULL
));
426 /* Make sure remote branch has been created */
427 cl_git_pass(git_branch_lookup(&branch
, src_repo
, "bare/master", GIT_BRANCH_REMOTE
));
429 git_reference_free(branch
);
430 git_remote_free(new_remote
);
431 git_repository_free(dst_repo
);
432 cl_fixture_cleanup("localbare.git");
433 git_repository_free(src_repo
);
434 cl_fixture_cleanup("testrepo.git");
437 void test_network_remote_local__push_delete(void)
439 git_repository
*src_repo
;
440 git_repository
*dst_repo
;
443 char *spec_push
[] = { "refs/heads/master" };
444 char *spec_delete
[] = { ":refs/heads/master" };
445 git_strarray specs
= {
450 src_repo
= cl_git_sandbox_init("testrepo.git");
451 cl_git_pass(git_repository_init(&dst_repo
, "target.git", 1));
453 cl_git_pass(git_remote_create(&remote
, src_repo
, "origin", "./target.git"));
455 /* Push the master branch and verify it's there */
456 cl_git_pass(git_remote_push(remote
, &specs
, NULL
));
457 cl_git_pass(git_reference_lookup(&ref
, dst_repo
, "refs/heads/master"));
458 git_reference_free(ref
);
460 specs
.strings
= spec_delete
;
461 cl_git_pass(git_remote_push(remote
, &specs
, NULL
));
462 cl_git_fail(git_reference_lookup(&ref
, dst_repo
, "refs/heads/master"));
464 git_remote_free(remote
);
465 git_repository_free(dst_repo
);
466 cl_fixture_cleanup("target.git");
467 cl_git_sandbox_cleanup();
470 void test_network_remote_local__anonymous_remote_inmemory_repo(void)
472 git_repository
*inmemory
;
475 git_buf_sets(&file_path_buf
, cl_git_path_url(cl_fixture("testrepo.git")));
477 cl_git_pass(git_repository_new(&inmemory
));
478 cl_git_pass(git_remote_create_anonymous(&remote
, inmemory
, git_buf_cstr(&file_path_buf
)));
479 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
480 cl_assert(git_remote_connected(remote
));
481 git_remote_disconnect(remote
);
482 git_remote_free(remote
);
483 git_repository_free(inmemory
);