submodules.c 15.7 KB
Newer Older
Russell Belfer committed
1
#include "clar_libgit2.h"
2
#include "fileops.h"
3
#include "status_helpers.h"
Russell Belfer committed
4
#include "../submodule/submodule_helpers.h"
Russell Belfer committed
5 6 7 8 9 10 11 12 13 14 15

static git_repository *g_repo = NULL;

void test_status_submodules__initialize(void)
{
}

void test_status_submodules__cleanup(void)
{
}

16 17 18 19
void test_status_submodules__api(void)
{
	git_submodule *sm;

20 21
	g_repo = setup_fixture_submodules();

22 23 24 25 26 27
	cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND);

	cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
	cl_assert(sm != NULL);
Russell Belfer committed
28 29
	cl_assert_equal_s("testrepo", git_submodule_name(sm));
	cl_assert_equal_s("testrepo", git_submodule_path(sm));
30
	git_submodule_free(sm);
31 32
}

Russell Belfer committed
33 34 35 36
void test_status_submodules__0(void)
{
	int counts = 0;

37 38
	g_repo = setup_fixture_submodules();

Russell Belfer committed
39 40 41 42 43
	cl_assert(git_path_isdir("submodules/.git"));
	cl_assert(git_path_isdir("submodules/testrepo/.git"));
	cl_assert(git_path_isfile("submodules/.gitmodules"));

	cl_git_pass(
44
		git_status_foreach(g_repo, cb_status__count, &counts)
Russell Belfer committed
45 46
	);

47
	cl_assert_equal_i(6, counts);
Russell Belfer committed
48 49 50 51 52 53 54 55 56 57 58 59
}

static const char *expected_files[] = {
	".gitmodules",
	"added",
	"deleted",
	"ignored",
	"modified",
	"untracked"
};

static unsigned int expected_status[] = {
60
	GIT_STATUS_WT_MODIFIED,
Russell Belfer committed
61 62 63 64 65 66 67
	GIT_STATUS_INDEX_NEW,
	GIT_STATUS_INDEX_DELETED,
	GIT_STATUS_IGNORED,
	GIT_STATUS_WT_MODIFIED,
	GIT_STATUS_WT_NEW
};

68
static int cb_status__match(const char *p, unsigned int s, void *payload)
Russell Belfer committed
69
{
70 71
	status_entry_counts *counts = payload;
	int idx = counts->entry_count++;
Russell Belfer committed
72

73 74 75 76 77 78 79 80 81
	clar__assert_equal(
		counts->file, counts->line,
		"Status path mismatch", 1,
		"%s", counts->expected_paths[idx], p);

	clar__assert_equal(
		counts->file, counts->line,
		"Status code mismatch", 1,
		"%o", counts->expected_statuses[idx], s);
Russell Belfer committed
82 83 84 85 86 87

	return 0;
}

void test_status_submodules__1(void)
{
88
	status_entry_counts counts;
Russell Belfer committed
89

90 91
	g_repo = setup_fixture_submodules();

Russell Belfer committed
92 93 94 95
	cl_assert(git_path_isdir("submodules/.git"));
	cl_assert(git_path_isdir("submodules/testrepo/.git"));
	cl_assert(git_path_isfile("submodules/.gitmodules"));

96
	status_counts_init(counts, expected_files, expected_status);
97

98
	cl_git_pass( git_status_foreach(g_repo, cb_status__match, &counts) );
Russell Belfer committed
99

100
	cl_assert_equal_i(6, counts.entry_count);
Russell Belfer committed
101
}
102 103 104

void test_status_submodules__single_file(void)
{
105
	unsigned int status = 0;
106
	g_repo = setup_fixture_submodules();
107
	cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
108
	cl_assert(!status);
109
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

void test_status_submodules__moved_head(void)
{
	git_submodule *sm;
	git_repository *smrepo;
	git_oid oid;
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *expected_files_with_sub[] = {
		".gitmodules",
		"added",
		"deleted",
		"ignored",
		"modified",
		"testrepo",
		"untracked"
	};
	static unsigned int expected_status_with_sub[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_IGNORED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW
	};

137 138
	g_repo = setup_fixture_submodules();

139 140
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
	cl_git_pass(git_submodule_open(&smrepo, sm));
141
	git_submodule_free(sm);
142 143 144 145

	/* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
	cl_git_pass(
		git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
146
	cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
147 148 149 150 151

	/* first do a normal status, which should now include the submodule */

	opts.flags = GIT_STATUS_OPT_DEFAULTS;

152 153
	status_counts_init(
		counts, expected_files_with_sub, expected_status_with_sub);
154 155 156 157 158 159 160 161
	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* try again with EXCLUDE_SUBMODULES which should skip it */

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;

162
	status_counts_init(counts, expected_files, expected_status);
163 164 165
	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);
166

Edward Thomson committed
167
	git_repository_free(smrepo);
168
}
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

