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

3
#include "futils.h"
4
#include "fetchhead.h"
5

6 7 8 9 10 11 12 13 14 15 16 17 18
#include "fetchhead_data.h"

#define DO_LOCAL_TEST 0

static git_repository *g_repo;

void test_fetchhead_nonetwork__initialize(void)
{
	g_repo = NULL;
}

static void cleanup_repository(void *path)
{
19
	if (g_repo) {
20
		git_repository_free(g_repo);
21 22 23
		g_repo = NULL;
	}

24 25 26
	cl_fixture_cleanup((const char *)path);
}

27
static void populate_fetchhead(git_vector *out, git_repository *repo)
28
{
29 30
	git_fetchhead_ref *fetchhead_ref;
	git_oid oid;
31

32
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
33 34
		"49322bb17d3acc9146f98c97d078513228bbf3c0",
		GIT_OID_SHA1));
35
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 1,
36
		"refs/heads/master",
37
		"https://github.com/libgit2/TestGitRepository"));
38
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
39

40
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
41 42
		"0966a434eb1a025db6b71485ab63a3bfbea520b6",
		GIT_OID_SHA1));
43
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
44
		"refs/heads/first-merge",
45
		"https://github.com/libgit2/TestGitRepository"));
46
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
47

48
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
49 50
		"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",
		GIT_OID_SHA1));
51
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
52
		"refs/heads/no-parent",
53
		"https://github.com/libgit2/TestGitRepository"));
54
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
55

56
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
57 58
		"d96c4e80345534eccee5ac7b07fc7603b56124cb",
		GIT_OID_SHA1));
59
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
60
		"refs/tags/annotated_tag",
61
		"https://github.com/libgit2/TestGitRepository"));
62
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
63

64
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
65 66
		"55a1a760df4b86a02094a904dfa511deb5655905",
		GIT_OID_SHA1));
67
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
68
		"refs/tags/blob",
69
		"https://github.com/libgit2/TestGitRepository"));
70
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
71

72
	cl_git_pass(git_oid__fromstr(&oid,
Edward Thomson committed
73 74
		"8f50ba15d49353813cc6e20298002c0d17b0a9ee",
		GIT_OID_SHA1));
75
	cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
76
		"refs/tags/commit_tree",
77
		"https://github.com/libgit2/TestGitRepository"));
78
	cl_git_pass(git_vector_insert(out, fetchhead_ref));
79

80 81 82 83 84 85 86
	cl_git_pass(git_fetchhead_write(repo, out));
}

void test_fetchhead_nonetwork__write(void)
{
	git_vector fetchhead_vector = GIT_VECTOR_INIT;
	git_fetchhead_ref *fetchhead_ref;
87
	git_str fetchhead_buf = GIT_STR_INIT;
88 89 90
	int equals = 0;
	size_t i;

91
	cl_git_pass(git_vector_init(&fetchhead_vector, 6, NULL));
92 93 94 95 96

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	populate_fetchhead(&fetchhead_vector, g_repo);
97 98 99 100

	cl_git_pass(git_futils_readbuffer(&fetchhead_buf,
		"./test1/.git/FETCH_HEAD"));

101
	equals = (strcmp(fetchhead_buf.ptr, FETCH_HEAD_WILDCARD_DATA_LOCAL) == 0);
102

103
	git_str_dispose(&fetchhead_buf);
104

105 106 107 108
	git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) {
		git_fetchhead_ref_free(fetchhead_ref);
	}

109 110 111 112 113
	git_vector_free(&fetchhead_vector);

	cl_assert(equals);
}

114 115 116
typedef struct {
	git_vector *fetchhead_vector;
	size_t idx;
117
} fetchhead_ref_cb_data;
118 119 120 121 122 123 124 125 126 127 128

