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