]>
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 BS |
17 | git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; |
18 | ||
1c7eb971 | 19 | g_repo = NULL; |
18b2d560 BS |
20 | |
21 | memset(&g_options, 0, sizeof(git_clone_options)); | |
22 | g_options.version = GIT_CLONE_OPTIONS_VERSION; | |
730df6d0 BS |
23 | g_options.checkout_opts = dummy_opts; |
24 | g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
764df57e BS |
25 | } |
26 | ||
24393ea6 | 27 | void test_clone_nonetwork__cleanup(void) |
764df57e | 28 | { |
9094d30b | 29 | if (g_repo) { |
1c7eb971 | 30 | git_repository_free(g_repo); |
9094d30b SC |
31 | g_repo = NULL; |
32 | } | |
33 | ||
b5b28120 SC |
34 | if (g_ref) { |
35 | git_reference_free(g_ref); | |
36 | g_ref = NULL; | |
37 | } | |
38 | ||
39 | if (g_remote) { | |
40 | git_remote_free(g_remote); | |
41 | g_remote = NULL; | |
42 | } | |
43 | ||
f46769e5 | 44 | cl_fixture_cleanup("./foo"); |
764df57e BS |
45 | } |
46 | ||
65415ea2 | 47 | void test_clone_nonetwork__bad_url(void) |
764df57e | 48 | { |
1c7eb971 | 49 | /* Clone should clean up the mess if the URL isn't a git repository */ |
b412d563 | 50 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); |
18b2d560 BS |
51 | cl_assert(!git_path_exists("./foo")); |
52 | g_options.bare = true; | |
b412d563 | 53 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); |
1c7eb971 | 54 | cl_assert(!git_path_exists("./foo")); |
764df57e BS |
55 | } |
56 | ||
926acbcf JM |
57 | static int dont_call_me(void *state, git_buf *path) |
58 | { | |
59 | GIT_UNUSED(state); | |
60 | GIT_UNUSED(path); | |
61 | return GIT_ERROR; | |
62 | } | |
63 | ||
64 | void test_clone_nonetwork__do_not_clean_existing_directory(void) | |
65 | { | |
66 | git_buf path_buf = GIT_BUF_INIT; | |
67 | ||
68 | git_buf_put(&path_buf, "./foo", 5); | |
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_exists("./foo")); | |
75 | ||
76 | /* Make sure the directory is empty. */ | |
77 | cl_git_pass(git_path_direach(&path_buf, | |
78 | dont_call_me, | |
79 | NULL)); | |
80 | ||
81 | /* Try again with a bare repository. */ | |
82 | g_options.bare = true; | |
83 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); | |
84 | cl_assert(git_path_exists("./foo")); | |
85 | ||
86 | /* Make sure the directory is empty. */ | |
87 | cl_git_pass(git_path_direach(&path_buf, | |
88 | dont_call_me, | |
89 | NULL)); | |
90 | ||
91 | git_buf_free(&path_buf); | |
92 | } | |
93 | ||
65415ea2 | 94 | void test_clone_nonetwork__local(void) |
764df57e | 95 | { |
b412d563 | 96 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e BS |
97 | } |
98 | ||
8a8820d8 JM |
99 | void test_clone_nonetwork__local_absolute_path(void) |
100 | { | |
6bd09ecc | 101 | const char *local_src; |
6bd09ecc | 102 | local_src = cl_fixture("testrepo.git"); |
b412d563 | 103 | cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options)); |
8a8820d8 JM |
104 | } |
105 | ||
65415ea2 | 106 | void test_clone_nonetwork__local_bare(void) |
ebecf1e7 | 107 | { |
18b2d560 | 108 | g_options.bare = true; |
b412d563 | 109 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 110 | } |
764df57e | 111 | |
65415ea2 | 112 | void test_clone_nonetwork__fail_when_the_target_is_a_file(void) |
ebecf1e7 | 113 | { |
1c7eb971 | 114 | cl_git_mkfile("./foo", "Bar!"); |
b412d563 | 115 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 116 | } |
117 | ||
65415ea2 | 118 | void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) |
ebecf1e7 | 119 | { |
1c7eb971 BS |
120 | p_mkdir("./foo", GIT_DIR_MODE); |
121 | cl_git_mkfile("./foo/bar", "Baz!"); | |
b412d563 | 122 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e | 123 | } |
621b50e4 BS |
124 | |
125 | void test_clone_nonetwork__custom_origin_name(void) | |
126 | { | |
621b50e4 BS |
127 | g_options.remote_name = "my_origin"; |
128 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
129 | ||
b5b28120 | 130 | cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin")); |
621b50e4 BS |
131 | } |
132 | ||
133 | void test_clone_nonetwork__custom_push_url(void) | |
134 | { | |
621b50e4 BS |
135 | const char *url = "http://example.com"; |
136 | ||
621b50e4 BS |
137 | g_options.pushurl = url; |
138 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
139 | ||
b5b28120 SC |
140 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
141 | cl_assert_equal_s(url, git_remote_pushurl(g_remote)); | |
621b50e4 BS |
142 | } |
143 | ||
144 | void test_clone_nonetwork__custom_fetch_spec(void) | |
145 | { | |
621b50e4 BS |
146 | const git_refspec *actual_fs; |
147 | const char *spec = "+refs/heads/master:refs/heads/foo"; | |
148 | ||
621b50e4 BS |
149 | g_options.fetch_spec = spec; |
150 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
151 | ||
b5b28120 | 152 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
1ffd0806 | 153 | actual_fs = git_remote_get_refspec(g_remote, 0); |
621b50e4 BS |
154 | cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); |
155 | cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); | |
156 | ||
b5b28120 | 157 | cl_git_pass(git_reference_lookup(&g_ref, g_repo, "refs/heads/foo")); |
764df57e | 158 | } |
621b50e4 BS |
159 | |
160 | void test_clone_nonetwork__custom_push_spec(void) | |
161 | { | |
621b50e4 BS |
162 | const git_refspec *actual_fs; |
163 | const char *spec = "+refs/heads/master:refs/heads/foo"; | |
164 | ||
621b50e4 BS |
165 | g_options.push_spec = spec; |
166 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
167 | ||
b5b28120 | 168 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
1ffd0806 | 169 | actual_fs = git_remote_get_refspec(g_remote, git_remote_refspec_count(g_remote) - 1); |
621b50e4 BS |
170 | cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); |
171 | cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); | |
621b50e4 BS |
172 | } |
173 | ||
174 | void test_clone_nonetwork__custom_autotag(void) | |
175 | { | |
6f748f38 | 176 | git_remote *origin; |
621b50e4 BS |
177 | git_strarray tags = {0}; |
178 | ||
621b50e4 BS |
179 | g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_NONE; |
180 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
181 | ||
182 | cl_git_pass(git_tag_list(&tags, g_repo)); | |
090d5e1f | 183 | cl_assert_equal_sz(0, tags.count); |
b97fabfa | 184 | |
6f748f38 JM |
185 | cl_git_pass(git_remote_load(&origin, g_repo, "origin")); |
186 | cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_NONE, origin->download_tags); | |
187 | ||
188 | git_strarray_free(&tags); | |
24988894 | 189 | git_remote_free(origin); |
6f748f38 JM |
190 | } |
191 | ||
192 | void test_clone_nonetwork__custom_autotag_tags_all(void) | |
193 | { | |
194 | git_strarray tags = {0}; | |
195 | git_remote *origin; | |
196 | ||
197 | g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_ALL; | |
198 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
199 | ||
200 | cl_git_pass(git_remote_load(&origin, g_repo, "origin")); | |
201 | cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_ALL, origin->download_tags); | |
202 | ||
b97fabfa | 203 | git_strarray_free(&tags); |
24988894 | 204 | git_remote_free(origin); |
621b50e4 BS |
205 | } |
206 | ||
922dd978 BS |
207 | void test_clone_nonetwork__cope_with_already_existing_directory(void) |
208 | { | |
922dd978 BS |
209 | p_mkdir("./foo", GIT_DIR_MODE); |
210 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
211 | } | |
212 | ||
213 | void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void) | |
214 | { | |
215 | git_buf path = GIT_BUF_INIT; | |
922dd978 BS |
216 | |
217 | g_options.checkout_opts.checkout_strategy = 0; | |
218 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
219 | ||
220 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); | |
221 | cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path))); | |
222 | ||
223 | git_buf_free(&path); | |
224 | } | |
225 | ||
f1d4a35e SC |
226 | void test_clone_nonetwork__can_checkout_given_branch(void) |
227 | { | |
228 | g_options.checkout_branch = "test"; | |
229 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
230 | ||
231 | cl_assert_equal_i(0, git_repository_head_orphan(g_repo)); | |
232 | ||
233 | cl_git_pass(git_repository_head(&g_ref, g_repo)); | |
234 | cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test"); | |
235 | } | |
236 | ||
aa928de0 FL |
237 | void test_clone_nonetwork__can_detached_head(void) |
238 | { | |
2ebc3c66 | 239 | git_object *obj; |
aa928de0 FL |
240 | git_repository *cloned; |
241 | git_reference *cloned_head; | |
242 | ||
243 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
244 | ||
2ebc3c66 BS |
245 | cl_git_pass(git_revparse_single(&obj, g_repo, "master~1")); |
246 | cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj))); | |
aa928de0 FL |
247 | |
248 | cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); | |
249 | ||
250 | cl_assert(git_repository_head_detached(cloned)); | |
251 | ||
252 | cl_git_pass(git_repository_head(&cloned_head, cloned)); | |
2ebc3c66 | 253 | cl_assert(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head))); |
aa928de0 | 254 | |
2ebc3c66 | 255 | git_object_free(obj); |
aa928de0 FL |
256 | git_reference_free(cloned_head); |
257 | git_repository_free(cloned); | |
258 | ||
259 | cl_fixture_cleanup("./foo1"); | |
260 | } |