static int fetchhead_ref_cb(const char *name, const char *url,
	const git_oid *oid, unsigned int is_merge, void *payload)
{
	fetchhead_ref_cb_data *cb_data = payload;
	git_fetchhead_ref *expected;

	cl_assert(payload);

	expected = git_vector_get(cb_data->fetchhead_vector, cb_data->idx);

129
	cl_assert_equal_oid(&expected->oid, oid);
130 131 132
	cl_assert(expected->is_merge == is_merge);

	if (expected->ref_name)
133
		cl_assert_equal_s(expected->ref_name, name);
134 135 136 137
	else
		cl_assert(name == NULL);

	if (expected->remote_url)
138
		cl_assert_equal_s(expected->remote_url, url);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	else
		cl_assert(url == NULL);

	cb_data->idx++;

	return 0;
}

void test_fetchhead_nonetwork__read(void)
{
	git_vector fetchhead_vector = GIT_VECTOR_INIT;
	git_fetchhead_ref *fetchhead_ref;
	fetchhead_ref_cb_data cb_data;
	size_t i;

	memset(&cb_data, 0x0, sizeof(fetchhead_ref_cb_data));

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	populate_fetchhead(&fetchhead_vector, g_repo);

	cb_data.fetchhead_vector = &fetchhead_vector;

	cl_git_pass(git_repository_fetchhead_foreach(g_repo, fetchhead_ref_cb, &cb_data));

	git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) {
		git_fetchhead_ref_free(fetchhead_ref);
	}

	git_vector_free(&fetchhead_vector);
}

static int read_old_style_cb(const char *name, const char *url,
	const git_oid *oid, unsigned int is_merge, void *payload)
{
	git_oid expected;

	GIT_UNUSED(payload);

179
	git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
180 181 182

	cl_assert(name == NULL);
	cl_assert(url == NULL);
183
	cl_assert_equal_oid(&expected, oid);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	cl_assert(is_merge == 1);

	return 0;
}

void test_fetchhead_nonetwork__read_old_style(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\n");

	cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_old_style_cb, NULL));
}

static int read_type_missing(const char *ref_name, const char *remote_url,
	const git_oid *oid, unsigned int is_merge, void *payload)
{
	git_oid expected;

	GIT_UNUSED(payload);

206
	git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
207

208 209
	cl_assert_equal_s("name", ref_name);
	cl_assert_equal_s("remote_url", remote_url);
210
	cl_assert_equal_oid(&expected, oid);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	cl_assert(is_merge == 0);

	return 0;
}

void test_fetchhead_nonetwork__type_missing(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\t'name' of remote_url\n");

	cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_type_missing, NULL));
}

static int read_name_missing(const char *ref_name, const char *remote_url,
	const git_oid *oid, unsigned int is_merge, void *payload)
{
	git_oid expected;

	GIT_UNUSED(payload);

233
	git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
234 235

	cl_assert(ref_name == NULL);
236
	cl_assert_equal_s("remote_url", remote_url);
237
	cl_assert_equal_oid(&expected, oid);
238 239 240 241 242 243 244 245 246 247 248 249 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 296 297 298 299 300 301
	cl_assert(is_merge == 0);

	return 0;
}

void test_fetchhead_nonetwork__name_missing(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tremote_url\n");

	cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_name_missing, NULL));
}

static int read_noop(const char *ref_name, const char *remote_url,
	const git_oid *oid, unsigned int is_merge, void *payload)
{
	GIT_UNUSED(ref_name);
	GIT_UNUSED(remote_url);
	GIT_UNUSED(oid);
	GIT_UNUSED(is_merge);
	GIT_UNUSED(payload);

	return 0;
}

void test_fetchhead_nonetwork__nonexistent(void)
{
	int error;

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_fail((error = git_repository_fetchhead_foreach(g_repo, read_noop, NULL)));
	cl_assert(error == GIT_ENOTFOUND);
}

void test_fetchhead_nonetwork__invalid_unterminated_last_line(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "unterminated");
	cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}

void test_fetchhead_nonetwork__invalid_oid(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "shortoid\n");
	cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}

void test_fetchhead_nonetwork__invalid_for_merge(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tinvalid-merge\t\n");
	cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));

