worktree.c 17.2 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "worktree_helpers.h"
3
#include "submodule/submodule_helpers.h"
4

5
#include "checkout.h"
6
#include "repository.h"
7
#include "worktree.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

#define COMMON_REPO "testrepo"
#define WORKTREE_REPO "testrepo-worktree"

static worktree_fixture fixture =
	WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);

void test_worktree_worktree__initialize(void)
{
	setup_fixture_worktree(&fixture);
}

void test_worktree_worktree__cleanup(void)
{
	cleanup_fixture_worktree(&fixture);
}

void test_worktree_worktree__list(void)
{
	git_strarray wts;

	cl_git_pass(git_worktree_list(&wts, fixture.repo));
	cl_assert_equal_i(wts.count, 1);
	cl_assert_equal_s(wts.strings[0], "testrepo-worktree");

33
	git_strarray_dispose(&wts);
34 35 36 37 38 39 40 41 42 43 44
}

void test_worktree_worktree__list_with_invalid_worktree_dirs(void)
{
	const char *filesets[3][2] = {
		{ "gitdir", "commondir" },
		{ "gitdir", "HEAD" },
		{ "HEAD", "commondir" },
	};
	git_buf path = GIT_BUF_INIT;
	git_strarray wts;
45
	size_t i, j, len;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

	cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid",
		    fixture.repo->commondir));
	cl_git_pass(p_mkdir(path.ptr, 0755));

	len = path.size;

	for (i = 0; i < ARRAY_SIZE(filesets); i++) {

		for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
			git_buf_truncate(&path, len);
			cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
			cl_git_pass(p_close(p_creat(path.ptr, 0644)));
		}

		cl_git_pass(git_worktree_list(&wts, fixture.worktree));
		cl_assert_equal_i(wts.count, 1);
		cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
64
		git_strarray_dispose(&wts);
65 66 67 68 69 70 71 72

		for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
			git_buf_truncate(&path, len);
			cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
			p_unlink(path.ptr);
		}
	}

73
	git_buf_dispose(&path);
74 75 76 77 78 79 80 81 82 83
}

void test_worktree_worktree__list_in_worktree_repo(void)
{
	git_strarray wts;

	cl_git_pass(git_worktree_list(&wts, fixture.worktree));
	cl_assert_equal_i(wts.count, 1);
	cl_assert_equal_s(wts.strings[0], "testrepo-worktree");

84
	git_strarray_dispose(&wts);
85 86 87 88 89 90 91 92 93 94 95 96 97
}

void test_worktree_worktree__list_without_worktrees(void)
{
	git_repository *repo;
	git_strarray wts;

	repo = cl_git_sandbox_init("testrepo2");
	cl_git_pass(git_worktree_list(&wts, repo));
	cl_assert_equal_i(wts.count, 0);

	git_repository_free(repo);
}
98 99 100 101 102 103 104 105

void test_worktree_worktree__lookup(void)
{
	git_worktree *wt;
	git_buf gitdir_path = GIT_BUF_INIT;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));

106
	cl_git_pass(git_buf_joinpath(&gitdir_path, fixture.repo->commondir, "worktrees/testrepo-worktree/"));
107 108

	cl_assert_equal_s(wt->gitdir_path, gitdir_path.ptr);
109
	cl_assert_equal_s(wt->parent_path, fixture.repo->workdir);
110
	cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink);
111
	cl_assert_equal_s(wt->commondir_path, fixture.repo->gitdir);
112 113
	cl_assert_equal_s(wt->commondir_path, fixture.repo->commondir);

114
	git_buf_dispose(&gitdir_path);
115 116 117 118 119 120 121 122 123 124
	git_worktree_free(wt);
}

void test_worktree_worktree__lookup_nonexistent_worktree(void)
{
	git_worktree *wt;

	cl_git_fail(git_worktree_lookup(&wt, fixture.repo, "nonexistent"));
	cl_assert_equal_p(wt, NULL);
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

void test_worktree_worktree__open(void)
{
	git_worktree *wt;
	git_repository *repo;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));

	cl_git_pass(git_repository_open_from_worktree(&repo, wt));
	cl_assert_equal_s(git_repository_workdir(repo),
		git_repository_workdir(fixture.worktree));

	git_repository_free(repo);
	git_worktree_free(wt);
}

void test_worktree_worktree__open_invalid_commondir(void)
{
	git_worktree *wt;
	git_repository *repo;
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;

	cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/commondir"));
	cl_git_pass(git_buf_printf(&path,
		    "%s/worktrees/testrepo-worktree/commondir",
		    fixture.repo->commondir));
	cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_fail(git_repository_open_from_worktree(&repo, wt));

156 157
	git_buf_dispose(&buf);
	git_buf_dispose(&path);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	git_worktree_free(wt);
}

