nonetwork.c 7.33 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"

#include "git2/clone.h"
#include "repository.h"
5
#include "remote.h"
6

7
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
8

9
static git_clone_options g_options;
10
static git_repository *g_repo;
11 12
static git_reference* g_ref;
static git_remote* g_remote;
13

14
void test_clone_nonetwork__initialize(void)
15
{
16 17
	git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT;

Ben Straub committed
18
	g_repo = NULL;
19 20 21

	memset(&g_options, 0, sizeof(git_clone_options));
	g_options.version = GIT_CLONE_OPTIONS_VERSION;
22 23
	g_options.checkout_opts = dummy_opts;
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
24 25
}

26 27
void test_clone_nonetwork__cleanup(void)
{
28
	if (g_repo) {
Ben Straub committed
29
		git_repository_free(g_repo);
30 31 32
		g_repo = NULL;
	}

33 34 35 36 37 38 39 40 41 42
	if (g_ref) {
		git_reference_free(g_ref);
		g_ref = NULL;
	}

	if (g_remote) {
		git_remote_free(g_remote);
		g_remote = NULL;
	}

Vicent Marti committed
43
	cl_fixture_cleanup("./foo");
44 45
}

46
void test_clone_nonetwork__bad_url(void)
47
{
Ben Straub committed
48
	/* Clone should clean up the mess if the URL isn't a git repository */
49
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
50 51
	cl_assert(!git_path_exists("./foo"));
	g_options.bare = true;
52
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
Ben Straub committed
53
	cl_assert(!git_path_exists("./foo"));
54 55
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static int dont_call_me(void *state, git_buf *path)
{
	GIT_UNUSED(state);
	GIT_UNUSED(path);
	return GIT_ERROR;
}

void test_clone_nonetwork__do_not_clean_existing_directory(void)
{
	git_buf path_buf = GIT_BUF_INIT;

	git_buf_put(&path_buf, "./foo", 5);

	/* Clone should not remove the directory if it already exists, but
	 * Should clean up entries it creates. */
	p_mkdir("./foo", GIT_DIR_MODE);
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
	cl_assert(git_path_exists("./foo"));

	/* Make sure the directory is empty. */
	cl_git_pass(git_path_direach(&path_buf,
		dont_call_me,
		NULL));

	/* Try again with a bare repository. */
	g_options.bare = true;
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
	cl_assert(git_path_exists("./foo"));

	/* Make sure the directory is empty. */
	cl_git_pass(git_path_direach(&path_buf,
		dont_call_me,
		NULL));

	git_buf_free(&path_buf);
}

93
void test_clone_nonetwork__local(void)
94
{
95
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
96 97
}

98 99
void test_clone_nonetwork__local_absolute_path(void)
{
nulltoken committed
100 101
	const char *local_src;
	local_src = cl_fixture("testrepo.git");
102
	cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
103 104
}

105
void test_clone_nonetwork__local_bare(void)
nulltoken committed
106
{
107
	g_options.bare = true;
108
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
nulltoken committed
109
}
110

111
void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
nulltoken committed
112
{
Ben Straub committed
113
	cl_git_mkfile("./foo", "Bar!");
114
	cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
nulltoken committed
115 116
}

117
void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
nulltoken committed
118
{
Ben Straub committed
119 120
	p_mkdir("./foo", GIT_DIR_MODE);
	cl_git_mkfile("./foo/bar", "Baz!");
121
	cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
122
}
Ben Straub committed
123 124 125 126 127 128

void test_clone_nonetwork__custom_origin_name(void)
{
	g_options.remote_name = "my_origin";
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

129
	cl_git_pass(git_remote_load(&g_remote, g_repo, "my_origin"));
Ben Straub committed
130 131 132 133 134 135 136 137 138
}

void test_clone_nonetwork__custom_push_url(void)
{
	const char *url = "http://example.com";

	g_options.pushurl = url;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

139 140
	cl_git_pass(git_remote_load(&g_remote, g_repo, "origin"));
	cl_assert_equal_s(url, git_remote_pushurl(g_remote));
Ben Straub committed
141 142 143 144 145 146 147 148 149 150
}

void test_clone_nonetwork__custom_fetch_spec(void)
{
	const git_refspec *actual_fs;
	const char *spec = "+refs/heads/master:refs/heads/foo";

	g_options.fetch_spec = spec;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

151
	cl_git_pass(git_remote_load(&g_remote, g_repo, "origin"));
152
	actual_fs = git_remote_get_refspec(g_remote, 0);
Ben Straub committed
153 154 155
	cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs));
	cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs));

156
	cl_git_pass(git_reference_lookup(&g_ref, g_repo, "refs/heads/foo"));
157
}
Ben Straub committed
158 159 160 161 162 163 164 165 166

void test_clone_nonetwork__custom_push_spec(void)
{
	const git_refspec *actual_fs;
	const char *spec = "+refs/heads/master:refs/heads/foo";

	g_options.push_spec = spec;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

167
	cl_git_pass(git_remote_load(&g_remote, g_repo, "origin"));
168
	actual_fs = git_remote_get_refspec(g_remote, git_remote_refspec_count(g_remote) - 1);
Ben Straub committed
169 170 171 172 173 174
	cl_assert_equal_s("refs/heads/master", git_refspec_src(actual_fs));
	cl_assert_equal_s("refs/heads/foo", git_refspec_dst(actual_fs));
}

void test_clone_nonetwork__custom_autotag(void)
{
175
	git_remote *origin;
Ben Straub committed
176 177 178 179 180 181
	git_strarray tags = {0};

	g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_NONE;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

	cl_git_pass(git_tag_list(&tags, g_repo));
182
	cl_assert_equal_sz(0, tags.count);
nulltoken committed
183

184 185 186 187
	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_NONE, origin->download_tags);

	git_strarray_free(&tags);
nulltoken committed
188
	git_remote_free(origin);
189 190 191 192 193 194 195 196 197 198 199 200 201
}

void test_clone_nonetwork__custom_autotag_tags_all(void)
{
	git_strarray tags = {0};
	git_remote *origin;

	g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_ALL, origin->download_tags);

nulltoken committed
202
	git_strarray_free(&tags);
nulltoken committed
203
	git_remote_free(origin);
Ben Straub committed
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
void test_clone_nonetwork__cope_with_already_existing_directory(void)
{
	p_mkdir("./foo", GIT_DIR_MODE);
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}

void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
{
	git_buf path = GIT_BUF_INIT;

	g_options.checkout_opts.checkout_strategy = 0;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));

	git_buf_free(&path);
}

225 226 227 228 229 230 231 232 233 234 235
void test_clone_nonetwork__can_checkout_given_branch(void)
{
	g_options.checkout_branch = "test";
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

	cl_assert_equal_i(0, git_repository_head_orphan(g_repo));

	cl_git_pass(git_repository_head(&g_ref, g_repo));
	cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
}

236 237
void test_clone_nonetwork__can_detached_head(void)
{
238
	git_object *obj;
239 240 241 242 243
	git_repository *cloned;
	git_reference *cloned_head;

	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));

244 245
	cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
	cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
246 247 248 249 250 251

	cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));

	cl_assert(git_repository_head_detached(cloned));

	cl_git_pass(git_repository_head(&cloned_head, cloned));
252
	cl_assert(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head)));
253

254
	git_object_free(obj);
255 256 257 258 259
	git_reference_free(cloned_head);
	git_repository_free(cloned);

	cl_fixture_cleanup("./foo1");
}