void test_status_submodules__dirty_workdir_only(void)
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *expected_files_with_sub[] = {
		".gitmodules",
		"added",
		"deleted",
		"ignored",
		"modified",
		"testrepo",
		"untracked"
	};
	static unsigned int expected_status_with_sub[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_IGNORED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW
	};

193 194
	g_repo = setup_fixture_submodules();

195 196 197 198 199 200 201
	cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
	cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");

	/* first do a normal status, which should now include the submodule */

	opts.flags = GIT_STATUS_OPT_DEFAULTS;

202 203
	status_counts_init(
		counts, expected_files_with_sub, expected_status_with_sub);
204 205 206 207 208 209 210 211
	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* try again with EXCLUDE_SUBMODULES which should skip it */

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;

212
	status_counts_init(counts, expected_files, expected_status);
213 214 215 216
	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);
}
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

void test_status_submodules__uninitialized(void)
{
	git_repository *cloned_repo;
	git_status_list *statuslist;

	g_repo = cl_git_sandbox_init("submod2");

	cl_git_pass(git_clone(&cloned_repo, "submod2", "submod2-clone", NULL));

	cl_git_pass(git_status_list_new(&statuslist, cloned_repo, NULL));
	cl_assert_equal_i(0, git_status_list_entrycount(statuslist));

	git_status_list_free(statuslist);
	git_repository_free(cloned_repo);
	cl_git_sandbox_cleanup();
}
234 235 236 237 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 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

void test_status_submodules__contained_untracked_repo(void)
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	git_repository *contained;
	static const char *expected_files_not_ignored[] = {
		".gitmodules",
		"added",
		"deleted",
		"modified",
		"untracked"
	};
	static unsigned int expected_status_not_ignored[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW,
	};
	static const char *expected_files_with_untracked[] = {
		".gitmodules",
		"added",
		"deleted",
		"dir/file.md",
		"modified",
		"untracked"
	};
	static const char *expected_files_with_untracked_dir[] = {
		".gitmodules",
		"added",
		"deleted",
		"dir/",
		"modified",
		"untracked"
	};
	static unsigned int expected_status_with_untracked[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_WT_NEW,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW
	};

	g_repo = setup_fixture_submodules();

	/* skip empty directory */

	cl_must_pass(p_mkdir("submodules/dir", 0777));
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;

	status_counts_init(
		counts, expected_files_not_ignored, expected_status_not_ignored);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(5, counts.entry_count);

	/* still skipping because empty == ignored */

	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_not_ignored, expected_status_not_ignored);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(5, counts.entry_count);

	/* find non-ignored contents of directory */

	cl_git_mkfile("submodules/dir/file.md", "hello");
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_with_untracked, expected_status_with_untracked);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);

	/* but skip if all content is ignored */

	cl_git_append2file("submodules/.git/info/exclude", "\n*.md\n\n");
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_not_ignored, expected_status_not_ignored);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(5, counts.entry_count);

	/* same is true if it contains a git link */

	cl_git_mkfile("submodules/dir/.git", "gitlink: ../.git");
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_not_ignored, expected_status_not_ignored);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(5, counts.entry_count);

	/* but if it contains tracked files, it should just show up as a
	 * directory and exclude the files in it
	 */

	cl_git_mkfile("submodules/dir/another_file", "hello");
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_with_untracked_dir,
		expected_status_with_untracked);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);

	/* that applies to a git repo with a .git directory too */

	cl_must_pass(p_unlink("submodules/dir/.git"));
	cl_git_pass(git_repository_init(&contained, "submodules/dir", false));
	git_repository_free(contained);
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	status_counts_init(
		counts, expected_files_with_untracked_dir,
		expected_status_with_untracked);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);

	/* same result even if we don't recurse into subdirectories */

	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;

	status_counts_init(
		counts, expected_files_with_untracked_dir,
		expected_status_with_untracked);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);

	/* and if we remove the untracked file, it goes back to ignored */

	cl_must_pass(p_unlink("submodules/dir/another_file"));

	status_counts_init(
		counts, expected_files_not_ignored, expected_status_not_ignored);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(5, counts.entry_count);
}
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