302
	cl_assert(git__prefixcmp(git_error_last()->message, "invalid for-merge") == 0);
303 304 305 306 307 308 309 310 311 312
}

void test_fetchhead_nonetwork__invalid_description(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\n");
	cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));

313
	cl_assert(git__prefixcmp(git_error_last()->message, "invalid description") == 0);
314 315
}

316 317 318 319 320 321 322 323 324 325 326 327
static int assert_master_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
{
	GIT_UNUSED(url);
	GIT_UNUSED(id);
	GIT_UNUSED(data);

	if (!strcmp("refs/heads/master", ref) && !is_merge)
		return -1;

	return 0;
}

328 329 330 331 332 333 334 335 336 337
static int assert_none_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
{
	GIT_UNUSED(ref);
	GIT_UNUSED(url);
	GIT_UNUSED(id);
	GIT_UNUSED(data);

	return is_merge ? -1 : 0;
}

338 339 340 341 342 343 344 345 346 347 348 349
void test_fetchhead_nonetwork__unborn_with_upstream(void)
{
	git_repository *repo;
	git_remote *remote;

	/* 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));

	/* Simulate someone pushing to it by changing to one that has stuff */
350
	cl_git_pass(git_remote_set_url(repo, "origin", cl_fixture("testrepo.git")));
351
	cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
352

353
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
354 355 356 357 358 359 360
	git_remote_free(remote);

	cl_git_pass(git_repository_fetchhead_foreach(repo, assert_master_for_merge, NULL));

	git_repository_free(repo);
	cl_fixture_cleanup("./repowithunborn");
}
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
void test_fetchhead_nonetwork__fetch_into_repo_with_symrefs(void)
{
	git_repository *repo;
	git_remote *remote;
	git_reference *symref;

	repo = cl_git_sandbox_init("empty_standard_repo");

	/*
	 * Testing for a specific constellation where the repository has at
	 * least one symbolic reference in its refdb.
	 */
	cl_git_pass(git_reference_symbolic_create(&symref, repo, "refs/heads/symref", "refs/heads/master", 0, NULL));

	cl_git_pass(git_remote_set_url(repo, "origin", cl_fixture("testrepo.git")));
	cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));

	git_remote_free(remote);
	git_reference_free(symref);
	cl_git_sandbox_cleanup();
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void test_fetchhead_nonetwork__fetch_into_repo_with_invalid_head(void)
{
	git_remote *remote;
	char *strings[] = { "refs/heads/*:refs/remotes/origin/*" };
	git_strarray refspecs = { strings, 1 };

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	/* HEAD pointing to nonexistent branch */
	cl_git_rewritefile("./test1/.git/HEAD", "ref: refs/heads/\n");

	cl_git_pass(git_remote_create_anonymous(&remote, g_repo, cl_fixture("testrepo.git")));
	cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
	cl_git_pass(git_repository_fetchhead_foreach(g_repo, assert_none_for_merge, NULL));

	git_remote_free(remote);
}

404 405 406 407 408 409 410 411
void test_fetchhead_nonetwork__quote_in_branch_name(void)
{
	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_rewritefile("./test1/.git/FETCH_HEAD", FETCH_HEAD_QUOTE_DATA);
	cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}
412 413

static bool found_master;
414 415
static bool found_haacked;
static bool find_master_haacked_called;
416

417
static int find_master_haacked(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload)
418 419 420 421 422
{
	GIT_UNUSED(remote_url);
	GIT_UNUSED(oid);
	GIT_UNUSED(payload);

423
	find_master_haacked_called = true;
424 425 426 427 428

	if (!strcmp("refs/heads/master", ref_name)) {
		cl_assert(is_merge);
		found_master = true;
	}
429 430 431 432
	if (!strcmp("refs/heads/haacked", ref_name)) {
		cl_assert(is_merge);
		found_haacked = true;
	}
433 434 435 436 437 438 439

	return 0;
}

