Subject: [PATCH 4/4] worktree add: Add unit tests for --orphan

From: Jacob Abel <[email-masked]>
Date: Fri, 04 Nov 2022 01:03:35 +0000

Signed-off-by: Jacob Abel <[email-masked]>
---
 t/t2400-worktree-add.sh | 54 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index d587e0b20d..064e1336e2 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -310,6 +310,26 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
 	test_must_fail git worktree add -B poodle --detach bamboo main
 '

+test_expect_success '"add" --orphan/-b mutually exclusive' '
+	test_must_fail git worktree add --orphan poodle -b poodle bamboo main
+'
+
+test_expect_success '"add" --orphan/-B mutually exclusive' '
+	test_must_fail git worktree add --orphan poodle -B poodle bamboo main
+'
+
+test_expect_success '"add" --orphan/--detach mutually exclusive' '
+	test_must_fail git worktree add --orphan poodle --detach bamboo main
+'
+
+test_expect_success '"add" --orphan/--no-checkout mutually exclusive' '
+	test_must_fail git worktree add --orphan poodle --no-checkout bamboo main
+'
+
+test_expect_success '"add" -B/--detach mutually exclusive' '
+	test_must_fail git worktree add -B poodle --detach bamboo main
+'
+
 test_expect_success '"add -B" fails if the branch is checked out' '
 	git rev-parse newmain >before &&
 	test_must_fail git worktree add -B newmain bamboo main &&
@@ -330,6 +350,40 @@ test_expect_success 'add --quiet' '
 	test_must_be_empty actual
 '

+test_expect_success '"add --orphan"' '
+	git worktree add --orphan neworphan orphandir main &&
+	git -C orphandir symbolic-ref HEAD >actual &&
+	echo refs/heads/neworphan >expected &&
+	test_cmp expected actual &&
+	(
+		cd orphandir &&
+		git diff main
+	)
+'
+
+test_expect_success '"add --orphan" fails if the branch already exists' '
+	git worktree add -b existingbranch orphandir2 main &&
+	test_must_fail git worktree add --orphan existingbranch orphandir3 main &&
+	[ ! -d orphandir3 ]
+'
+
+test_expect_success '"add --orphan" fails if the commit-ish doesnt exist' '
+	test_must_fail git worktree add --orphan badcommitish orphandir4 eee2222 &&
+	[ ! -d orphandir4 ]
+'
+
+test_expect_success '"add --orphan" with empty repository' '
+	(
+		mkdir emptyorphanrepo &&
+		cd emptyorphanrepo &&
+		GIT_DIR=".git" git init --bare &&
+		git worktree add --orphan newbranch worktreedir &&
+		git -C worktreedir symbolic-ref HEAD >actual &&
+		echo refs/heads/newbranch >expected &&
+		test_cmp expected actual
+	)
+'
+
 test_expect_success 'local clone from linked checkout' '
 	git clone --local here here-clone &&
 	( cd here-clone && git fsck )
--
2.37.4