void test_worktree_worktree__open_invalid_gitdir(void)
{
	git_worktree *wt;
	git_repository *repo;
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;

	cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
	cl_git_pass(git_buf_printf(&path,
		    "%s/worktrees/testrepo-worktree/gitdir",
		    fixture.repo->commondir));
	cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_fail(git_repository_open_from_worktree(&repo, wt));

176 177
	git_buf_dispose(&buf);
	git_buf_dispose(&path);
178 179 180 181 182 183 184 185 186 187 188
	git_worktree_free(wt);
}

void test_worktree_worktree__open_invalid_parent(void)
{
	git_worktree *wt;
	git_repository *repo;
	git_buf buf = GIT_BUF_INIT;

	cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
	cl_git_pass(git_futils_writebuffer(&buf,
189
		    fixture.worktree->gitlink, O_RDWR, 0644));
190 191 192 193

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_fail(git_repository_open_from_worktree(&repo, wt));

194
	git_buf_dispose(&buf);
195 196
	git_worktree_free(wt);
}
197

198 199 200 201 202 203 204 205
void test_worktree_worktree__init(void)
{
	git_worktree *wt;
	git_repository *repo;
	git_reference *branch;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
206
	cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
207 208 209

	/* Open and verify created repo */
	cl_git_pass(git_repository_open(&repo, path.ptr));
210
	cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-new/") == 0);
211 212
	cl_git_pass(git_branch_lookup(&branch, repo, "worktree-new", GIT_BRANCH_LOCAL));

213
	git_buf_dispose(&path);
214 215 216 217 218
	git_worktree_free(wt);
	git_reference_free(branch);
	git_repository_free(repo);
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
void test_worktree_worktree__add_locked(void)
{
	git_worktree *wt;
	git_repository *repo;
	git_reference *branch;
	git_buf path = GIT_BUF_INIT;
	git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT;

	opts.lock = 1;

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-locked"));
	cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-locked", path.ptr, &opts));

	/* Open and verify created repo */
	cl_assert(git_worktree_is_locked(NULL, wt));
	cl_git_pass(git_repository_open(&repo, path.ptr));
	cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-locked/") == 0);
	cl_git_pass(git_branch_lookup(&branch, repo, "worktree-locked", GIT_BRANCH_LOCAL));

238
	git_buf_dispose(&path);
239 240 241 242 243
	git_worktree_free(wt);
	git_reference_free(branch);
	git_repository_free(repo);
}

244 245 246 247 248 249 250 251 252 253 254 255
void test_worktree_worktree__init_existing_branch(void)
{
	git_reference *head, *branch;
	git_commit *commit;
	git_worktree *wt;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_repository_head(&head, fixture.repo));
	cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid));
	cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new", commit, false));

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
256
	cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
257

258
	git_buf_dispose(&path);
259 260 261 262 263
	git_commit_free(commit);
	git_reference_free(head);
	git_reference_free(branch);
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
void test_worktree_worktree__add_with_explicit_branch(void)
{
	git_reference *head, *branch, *wthead;
	git_commit *commit;
	git_worktree *wt;
	git_repository *wtrepo;
	git_buf path = GIT_BUF_INIT;
	git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT;

	cl_git_pass(git_repository_head(&head, fixture.repo));
	cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid));
	cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-with-ref", commit, false));

	opts.ref = branch;

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-with-different-name"));
	cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-with-different-name", path.ptr, &opts));
	cl_git_pass(git_repository_open_from_worktree(&wtrepo, wt));
	cl_git_pass(git_repository_head(&wthead, wtrepo));
	cl_assert_equal_s(git_reference_name(wthead), "refs/heads/worktree-with-ref");

285
	git_buf_dispose(&path);
286 287 288 289 290
	git_commit_free(commit);
	git_reference_free(head);
	git_reference_free(branch);
	git_reference_free(wthead);
	git_repository_free(wtrepo);
291
	git_worktree_free(wt);
292 293 294
}


295 296 297 298 299 300
void test_worktree_worktree__init_existing_worktree(void)
{
	git_worktree *wt;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
301
	cl_git_fail(git_worktree_add(&wt, fixture.repo, "testrepo-worktree", path.ptr, NULL));
302 303

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
304
	cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink);
305

306
	git_buf_dispose(&path);
307 308 309 310 311 312 313 314 315 316 317 318 319 320
	git_worktree_free(wt);
}

