1 #include "clar_libgit2.h"
3 static git_repository
*_repo
;
6 void test_online_fetch__initialize(void)
8 cl_git_pass(git_repository_init(&_repo
, "./fetch", 0));
11 void test_online_fetch__cleanup(void)
13 git_repository_free(_repo
);
16 cl_fixture_cleanup("./fetch");
19 static int update_tips(const char *refname
, const git_oid
*a
, const git_oid
*b
, void *data
)
21 GIT_UNUSED(refname
); GIT_UNUSED(a
); GIT_UNUSED(b
); GIT_UNUSED(data
);
28 static int progress(const git_transfer_progress
*stats
, void *payload
)
30 size_t *bytes_received
= (size_t *)payload
;
31 *bytes_received
= stats
->received_bytes
;
35 static void do_fetch(const char *url
, git_remote_autotag_option_t flag
, int n
)
38 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
39 size_t bytes_received
= 0;
41 callbacks
.transfer_progress
= progress
;
42 callbacks
.update_tips
= update_tips
;
43 callbacks
.payload
= &bytes_received
;
46 cl_git_pass(git_remote_create(&remote
, _repo
, "test", url
));
47 git_remote_set_callbacks(remote
, &callbacks
);
48 git_remote_set_autotag(remote
, flag
);
49 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
50 cl_git_pass(git_remote_download(remote
, NULL
));
51 cl_git_pass(git_remote_update_tips(remote
, NULL
));
52 git_remote_disconnect(remote
);
53 cl_assert_equal_i(counter
, n
);
54 cl_assert(bytes_received
> 0);
56 git_remote_free(remote
);
59 void test_online_fetch__default_git(void)
61 do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, 6);
64 void test_online_fetch__default_http(void)
66 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, 6);
69 void test_online_fetch__default_https(void)
71 do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, 6);
74 void test_online_fetch__no_tags_git(void)
76 do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE
, 3);
79 void test_online_fetch__no_tags_http(void)
81 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE
, 3);
84 void test_online_fetch__fetch_twice(void)
87 cl_git_pass(git_remote_create(&remote
, _repo
, "test", "git://github.com/libgit2/TestGitRepository.git"));
88 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
89 cl_git_pass(git_remote_download(remote
, NULL
));
90 git_remote_disconnect(remote
);
92 git_remote_connect(remote
, GIT_DIRECTION_FETCH
);
93 cl_git_pass(git_remote_download(remote
, NULL
));
94 git_remote_disconnect(remote
);
96 git_remote_free(remote
);
99 static int transferProgressCallback(const git_transfer_progress
*stats
, void *payload
)
101 bool *invoked
= (bool *)payload
;
108 void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
110 git_repository
*_repository
;
111 bool invoked
= false;
113 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
114 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
117 cl_git_pass(git_clone(&_repository
, "https://github.com/libgit2/TestGitRepository.git",
118 "./fetch/lg2", &opts
));
119 git_repository_free(_repository
);
121 cl_git_pass(git_repository_open(&_repository
, "./fetch/lg2"));
123 cl_git_pass(git_remote_lookup(&remote
, _repository
, "origin"));
124 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
126 cl_assert_equal_i(false, invoked
);
128 callbacks
.transfer_progress
= &transferProgressCallback
;
129 callbacks
.payload
= &invoked
;
130 git_remote_set_callbacks(remote
, &callbacks
);
131 cl_git_pass(git_remote_download(remote
, NULL
));
133 cl_assert_equal_i(false, invoked
);
135 cl_git_pass(git_remote_update_tips(remote
, NULL
));
136 git_remote_disconnect(remote
);
138 git_remote_free(remote
);
139 git_repository_free(_repository
);
142 static int cancel_at_half(const git_transfer_progress
*stats
, void *payload
)
146 if (stats
->received_objects
> (stats
->total_objects
/2))
151 void test_online_fetch__can_cancel(void)
154 size_t bytes_received
= 0;
155 git_remote_callbacks callbacks
= GIT_REMOTE_CALLBACKS_INIT
;
157 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
158 "http://github.com/libgit2/TestGitRepository.git"));
160 callbacks
.transfer_progress
= cancel_at_half
;
161 callbacks
.payload
= &bytes_received
;
162 git_remote_set_callbacks(remote
, &callbacks
);
164 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
165 cl_git_fail_with(git_remote_download(remote
, NULL
), -4321);
166 git_remote_disconnect(remote
);
167 git_remote_free(remote
);
170 void test_online_fetch__ls_disconnected(void)
172 const git_remote_head
**refs
;
173 size_t refs_len_before
, refs_len_after
;
176 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
177 "http://github.com/libgit2/TestGitRepository.git"));
178 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
179 cl_git_pass(git_remote_ls(&refs
, &refs_len_before
, remote
));
180 git_remote_disconnect(remote
);
181 cl_git_pass(git_remote_ls(&refs
, &refs_len_after
, remote
));
183 cl_assert_equal_i(refs_len_before
, refs_len_after
);
185 git_remote_free(remote
);
188 void test_online_fetch__remote_symrefs(void)
190 const git_remote_head
**refs
;
194 cl_git_pass(git_remote_create(&remote
, _repo
, "test",
195 "http://github.com/libgit2/TestGitRepository.git"));
196 cl_git_pass(git_remote_connect(remote
, GIT_DIRECTION_FETCH
));
197 git_remote_disconnect(remote
);
198 cl_git_pass(git_remote_ls(&refs
, &refs_len
, remote
));
200 cl_assert_equal_s("HEAD", refs
[0]->name
);
201 cl_assert_equal_s("refs/heads/master", refs
[0]->symref_target
);
203 git_remote_free(remote
);
206 void test_online_fetch__twice(void)
210 cl_git_pass(git_remote_create(&remote
, _repo
, "test", "http://github.com/libgit2/TestGitRepository.git"));
211 cl_git_pass(git_remote_fetch(remote
, NULL
, NULL
));
212 cl_git_pass(git_remote_fetch(remote
, NULL
, NULL
));
214 git_remote_free(remote
);