]> git.proxmox.com Git - libgit2.git/blame - tests-clar/clone/nonetwork.c
Prevent segfault with a badly-formed URL
[libgit2.git] / tests-clar / clone / nonetwork.c
CommitLineData
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 10static git_clone_options g_options;
764df57e 11static git_repository *g_repo;
b5b28120
SC
12static git_reference* g_ref;
13static git_remote* g_remote;
764df57e 14
65415ea2 15void 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
BS
24 g_options.checkout_opts = dummy_opts;
25 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
0e0cf787 26 g_options.remote_callbacks = dummy_callbacks;
764df57e
BS
27}
28
24393ea6 29void 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 49void 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
BS
60 cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
61 "./bar", &g_options));
764df57e
BS
62}
63
219d3457
RB
64void test_clone_nonetwork__do_not_clean_existing_directory(void)
65{
926acbcf
JM
66 /* Clone should not remove the directory if it already exists, but
67 * Should clean up entries it creates. */
68 p_mkdir("./foo", GIT_DIR_MODE);
69 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
d0849f83 70 cl_assert(git_path_is_empty_dir("./foo"));
926acbcf
JM
71
72 /* Try again with a bare repository. */
73 g_options.bare = true;
74 cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
d0849f83 75 cl_assert(git_path_is_empty_dir("./foo"));
926acbcf
JM
76}
77
65415ea2 78void test_clone_nonetwork__local(void)
764df57e 79{
b412d563 80 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
764df57e
BS
81}
82
8a8820d8
JM
83void test_clone_nonetwork__local_absolute_path(void)
84{
6bd09ecc 85 const char *local_src;
6bd09ecc 86 local_src = cl_fixture("testrepo.git");
b412d563 87 cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
8a8820d8
JM
88}
89
65415ea2 90void test_clone_nonetwork__local_bare(void)
ebecf1e7 91{
18b2d560 92 g_options.bare = true;
b412d563 93 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
ebecf1e7 94}
764df57e 95
65415ea2 96void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
ebecf1e7 97{
1c7eb971 98 cl_git_mkfile("./foo", "Bar!");
b412d563 99 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
ebecf1e7 100}
101
65415ea2 102void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
ebecf1e7 103{
1c7eb971
BS
104 p_mkdir("./foo", GIT_DIR_MODE);
105 cl_git_mkfile("./foo/bar", "Baz!");
b412d563 106 cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
764df57e 107}
621b50e4 108
c833893c
CMN
109void test_clone_nonetwork__custom_origin_name(void)
110{
111 g_options.remote_name = "my_origin";
112 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
113
114 cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin"));
115}
116
fdc7e5e3
CMN
117void test_clone_nonetwork__defaults(void)
118{
119 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL));
120 cl_assert(g_repo);
121 cl_git_pass(git_remote_load(&g_remote, g_repo, "origin"));
122}
c833893c 123
922dd978
BS
124void test_clone_nonetwork__cope_with_already_existing_directory(void)
125{
922dd978
BS
126 p_mkdir("./foo", GIT_DIR_MODE);
127 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
128}
129
130void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
131{
132 git_buf path = GIT_BUF_INIT;
922dd978
BS
133
134 g_options.checkout_opts.checkout_strategy = 0;
135 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
136
137 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
138 cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
139
140 git_buf_free(&path);
141}
142
f1d4a35e
SC
143void test_clone_nonetwork__can_checkout_given_branch(void)
144{
145 g_options.checkout_branch = "test";
146 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
147
605da51a 148 cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
f1d4a35e
SC
149
150 cl_git_pass(git_repository_head(&g_ref, g_repo));
151 cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
152}
153
aa928de0
FL
154void test_clone_nonetwork__can_detached_head(void)
155{
2ebc3c66 156 git_object *obj;
aa928de0
FL
157 git_repository *cloned;
158 git_reference *cloned_head;
159
160 cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
161
2ebc3c66
BS
162 cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
163 cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
aa928de0
FL
164
165 cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
166
167 cl_assert(git_repository_head_detached(cloned));
168
169 cl_git_pass(git_repository_head(&cloned_head, cloned));
2ebc3c66 170 cl_assert(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head)));
aa928de0 171
2ebc3c66 172 git_object_free(obj);
aa928de0
FL
173 git_reference_free(cloned_head);
174 git_repository_free(cloned);
175
176 cl_fixture_cleanup("./foo1");
177}