void test_worktree_worktree__init_existing_path(void)
{
	const char *wtfiles[] = { "HEAD", "commondir", "gitdir", "index" };
	git_worktree *wt;
	git_buf path = GIT_BUF_INIT;
	unsigned i;

	/* Delete files to verify they have not been created by
	 * the init call */
	for (i = 0; i < ARRAY_SIZE(wtfiles); i++) {
		cl_git_pass(git_buf_joinpath(&path,
321
			    fixture.worktree->gitdir, wtfiles[i]));
322 323 324 325
		cl_git_pass(p_unlink(path.ptr));
	}

	cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../testrepo-worktree"));
326
	cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
327 328 329 330

	/* Verify files have not been re-created */
	for (i = 0; i < ARRAY_SIZE(wtfiles); i++) {
		cl_git_pass(git_buf_joinpath(&path,
331
			    fixture.worktree->gitdir, wtfiles[i]));
332 333 334
		cl_assert(!git_path_exists(path.ptr));
	}

335
	git_buf_dispose(&path);
336 337
}

338 339 340 341 342 343 344 345 346 347 348 349
void test_worktree_worktree__init_submodule(void)
{
	git_repository *repo, *sm, *wt;
	git_worktree *worktree;
	git_buf path = GIT_BUF_INIT;

	cleanup_fixture_worktree(&fixture);
	repo = setup_fixture_submod2();

	cl_git_pass(git_buf_joinpath(&path, repo->workdir, "sm_unchanged"));
	cl_git_pass(git_repository_open(&sm, path.ptr));
	cl_git_pass(git_buf_joinpath(&path, repo->workdir, "../worktree/"));
350
	cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr, NULL));
351 352
	cl_git_pass(git_repository_open_from_worktree(&wt, worktree));

353
	cl_git_pass(git_path_prettify_dir(&path, path.ptr, NULL));
354
	cl_assert_equal_s(path.ptr, wt->workdir);
355
	cl_git_pass(git_path_prettify_dir(&path, sm->commondir, NULL));
356 357 358 359 360
	cl_assert_equal_s(sm->commondir, wt->commondir);

	cl_git_pass(git_buf_joinpath(&path, sm->gitdir, "worktrees/repo-worktree/"));
	cl_assert_equal_s(path.ptr, wt->gitdir);

361
	git_buf_dispose(&path);
362 363 364 365 366
	git_worktree_free(worktree);
	git_repository_free(sm);
	git_repository_free(wt);
}

367 368 369 370 371 372 373 374 375 376
void test_worktree_worktree__validate(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_pass(git_worktree_validate(wt));

	git_worktree_free(wt);
}

377 378 379 380 381 382
void test_worktree_worktree__name(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_assert_equal_s(git_worktree_name(wt), "testrepo-worktree");
383

384 385 386 387 388 389 390 391 392 393 394
	git_worktree_free(wt);
}

void test_worktree_worktree__path(void)
{
	git_worktree *wt;
	git_buf expected_path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&expected_path, clar_sandbox_path(), "testrepo-worktree"));
	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_assert_equal_s(git_worktree_path(wt), expected_path.ptr);
395 396

	git_buf_dispose(&expected_path);
397 398 399
	git_worktree_free(wt);
}

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
void test_worktree_worktree__validate_invalid_commondir(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	git__free(wt->commondir_path);
	wt->commondir_path = "/path/to/invalid/commondir";

	cl_git_fail(git_worktree_validate(wt));

	wt->commondir_path = NULL;
	git_worktree_free(wt);
}

void test_worktree_worktree__validate_invalid_gitdir(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	git__free(wt->gitdir_path);
	wt->gitdir_path = "/path/to/invalid/gitdir";
	cl_git_fail(git_worktree_validate(wt));

	wt->gitdir_path = NULL;
	git_worktree_free(wt);
}

void test_worktree_worktree__validate_invalid_parent(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	git__free(wt->parent_path);
	wt->parent_path = "/path/to/invalid/parent";
	cl_git_fail(git_worktree_validate(wt));

	wt->parent_path = NULL;
	git_worktree_free(wt);
}
439 440 441 442 443 444 445 446 447 448 449 450 451 452

void test_worktree_worktree__lock_with_reason(void)
{
	git_worktree *wt;
	git_buf reason = GIT_BUF_INIT;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));

	cl_assert(!git_worktree_is_locked(NULL, wt));
	cl_git_pass(git_worktree_lock(wt, "because"));
	cl_assert(git_worktree_is_locked(&reason, wt) > 0);
	cl_assert_equal_s(reason.ptr, "because");
	cl_assert(wt->locked);

453
	git_buf_dispose(&reason);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	git_worktree_free(wt);
}

void test_worktree_worktree__lock_without_reason(void)
{
	git_worktree *wt;
	git_buf reason = GIT_BUF_INIT;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));

	cl_assert(!git_worktree_is_locked(NULL, wt));
	cl_git_pass(git_worktree_lock(wt, NULL));
	cl_assert(git_worktree_is_locked(&reason, wt) > 0);
	cl_assert_equal_i(reason.size, 0);
	cl_assert(wt->locked);

