]>
Commit | Line | Data |
---|---|---|
764df57e BS |
1 | #include "clar_libgit2.h" |
2 | ||
3 | #include "git2/clone.h" | |
4330ab26 | 4 | #include "remote.h" |
114f5a6c RB |
5 | #include "fileops.h" |
6 | #include "repository.h" | |
764df57e | 7 | |
86a2da6e | 8 | #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" |
822d9dd5 | 9 | |
18b2d560 | 10 | static git_clone_options g_options; |
764df57e | 11 | static git_repository *g_repo; |
b5b28120 SC |
12 | static git_reference* g_ref; |
13 | static git_remote* g_remote; | |
764df57e | 14 | |
65415ea2 | 15 | void test_clone_nonetwork__initialize(void) |
764df57e | 16 | { |
730df6d0 | 17 | git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; |
0e0cf787 | 18 | git_remote_callbacks dummy_callbacks = GIT_REMOTE_CALLBACKS_INIT; |
730df6d0 | 19 | |
1c7eb971 | 20 | g_repo = NULL; |
18b2d560 BS |
21 | |
22 | memset(&g_options, 0, sizeof(git_clone_options)); | |
23 | g_options.version = GIT_CLONE_OPTIONS_VERSION; | |
730df6d0 | 24 | g_options.checkout_opts = dummy_opts; |
8f1066a0 | 25 | g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; |
0e0cf787 | 26 | g_options.remote_callbacks = dummy_callbacks; |
764df57e BS |
27 | } |
28 | ||
24393ea6 | 29 | void test_clone_nonetwork__cleanup(void) |
764df57e | 30 | { |
9094d30b | 31 | if (g_repo) { |
1c7eb971 | 32 | git_repository_free(g_repo); |
9094d30b SC |
33 | g_repo = NULL; |
34 | } | |
35 | ||
b5b28120 SC |
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 | ||
f46769e5 | 46 | cl_fixture_cleanup("./foo"); |
764df57e BS |
47 | } |
48 | ||
ff0ef88c | 49 | void test_clone_nonetwork__bad_urls(void) |
764df57e | 50 | { |
1c7eb971 | 51 | /* Clone should clean up the mess if the URL isn't a git repository */ |
b412d563 | 52 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); |
18b2d560 BS |
53 | cl_assert(!git_path_exists("./foo")); |
54 | g_options.bare = true; | |
b412d563 | 55 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); |
1c7eb971 | 56 | cl_assert(!git_path_exists("./foo")); |
ff0ef88c BS |
57 | |
58 | cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options)); | |
ff0ef88c | 59 | cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options)); |
7be5104d | 60 | cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git", |
887df99f BS |
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)); | |
764df57e BS |
64 | } |
65 | ||
219d3457 RB |
66 | void test_clone_nonetwork__do_not_clean_existing_directory(void) |
67 | { | |
926acbcf JM |
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)); | |
d0849f83 | 72 | cl_assert(git_path_is_empty_dir("./foo")); |
926acbcf JM |
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)); | |
d0849f83 | 77 | cl_assert(git_path_is_empty_dir("./foo")); |
926acbcf JM |
78 | } |
79 | ||
65415ea2 | 80 | void test_clone_nonetwork__local(void) |
764df57e | 81 | { |
b412d563 | 82 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e BS |
83 | } |
84 | ||
8a8820d8 JM |
85 | void test_clone_nonetwork__local_absolute_path(void) |
86 | { | |
6bd09ecc | 87 | const char *local_src; |
6bd09ecc | 88 | local_src = cl_fixture("testrepo.git"); |
b412d563 | 89 | cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options)); |
8a8820d8 JM |
90 | } |
91 | ||
65415ea2 | 92 | void test_clone_nonetwork__local_bare(void) |
ebecf1e7 | 93 | { |
18b2d560 | 94 | g_options.bare = true; |
b412d563 | 95 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 96 | } |
764df57e | 97 | |
65415ea2 | 98 | void test_clone_nonetwork__fail_when_the_target_is_a_file(void) |
ebecf1e7 | 99 | { |
1c7eb971 | 100 | cl_git_mkfile("./foo", "Bar!"); |
b412d563 | 101 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 102 | } |
103 | ||
65415ea2 | 104 | void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) |
ebecf1e7 | 105 | { |
1c7eb971 BS |
106 | p_mkdir("./foo", GIT_DIR_MODE); |
107 | cl_git_mkfile("./foo/bar", "Baz!"); | |
b412d563 | 108 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e | 109 | } |
621b50e4 | 110 | |
c833893c CMN |
111 | void test_clone_nonetwork__custom_origin_name(void) |
112 | { | |
113 | g_options.remote_name = "my_origin"; | |
114 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
115 | ||
116 | cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin")); | |
117 | } | |
118 | ||
fdc7e5e3 CMN |
119 | void test_clone_nonetwork__defaults(void) |
120 | { | |
121 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL)); | |
122 | cl_assert(g_repo); | |
123 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); | |
124 | } | |
c833893c | 125 | |
922dd978 BS |
126 | void test_clone_nonetwork__cope_with_already_existing_directory(void) |
127 | { | |
922dd978 BS |
128 | p_mkdir("./foo", GIT_DIR_MODE); |
129 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
130 | } | |
131 | ||
132 | void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void) | |
133 | { | |
134 | git_buf path = GIT_BUF_INIT; | |
922dd978 BS |
135 | |
136 | g_options.checkout_opts.checkout_strategy = 0; | |
137 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
138 | ||
139 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); | |
140 | cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path))); | |
141 | ||
142 | git_buf_free(&path); | |
143 | } | |
144 | ||
f1d4a35e SC |
145 | void test_clone_nonetwork__can_checkout_given_branch(void) |
146 | { | |
147 | g_options.checkout_branch = "test"; | |
148 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
149 | ||
605da51a | 150 | cl_assert_equal_i(0, git_repository_head_unborn(g_repo)); |
f1d4a35e SC |
151 | |
152 | cl_git_pass(git_repository_head(&g_ref, g_repo)); | |
153 | cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test"); | |
8f1066a0 RB |
154 | |
155 | cl_assert(git_path_exists("foo/readme.txt")); | |
156 | } | |
157 | ||
158 | static int clone_cancel_fetch_transfer_progress_cb( | |
159 | const git_transfer_progress *stats, void *data) | |
160 | { | |
161 | GIT_UNUSED(stats); GIT_UNUSED(data); | |
162 | return -54321; | |
163 | } | |
164 | ||
165 | void test_clone_nonetwork__can_cancel_clone_in_fetch(void) | |
166 | { | |
167 | g_options.checkout_branch = "test"; | |
168 | ||
169 | g_options.remote_callbacks.transfer_progress = | |
170 | clone_cancel_fetch_transfer_progress_cb; | |
171 | ||
172 | cl_git_fail_with(git_clone( | |
173 | &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options), | |
174 | -54321); | |
175 | ||
176 | cl_assert(!g_repo); | |
177 | cl_assert(!git_path_exists("foo/readme.txt")); | |
178 | } | |
179 | ||
180 | static int clone_cancel_checkout_cb( | |
181 | git_checkout_notify_t why, | |
182 | const char *path, | |
183 | const git_diff_file *b, | |
184 | const git_diff_file *t, | |
185 | const git_diff_file *w, | |
186 | void *payload) | |
187 | { | |
188 | const char *at_file = payload; | |
189 | GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w); | |
190 | if (!strcmp(path, at_file)) | |
191 | return -12345; | |
192 | return 0; | |
193 | } | |
194 | ||
195 | void test_clone_nonetwork__can_cancel_clone_in_checkout(void) | |
196 | { | |
197 | g_options.checkout_branch = "test"; | |
198 | ||
199 | g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED; | |
200 | g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb; | |
201 | g_options.checkout_opts.notify_payload = "readme.txt"; | |
202 | ||
203 | cl_git_fail_with(git_clone( | |
204 | &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options), | |
205 | -12345); | |
206 | ||
207 | cl_assert(!g_repo); | |
208 | cl_assert(!git_path_exists("foo/readme.txt")); | |
f1d4a35e SC |
209 | } |
210 | ||
aa928de0 FL |
211 | void test_clone_nonetwork__can_detached_head(void) |
212 | { | |
2ebc3c66 | 213 | git_object *obj; |
aa928de0 FL |
214 | git_repository *cloned; |
215 | git_reference *cloned_head; | |
216 | ||
217 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
218 | ||
2ebc3c66 | 219 | cl_git_pass(git_revparse_single(&obj, g_repo, "master~1")); |
94f263f5 | 220 | cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj), NULL, NULL)); |
aa928de0 FL |
221 | |
222 | cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); | |
223 | ||
224 | cl_assert(git_repository_head_detached(cloned)); | |
225 | ||
226 | cl_git_pass(git_repository_head(&cloned_head, cloned)); | |
2ebc3c66 | 227 | cl_assert(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head))); |
aa928de0 | 228 | |
2ebc3c66 | 229 | git_object_free(obj); |
aa928de0 FL |
230 | git_reference_free(cloned_head); |
231 | git_repository_free(cloned); | |
232 | ||
233 | cl_fixture_cleanup("./foo1"); | |
234 | } |