void test_status_submodules__broken_stuff_that_git_allows(void)
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	git_repository *contained;
	static const char *expected_files_with_broken[] = {
		".gitmodules",
		"added",
		"broken/tracked",
		"deleted",
		"ignored",
		"modified",
		"untracked"
	};
	static unsigned int expected_status_with_broken[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_IGNORED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW,
	};

	g_repo = setup_fixture_submodules();

	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
		GIT_STATUS_OPT_INCLUDE_IGNORED;

	/* make a directory and stick a tracked item into the index */
	{
		git_index *idx;
		cl_must_pass(p_mkdir("submodules/broken", 0777));
		cl_git_mkfile("submodules/broken/tracked", "tracked content");
		cl_git_pass(git_repository_index(&idx, g_repo));
		cl_git_pass(git_index_add_bypath(idx, "broken/tracked"));
		cl_git_pass(git_index_write(idx));
		git_index_free(idx);
	}

	status_counts_init(
		counts, expected_files_with_broken, expected_status_with_broken);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* directory with tracked items that looks a little bit like a repo */

	cl_must_pass(p_mkdir("submodules/broken/.git", 0777));
	cl_must_pass(p_mkdir("submodules/broken/.git/info", 0777));
	cl_git_mkfile("submodules/broken/.git/info/exclude", "# bogus");

	status_counts_init(
		counts, expected_files_with_broken, expected_status_with_broken);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* directory with tracked items that is a repo */

	cl_git_pass(git_futils_rmdir_r(
		"submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
	cl_git_pass(git_repository_init(&contained, "submodules/broken", false));
	git_repository_free(contained);

	status_counts_init(
		counts, expected_files_with_broken, expected_status_with_broken);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* directory with tracked items that claims to be a submodule but is not */

	cl_git_pass(git_futils_rmdir_r(
		"submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
	cl_git_append2file("submodules/.gitmodules",
		"\n[submodule \"broken\"]\n"
		"\tpath = broken\n"
		"\turl = https://github.com/not/used\n\n");

	status_counts_init(
		counts, expected_files_with_broken, expected_status_with_broken);
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
void test_status_submodules__entry_but_dir_tracked(void)
{
	git_repository *repo;
	git_status_list *status;
	git_diff *diff;
	git_index *index;
	git_tree *tree;

	cl_git_pass(git_repository_init(&repo, "mixed-submodule", 0));
	cl_git_mkfile("mixed-submodule/.gitmodules", "[submodule \"sub\"]\n path = sub\n url = ../foo\n");
	cl_git_pass(p_mkdir("mixed-submodule/sub", 0777));
	cl_git_mkfile("mixed-submodule/sub/file", "");

	/* Create the commit with sub/file as a file, and an entry for sub in the modules list */
	{
		git_oid tree_id, commit_id;
		git_signature *sig;
		git_reference *ref;

		cl_git_pass(git_repository_index(&index, repo));
		cl_git_pass(git_index_add_bypath(index, ".gitmodules"));
		cl_git_pass(git_index_add_bypath(index, "sub/file"));
		cl_git_pass(git_index_write(index));
		cl_git_pass(git_index_write_tree(&tree_id, index));
		cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "sloppy@example.com"));
		cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
		cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
506
		cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
		git_reference_free(ref);
		git_signature_free(sig);
	}

	cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, index, NULL));
	cl_assert_equal_i(0, git_diff_num_deltas(diff));
	git_diff_free(diff);

	cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
	cl_assert_equal_i(0, git_diff_num_deltas(diff));
	git_diff_free(diff);

	cl_git_pass(git_status_list_new(&status, repo, NULL));
	cl_assert_equal_i(0, git_status_list_entrycount(status));

	git_status_list_free(status);
	git_index_free(index);
	git_tree_free(tree);
	git_repository_free(repo);
}
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

void test_status_submodules__mixed_case(void)
{
	git_status_list *status;
	git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
	const git_status_entry *s;
	size_t i;

	status_opts.flags =
		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_INCLUDE_IGNORED |
		GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
		GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
		GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
		GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
		GIT_STATUS_OPT_RENAMES_FROM_REWRITES |
		GIT_STATUS_OPT_INCLUDE_UNREADABLE |
		GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED;

    g_repo = setup_fixture_submod3();

	cl_git_pass(git_status_list_new(&status, g_repo, &status_opts));

	for (i = 0; i < git_status_list_entrycount(status); i++) {
		s = git_status_byindex(status, i);

		if (s->head_to_index &&
			strcmp(s->head_to_index->old_file.path, ".gitmodules") == 0)
			continue;

		cl_assert_equal_i(0, s->status);
	}

	git_status_list_free(status);
}