more opaque foot, fix branch, add del by name
This commit is contained in:
parent
349e8058b1
commit
0c36d6af65
4 changed files with 98 additions and 33 deletions
|
|
@ -1,5 +1,7 @@
|
|||
branchdel() {
|
||||
# branchd — remove current branch worktree (function form)
|
||||
# branchdel — remove a branch worktree (optional branch arg)
|
||||
local branch_arg
|
||||
branch_arg="$1"
|
||||
local wt_path
|
||||
wt_path=$(pwd)
|
||||
local common_dir repo_dir
|
||||
|
|
@ -16,40 +18,76 @@ branchdel() {
|
|||
return 1
|
||||
fi
|
||||
|
||||
if [ "$repo_dir" = "$wt_path" ]; then
|
||||
echo "Inside the root directory of repo, will not delete." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local current default_branch
|
||||
# determine current branch in this worktree
|
||||
local current default_branch branch target_wt
|
||||
current=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || { echo "Not inside a git repository." >&2; return 1; }
|
||||
|
||||
default_branch=$(getdefault)
|
||||
|
||||
if [ "$current" = "$default_branch" ] || [ "$current" = "default" ]; then
|
||||
echo "Already on default branch ($default_branch). Won't remove."
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Switching to default branch '$default_branch'..."
|
||||
# Use branch function if available, otherwise checkout directly in repo
|
||||
if declare -f branch >/dev/null 2>&1; then
|
||||
branch default || { echo "Failed to switch to default branch" >&2; return 1; }
|
||||
# choose branch: provided arg or current
|
||||
if [ -z "$branch_arg" ]; then
|
||||
branch="$current"
|
||||
else
|
||||
git -C "$repo_dir" checkout "$default_branch" || { echo "Failed to checkout default branch" >&2; return 1; }
|
||||
branch="$branch_arg"
|
||||
fi
|
||||
# normalize branch name if refs/heads/ was provided
|
||||
branch="${branch#refs/heads/}"
|
||||
|
||||
# don't remove default
|
||||
if [ "$branch" = "$default_branch" ] || [ "$branch" = "default" ]; then
|
||||
echo "Refusing to remove default branch worktree ($default_branch)." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "Removing worktree at: $wt_path"
|
||||
if git -C "$repo_dir" worktree remove "$wt_path" 2>/dev/null; then
|
||||
echo "Removed worktree: $wt_path"
|
||||
# find the worktree path for the requested branch
|
||||
target_wt=$(git -C "$repo_dir" worktree list --porcelain 2>/dev/null | awk -v b="refs/heads/$branch" '
|
||||
$1=="worktree" { w=$2 }
|
||||
$1=="branch" && $2==b { print w; exit }
|
||||
')
|
||||
|
||||
# if not found in worktree list, check main worktree branch
|
||||
if [ -z "$target_wt" ]; then
|
||||
local main_branch
|
||||
main_branch=$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD 2>/dev/null || true)
|
||||
if [ "$main_branch" = "$branch" ]; then
|
||||
target_wt="$repo_dir"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$target_wt" ]; then
|
||||
echo "No worktree found for branch '$branch'." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ "$target_wt" = "$repo_dir" ]; then
|
||||
echo "Branch '$branch' is the main worktree at '$repo_dir'. Will not delete main worktree." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# if we're currently in that branch/worktree, switch to default and cd to repo root first
|
||||
if [ "$current" = "$branch" ]; then
|
||||
echo "Currently on branch '$branch' in '$wt_path'. Switching to default branch '$default_branch' in main worktree..."
|
||||
if declare -f branch >/dev/null 2>&1; then
|
||||
branch default || { echo "Failed to switch to default branch" >&2; return 1; }
|
||||
else
|
||||
git -C "$repo_dir" checkout "$default_branch" || { echo "Failed to checkout default branch" >&2; return 1; }
|
||||
fi
|
||||
cd "$repo_dir" || { echo "Failed to change directory to repo root: $repo_dir" >&2; return 1; }
|
||||
fi
|
||||
|
||||
echo "Removing worktree at: $target_wt"
|
||||
if git -C "$repo_dir" worktree remove "$target_wt" 2>/dev/null; then
|
||||
rm -rf -- "$target_wt" 2>/dev/null || true
|
||||
echo "Removed worktree: $target_wt"
|
||||
return 0
|
||||
fi
|
||||
# try with --force as a fallback
|
||||
if git -C "$repo_dir" worktree remove --force "$wt_path" 2>/dev/null; then
|
||||
echo "Removed worktree (forced): $wt_path"
|
||||
if git -C "$repo_dir" worktree remove --force "$target_wt" 2>/dev/null; then
|
||||
rm -rf -- "$target_wt" 2>/dev/null || true
|
||||
echo "Removed worktree (forced): $target_wt"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Failed to remove worktree: $wt_path" >&2
|
||||
echo "Failed to remove worktree: $target_wt" >&2
|
||||
return 1
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue