]> git.proxmox.com Git - libgit2.git/blame - tests-clar/online/clone.c
Move some diff helpers into separate file
[libgit2.git] / tests-clar / online / clone.c
CommitLineData
65415ea2
BS
1#include "clar_libgit2.h"
2
3#include "git2/clone.h"
520dcc1c 4#include "git2/cred_helpers.h"
65415ea2 5#include "repository.h"
6f748f38 6#include "remote.h"
65415ea2 7
44f36f6e
BS
8#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
9#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
5f10853e 10#define BB_REPO_URL "https://libgit2@bitbucket.org/libgit2/testgitrepository.git"
cf7038a6 11#define BB_REPO_URL_WITH_PASS "https://libgit2:libgit2@bitbucket.org/libgit2/testgitrepository.git"
54ffc1f7 12#define BB_REPO_URL_WITH_WRONG_PASS "https://libgit2:wrong@bitbucket.org/libgit2/testgitrepository.git"
65415ea2
BS
13
14static git_repository *g_repo;
18b2d560 15static git_clone_options g_options;
65415ea2 16
6443eaf2 17void test_online_clone__initialize(void)
65415ea2 18{
730df6d0
BS
19 git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT;
20
65415ea2 21 g_repo = NULL;
18b2d560
BS
22
23 memset(&g_options, 0, sizeof(git_clone_options));
24 g_options.version = GIT_CLONE_OPTIONS_VERSION;
730df6d0
BS
25 g_options.checkout_opts = dummy_opts;
26 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
65415ea2
BS
27}
28
6443eaf2 29void test_online_clone__cleanup(void)
65415ea2 30{
9094d30b 31 if (g_repo) {
65415ea2 32 git_repository_free(g_repo);
9094d30b
SC
33 g_repo = NULL;
34 }
7761ce21 35 cl_fixture_cleanup("./foo");
65415ea2
BS
36}
37
6443eaf2 38void test_online_clone__network_full(void)
65415ea2
BS
39{
40 git_remote *origin;
41
b412d563 42 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
65415ea2
BS
43 cl_assert(!git_repository_is_bare(g_repo));
44 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
add5efe7 45
6f748f38
JM
46 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);
47
add5efe7 48 git_remote_free(origin);
65415ea2
BS
49}
50
6443eaf2 51void test_online_clone__network_bare(void)
65415ea2
BS
52{
53 git_remote *origin;
54
18b2d560 55 g_options.bare = true;
65415ea2 56
b412d563 57 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
65415ea2
BS
58 cl_assert(git_repository_is_bare(g_repo));
59 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
add5efe7 60
61 git_remote_free(origin);
65415ea2
BS
62}
63
6443eaf2 64void test_online_clone__empty_repository(void)
65415ea2
BS
65{
66 git_reference *head;
67
b412d563 68 cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options));
65415ea2
BS
69
70 cl_assert_equal_i(true, git_repository_is_empty(g_repo));
71 cl_assert_equal_i(true, git_repository_head_orphan(g_repo));
72
73 cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
74 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
2508cc66 75 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
65415ea2
BS
76
77 git_reference_free(head);
78}
4d968f13 79
9c05c17b 80static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
183d8bdd 81{
183d8bdd 82 bool *was_called = (bool*)payload;
1fc375e6 83 GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
183d8bdd
BS
84 (*was_called) = true;
85}
86
fe95ac1b 87static int fetch_progress(const git_transfer_progress *stats, void *payload)
aa1e8674 88{
aa1e8674 89 bool *was_called = (bool*)payload;
1fc375e6 90 GIT_UNUSED(stats);
aa1e8674 91 (*was_called) = true;
fe95ac1b 92 return 0;
aa1e8674
BS
93}
94
6443eaf2 95void test_online_clone__can_checkout_a_cloned_repo(void)
4d968f13 96{
4d968f13 97 git_buf path = GIT_BUF_INIT;
c4f68b32 98 git_reference *head;
aa1e8674
BS
99 bool checkout_progress_cb_was_called = false,
100 fetch_progress_cb_was_called = false;
4d968f13 101
b3fb9237 102 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
730df6d0
BS
103 g_options.checkout_opts.progress_cb = &checkout_progress;
104 g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
18b2d560
BS
105 g_options.fetch_progress_cb = &fetch_progress;
106 g_options.fetch_progress_payload = &fetch_progress_cb_was_called;
4d968f13 107
b412d563 108 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
4d968f13 109
110 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
111 cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path)));
c4f68b32 112
113 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
114 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
2508cc66 115 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
add5efe7 116
aa1e8674
BS
117 cl_assert_equal_i(true, checkout_progress_cb_was_called);
118 cl_assert_equal_i(true, fetch_progress_cb_was_called);
183d8bdd 119
add5efe7 120 git_reference_free(head);
121 git_buf_free(&path);
4d968f13 122}
621b50e4
BS
123
124static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *payload)
125{
126 int *callcount = (int*)payload;
127 GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b);
128 *callcount = *callcount + 1;
129 return 0;
130}
131
ffb02b16 132void test_online_clone__custom_remote_callbacks(void)
621b50e4
BS
133{
134 git_remote_callbacks remote_callbacks = GIT_REMOTE_CALLBACKS_INIT;
135 int callcount = 0;
136
621b50e4
BS
137 g_options.remote_callbacks = &remote_callbacks;
138 remote_callbacks.update_tips = update_tips;
139 remote_callbacks.payload = &callcount;
140
141 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
142 cl_assert(callcount > 0);
143}
144
ffb02b16 145void test_online_clone__credentials(void)
621b50e4
BS
146{
147 /* Remote URL environment variable must be set. User and password are optional. */
148 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
520dcc1c 149 git_cred_userpass_payload user_pass = {
621b50e4
BS
150 cl_getenv("GITTEST_REMOTE_USER"),
151 cl_getenv("GITTEST_REMOTE_PASS")
152 };
153
154 if (!remote_url) return;
155
520dcc1c 156 g_options.cred_acquire_cb = git_cred_userpass;
621b50e4
BS
157 g_options.cred_acquire_payload = &user_pass;
158
159 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
5f10853e
BS
160 git_repository_free(g_repo); g_repo = NULL;
161 cl_fixture_cleanup("./foo");
162}
163
164void test_online_clone__bitbucket_style(void)
165{
166 git_cred_userpass_payload user_pass = {
167 "libgit2", "libgit2"
168 };
169
170 g_options.cred_acquire_cb = git_cred_userpass;
171 g_options.cred_acquire_payload = &user_pass;
172
173 cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
174 git_repository_free(g_repo); g_repo = NULL;
175 cl_fixture_cleanup("./foo");
cf7038a6 176
54ffc1f7
BS
177 /* User and pass from URL */
178 user_pass.password = "wrong";
cf7038a6
BS
179 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_PASS, "./foo", &g_options));
180 git_repository_free(g_repo); g_repo = NULL;
181 cl_fixture_cleanup("./foo");
54ffc1f7
BS
182
183 /* Wrong password in URL, fall back to user_pass */
184 user_pass.password = "libgit2";
185 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_WRONG_PASS, "./foo", &g_options));
186 git_repository_free(g_repo); g_repo = NULL;
187 cl_fixture_cleanup("./foo");
621b50e4 188}
fe95ac1b
BS
189
190static int cancel_at_half(const git_transfer_progress *stats, void *payload)
191{
192 GIT_UNUSED(payload);
193
194 if (stats->received_objects > (stats->total_objects/2))
def60ea4 195 return 1;
fe95ac1b
BS
196 return 0;
197}
198
199void test_online_clone__can_cancel(void)
200{
201 g_options.fetch_progress_cb = cancel_at_half;
202 cl_git_fail_with(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), GIT_EUSER);
203}