-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnwt.zsh
More file actions
118 lines (100 loc) · 4.52 KB
/
nwt.zsh
File metadata and controls
118 lines (100 loc) · 4.52 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
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env zsh
# nwt (New Worktree) - Smart Git Worktree Manager for Warp Terminal
#
# This function creates git worktrees with new branches and intelligently
# opens them in Warp terminal, with smart tab reuse when possible.
#
# Usage: new_worktree <branch-name> [base-branch]
# nwt <branch-name> [base-branch] (alias)
#
# Features:
# - Auto-detects default branch (main/master/develop)
# - Handles existing worktrees and branches intelligently
# - Smart Warp tab reuse (opens existing tabs when possible)
# - Support for branch names with slashes (feature/api/endpoint)
# - Clear status messages and error handling
new_worktree() {
if [[ $# -eq 0 ]]; then
echo "Usage: new_worktree <branch-name> [base-branch]"
echo "Example: new_worktree feature/new-feature"
echo "Example: new_worktree feature/new-feature main"
return 1
fi
local branch_name="$1"
local repo_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -z "$repo_root" ]]; then
echo "Error: Not in a git repository"
return 1
fi
local repo_name=$(basename "$repo_root")
local worktree_dir="${repo_root}/../${repo_name}-${branch_name}"
# Check if worktree already exists in git worktree list
local existing_worktree=$(git worktree list --porcelain | grep -B2 "branch refs/heads/$branch_name" | grep "worktree" | cut -d' ' -f2)
if [[ -n "$existing_worktree" ]]; then
echo "📁 Worktree already exists at: $existing_worktree"
# Check if we're currently in a Warp terminal at the target location
if [[ "$PWD" == "$existing_worktree"* ]] && [[ -n "$WARP_IS_LOCAL_SHELL_SESSION" ]]; then
echo "✅ Already in the worktree directory in current Warp tab!"
return 0
fi
# Use Warp's smart tab handling by opening the directory
# Warp will intelligently reuse existing tabs when possible
echo "Opening worktree (Warp will reuse existing tab if available)..."
open -a "Warp" "$existing_worktree"
# Give a helpful message about what happened
echo "✅ Opened worktree: $existing_worktree"
echo "💡 If you had a tab open for this path, Warp should have switched to it"
return 0
fi
# Check if branch already exists but not in a worktree
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
echo "🔀 Branch '$branch_name' already exists"
echo "Creating worktree for existing branch..."
if git worktree add "$worktree_dir" "$branch_name"; then
echo "✅ Worktree created for existing branch"
open -a "Warp" "$worktree_dir"
echo "✅ New Warp window opened at: $worktree_dir"
return 0
else
echo "❌ Failed to create worktree for existing branch"
return 1
fi
fi
# Auto-detect default branch if not specified (only for new branches)
local base_branch="$2"
if [[ -z "$base_branch" ]]; then
# Try to get the default branch from remote
base_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
# If that fails, try common default branches
if [[ -z "$base_branch" ]]; then
if git show-ref --verify --quiet refs/heads/main; then
base_branch="main"
elif git show-ref --verify --quiet refs/heads/master; then
base_branch="master"
elif git show-ref --verify --quiet refs/heads/develop; then
base_branch="develop"
else
# Fallback to current branch
base_branch=$(git branch --show-current)
echo "⚠️ Warning: Could not detect default branch, using current branch: $base_branch"
fi
fi
fi
echo "Creating new worktree and branch..."
echo "Repository: $repo_root"
echo "New branch: $branch_name (from $base_branch)"
echo "Worktree location: $worktree_dir"
# Create the worktree with new branch
if git worktree add -b "$branch_name" "$worktree_dir" "$base_branch"; then
echo "✅ Worktree and branch created successfully"
# Open new Warp terminal window at the worktree location
echo "Opening new Warp window..."
open -a "Warp" "$worktree_dir"
echo "✅ New Warp window opened at: $worktree_dir"
else
echo "❌ Failed to create worktree"
return 1
fi
}
# Alias for shorter command
alias nwt='new_worktree'