1 #include "clar_libgit2.h"
6 static git_repository
*repo
;
7 static git_buf file_path_buf
= GIT_BUF_INIT
;
8 static git_remote
*remote
;
10 static char *push_refspec_strings
[] = {
13 static git_strarray push_array
= {
18 void test_network_remote_local__initialize(void)
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
);
25 void test_network_remote_local__cleanup(void)
27 git_buf_free(&file_path_buf
);
29 git_remote_free(remote
);
32 git_repository_free(repo
);
35 cl_fixture_cleanup("remotelocal");
38 static void connect_to_local_repository(const char *local_repository
)
40 git_buf_sets(&file_path_buf
, cl_git_path_url(local_repository
));
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
));
46 void test_network_remote_local__connected(void)
48 connect_to_local_repository(cl_fixture("testrepo.git"));
49 cl_assert(git_remote_connected(remote
));
51 git_remote_disconnect(remote
);
52 cl_assert(!git_remote_connected(remote
));
55 void test_network_remote_local__retrieve_advertised_references(void)
57 const git_remote_head
**refs
;
60 connect_to_local_repository(cl_fixture("testrepo.git"));
62 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
64 cl_assert_equal_i(refs_len
, 28);
67 void test_network_remote_local__retrieve_advertised_before_connect(void)
69 const git_remote_head
**refs
;
72 git_buf_sets(&file_path_buf
, cl_git_path_url(cl_fixture("testrepo.git")));
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
));
78 void test_network_remote_local__retrieve_advertised_references_after_disconnect(void)
80 const git_remote_head
**refs
;
83 connect_to_local_repository(cl_fixture("testrepo.git"));
84 git_remote_disconnect(remote
);
86 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
88 cl_assert_equal_i(refs_len
, 28);
91 void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void)
93 const git_remote_head
**refs
;
96 cl_fixture_sandbox("testrepo.git");
97 cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git"));
99 connect_to_local_repository("spaced testrepo.git");
101 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
103 cl_assert_equal_i(refs_len
, 28);
105 git_remote_free(remote
); /* Disconnect from the "spaced repo" before the cleanup */
108 cl_fixture_cleanup("spaced testrepo.git");
111 void test_network_remote_local__nested_tags_are_completely_peeled(void)
113 const git_remote_head
**refs
;
116 connect_to_local_repository(cl_fixture("testrepo.git"));
118 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
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"));
126 void test_network_remote_local__shorthand_fetch_refspec0(void)
128 char *refspec_strings
[] = {
129 "master:remotes/sloppy/master",
130 "master:boh/sloppy/master",
132 git_strarray array
= {
139 connect_to_local_repository(cl_fixture("testrepo.git"));
141 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
143 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/sloppy/master"));
144 git_reference_free(ref
);
146 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/heads/boh/sloppy/master"));
147 git_reference_free(ref
);
150 void test_network_remote_local__shorthand_fetch_refspec1(void)
152 char *refspec_strings
[] = {
156 git_strarray array
= {
163 connect_to_local_repository(cl_fixture("testrepo.git"));
165 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
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"));
171 void test_network_remote_local__tagopt(void)
174 git_fetch_options fetch_opts
= GIT_FETCH_OPTIONS_INIT
;
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
));
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
);
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
);
191 void test_network_remote_local__push_to_bare_remote(void)
193 char *refspec_strings
[] = {
196 git_strarray array
= {
201 /* Should be able to push to a bare remote */
202 git_remote
*localremote
;
204 /* Get some commits */
205 connect_to_local_repository(cl_fixture("testrepo.git"));
206 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
208 /* Set up an empty bare repo to push into */
210 git_repository
*localbarerepo
;
211 cl_git_pass(git_repository_init(&localbarerepo
, "./localbare.git", 1));
212 git_repository_free(localbarerepo
);
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
));
220 cl_git_pass(git_remote_upload(localremote
, &push_array
, NULL
));
223 git_remote_free(localremote
);
224 cl_fixture_cleanup("localbare.git");
227 void test_network_remote_local__push_to_bare_remote_with_file_url(void)
229 char *refspec_strings
[] = {
232 git_strarray array
= {
236 /* Should be able to push to a bare remote */
237 git_remote
*localremote
;
240 /* Get some commits */
241 connect_to_local_repository(cl_fixture("testrepo.git"));
242 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
244 /* Set up an empty bare repo to push into */
246 git_repository
*localbarerepo
;
247 cl_git_pass(git_repository_init(&localbarerepo
, "./localbare.git", 1));
248 git_repository_free(localbarerepo
);
251 /* Create a file URL */
252 url
= cl_git_path_url("./localbare.git");
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
));
259 cl_git_pass(git_remote_upload(localremote
, &push_array
, NULL
));
262 git_remote_free(localremote
);
263 cl_fixture_cleanup("localbare.git");
267 void test_network_remote_local__push_to_non_bare_remote(void)
269 char *refspec_strings
[] = {
272 git_strarray array
= {
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
;
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
));
284 /* Set up an empty non-bare repo to push into */
286 git_repository
*remoterepo
= NULL
;
287 cl_git_pass(git_repository_init(&remoterepo
, "localnonbare", 0));
288 git_repository_free(remoterepo
);
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
));
296 cl_git_fail_with(GIT_EBAREREPO
, git_remote_upload(localremote
, &push_array
, NULL
));
299 git_remote_free(localremote
);
300 cl_fixture_cleanup("localbare.git");
303 void test_network_remote_local__fetch(void)
305 char *refspec_strings
[] = {
306 "master:remotes/sloppy/master",
308 git_strarray array
= {
314 const git_reflog_entry
*entry
;
317 connect_to_local_repository(cl_fixture("testrepo.git"));
319 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, "UPDAAAAAATE!!"));
321 cl_git_pass(git_reference_lookup(&ref
, repo
, "refs/remotes/sloppy/master"));
322 git_reference_free(ref
);
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
));
330 git_reflog_free(log
);
333 void test_network_remote_local__reflog(void)
335 char *refspec_strings
[] = {
336 "master:remotes/sloppy/master",
338 git_strarray array
= {
344 const git_reflog_entry
*entry
;
346 connect_to_local_repository(cl_fixture("testrepo.git"));
348 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, "UPDAAAAAATE!!"));
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
));
356 git_reflog_free(log
);
359 void test_network_remote_local__fetch_default_reflog_message(void)
361 char *refspec_strings
[] = {
362 "master:remotes/sloppy/master",
364 git_strarray array
= {
370 const git_reflog_entry
*entry
;
371 char expected_reflog_msg
[1024];
373 connect_to_local_repository(cl_fixture("testrepo.git"));
375 cl_git_pass(git_remote_fetch(remote
, &array
, NULL
, NULL
));
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
);
382 sprintf(expected_reflog_msg
, "fetch %s", git_remote_url(remote
));
383 cl_assert_equal_s(expected_reflog_msg
, git_reflog_entry_message(entry
));
385 git_reflog_free(log
);
388 void test_network_remote_local__opportunistic_update(void)
391 char *refspec_strings
[] = {
394 git_strarray array
= {
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
));
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
);
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
;
416 cl_fixture_sandbox("testrepo.git");
417 cl_git_pass(git_repository_open(&src_repo
, "testrepo.git"));
419 /* Set up an empty bare repo to push into */
420 cl_git_pass(git_repository_init(&dst_repo
, "./localbare.git", 1));
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
));
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");
436 void test_network_remote_local__push_delete(void)
438 git_repository
*src_repo
;
439 git_repository
*dst_repo
;
442 char *spec_push
[] = { "refs/heads/master" };
443 char *spec_delete
[] = { ":refs/heads/master" };
444 git_strarray specs
= {
449 src_repo
= cl_git_sandbox_init("testrepo.git");
450 cl_git_pass(git_repository_init(&dst_repo
, "target.git", 1));
452 cl_git_pass(git_remote_create(&remote
, src_repo
, "origin", "./target.git"));
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
);
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"));
463 git_remote_free(remote
);
464 git_repository_free(dst_repo
);
465 cl_fixture_cleanup("target.git");
466 cl_git_sandbox_cleanup();
469 void test_network_remote_local__anonymous_remote_inmemory_repo(void)
471 git_repository
*inmemory
;
474 git_buf_sets(&file_path_buf
, cl_git_path_url(cl_fixture("testrepo.git")));
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
);