#!/bin/sh # SPDX-License-Identifier: MIT set -eu export TZ=UTC LC_ALL=C export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null export GIT_AUTHOR_NAME='Example Author' export GIT_AUTHOR_EMAIL='author@example.invalid' export GIT_COMMITTER_NAME='Example Author' export GIT_COMMITTER_EMAIL='author@example.invalid' export GIT_AUTHOR_DATE='2026-10-25T12:00:00Z' export GIT_COMMITTER_DATE='2026-10-25T12:00:00Z' test "$(git --version)" = 'git version 2.56.0' mkdir worktree-example cd worktree-example mkdir primary cd primary git -c init.templateDir= init -q -b main printf 'base\n' > note.txt git add note.txt git commit -qm 'Base' git branch topic git worktree add -q ../second topic test "$(git branch --show-current)" = main test "$(git -C ../second branch --show-current)" = topic if git worktree add -q ../refused main 2>/dev/null; then printf 'Expected branch-in-use refusal\n' >&2 exit 1 fi printf 'guard: second checkout of main refused\n' printf 'main draft\n' > note.txt printf 'topic draft\n' > ../second/note.txt git -C ../second add note.txt test -z "$(git diff --cached)" test -n "$(git -C ../second diff --cached)" test "$(cat note.txt)" = 'main draft' test "$(cat ../second/note.txt)" = 'topic draft' printf 'state: working files and staged changes are separate\n' if git worktree remove ../second 2>/dev/null; then printf 'Expected dirty-worktree refusal\n' >&2 exit 1 fi printf 'guard: removal with staged work refused\n' git -C ../second commit -qm 'Topic draft' topic_tip=$(git rev-parse topic) test "$(git show topic:note.txt)" = 'topic draft' git worktree remove ../second test ! -d ../second test "$(git rev-parse topic)" = "$topic_tip" printf 'remove: directory removed; topic branch retained\n'