Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions commands/host/worktree
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ physical_path() {
}

# Succeeds if $1 is a linked worktree of the current repo (not the main one).
# Paths are compared exactly after resolving symlinks, so /x/site-feat never
# matches /x/site-feat-2. Keep in sync with is_linked_worktree in worktree-remove.
is_linked_worktree_of_source() {
local want main
want=$(cd "$1" 2>/dev/null && pwd -P) || return 1
Expand Down Expand Up @@ -290,7 +292,7 @@ elif $IS_GIT; then
# Different branch names can sanitize to the same directory
# (feat_login / feat-login / Feat-Login). Never let --force delete
# another branch's worktree.
if [[ -d "$TARGET_DIR" ]] && git worktree list 2>/dev/null | grep -q "$(cd "$TARGET_DIR" 2>/dev/null && pwd -P)"; then
if [[ -d "$TARGET_DIR" ]] && is_linked_worktree_of_source "$TARGET_DIR"; then
TARGET_BRANCH=$(git -C "$TARGET_DIR" symbolic-ref --short -q HEAD 2>/dev/null || true)
if [[ "$TARGET_BRANCH" != "$BRANCH" ]]; then
die "${TARGET_DIR} is a worktree for branch '${TARGET_BRANCH:-(detached HEAD)}', not '${BRANCH}'.\nBoth names sanitize to '${SANITIZED}'. Remove it first with 'ddev worktree-remove' if you no longer need it."
Expand All @@ -305,7 +307,7 @@ if ! $ADOPT && [[ -d "$TARGET_DIR" ]]; then
if $FORCE; then
warn "Target directory exists — removing (--force)"
# If it's a git worktree, remove it properly
if git worktree list 2>/dev/null | grep -q "$(cd "$TARGET_DIR" 2>/dev/null && pwd)"; then
if is_linked_worktree_of_source "$TARGET_DIR"; then
git worktree remove --force "$TARGET_DIR" 2>/dev/null || true
fi
# Stop DDEV if running
Expand Down
17 changes: 7 additions & 10 deletions commands/host/worktree-remove
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ is_omit_project_name_enabled() {
[[ -f "$global_config" ]] && grep -q '^omit_project_name_by_default: true' "$global_config"
}

is_git_worktree() {
local dir="$1" source_dir="$2"
git -C "$source_dir" worktree list 2>/dev/null | grep -q "$(cd "$dir" 2>/dev/null && pwd)"
}

# Echoes the worktree marker config in $1/.ddev, or nothing if absent.
# config.worktree.yaml is the pre-v1.1.0 name, still honored so worktrees
# created by older versions remain removable.
Expand Down Expand Up @@ -70,7 +65,9 @@ linked_worktrees() {
done < <(git -C "$SOURCE_DIR" worktree list --porcelain 2>/dev/null | sed -n 's/^worktree //p')
}

# Succeeds if $1 is one of the source repo's linked worktrees.
# Succeeds if $1 is one of the source repo's linked worktrees. Paths are
# compared exactly after resolving symlinks, so /x/site-junk never matches
# /x/site-junk-2, and a symlinked checkout still matches git's physical path.
is_linked_worktree() {
local want wt
want=$(cd "$1" 2>/dev/null && pwd -P) || return 1
Expand Down Expand Up @@ -166,7 +163,7 @@ if $LIST_MODE; then
printf " %-30s %-12s %s\n" "$suffix" "($status)" "$dir"
found=$((found + 1))
fi
elif is_git_worktree "$dir" "$SOURCE_DIR"; then
elif is_linked_worktree "$dir"; then
wt_name=$(basename "${dir%/}")
suffix="${wt_name#"${SOURCE_DIRNAME}"-}"
status="stopped"
Expand Down Expand Up @@ -226,7 +223,7 @@ else
dir="${PARENT_DIR}/${SOURCE_DIRNAME}-${candidate}"
TRIED+=("$dir")
[[ -d "$dir" ]] || continue
if worktree_config "$dir" >/dev/null || is_git_worktree "$dir" "$SOURCE_DIR"; then
if worktree_config "$dir" >/dev/null || is_linked_worktree "$dir"; then
SANITIZED="$candidate"
TARGET_DIR="$dir"
break
Expand All @@ -246,7 +243,7 @@ fi

# Safety check: verify this was created by 'ddev worktree'
if ! CONFIG_WORKTREE=$(worktree_config "$TARGET_DIR"); then
if is_git_worktree "$TARGET_DIR" "$SOURCE_DIR"; then
if is_linked_worktree "$TARGET_DIR"; then
info "No config.worktree.local.yaml — verified as git worktree of this project"
else
die "Safety check failed: ${TARGET_DIR} does not contain .ddev/config.worktree.local.yaml and is not a recognized git worktree.\nThis directory was not created by 'ddev worktree' — refusing to remove."
Expand Down Expand Up @@ -290,7 +287,7 @@ info "Removing DDEV project..."

# Check if this is a git worktree
IS_WORKTREE=false
if git -C "$SOURCE_DIR" worktree list 2>/dev/null | grep -q "$(cd "$TARGET_DIR" && pwd)"; then
if is_linked_worktree "$TARGET_DIR"; then
IS_WORKTREE=true
fi

Expand Down
39 changes: 39 additions & 0 deletions tests/unit/worktree-remove.bats
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ setup() {
[ -f "${PARENT}/site-junk/keep-me" ]
}

@test "refuses a directory whose path is a prefix of a real worktree's" {
# site-junk-2 is a real worktree; site-junk only shares its path prefix.
git worktree add -q -b junk-2 "${PARENT}/site-junk-2"
mkdir "${PARENT}/site-junk"
touch "${PARENT}/site-junk/keep-me"

run worktree_remove junk
[ "$status" -ne 0 ]
[[ "$output" == *"Safety check failed"* ]] || false
[ -f "${PARENT}/site-junk/keep-me" ]
[ -d "${PARENT}/site-junk-2" ]
}

@test "--list skips a directory whose path is a prefix of a real worktree's" {
git worktree add -q -b junk-2 "${PARENT}/site-junk-2"
mkdir "${PARENT}/site-junk"

run worktree_remove --list
[ "$status" -eq 0 ]
[[ "$output" == *"${PARENT}/site-junk-2/"* ]] || false
[[ "$output" != *"${PARENT}/site-junk/"* ]] || false
}

@test "removes the git worktree when run from a symlinked project path" {
worktree feat-x --no-db --no-start
ln -s "${SANDBOX}/work" "${SANDBOX}/link"
cd "${SANDBOX}/link/site"

run worktree_remove feat-x
[ "$status" -eq 0 ]
[[ "$output" == *"Git worktree removed"* ]] || false
[ ! -e "${PARENT}/site-feat-x" ]
[ "$(git worktree list --porcelain | grep -c site-feat-x)" -eq 0 ]

# A stale registration would make this fail ("missing but already registered").
run worktree feat-x --no-db --no-start
[ "$status" -eq 0 ]
}

@test "reports a missing clone with a hint" {
run worktree_remove nope
[ "$status" -ne 0 ]
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/worktree.bats
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ setup() {
[ -f "${PARENT}/site-feat-login/wip.txt" ]
}

@test "--force replaces a plain directory whose path is a prefix of a real worktree's" {
# site-feat-2 is a real worktree; site-feat only shares its path prefix.
git worktree add -q -b feat-2 "${PARENT}/site-feat-2"
mkdir "${PARENT}/site-feat"

run worktree feat --force --no-db --no-start
[ "$status" -eq 0 ]
[ "$(git -C "${PARENT}/site-feat" symbolic-ref --short HEAD)" = "feat" ]
[ -d "${PARENT}/site-feat-2" ]
}

@test "--force from a symlinked project path unregisters the old worktree" {
worktree feat-x --no-db --no-start
ln -s "${SANDBOX}/work" "${SANDBOX}/link"
cd "${SANDBOX}/link/site"

# A stale registration would make git refuse ("missing but already registered").
run worktree feat-x --force --no-db --no-start
[ "$status" -eq 0 ]
[ "$(git worktree list --porcelain | grep -c '^worktree .*/site-feat-x$')" -eq 1 ]
}

@test "stops before the database export when the branch is checked out elsewhere" {
git worktree add -q -b feat-x "${PARENT}/somewhere-else"
run worktree feat-x
Expand Down
Loading