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

#include "git2/clone.h"
4 5
#include "git2/sys/commit.h"
#include "../submodule/submodule_helpers.h"
6
#include "remote.h"
7 8
#include "fileops.h"
#include "repository.h"
9

10
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
11

12
static git_clone_options g_options;
13
static git_repository *g_repo;
14 15
static git_reference* g_ref;
static git_remote* g_remote;
16

17
void test_clone_nonetwork__initialize(void)
18
{
19
	git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
20
	git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
21

Ben Straub committed
22
	g_repo = NULL;
23 24 25

	memset(&g_options, 0, sizeof(git_clone_options));
	g_options.version = GIT_CLONE_OPTIONS_VERSION;
26
	g_options.checkout_opts = dummy_opts;
27
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
28
	g_options.fetch_opts = dummy_fetch;
29 30
}

31 32
void test_clone_nonetwork__cleanup(void)
{
33
	if (g_repo) {
Ben Straub committed
34
		git_repository_free(g_repo);
35 36 37
		g_repo = NULL;
	}

38 39 40 41 42 43 44 45 46 47
	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
48
	cl_fixture_cleanup("./foo");
49 50
}

Ben Straub committed
51
void test_clone_nonetwork__bad_urls(void)
52
{
Ben Straub committed
53
	/* Clone should clean up the mess if the URL isn't a git repository */
54
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
55 56
	cl_assert(!git_path_exists("./foo"));
	g_options.bare = true;
57
	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
Ben Straub committed
58
	cl_assert(!git_path_exists("./foo"));
Ben Straub committed
59 60 61

	cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options));
	cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options));
62
	cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
Ben Straub committed
63 64 65
				"./foo", &g_options));
	cl_git_fail(git_clone(&g_repo, "arrbee:my/bad:password@github.com:1111/strange:words.git",
				"./foo", &g_options));
66 67
}

