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