-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree-rm.sh
More file actions
executable file
·53 lines (43 loc) · 1.57 KB
/
Copy pathworktree-rm.sh
File metadata and controls
executable file
·53 lines (43 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# Usage: source worktree-rm.sh [branch-name]
# If run from inside a worktree, auto-detects the branch.
# Deletes the Neon DB branch and removes the git worktree.
# Must be sourced (not executed) to cd back to the main repo.
worktree_rm() {
set -euo pipefail
local MAIN_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local BRANCH_NAME="${1:-}"
local WORKTREE_PATH
# Auto-detect if running inside a worktree
if [[ -z "$BRANCH_NAME" ]]; then
local GIT_DIR="$(git rev-parse --git-dir 2>/dev/null)"
local GIT_COMMON="$(git rev-parse --git-common-dir 2>/dev/null)"
if [[ "$GIT_DIR" != "$GIT_COMMON" ]]; then
BRANCH_NAME="$(git branch --show-current)"
WORKTREE_PATH="$(git rev-parse --show-toplevel)"
echo "Detected worktree: $BRANCH_NAME"
else
echo "Usage: source worktree-rm.sh [branch-name]"
echo " Or run from inside a worktree to auto-detect."
return 1
fi
else
WORKTREE_PATH="$MAIN_REPO/../personal-finance-visoneer-$BRANCH_NAME"
fi
if [[ ! -d "$WORKTREE_PATH" ]]; then
echo "Error: Worktree not found at $WORKTREE_PATH"
return 1
fi
read -p "Remove worktree '$BRANCH_NAME' (including Neon branch)? [y/N] " confirm
if [[ "$confirm" != [yY] ]]; then
echo "Aborted"
return 0
fi
# Delete Neon DB branch
bash "$MAIN_REPO/scripts/db-branch-delete.sh" "$BRANCH_NAME" || true
cd "$MAIN_REPO"
git worktree remove "$WORKTREE_PATH"
git branch -d "$BRANCH_NAME" 2>/dev/null || echo "Branch not deleted (unmerged or already gone)"
echo "✓ Worktree removed"
}
worktree_rm "$@"