]>
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 | ||
65415ea2 | 55 | void test_clone_nonetwork__local(void) |
764df57e | 56 | { |
b412d563 | 57 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e BS |
58 | } |
59 | ||
8a8820d8 JM |
60 | void test_clone_nonetwork__local_absolute_path(void) |
61 | { | |
6bd09ecc | 62 | const char *local_src; |
6bd09ecc | 63 | local_src = cl_fixture("testrepo.git"); |
b412d563 | 64 | cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options)); |
8a8820d8 JM |
65 | } |
66 | ||
65415ea2 | 67 | void test_clone_nonetwork__local_bare(void) |
ebecf1e7 | 68 | { |
18b2d560 | 69 | g_options.bare = true; |
b412d563 | 70 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 71 | } |
764df57e | 72 | |
65415ea2 | 73 | void test_clone_nonetwork__fail_when_the_target_is_a_file(void) |
ebecf1e7 | 74 | { |
1c7eb971 | 75 | cl_git_mkfile("./foo", "Bar!"); |
b412d563 | 76 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
ebecf1e7 | 77 | } |
78 | ||
65415ea2 | 79 | void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) |
ebecf1e7 | 80 | { |
1c7eb971 BS |
81 | p_mkdir("./foo", GIT_DIR_MODE); |
82 | cl_git_mkfile("./foo/bar", "Baz!"); | |
b412d563 | 83 | cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); |
764df57e | 84 | } |
621b50e4 BS |
85 | |
86 | void test_clone_nonetwork__custom_origin_name(void) | |
87 | { | |
621b50e4 BS |
88 | g_options.remote_name = "my_origin"; |
89 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
90 | ||
b5b28120 | 91 | cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin")); |
621b50e4 BS |
92 | } |
93 | ||
94 | void test_clone_nonetwork__custom_push_url(void) | |
95 | { | |
621b50e4 BS |
96 | const char *url = "http://example.com"; |
97 | ||
621b50e4 BS |
98 | g_options.pushurl = url; |
99 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
100 | ||
b5b28120 SC |
101 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
102 | cl_assert_equal_s(url, git_remote_pushurl(g_remote)); | |
621b50e4 BS |
103 | } |
104 | ||
105 | void test_clone_nonetwork__custom_fetch_spec(void) | |
106 | { | |
621b50e4 BS |
107 | const git_refspec *actual_fs; |
108 | const char *spec = "+refs/heads/master:refs/heads/foo"; | |
109 | ||
621b50e4 BS |
110 | g_options.fetch_spec = spec; |
111 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
112 | ||
b5b28120 SC |
113 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
114 | actual_fs = git_remote_fetchspec(g_remote); | |
621b50e4 BS |
115 | cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); |
116 | cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); | |
117 | ||
b5b28120 | 118 | cl_git_pass(git_reference_lookup(&g_ref, g_repo, "refs/heads/foo")); |
764df57e | 119 | } |
621b50e4 BS |
120 | |
121 | void test_clone_nonetwork__custom_push_spec(void) | |
122 | { | |
621b50e4 BS |
123 | const git_refspec *actual_fs; |
124 | const char *spec = "+refs/heads/master:refs/heads/foo"; | |
125 | ||
621b50e4 BS |
126 | g_options.push_spec = spec; |
127 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
128 | ||
b5b28120 SC |
129 | cl_git_pass(git_remote_load(&g_remote, g_repo, "origin")); |
130 | actual_fs = git_remote_pushspec(g_remote); | |
621b50e4 BS |
131 | cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs)); |
132 | cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs)); | |
621b50e4 BS |
133 | } |
134 | ||
135 | void test_clone_nonetwork__custom_autotag(void) | |
136 | { | |
137 | git_strarray tags = {0}; | |
138 | ||
621b50e4 BS |
139 | g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_NONE; |
140 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
141 | ||
142 | cl_git_pass(git_tag_list(&tags, g_repo)); | |
090d5e1f | 143 | cl_assert_equal_sz(0, tags.count); |
b97fabfa | 144 | |
145 | git_strarray_free(&tags); | |
621b50e4 BS |
146 | } |
147 | ||
922dd978 BS |
148 | void test_clone_nonetwork__cope_with_already_existing_directory(void) |
149 | { | |
922dd978 BS |
150 | p_mkdir("./foo", GIT_DIR_MODE); |
151 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
152 | } | |
153 | ||
154 | void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void) | |
155 | { | |
156 | git_buf path = GIT_BUF_INIT; | |
922dd978 BS |
157 | |
158 | g_options.checkout_opts.checkout_strategy = 0; | |
159 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
160 | ||
161 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); | |
162 | cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path))); | |
163 | ||
164 | git_buf_free(&path); | |
165 | } | |
166 | ||
f1d4a35e SC |
167 | void test_clone_nonetwork__can_checkout_given_branch(void) |
168 | { | |
169 | g_options.checkout_branch = "test"; | |
170 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
171 | ||
172 | cl_assert_equal_i(0, git_repository_head_orphan(g_repo)); | |
173 | ||
174 | cl_git_pass(git_repository_head(&g_ref, g_repo)); | |
175 | cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test"); | |
176 | } | |
177 | ||
aa928de0 FL |
178 | void test_clone_nonetwork__can_detached_head(void) |
179 | { | |
180 | git_object *commit; | |
181 | git_repository *cloned; | |
182 | git_reference *cloned_head; | |
183 | ||
184 | cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); | |
185 | ||
186 | cl_git_pass(git_revparse_single(&commit, g_repo, "master~1")); | |
187 | cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(commit))); | |
188 | ||
189 | cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); | |
190 | ||
191 | cl_assert(git_repository_head_detached(cloned)); | |
192 | ||
193 | cl_git_pass(git_repository_head(&cloned_head, cloned)); | |
194 | cl_assert(!git_oid_cmp(git_object_id(commit), git_reference_target(cloned_head))); | |
195 | ||
196 | git_commit_free((git_commit*)commit); | |
197 | git_reference_free(cloned_head); | |
198 | git_repository_free(cloned); | |
199 | ||
200 | cl_fixture_cleanup("./foo1"); | |
201 | } |