68 69
void test_clone_nonetwork__do_not_clean_existing_directory(void)
{
70 71 72 73
	/* 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));
74
	cl_assert(git_path_is_empty_dir("./foo"));
75 76 77 78

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

82
void test_clone_nonetwork__local(void)
83
{
84
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
85 86
}

87 88
void test_clone_nonetwork__local_absolute_path(void)
{
nulltoken committed
89 90
	const char *local_src;
	local_src = cl_fixture("testrepo.git");
91
	cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
92 93
}

94
void test_clone_nonetwork__local_bare(void)
nulltoken committed
95
{
96
	g_options.bare = true;
97
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
nulltoken committed
98
}
99

100
void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
nulltoken committed
101
{
Ben Straub committed
102
	cl_git_mkfile("./foo", "Bar!");
103
	cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
nulltoken committed
104 105
}

106
void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
nulltoken committed
107
{
Ben Straub committed
108 109
	p_mkdir("./foo", GIT_DIR_MODE);
	cl_git_mkfile("./foo/bar", "Baz!");
110
	cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
111
}
Ben Straub committed
112

113 114 115 116 117 118 119 120 121 122 123 124 125
int custom_origin_name_remote_create(
	git_remote **out,
	git_repository *repo,
	const char *name,
	const char *url,
	void *payload)
{
	GIT_UNUSED(name);
	GIT_UNUSED(payload);

	return git_remote_create(out, repo, "my_origin", url);
}

126 127
void test_clone_nonetwork__custom_origin_name(void)
{
128 129
	g_options.remote_cb = custom_origin_name_remote_create;
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
130

131
	cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin"));
132 133
}

134 135 136 137
void test_clone_nonetwork__defaults(void)
{
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL));
	cl_assert(g_repo);
138
	cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
139
}
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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);
}

160 161 162 163 164
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));

165
	cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
166 167 168

	cl_git_pass(git_repository_head(&g_ref, g_repo));
	cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

	cl_assert(git_path_exists("foo/readme.txt"));
}

static int clone_cancel_fetch_transfer_progress_cb(
	const git_transfer_progress *stats, void *data)
{
	GIT_UNUSED(stats); GIT_UNUSED(data);
	return -54321;
}

void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
{
	g_options.checkout_branch = "test";

184
	g_options.fetch_opts.callbacks.transfer_progress =
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		clone_cancel_fetch_transfer_progress_cb;

	cl_git_fail_with(git_clone(
		&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
		-54321);

	cl_assert(!g_repo);
	cl_assert(!git_path_exists("foo/readme.txt"));
}

static int clone_cancel_checkout_cb(
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *b,
	const git_diff_file *t,
	const git_diff_file *w,
	void *payload)
{
	const char *at_file = payload;
	GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
	if (!strcmp(path, at_file))
		return -12345;
	return 0;
}

void test_clone_nonetwork__can_cancel_clone_in_checkout(void)
{
	g_options.checkout_branch = "test";

	g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
	g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb;
	g_options.checkout_opts.notify_payload = "readme.txt";

	cl_git_fail_with(git_clone(
		&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
		-12345);

	cl_assert(!g_repo);
	cl_assert(!git_path_exists("foo/readme.txt"));
224 225
}

226 227
void test_clone_nonetwork__can_detached_head(void)
{
228
	git_object *obj;
229 230 231 232 233
	git_repository *cloned;
	git_reference *cloned_head;

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

234
	cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
235
	cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
236 237 238 239 240 241

	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));
242
	cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
243

244
	git_object_free(obj);
245 246 247 248 249
	git_reference_free(cloned_head);
	git_repository_free(cloned);

	cl_fixture_cleanup("./foo1");
}
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
void test_clone_nonetwork__clone_tag_to_tree(void)
{
	git_repository *stage;
	git_index_entry entry;
	git_index *index;
	git_odb *odb;
	git_oid tree_id;
	git_tree *tree;
	git_reference *tag;
	git_tree_entry *tentry;
	const char *file_path = "some/deep/path.txt";
	const char *file_content = "some content\n";
	const char *tag_name = "refs/tags/tree-tag";

	stage = cl_git_sandbox_init("testrepo.git");
	cl_git_pass(git_repository_odb(&odb, stage));
	cl_git_pass(git_index_new(&index));

	memset(&entry, 0, sizeof(git_index_entry));
	entry.path = file_path;
	entry.mode = GIT_FILEMODE_BLOB;
	cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJ_BLOB));

	cl_git_pass(git_index_add(index, &entry));
	cl_git_pass(git_index_write_tree_to(&tree_id, index, stage));
	cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL));
	git_reference_free(tag);
	git_odb_free(odb);
	git_index_free(index);

	g_options.local = GIT_CLONE_NO_LOCAL;
	cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options));
	git_repository_free(stage);

	cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name));
	cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag)));
	git_reference_free(tag);

	cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path));
	git_tree_entry_free(tentry);
	git_tree_free(tree);

	cl_fixture_cleanup("testrepo.git");
}

296 297 298 299
static void assert_correct_reflog(const char *name)
{
	git_reflog *log;
	const git_reflog_entry *entry;
300
	git_buf expected_message = GIT_BUF_INIT;
301

302 303
	git_buf_printf(&expected_message,
		"clone: from %s", cl_git_fixture_url("testrepo.git"));
304 305 306 307

	cl_git_pass(git_reflog_read(&log, g_repo, name));
	cl_assert_equal_i(1, git_reflog_entrycount(log));
	entry = git_reflog_entry_byindex(log, 0);
308
	cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry));
309 310

	git_reflog_free(log);
311 312

	git_buf_free(&expected_message);
313 314 315 316 317 318 319 320 321
}

void test_clone_nonetwork__clone_updates_reflog_properly(void)
{
	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
	assert_correct_reflog("HEAD");
	assert_correct_reflog("refs/heads/master");
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
static void cleanup_repository(void *path)
{
	if (g_repo) {
		git_repository_free(g_repo);
		g_repo = NULL;
	}

	cl_fixture_cleanup((const char *)path);
}

void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
{
	git_config *config;
	git_repository *repo;
	const char *str;

	/* Create an empty repo to clone from */
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
	cl_set_cleanup(&cleanup_repository, "./repowithunborn");
	cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));

344
	cl_git_pass(git_repository_config_snapshot(&config, repo));
345 346 347 348 349 350 351 352 353 354

	cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
	cl_assert_equal_s("origin", str);
	cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
	cl_assert_equal_s("refs/heads/master", str);

	git_config_free(config);
	git_repository_free(repo);
	cl_fixture_cleanup("./repowithunborn");
}
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

static int just_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
{
	GIT_UNUSED(url); GIT_UNUSED(payload);

	return git_remote_lookup(out, repo, name);
}

static int just_return_repo(git_repository **out, const char *path, int bare, void *payload)
{
	git_submodule *sm = payload;

	GIT_UNUSED(path); GIT_UNUSED(bare);

	return git_submodule_open(out, sm);
}

void test_clone_nonetwork__clone_submodule(void)
{
	git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
	git_index *index;
	git_oid tree_id, commit_id;
	git_submodule *sm;
	git_signature *sig;
	git_repository *sm_repo;

	cl_git_pass(git_repository_init(&g_repo, "willaddsubmodule", false));


	/* Create the submodule structure, clone into it and finalize */
	cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo", true));

	clone_opts.repository_cb = just_return_repo;
	clone_opts.repository_cb_payload = sm;
	clone_opts.remote_cb = just_return_origin;
	clone_opts.remote_cb_payload = sm;
	cl_git_pass(git_clone(&sm_repo, cl_fixture("testrepo.git"), "testrepo", &clone_opts));
	cl_git_pass(git_submodule_add_finalize(sm));
	git_repository_free(sm_repo);
	git_submodule_free(sm);

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_write_tree(&tree_id, index));
	git_index_free(index);

	cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local"));
	cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n",
					       &tree_id, 0, NULL));

	git_signature_free(sig);

	assert_submodule_exists(g_repo, "testrepo");
}