-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree-checkout.sh
More file actions
executable file
·106 lines (89 loc) · 3.24 KB
/
worktree-checkout.sh
File metadata and controls
executable file
·106 lines (89 loc) · 3.24 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Symlink .repos from main worktree into a new worktree
link_repos() {
local main_worktree="$1"
local worktree_path="$2"
if [ -d "$main_worktree/.repos" ]; then
ln -s "$main_worktree/.repos" "$worktree_path/.repos"
echo "Linked .repos into '$worktree_path'"
fi
}
# Check if required commands exist
if ! command_exists fzf; then
echo "Error: fzf is not installed. Please install it first."
exit 1
fi
if ! command_exists git; then
echo "Error: git is not installed"
exit 1
fi
git fetch --all --prune
# Get branch name either from argument or fzf selection
if [ $# -ge 1 ]; then
BRANCH_NAME=$1
else
# List all branches (both local and remote) and use fzf to select
BRANCH_NAME=$({
echo "+ Create new branch"
git branch -r | grep -v HEAD | sed 's#[[:space:]]*origin/##'
git branch --format='%(refname:short) %(upstream:track)' | grep -v '\[gone\]' | awk '{print $1}'
} | sort -u | fzf --height 40% --prompt="Select branch > " --preview "git log --color=always -n 50 {}")
# Exit if no branch selected
if [ -z "$BRANCH_NAME" ]; then
echo "No branch selected"
exit 1
fi
fi
if [ "$BRANCH_NAME" = "+ Create new branch" ]; then
read -p "New branch name: " BRANCH_NAME
if [ -z "$BRANCH_NAME" ]; then
echo "No branch name provided"
exit 1
fi
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
DEFAULT_BRANCH=${DEFAULT_BRANCH:-main}
MAIN_WORKTREE=$(git worktree list --porcelain | awk '/^worktree /{ print substr($0, 10); exit }')
WORKTREE_BASE=$(dirname "$MAIN_WORKTREE")
WORKTREE_PATH="$WORKTREE_BASE/$BRANCH_NAME"
git worktree add -b "$BRANCH_NAME" "$WORKTREE_PATH" "origin/$DEFAULT_BRANCH"
if [ $? -eq 0 ]; then
link_repos "$MAIN_WORKTREE" "$WORKTREE_PATH"
echo "Created branch '$BRANCH_NAME' from '$DEFAULT_BRANCH' at '$WORKTREE_PATH'"
cursor "$WORKTREE_PATH"
(cd "$WORKTREE_PATH" && pnpm install)
else
echo "Failed to create worktree"
exit 1
fi
exit 0
fi
# Check if worktree already exists for this branch
EXISTING_PATH=$(git worktree list --porcelain | awk -v branch="$BRANCH_NAME" '
/^worktree / { path = substr($0, 10) }
/^branch refs\/heads\// { if (substr($0, 19) == branch) print path }
')
if [ -n "$EXISTING_PATH" ]; then
echo "Worktree for '$BRANCH_NAME' already exists at '$EXISTING_PATH'"
cursor "$EXISTING_PATH"
(cd "$EXISTING_PATH" && pnpm install)
exit 0
fi
# Resolve worktree path relative to main worktree's parent (so worktrees are always siblings)
MAIN_WORKTREE=$(git worktree list --porcelain | awk '/^worktree /{ print substr($0, 10); exit }')
WORKTREE_BASE=$(dirname "$MAIN_WORKTREE")
WORKTREE_PATH="$WORKTREE_BASE/${2:-$BRANCH_NAME}"
# Create new worktree
git worktree add "$WORKTREE_PATH" "$BRANCH_NAME"
if [ $? -eq 0 ]; then
link_repos "$MAIN_WORKTREE" "$WORKTREE_PATH"
echo "Successfully created worktree for branch '$BRANCH_NAME' at '$WORKTREE_PATH'"
cursor "$WORKTREE_PATH"
(cd "$WORKTREE_PATH" && pnpm install)
else
echo "Failed to create worktree"
exit 1
fi