1 #include "clar_libgit2.h"
4 static git_repository
*_repo
;
7 static char *_remote_proxy_scheme
= NULL
;
8 static char *_remote_proxy_host
= NULL
;
9 static char *_remote_proxy_user
= NULL
;
10 static char *_remote_proxy_pass
= NULL
;
11 static char *_remote_redirect_initial
= NULL
;
12 static char *_remote_redirect_subsequent
= NULL
;
14 void test_online_fetch__initialize(void)
16 cl_git_pass(git_repository_init(&_repo
, "./fetch", 0));
18 _remote_proxy_scheme
= cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
19 _remote_proxy_host
= cl_getenv("GITTEST_REMOTE_PROXY_HOST");
20 _remote_proxy_user
= cl_getenv("GITTEST_REMOTE_PROXY_USER");
21 _remote_proxy_pass
= cl_getenv("GITTEST_REMOTE_PROXY_PASS");
22 _remote_redirect_initial
= cl_getenv("GITTEST_REMOTE_REDIRECT_INITIAL");
23 _remote_redirect_subsequent
= cl_getenv("GITTEST_REMOTE_REDIRECT_SUBSEQUENT");
26 void test_online_fetch__cleanup(void)
28 git_repository_free(_repo
);
31 cl_fixture_cleanup("./fetch");
32 cl_fixture_cleanup("./redirected");
34 git__free(_remote_proxy_scheme
);
35 git__free(_remote_proxy_host
);
36 git__free(_remote_proxy_user
);
37 git__free(_remote_proxy_pass
);
38 git__free(_remote_redirect_initial
);
39 git__free(_remote_redirect_subsequent
);
42 static int update_tips(const char *refname
, const git_oid
*a
, const git_oid
*b
, void *data
)
44 GIT_UNUSED(refname
); GIT_UNUSED(a
); GIT_UNUSED(b
); GIT_UNUSED(data
);
51 static int progress(const git_indexer_progress
*stats
, void *payload
)
53 size_t *bytes_received
= (size_t *)payload
;
54 *bytes_received
= stats
->received_bytes
;
58 static void do_fetch(const char *url
, git_remote_autotag_option_t flag
, int n
)
61 git_fetch_options options
= GIT_FETCH_OPTIONS_INIT
;
62 size_t bytes_received
= 0;
64 options
.callbacks
.transfer_progress
= progress
;
65 options
.callbacks
.update_tips
= update_tips
;
66 options
.callbacks
.payload
= &bytes_received
;
67 options
.download_tags
= flag
;
70 cl_git_pass(git_remote_create(&remote
, _repo
, "test", url
));
71 cl_git_pass(git_remote_fetch(remote
, NULL
, &options
, NULL
));
72 cl_assert_equal_i(counter
, n
);
73 cl_assert(bytes_received
> 0);
75 git_remote_free(remote
);
78 void test_online_fetch__default_http(void)
80 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, 6);
83 void test_online_fetch__default_https(void)
85 do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, 6);
88 void test_online_fetch__no_tags_git(void)
90 do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE
, 3);
93 void test_online_fetch__no_tags_http(void)
95 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE
, 3);
98 void test_online_fetch__fetch_twice(void)
101 cl_git_pass(git_remote_create(&remote
, _repo
, "test", "https://github.com/libgit2/TestGitRepository.git"));
102 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
103 cl_git_pass(git_remote_download(remote
, NULL
, NULL
));
104 git_remote_disconnect(remote
);
106 git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
);
107 cl_git_pass(git_remote_download(remote
, NULL
, NULL
));
108 git_remote_disconnect(remote
);
110 git_remote_free(remote
);
113 static int transferProgressCallback(const git_indexer_progress
*stats
, void *payload
)
115 bool *invoked
= (bool *)payload
;
122 void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
124 git_repository
*_repository
;
125 bool invoked
= false;
127 git_fetch_options options
= GIT_FETCH_OPTIONS_INIT
;
128 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
131 cl_git_pass(git_clone(&_repository
, "https://github.com/libgit2/TestGitRepository.git",
132 "./fetch/lg2", &opts
));
133 git_repository_free(_repository
);
135 cl_git_pass(git_repository_open(&_repository
, "./fetch/lg2"));
137 cl_git_pass(git_remote_lookup(&remote
, _repository
, "origin"));
138 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
140 cl_assert_equal_i(false, invoked
);
142 options
.callbacks
.transfer_progress
= &transferProgressCallback
;
143 options
.callbacks
.payload
= &invoked
;
144 cl_git_pass(git_remote_download(remote
, NULL
, &options
));
146 cl_assert_equal_i(false, invoked
);
148 cl_git_pass(git_remote_update_tips(remote
, &options
.callbacks
, 1, options
.download_tags
, NULL
));
149 git_remote_disconnect(remote
);
151 git_remote_free(remote
);
152 git_repository_free(_repository
);
155 static int cancel_at_half(const git_indexer_progress
*stats
, void *payload
)
159 if (stats
->received_objects
> (stats
->total_objects
/2))
164 void test_online_fetch__can_cancel(void)
167 size_t bytes_received
= 0;
168 git_fetch_options options
= GIT_FETCH_OPTIONS_INIT
;
170 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
171 "http://github.com/libgit2/TestGitRepository.git"));
173 options
.callbacks
.transfer_progress
= cancel_at_half
;
174 options
.callbacks
.payload
= &bytes_received
;
176 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
177 cl_git_fail_with(git_remote_download(remote
, NULL
, &options
), -4321);
178 git_remote_disconnect(remote
);
179 git_remote_free(remote
);
182 void test_online_fetch__ls_disconnected(void)
184 const git_remote_head
**refs
;
185 size_t refs_len_before
, refs_len_after
;
188 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
189 "http://github.com/libgit2/TestGitRepository.git"));
190 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
191 cl_git_pass(git_remote_ls(&refs
, &refs_len_before
, remote
));
192 git_remote_disconnect(remote
);
193 cl_git_pass(git_remote_ls(&refs
, &refs_len_after
, remote
));
195 cl_assert_equal_i(refs_len_before
, refs_len_after
);
197 git_remote_free(remote
);
200 void test_online_fetch__remote_symrefs(void)
202 const git_remote_head
**refs
;
206 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
207 "http://github.com/libgit2/TestGitRepository.git"));
208 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, NULL
, NULL
));
209 git_remote_disconnect(remote
);
210 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
212 cl_assert_equal_s("HEAD", refs
[0]->name
);
213 cl_assert_equal_s("refs/heads/master", refs
[0]->symref_target
);
215 git_remote_free(remote
);
218 void test_online_fetch__twice(void)
222 cl_git_pass(git_remote_create(&remote
, _repo
, "test", "http://github.com/libgit2/TestGitRepository.git"));
223 cl_git_pass(git_remote_fetch(remote
, NULL
, NULL
, NULL
));
224 cl_git_pass(git_remote_fetch(remote
, NULL
, NULL
, NULL
));
226 git_remote_free(remote
);
229 void test_online_fetch__proxy(void)
232 git_str url
= GIT_STR_INIT
;
233 git_fetch_options fetch_opts
;
235 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
238 cl_git_pass(git_str_printf(&url
, "%s://%s:%s@%s/",
239 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
240 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
242 cl_git_pass(git_fetch_options_init(&fetch_opts
, GIT_FETCH_OPTIONS_VERSION
));
243 fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
244 fetch_opts
.proxy_opts
.url
= url
.ptr
;
246 cl_git_pass(git_remote_create(&remote
, _repo
, "test", "https://github.com/libgit2/TestGitRepository.git"));
247 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
, NULL
, &fetch_opts
.proxy_opts
, NULL
));
248 cl_git_pass(git_remote_fetch(remote
, NULL
, &fetch_opts
, NULL
));
250 git_remote_free(remote
);
251 git_str_dispose(&url
);
254 static int do_redirected_fetch(const char *url
, const char *name
, const char *config
)
256 git_repository
*repo
;
260 cl_git_pass(git_repository_init(&repo
, "./redirected", 0));
261 cl_fixture_cleanup(name
);
264 cl_repo_set_string(repo
, "http.followRedirects", config
);
266 cl_git_pass(git_remote_create(&remote
, repo
, name
, url
));
267 error
= git_remote_fetch(remote
, NULL
, NULL
, NULL
);
269 git_remote_free(remote
);
270 git_repository_free(repo
);
272 cl_fixture_cleanup("./redirected");
277 void test_online_fetch__redirect_config(void)
279 if (!_remote_redirect_initial
|| !_remote_redirect_subsequent
)
282 /* config defaults */
283 cl_git_pass(do_redirected_fetch(_remote_redirect_initial
, "initial", NULL
));
284 cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent
, "subsequent", NULL
));
286 /* redirect=initial */
287 cl_git_pass(do_redirected_fetch(_remote_redirect_initial
, "initial", "initial"));
288 cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent
, "subsequent", "initial"));
291 cl_git_fail(do_redirected_fetch(_remote_redirect_initial
, "initial", "false"));
292 cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent
, "subsequent", "false"));
295 void test_online_fetch__reachable_commit(void)
298 git_strarray refspecs
;
301 git_str fetchhead
= GIT_STR_INIT
;
302 char *refspec
= "+2c349335b7f797072cf729c4f3bb0914ecb6dec9:refs/success";
304 refspecs
.strings
= &refspec
;
307 git_oid_fromstr(&expected_id
, "2c349335b7f797072cf729c4f3bb0914ecb6dec9");
309 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
310 "https://github.com/libgit2/TestGitRepository"));
311 cl_git_pass(git_remote_fetch(remote
, &refspecs
, NULL
, NULL
));
313 cl_git_pass(git_revparse_single(&obj
, _repo
, "refs/success"));
314 cl_assert_equal_oid(&expected_id
, git_object_id(obj
));
316 cl_git_pass(git_futils_readbuffer(&fetchhead
, "./fetch/.git/FETCH_HEAD"));
317 cl_assert_equal_s(fetchhead
.ptr
,
318 "2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
320 git_str_dispose(&fetchhead
);
321 git_object_free(obj
);
322 git_remote_free(remote
);
325 void test_online_fetch__reachable_commit_without_destination(void)
328 git_strarray refspecs
;
331 git_str fetchhead
= GIT_STR_INIT
;
332 char *refspec
= "2c349335b7f797072cf729c4f3bb0914ecb6dec9";
334 refspecs
.strings
= &refspec
;
337 git_oid_fromstr(&expected_id
, "2c349335b7f797072cf729c4f3bb0914ecb6dec9");
339 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
340 "https://github.com/libgit2/TestGitRepository"));
341 cl_git_pass(git_remote_fetch(remote
, &refspecs
, NULL
, NULL
));
343 cl_git_fail_with(GIT_ENOTFOUND
, git_revparse_single(&obj
, _repo
, "refs/success"));
345 cl_git_pass(git_futils_readbuffer(&fetchhead
, "./fetch/.git/FETCH_HEAD"));
346 cl_assert_equal_s(fetchhead
.ptr
,
347 "2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
349 git_str_dispose(&fetchhead
);
350 git_object_free(obj
);
351 git_remote_free(remote
);