-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathworktree-create.sh
More file actions
executable file
·47 lines (38 loc) · 1.28 KB
/
worktree-create.sh
File metadata and controls
executable file
·47 lines (38 loc) · 1.28 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
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <name>" >&2
exit 1
fi
POSITIONAL_ARGS=("$@")
# For simplicity, we use the same value for both the worktree name and branch name.
WORKTREE_NAME="${POSITIONAL_ARGS[0]}"
BRANCH_NAME="${POSITIONAL_ARGS[0]}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
WORKTREE_DIR="$REPO_ROOT/.worktrees/$WORKTREE_NAME"
if [[ -d "$WORKTREE_DIR" ]]; then
echo "Error: worktree directory already exists: $WORKTREE_DIR" >&2
exit 1
fi
if git -C "$REPO_ROOT" branch --list "$BRANCH_NAME" | grep -q .; then
echo "Error: branch '$BRANCH_NAME' already exists. Delete it first or choose a different name." >&2
exit 1
fi
echo "Creating worktree '$WORKTREE_NAME' on branch '$BRANCH_NAME'..."
git -C "$REPO_ROOT" worktree add "$WORKTREE_DIR" -b "$BRANCH_NAME"
if command -v uv &>/dev/null; then
echo "Setting up virtual environment with uv..."
uv venv "$WORKTREE_DIR/.venv"
else
echo "uv not found — falling back to python -m venv..."
python -m venv "$WORKTREE_DIR/.venv"
fi
echo ""
echo "Worktree ready!"
echo " Path: $WORKTREE_DIR"
echo " Branch: $BRANCH_NAME"
echo ""
echo "To start working:"
echo " cd $WORKTREE_DIR"
echo " source .venv/bin/activate"