]> git.proxmox.com Git - libgit2.git/blob - tests/clone/nonetwork.c
Merge pull request #2653 from ethomson/contributing
[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_CREATE;
26 g_options.remote_callbacks = dummy_callbacks;
27 cl_git_pass(git_signature_now(&g_options.signature, "Me", "foo@example.com"));
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 git_signature_free(g_options.signature);
48 cl_fixture_cleanup("./foo");
49 }
50
51 void test_clone_nonetwork__bad_urls(void)
52 {
53 /* Clone should clean up the mess if the URL isn't a git repository */
54 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
55 cl_assert(!git_path_exists("./foo"));
56 g_options.bare = true;
57 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
58 cl_assert(!git_path_exists("./foo"));
59
60 cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options));
61 cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options));
62 cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
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));
66 }
67
68 void test_clone_nonetwork__do_not_clean_existing_directory(void)
69 {
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));
74 cl_assert(git_path_is_empty_dir("./foo"));
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));
79 cl_assert(git_path_is_empty_dir("./foo"));
80 }
81
82 void test_clone_nonetwork__local(void)
83 {
84 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
85 }
86
87 void test_clone_nonetwork__local_absolute_path(void)
88 {
89 const char *local_src;
90 local_src = cl_fixture("testrepo.git");
91 cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
92 }
93
94 void test_clone_nonetwork__local_bare(void)
95 {
96 g_options.bare = true;
97 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
98 }
99
100 void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
101 {
102 cl_git_mkfile("./foo", "Bar!");
103 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
104 }
105
106 void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
107 {
108 p_mkdir("./foo", GIT_DIR_MODE);
109 cl_git_mkfile("./foo/bar", "Baz!");
110 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
111 }
112
113 int 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
126 void test_clone_nonetwork__custom_origin_name(void)
127 {
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));
130
131 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin"));
132 }
133
134 void 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);
138 cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
139 }
140
141 void test_clone_nonetwork__cope_with_already_existing_directory(void)
142 {
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
147 void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
148 {
149 git_buf path = GIT_BUF_INIT;
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
160 void 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
165 cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
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");
169
170 cl_assert(git_path_exists("foo/readme.txt"));
171 }
172
173 static 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
180 void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
181 {
182 g_options.checkout_branch = "test";
183
184 g_options.remote_callbacks.transfer_progress =
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
195 static 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
210 void 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"));
224 }
225
226 void test_clone_nonetwork__can_detached_head(void)
227 {
228 git_object *obj;
229 git_repository *cloned;
230 git_reference *cloned_head;
231 git_reflog *log;
232 const git_reflog_entry *entry;
233
234 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
235
236 cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
237 cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj), NULL, NULL));
238
239 cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
240
241 cl_assert(git_repository_head_detached(cloned));
242
243 cl_git_pass(git_repository_head(&cloned_head, cloned));
244 cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
245
246 cl_git_pass(git_reflog_read(&log, cloned, "HEAD"));
247 entry = git_reflog_entry_byindex(log, 0);
248 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
249
250 git_object_free(obj);
251 git_reference_free(cloned_head);
252 git_reflog_free(log);
253 git_repository_free(cloned);
254
255 cl_fixture_cleanup("./foo1");
256 }
257
258 static void assert_correct_reflog(const char *name)
259 {
260 git_reflog *log;
261 const git_reflog_entry *entry;
262 char expected_log_message[128] = {0};
263
264 sprintf(expected_log_message, "clone: from %s", cl_git_fixture_url("testrepo.git"));
265
266 cl_git_pass(git_reflog_read(&log, g_repo, name));
267 cl_assert_equal_i(1, git_reflog_entrycount(log));
268 entry = git_reflog_entry_byindex(log, 0);
269 cl_assert_equal_s(expected_log_message, git_reflog_entry_message(entry));
270 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
271
272 git_reflog_free(log);
273 }
274
275 void test_clone_nonetwork__clone_updates_reflog_properly(void)
276 {
277 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
278 assert_correct_reflog("HEAD");
279 assert_correct_reflog("refs/heads/master");
280 }
281
282 static void cleanup_repository(void *path)
283 {
284 if (g_repo) {
285 git_repository_free(g_repo);
286 g_repo = NULL;
287 }
288
289 cl_fixture_cleanup((const char *)path);
290 }
291
292 void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
293 {
294 git_config *config;
295 git_repository *repo;
296 const char *str;
297
298 /* Create an empty repo to clone from */
299 cl_set_cleanup(&cleanup_repository, "./test1");
300 cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
301 cl_set_cleanup(&cleanup_repository, "./repowithunborn");
302 cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
303
304 cl_git_pass(git_repository_config(&config, repo));
305
306 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
307 cl_assert_equal_s("origin", str);
308 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
309 cl_assert_equal_s("refs/heads/master", str);
310
311 git_config_free(config);
312 git_repository_free(repo);
313 cl_fixture_cleanup("./repowithunborn");
314 }