]> git.proxmox.com Git - libgit2.git/blob - tests/clone/nonetwork.c
7ca49085cfe957402530425cdb391c8a20ec2b4d
[libgit2.git] / tests / clone / nonetwork.c
1 #include "clar_libgit2.h"
2
3 #include "git2/clone.h"
4 #include "../submodule/submodule_helpers.h"
5 #include "remote.h"
6 #include "futils.h"
7 #include "repository.h"
8
9 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
10
11 static git_clone_options g_options;
12 static git_repository *g_repo;
13 static git_reference* g_ref;
14 static git_remote* g_remote;
15
16 void test_clone_nonetwork__initialize(void)
17 {
18 git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
19 git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
20
21 g_repo = NULL;
22
23 memset(&g_options, 0, sizeof(git_clone_options));
24 g_options.version = GIT_CLONE_OPTIONS_VERSION;
25 g_options.checkout_opts = dummy_opts;
26 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
27 g_options.fetch_opts = dummy_fetch;
28 }
29
30 void test_clone_nonetwork__cleanup(void)
31 {
32 if (g_repo) {
33 git_repository_free(g_repo);
34 g_repo = NULL;
35 }
36
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
47 cl_fixture_cleanup("./foo");
48 }
49
50 void test_clone_nonetwork__bad_urls(void)
51 {
52 /* Clone should clean up the mess if the URL isn't a git repository */
53 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
54 cl_assert(!git_path_exists("./foo"));
55 g_options.bare = true;
56 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
57 cl_assert(!git_path_exists("./foo"));
58
59 cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options));
60 cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options));
61 cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
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));
65 }
66
67 void test_clone_nonetwork__do_not_clean_existing_directory(void)
68 {
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));
73 cl_assert(git_path_is_empty_dir("./foo"));
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));
78 cl_assert(git_path_is_empty_dir("./foo"));
79 }
80
81 void test_clone_nonetwork__local(void)
82 {
83 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
84 }
85
86 void test_clone_nonetwork__local_absolute_path(void)
87 {
88 const char *local_src;
89 local_src = cl_fixture("testrepo.git");
90 cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
91 }
92
93 void test_clone_nonetwork__local_bare(void)
94 {
95 g_options.bare = true;
96 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
97 }
98
99 void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
100 {
101 cl_git_mkfile("./foo", "Bar!");
102 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
103 }
104
105 void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
106 {
107 p_mkdir("./foo", GIT_DIR_MODE);
108 cl_git_mkfile("./foo/bar", "Baz!");
109 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
110 }
111
112 int custom_origin_name_remote_create(
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
125 void test_clone_nonetwork__custom_origin_name(void)
126 {
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));
129
130 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin"));
131 }
132
133 void 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);
137 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
138 }
139
140 void test_clone_nonetwork__cope_with_already_existing_directory(void)
141 {
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
146 void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
147 {
148 git_buf path = GIT_BUF_INIT;
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
153 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
154 cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
155
156 git_buf_dispose(&path);
157 }
158
159 void test_clone_nonetwork__can_checkout_given_branch(void)
160 {
161 g_options.checkout_branch = "test";
162 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
163
164 cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
165
166 cl_git_pass(git_repository_head(&g_ref, g_repo));
167 cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
168
169 cl_assert(git_path_exists("foo/readme.txt"));
170 }
171
172 static int clone_cancel_fetch_transfer_progress_cb(
173 const git_indexer_progress *stats, void *data)
174 {
175 GIT_UNUSED(stats); GIT_UNUSED(data);
176 return -54321;
177 }
178
179 void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
180 {
181 g_options.checkout_branch = "test";
182
183 g_options.fetch_opts.callbacks.transfer_progress =
184 clone_cancel_fetch_transfer_progress_cb;
185
186 cl_git_fail_with(git_clone(
187 &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
188 -54321);
189
190 cl_assert(!g_repo);
191 cl_assert(!git_path_exists("foo/readme.txt"));
192 }
193
194 static int clone_cancel_checkout_cb(
195 git_checkout_notify_t why,
196 const char *path,
197 const git_diff_file *b,
198 const git_diff_file *t,
199 const git_diff_file *w,
200 void *payload)
201 {
202 const char *at_file = payload;
203 GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
204 if (!strcmp(path, at_file))
205 return -12345;
206 return 0;
207 }
208
209 void test_clone_nonetwork__can_cancel_clone_in_checkout(void)
210 {
211 g_options.checkout_branch = "test";
212
213 g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
214 g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb;
215 g_options.checkout_opts.notify_payload = "readme.txt";
216
217 cl_git_fail_with(git_clone(
218 &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
219 -12345);
220
221 cl_assert(!g_repo);
222 cl_assert(!git_path_exists("foo/readme.txt"));
223 }
224
225 void test_clone_nonetwork__can_detached_head(void)
226 {
227 git_object *obj;
228 git_repository *cloned;
229 git_reference *cloned_head;
230
231 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
232
233 cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
234 cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
235
236 cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
237
238 cl_assert(git_repository_head_detached(cloned));
239
240 cl_git_pass(git_repository_head(&cloned_head, cloned));
241 cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
242
243 git_object_free(obj);
244 git_reference_free(cloned_head);
245 git_repository_free(cloned);
246
247 cl_fixture_cleanup("./foo1");
248 }
249
250 void test_clone_nonetwork__clone_tag_to_tree(void)
251 {
252 git_repository *stage;
253 git_index_entry entry;
254 git_index *index;
255 git_odb *odb;
256 git_oid tree_id;
257 git_tree *tree;
258 git_reference *tag;
259 git_tree_entry *tentry;
260 const char *file_path = "some/deep/path.txt";
261 const char *file_content = "some content\n";
262 const char *tag_name = "refs/tags/tree-tag";
263
264 stage = cl_git_sandbox_init("testrepo.git");
265 cl_git_pass(git_repository_odb(&odb, stage));
266 cl_git_pass(git_index_new(&index));
267
268 memset(&entry, 0, sizeof(git_index_entry));
269 entry.path = file_path;
270 entry.mode = GIT_FILEMODE_BLOB;
271 cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJECT_BLOB));
272
273 cl_git_pass(git_index_add(index, &entry));
274 cl_git_pass(git_index_write_tree_to(&tree_id, index, stage));
275 cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL));
276 git_reference_free(tag);
277 git_odb_free(odb);
278 git_index_free(index);
279
280 g_options.local = GIT_CLONE_NO_LOCAL;
281 cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options));
282 git_repository_free(stage);
283
284 cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name));
285 cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag)));
286 git_reference_free(tag);
287
288 cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path));
289 git_tree_entry_free(tentry);
290 git_tree_free(tree);
291
292 cl_fixture_cleanup("testrepo.git");
293 }
294
295 static void assert_correct_reflog(const char *name)
296 {
297 git_reflog *log;
298 const git_reflog_entry *entry;
299 git_buf expected_message = GIT_BUF_INIT;
300
301 git_buf_printf(&expected_message,
302 "clone: from %s", cl_git_fixture_url("testrepo.git"));
303
304 cl_git_pass(git_reflog_read(&log, g_repo, name));
305 cl_assert_equal_i(1, git_reflog_entrycount(log));
306 entry = git_reflog_entry_byindex(log, 0);
307 cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry));
308
309 git_reflog_free(log);
310
311 git_buf_dispose(&expected_message);
312 }
313
314 void test_clone_nonetwork__clone_updates_reflog_properly(void)
315 {
316 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
317 assert_correct_reflog("HEAD");
318 assert_correct_reflog("refs/heads/master");
319 }
320
321 static void cleanup_repository(void *path)
322 {
323 if (g_repo) {
324 git_repository_free(g_repo);
325 g_repo = NULL;
326 }
327
328 cl_fixture_cleanup((const char *)path);
329 }
330
331 void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
332 {
333 git_config *config;
334 git_repository *repo;
335 const char *str;
336
337 /* Create an empty repo to clone from */
338 cl_set_cleanup(&cleanup_repository, "./test1");
339 cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
340 cl_set_cleanup(&cleanup_repository, "./repowithunborn");
341 cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
342
343 cl_git_pass(git_repository_config_snapshot(&config, repo));
344
345 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
346 cl_assert_equal_s("origin", str);
347 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
348 cl_assert_equal_s("refs/heads/master", str);
349
350 git_config_free(config);
351 git_repository_free(repo);
352 cl_fixture_cleanup("./repowithunborn");
353 }