void test_fetchhead_nonetwork__create_when_refpecs_given(void)
{
	git_remote *remote;
440
	git_str path = GIT_STR_INIT;
441 442 443
	char *refspec1 = "refs/heads/master";
	char *refspec2 = "refs/heads/haacked";
	char *refspecs[] = { refspec1, refspec2 };
444
	git_strarray specs = {
445 446
		refspecs,
		2,
447 448 449 450 451
	};

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

452
	cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), "FETCH_HEAD"));
453 454
	cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_fixture("testrepo.git")));

455
	cl_assert(!git_fs_path_exists(path.ptr));
456
	cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
457
	cl_assert(git_fs_path_exists(path.ptr));
458

459 460
	cl_git_pass(git_repository_fetchhead_foreach(g_repo, find_master_haacked, NULL));
	cl_assert(find_master_haacked_called);
461
	cl_assert(found_master);
462
	cl_assert(found_haacked);
463 464

	git_remote_free(remote);
465
	git_str_dispose(&path);
466
}
467 468

static bool count_refs_called;
469 470 471 472 473
struct prefix_count {
	const char *prefix;
	int count;
	int expected;
};
474

475
static int count_refs(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload)
476
{
477 478
	int i;
	struct prefix_count *prefix_counts = (struct prefix_count *) payload;
479 480 481 482 483 484

	GIT_UNUSED(remote_url);
	GIT_UNUSED(oid);
	GIT_UNUSED(is_merge);

	count_refs_called = true;
485 486 487 488 489

	for (i = 0; prefix_counts[i].prefix; i++) {
		if (!git__prefixcmp(ref_name, prefix_counts[i].prefix))
			prefix_counts[i].count++;
	}
490 491 492 493 494 495 496

	return 0;
}

void test_fetchhead_nonetwork__create_with_multiple_refspecs(void)
{
	git_remote *remote;
497
	git_str path = GIT_STR_INIT;
498 499 500 501 502 503 504 505 506 507

	cl_set_cleanup(&cleanup_repository, "./test1");
	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

	cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_fixture("testrepo.git")));
	git_remote_free(remote);
	cl_git_pass(git_remote_add_fetch(g_repo, "origin", "+refs/notes/*:refs/origin/notes/*"));
	/* Pick up the new refspec */
	cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));

508
	cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), "FETCH_HEAD"));
509
	cl_assert(!git_fs_path_exists(path.ptr));
510
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
511
	cl_assert(git_fs_path_exists(path.ptr));
512

513 514 515 516
	{
		int i;
		struct prefix_count prefix_counts[] = {
			{"refs/notes/", 0, 1},
517
			{"refs/heads/", 0, 13},
518 519 520 521 522 523 524 525 526
			{"refs/tags/", 0, 7},
			{NULL, 0, 0},
		};

		cl_git_pass(git_repository_fetchhead_foreach(g_repo, count_refs, &prefix_counts));
		cl_assert(count_refs_called);
		for (i = 0; prefix_counts[i].prefix; i++)
			cl_assert_equal_i(prefix_counts[i].expected, prefix_counts[i].count);
	}
527 528

	git_remote_free(remote);
529
	git_str_dispose(&path);
530
}
531 532 533 534 535 536

void test_fetchhead_nonetwork__credentials_are_stripped(void)
{
	git_fetchhead_ref *ref;
	git_oid oid;

537
	cl_git_pass(git_oid__fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1));
538 539 540 541 542
	cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
		"refs/tags/commit_tree", "http://foo:bar@github.com/libgit2/TestGitRepository"));
	cl_assert_equal_s(ref->remote_url, "http://github.com/libgit2/TestGitRepository");
	git_fetchhead_ref_free(ref);

543
	cl_git_pass(git_oid__fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1));
544 545 546 547 548
	cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
		"refs/tags/commit_tree", "https://foo:bar@github.com/libgit2/TestGitRepository"));
	cl_assert_equal_s(ref->remote_url, "https://github.com/libgit2/TestGitRepository");
	git_fetchhead_ref_free(ref);
}