470
	git_buf_dispose(&reason);
471 472 473 474 475 476 477 478 479
	git_worktree_free(wt);
}

void test_worktree_worktree__unlock_unlocked_worktree(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_assert(!git_worktree_is_locked(NULL, wt));
480
	cl_assert_equal_i(1, git_worktree_unlock(wt));
481 482 483 484 485 486 487 488 489 490 491 492
	cl_assert(!wt->locked);

	git_worktree_free(wt);
}

void test_worktree_worktree__unlock_locked_worktree(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_pass(git_worktree_lock(wt, NULL));
	cl_assert(git_worktree_is_locked(NULL, wt));
493
	cl_assert_equal_i(0, git_worktree_unlock(wt));
494 495 496 497
	cl_assert(!wt->locked);

	git_worktree_free(wt);
}
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
void test_worktree_worktree__prune_without_opts_fails(void)
{
	git_worktree *wt;
	git_repository *repo;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_fail(git_worktree_prune(wt, NULL));

	/* Assert the repository is still valid */
	cl_git_pass(git_repository_open_from_worktree(&repo, wt));

	git_worktree_free(wt);
	git_repository_free(repo);
}

514 515
void test_worktree_worktree__prune_valid(void)
{
516
	git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
517 518 519
	git_worktree *wt;
	git_repository *repo;

520 521
	opts.flags = GIT_WORKTREE_PRUNE_VALID;

522
	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
523
	cl_git_pass(git_worktree_prune(wt, &opts));
524 525 526 527 528 529 530 531 532 533

	/* Assert the repository is not valid anymore */
	cl_git_fail(git_repository_open_from_worktree(&repo, wt));

	git_worktree_free(wt);
	git_repository_free(repo);
}

void test_worktree_worktree__prune_locked(void)
{
534
	git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
535 536 537 538 539 540
	git_worktree *wt;
	git_repository *repo;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
	cl_git_pass(git_worktree_lock(wt, NULL));

541 542
	opts.flags = GIT_WORKTREE_PRUNE_VALID;
	cl_git_fail(git_worktree_prune(wt, &opts));
543 544 545
	/* Assert the repository is still valid */
	cl_git_pass(git_repository_open_from_worktree(&repo, wt));

546 547 548
	opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_LOCKED;
	cl_git_pass(git_worktree_prune(wt, &opts));

549 550 551 552
	git_worktree_free(wt);
	git_repository_free(repo);
}

553
void test_worktree_worktree__prune_gitdir_only(void)
554
{
555
	git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
556 557
	git_worktree *wt;

558
	opts.flags = GIT_WORKTREE_PRUNE_VALID;
559
	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
560
	cl_git_pass(git_worktree_prune(wt, &opts));
561 562 563 564 565 566 567

	cl_assert(!git_path_exists(wt->gitdir_path));
	cl_assert(git_path_exists(wt->gitlink_path));

	git_worktree_free(wt);
}

568
void test_worktree_worktree__prune_worktree(void)
569
{
570
	git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
571 572
	git_worktree *wt;

573 574
	opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_WORKING_TREE;

575
	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
576
	cl_git_pass(git_worktree_prune(wt, &opts));
577 578 579 580 581 582

	cl_assert(!git_path_exists(wt->gitdir_path));
	cl_assert(!git_path_exists(wt->gitlink_path));

	git_worktree_free(wt);
}
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
static int foreach_worktree_cb(git_repository *worktree, void *payload)
{
	int *counter = (int *)payload;

	switch (*counter) {
	case 0:
		cl_assert_equal_s(git_repository_path(fixture.repo),
				  git_repository_path(worktree));
		cl_assert(!git_repository_is_worktree(worktree));
		break;
	case 1:
		cl_assert_equal_s(git_repository_path(fixture.worktree),
				  git_repository_path(worktree));
		cl_assert(git_repository_is_worktree(worktree));
		break;
	default:
		cl_fail("more worktrees found than expected");
	}

	(*counter)++;

	return 0;
}

void test_worktree_worktree__foreach_worktree_lists_all_worktrees(void)
{
	int counter = 0;
	cl_git_pass(git_repository_foreach_worktree(fixture.repo, foreach_worktree_cb, &counter));
}
613 614 615 616 617 618

void test_worktree_worktree__validate_invalid_worktreedir(void)
{
	git_worktree *wt;

	cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
619
	p_rename("testrepo-worktree", "testrepo-worktree-tmp");
620
	cl_git_fail(git_worktree_validate(wt));
621
	p_rename("testrepo-worktree-tmp", "testrepo-worktree");
622 623 624

	git_worktree_free(wt);
}