Make worktree rm actually work in the presence of submodules

Also adds the `--force` option to remove a worktree with unstaged
changes.
This commit is contained in:
2026-05-21 14:50:15 +01:00
parent 547f921d6e
commit c63b9cceb4

View File

@@ -102,7 +102,22 @@ Manage git worktrees by branch name."
fi
local -a rm_args=()
(( force )) && rm_args+=(--force)
if ! git worktree remove "${rm_args[@]}" "$wt_path"; then
local err
if err=$(git worktree remove "${rm_args[@]}" "$wt_path" 2>&1); then
:
elif [[ "$err" == *"working trees containing submodules"* ]]; then
if (( ! force )) && \
[[ -n "$(git -C "$wt_path" status --porcelain --ignore-submodules=all 2>/dev/null)" ]]; then
print -P "%F{red}error:%f worktree has uncommitted changes (use --force): $wt_path"
failed=1
continue
fi
if ! rm -rf "$wt_path" || ! git worktree prune; then
failed=1
continue
fi
else
print -- "$err" >&2
failed=1
continue
fi