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