]> git.proxmox.com Git - libgit2.git/blame - tests/clone/nonetwork.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / clone / nonetwork.c
CommitLineData
764df57e
BS
1#include "clar_libgit2.h"
2
3#include "git2/clone.h"
1e44ea97 4#include "../submodule/submodule_helpers.h"
4330ab26 5#include "remote.h"
22a2d3d5 6#include "futils.h"
114f5a6c 7#include "repository.h"
764df57e 8
86a2da6e 9#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
822d9dd5 10
18b2d560 11static git_clone_options g_options;
764df57e 12static git_repository *g_repo;
b5b28120
SC
13static git_reference* g_ref;
14static git_remote* g_remote;
764df57e 15
65415ea2 16void test_clone_nonetwork__initialize(void)
764df57e 17{
6affd71f 18 git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
8f0104ec 19 git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
730df6d0 20
1c7eb971 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 25 g_options.checkout_opts = dummy_opts;
6c9e86ad 26 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
8f0104ec 27 g_options.fetch_opts = dummy_fetch;
764df57e
BS
28}
29
24393ea6 30void test_clone_nonetwork__cleanup(void)
764df57e 31{
9094d30b 32 if (g_repo) {
1c7eb971 33 git_repository_free(g_repo);
9094d30b
SC
34 g_repo = NULL;
35 }
36
b5b28120
SC
37 if (g_ref) {
38 git_reference_free(g_ref);
39 g_ref = NULL;
40 }
41
42 if (g_remote) {
43 git_remote_free(g_remote);
44 g_remote = NULL;
45 }
46
f46769e5 47 cl_fixture_cleanup("./foo");
764df57e
BS
48}
49
ff0ef88c 50void test_clone_nonetwork__bad_urls(void)
764df57e 51{
1c7eb971 52 /* Clone should clean up the mess if the URL isn't a git repository */
b412d563 53 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
e579e0f7 54 cl_assert(!git_fs_path_exists("./foo"));
18b2d560 55 g_options.bare = true;
b412d563 56 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
e579e0f7 57 cl_assert(!git_fs_path_exists("./foo"));
ff0ef88c
BS
58
59 cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options));
ff0ef88c 60 cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options));
7be5104d 61 cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
887df99f
BS
62 "./foo", &g_options));
63 cl_git_fail(git_clone(&g_repo, "arrbee:my/bad:password@github.com:1111/strange:words.git",
64 "./foo", &g_options));
764df57e
BS
65}
66
219d3457
RB
67void test_clone_nonetwork__do_not_clean_existing_directory(void)
68{
926acbcf
JM
69 /* Clone should not remove the directory if it already exists, but
70 * Should clean up entries it creates. */
71 p_mkdir("./foo", GIT_DIR_MODE);
72 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
e579e0f7 73 cl_assert(git_fs_path_is_empty_dir("./foo"));
926acbcf
JM
74
75 /* Try again with a bare repository. */
76 g_options.bare = true;
77 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
e579e0f7 78 cl_assert(git_fs_path_is_empty_dir("./foo"));
926acbcf
JM
79}
80
65415ea2 81void test_clone_nonetwork__local(void)
764df57e 82{
b412d563 83 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
764df57e
BS
84}
85
8a8820d8
JM
86void test_clone_nonetwork__local_absolute_path(void)
87{
6bd09ecc 88 const char *local_src;
6bd09ecc 89 local_src = cl_fixture("testrepo.git");
b412d563 90 cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
8a8820d8
JM
91}
92
65415ea2 93void test_clone_nonetwork__local_bare(void)
ebecf1e7 94{
18b2d560 95 g_options.bare = true;
b412d563 96 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
ebecf1e7 97}
764df57e 98
65415ea2 99void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
ebecf1e7 100{
1c7eb971 101 cl_git_mkfile("./foo", "Bar!");
b412d563 102 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
ebecf1e7 103}
104
65415ea2 105void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
ebecf1e7 106{
1c7eb971
BS
107 p_mkdir("./foo", GIT_DIR_MODE);
108 cl_git_mkfile("./foo/bar", "Baz!");
b412d563 109 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
764df57e 110}
621b50e4 111
e579e0f7 112static int custom_origin_name_remote_create(
1697cd6f
PK
113 git_remote **out,
114 git_repository *repo,
115 const char *name,
116 const char *url,
117 void *payload)
118{
119 GIT_UNUSED(name);
120 GIT_UNUSED(payload);
121
122 return git_remote_create(out, repo, "my_origin", url);
123}
124
c833893c
CMN
125void test_clone_nonetwork__custom_origin_name(void)
126{
1697cd6f
PK
127 g_options.remote_cb = custom_origin_name_remote_create;
128 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
c833893c 129
209425ce 130 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin"));
c833893c
CMN
131}
132
fdc7e5e3
CMN
133void test_clone_nonetwork__defaults(void)
134{
135 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL));
136 cl_assert(g_repo);
209425ce 137 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
fdc7e5e3 138}
c833893c 139
922dd978
BS
140void test_clone_nonetwork__cope_with_already_existing_directory(void)
141{
922dd978
BS
142 p_mkdir("./foo", GIT_DIR_MODE);
143 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
144}
145
146void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
147{
e579e0f7 148 git_str path = GIT_STR_INIT;
922dd978
BS
149
150 g_options.checkout_opts.checkout_strategy = 0;
151 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
152
e579e0f7
MB
153 cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
154 cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&path)));
922dd978 155
e579e0f7 156 git_str_dispose(&path);
922dd978
BS
157}
158
f1d4a35e
SC
159void test_clone_nonetwork__can_checkout_given_branch(void)
160{
c25aa7cd
PP
161 git_reference *remote_head;
162
f1d4a35e
SC
163 g_options.checkout_branch = "test";
164 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
165
605da51a 166 cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
f1d4a35e
SC
167
168 cl_git_pass(git_repository_head(&g_ref, g_repo));
169 cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
8f1066a0 170
e579e0f7 171 cl_assert(git_fs_path_exists("foo/readme.txt"));
c25aa7cd
PP
172
173 cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD"));
174 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head));
175 cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head));
176
177 git_reference_free(remote_head);
8f1066a0
RB
178}
179
180static int clone_cancel_fetch_transfer_progress_cb(
22a2d3d5 181 const git_indexer_progress *stats, void *data)
8f1066a0
RB
182{
183 GIT_UNUSED(stats); GIT_UNUSED(data);
184 return -54321;
185}
186
187void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
188{
189 g_options.checkout_branch = "test";
190
8f0104ec 191 g_options.fetch_opts.callbacks.transfer_progress =
8f1066a0
RB
192 clone_cancel_fetch_transfer_progress_cb;
193
194 cl_git_fail_with(git_clone(
195 &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
196 -54321);
197
198 cl_assert(!g_repo);
e579e0f7 199 cl_assert(!git_fs_path_exists("foo/readme.txt"));
8f1066a0
RB
200}
201
202static int clone_cancel_checkout_cb(
203 git_checkout_notify_t why,
204 const char *path,
205 const git_diff_file *b,
206 const git_diff_file *t,
207 const git_diff_file *w,
208 void *payload)
209{
210 const char *at_file = payload;
211 GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
212 if (!strcmp(path, at_file))
213 return -12345;
214 return 0;
215}
216
217void test_clone_nonetwork__can_cancel_clone_in_checkout(void)
218{
219 g_options.checkout_branch = "test";
220
221 g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
222 g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb;
223 g_options.checkout_opts.notify_payload = "readme.txt";
224
225 cl_git_fail_with(git_clone(
226 &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
227 -12345);
228
229 cl_assert(!g_repo);
e579e0f7 230 cl_assert(!git_fs_path_exists("foo/readme.txt"));
f1d4a35e
SC
231}
232
aa928de0
FL
233void test_clone_nonetwork__can_detached_head(void)
234{
2ebc3c66 235 git_object *obj;
aa928de0
FL
236 git_repository *cloned;
237 git_reference *cloned_head;
238
239 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
240
2ebc3c66 241 cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
4e498646 242 cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
aa928de0
FL
243
244 cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
245
246 cl_assert(git_repository_head_detached(cloned));
247
248 cl_git_pass(git_repository_head(&cloned_head, cloned));
0cee70eb 249 cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
aa928de0 250
2ebc3c66 251 git_object_free(obj);
aa928de0
FL
252 git_reference_free(cloned_head);
253 git_repository_free(cloned);
254
255 cl_fixture_cleanup("./foo1");
256}
1cc974ab 257
d23fb5c9
CMN
258void test_clone_nonetwork__clone_tag_to_tree(void)
259{
260 git_repository *stage;
261 git_index_entry entry;
262 git_index *index;
263 git_odb *odb;
264 git_oid tree_id;
265 git_tree *tree;
266 git_reference *tag;
267 git_tree_entry *tentry;
268 const char *file_path = "some/deep/path.txt";
269 const char *file_content = "some content\n";
270 const char *tag_name = "refs/tags/tree-tag";
271
272 stage = cl_git_sandbox_init("testrepo.git");
273 cl_git_pass(git_repository_odb(&odb, stage));
274 cl_git_pass(git_index_new(&index));
275
276 memset(&entry, 0, sizeof(git_index_entry));
277 entry.path = file_path;
278 entry.mode = GIT_FILEMODE_BLOB;
ac3d33df 279 cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJECT_BLOB));
d23fb5c9
CMN
280
281 cl_git_pass(git_index_add(index, &entry));
282 cl_git_pass(git_index_write_tree_to(&tree_id, index, stage));
283 cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL));
284 git_reference_free(tag);
285 git_odb_free(odb);
286 git_index_free(index);
287
288 g_options.local = GIT_CLONE_NO_LOCAL;
289 cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options));
290 git_repository_free(stage);
291
292 cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name));
293 cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag)));
294 git_reference_free(tag);
295
296 cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path));
297 git_tree_entry_free(tentry);
298 git_tree_free(tree);
299
300 cl_fixture_cleanup("testrepo.git");
301}
302
1cc974ab
BS
303static void assert_correct_reflog(const char *name)
304{
305 git_reflog *log;
306 const git_reflog_entry *entry;
e579e0f7 307 git_str expected_message = GIT_STR_INIT;
1cc974ab 308
e579e0f7 309 git_str_printf(&expected_message,
4cc355c9 310 "clone: from %s", cl_git_fixture_url("testrepo.git"));
1cc974ab
BS
311
312 cl_git_pass(git_reflog_read(&log, g_repo, name));
313 cl_assert_equal_i(1, git_reflog_entrycount(log));
314 entry = git_reflog_entry_byindex(log, 0);
4cc355c9 315 cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry));
1cc974ab
BS
316
317 git_reflog_free(log);
4cc355c9 318
e579e0f7 319 git_str_dispose(&expected_message);
1cc974ab
BS
320}
321
322void test_clone_nonetwork__clone_updates_reflog_properly(void)
323{
1cc974ab
BS
324 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
325 assert_correct_reflog("HEAD");
326 assert_correct_reflog("refs/heads/master");
1cc974ab
BS
327}
328
6f6be8fe
CMN
329static void cleanup_repository(void *path)
330{
331 if (g_repo) {
332 git_repository_free(g_repo);
333 g_repo = NULL;
334 }
335
336 cl_fixture_cleanup((const char *)path);
337}
338
339void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
340{
341 git_config *config;
342 git_repository *repo;
343 const char *str;
344
345 /* Create an empty repo to clone from */
346 cl_set_cleanup(&cleanup_repository, "./test1");
347 cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
348 cl_set_cleanup(&cleanup_repository, "./repowithunborn");
349 cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
350
9a97f49e 351 cl_git_pass(git_repository_config_snapshot(&config, repo));
6f6be8fe
CMN
352
353 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
354 cl_assert_equal_s("origin", str);
355 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
356 cl_assert_equal_s("refs/heads/master", str);
357
358 git_config_free(config);
359 git_repository_free(repo);
360 cl_fixture_cleanup("./repowithunborn");
361}