@fro.bot/systematic 3.26.1 → 3.26.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HARNESSES.md +23 -8
- package/dist/index.js +20 -2
- package/dist/lib/bootstrap.d.ts +17 -2
- package/dist/pi.js +23 -5
- package/package.json +4 -4
- package/skills/ce-review/SKILL.md +4 -4
- package/skills/git-clean-gone-branches/SKILL.md +17 -7
- package/skills/git-worktree/SKILL.md +33 -309
- package/skills/git-worktree/scripts/worktree-manager.sh +568 -391
- package/skills/orchestrating-subagents/SKILL.md +1 -1
- package/skills/using-systematic/references/worktree-placement.md +1 -0
|
@@ -1,500 +1,677 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
|
|
3
3
|
# Git Worktree Manager
|
|
4
|
-
#
|
|
5
|
-
#
|
|
4
|
+
# Creates, lists, switches and cleans up Git worktrees under
|
|
5
|
+
# <main repository root>/.worktrees/<branch>.
|
|
6
|
+
#
|
|
7
|
+
# Non-interactive: it never reads stdin. Stdout carries key=value lines (or one
|
|
8
|
+
# bare path for switch). Stderr carries prose and `error: <code>: <message>`.
|
|
9
|
+
#
|
|
10
|
+
# Exit codes: 0 done, 1 environment or git failure, 2 usage error,
|
|
11
|
+
# 3 refused (a guard or a user decision is needed).
|
|
12
|
+
#
|
|
13
|
+
# Bash 3.2 compatible (stock macOS). Keep `set -e` only: no `set -u` (an empty
|
|
14
|
+
# "${a[@]}" fails on 3.2). Never pipe into `while`: the pipeline subshell loses
|
|
15
|
+
# the arrays the loop builds.
|
|
6
16
|
|
|
7
17
|
set -e
|
|
8
18
|
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
fi
|
|
19
|
+
# A stray GIT_DIR or GIT_WORK_TREE could point git at another repository, so
|
|
20
|
+
# the variables that locate a repository are dropped before the first git call.
|
|
21
|
+
# LC_ALL=C keeps the git output that is parsed locale-independent.
|
|
22
|
+
for _wt_var in GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE \
|
|
23
|
+
GIT_OBJECT_DIRECTORY GIT_NAMESPACE GIT_PREFIX; do
|
|
24
|
+
unset "$_wt_var"
|
|
25
|
+
done
|
|
26
|
+
unset _wt_var
|
|
27
|
+
export LC_ALL=C
|
|
28
|
+
|
|
29
|
+
TAB=$'\t'
|
|
30
|
+
|
|
31
|
+
# Prose for humans. Stdout stays machine-readable.
|
|
32
|
+
say() {
|
|
33
|
+
printf '%s\n' "$*" >&2
|
|
25
34
|
}
|
|
26
35
|
|
|
27
|
-
#
|
|
28
|
-
|
|
29
|
-
local
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if [[ -f "$f" ]]; then
|
|
37
|
-
local basename=$(basename "$f")
|
|
38
|
-
# Skip .env.example (that's typically committed to git)
|
|
39
|
-
if [[ "$basename" != ".env.example" ]]; then
|
|
40
|
-
env_files+=("$basename")
|
|
41
|
-
fi
|
|
42
|
-
fi
|
|
43
|
-
done
|
|
36
|
+
# die <exit-code> <code> <message...>
|
|
37
|
+
die() {
|
|
38
|
+
local code="$1" tag="$2"
|
|
39
|
+
shift 2
|
|
40
|
+
printf 'error: %s: %s\n' "$tag" "$*" >&2
|
|
41
|
+
exit "$code"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# ── Repository root ─────────────────────────────────────────────────────────
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
MAIN_ROOT=""
|
|
47
|
+
WORKTREE_DIR=""
|
|
48
|
+
|
|
49
|
+
# The main root is the parent of the git common directory, so a run from inside
|
|
50
|
+
# a linked worktree still lands at the main checkout. Layouts with no such root
|
|
51
|
+
# are refused.
|
|
52
|
+
resolve_main_root() {
|
|
53
|
+
local common_raw common superproject root toplevel gitdir
|
|
54
|
+
|
|
55
|
+
common_raw=$(git rev-parse --git-common-dir 2> /dev/null) ||
|
|
56
|
+
die 1 not-a-repository "not inside a git repository: $PWD"
|
|
57
|
+
common=$(cd "$common_raw" 2> /dev/null && pwd -P) ||
|
|
58
|
+
die 1 git-failure "cannot resolve the git common directory: $common_raw"
|
|
59
|
+
|
|
60
|
+
if [ "$(git rev-parse --is-bare-repository 2> /dev/null)" = "true" ] ||
|
|
61
|
+
[ "$(git --git-dir="$common" rev-parse --is-bare-repository 2> /dev/null)" = "true" ]; then
|
|
62
|
+
die 3 unsupported-layout "bare repository (or core.bare) has no main work tree: $common"
|
|
63
|
+
fi
|
|
64
|
+
superproject=$(git rev-parse --show-superproject-working-tree 2> /dev/null) || superproject=""
|
|
65
|
+
if [ -n "$superproject" ]; then
|
|
66
|
+
die 3 unsupported-layout "submodule of $superproject has no main work tree to place .worktrees in"
|
|
67
|
+
fi
|
|
68
|
+
if [ "$(basename "$common")" != ".git" ]; then
|
|
69
|
+
die 3 unsupported-layout "separate git directory $common has no main work tree to place .worktrees in"
|
|
48
70
|
fi
|
|
49
71
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
72
|
+
# A git directory named .git can still belong to a work tree elsewhere
|
|
73
|
+
# (core.worktree), so the inferred root must be its own work tree. Nothing is
|
|
74
|
+
# written before this check.
|
|
75
|
+
root=$(dirname "$common")
|
|
76
|
+
toplevel=$(git -C "$root" rev-parse --show-toplevel 2> /dev/null) || toplevel=""
|
|
77
|
+
toplevel=$(cd "$toplevel" 2> /dev/null && pwd -P) || toplevel=""
|
|
78
|
+
if [ "$toplevel" != "$root" ] ||
|
|
79
|
+
[ "$(git -C "$root" rev-parse --is-bare-repository 2> /dev/null)" = "true" ]; then
|
|
80
|
+
die 3 unsupported-layout "$root is not the work tree of $common; there is no main work tree to place .worktrees in"
|
|
81
|
+
fi
|
|
54
82
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
83
|
+
# In the main checkout (not a linked worktree) the caller's own work tree must
|
|
84
|
+
# be that root. Plain `git init --separate-git-dir` leaves core.worktree
|
|
85
|
+
# unset, so the check above passes for the storage directory; this one
|
|
86
|
+
# catches it. A linked worktree cannot tell this layout apart, so it is not
|
|
87
|
+
# checked further.
|
|
88
|
+
gitdir=$(git rev-parse --absolute-git-dir 2> /dev/null) && gitdir=$(cd "$gitdir" 2> /dev/null && pwd -P) || gitdir=""
|
|
89
|
+
if [ "$gitdir" = "$common" ]; then
|
|
90
|
+
toplevel=$(git rev-parse --show-toplevel 2> /dev/null) || toplevel=""
|
|
91
|
+
toplevel=$(cd "$toplevel" 2> /dev/null && pwd -P) || toplevel=""
|
|
92
|
+
if [ "$toplevel" != "$root" ]; then
|
|
93
|
+
die 3 unsupported-layout "your work tree ${toplevel:-(none)} is not $root, which holds the git directory $common"
|
|
58
94
|
fi
|
|
95
|
+
fi
|
|
59
96
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
97
|
+
MAIN_ROOT="$root"
|
|
98
|
+
WORKTREE_DIR="$MAIN_ROOT/.worktrees"
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
# ── Registered worktrees ────────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
# Read from `git worktree list --porcelain` into parallel arrays. The first
|
|
104
|
+
# entry is the main checkout.
|
|
105
|
+
WT_PATHS=()
|
|
106
|
+
WT_BRANCHES=()
|
|
107
|
+
WT_LOCKED=()
|
|
108
|
+
|
|
109
|
+
load_worktrees() {
|
|
110
|
+
local porcelain line wt_path="" wt_branch="" wt_locked="no"
|
|
111
|
+
|
|
112
|
+
porcelain=$(git -C "$MAIN_ROOT" worktree list --porcelain 2> /dev/null) ||
|
|
113
|
+
die 1 git-failure "git worktree list failed in $MAIN_ROOT"
|
|
114
|
+
|
|
115
|
+
WT_PATHS=()
|
|
116
|
+
WT_BRANCHES=()
|
|
117
|
+
WT_LOCKED=()
|
|
118
|
+
|
|
119
|
+
# The here-string reads a variable, not a pipe, so the arrays survive the
|
|
120
|
+
# loop. The extra newline closes the final record, which $(...) stripped.
|
|
121
|
+
while IFS= read -r line; do
|
|
122
|
+
case "$line" in
|
|
123
|
+
"worktree "*) wt_path="${line#worktree }" ;;
|
|
124
|
+
"branch "*)
|
|
125
|
+
wt_branch="${line#branch }"
|
|
126
|
+
wt_branch="${wt_branch#refs/heads/}"
|
|
127
|
+
;;
|
|
128
|
+
detached) wt_branch="(detached)" ;;
|
|
129
|
+
locked*) wt_locked="yes" ;;
|
|
130
|
+
"")
|
|
131
|
+
if [ -n "$wt_path" ]; then
|
|
132
|
+
WT_PATHS+=("$wt_path")
|
|
133
|
+
WT_BRANCHES+=("${wt_branch:-(unknown)}")
|
|
134
|
+
WT_LOCKED+=("$wt_locked")
|
|
135
|
+
fi
|
|
136
|
+
wt_path=""
|
|
137
|
+
wt_branch=""
|
|
138
|
+
wt_locked="no"
|
|
139
|
+
;;
|
|
140
|
+
esac
|
|
141
|
+
done <<< "$porcelain"$'\n'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
# Prints the name (path relative to .worktrees, for example feat/x) for a path
|
|
145
|
+
# under .worktrees, or nothing otherwise.
|
|
146
|
+
worktree_name() {
|
|
147
|
+
case "$1" in
|
|
148
|
+
"$WORKTREE_DIR"/*) printf '%s' "${1#"$WORKTREE_DIR"/}" ;;
|
|
149
|
+
esac
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
# True when $1 is the same path as $2 or inside it.
|
|
153
|
+
path_within() {
|
|
154
|
+
case "$1" in
|
|
155
|
+
"$2" | "$2"/*) return 0 ;;
|
|
156
|
+
esac
|
|
157
|
+
return 1
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
# Sets FOUND_IDX to the index of the managed worktree called $1, or -1.
|
|
161
|
+
find_by_name() {
|
|
162
|
+
local i=0 name
|
|
163
|
+
FOUND_IDX=-1
|
|
164
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
165
|
+
name=$(worktree_name "${WT_PATHS[$i]}")
|
|
166
|
+
if [ -n "$name" ] && [ "$name" = "$1" ]; then
|
|
167
|
+
FOUND_IDX="$i"
|
|
168
|
+
return 0
|
|
169
|
+
fi
|
|
170
|
+
i=$((i + 1))
|
|
63
171
|
done
|
|
172
|
+
return 0
|
|
173
|
+
}
|
|
64
174
|
|
|
65
|
-
|
|
175
|
+
# Exit 2 with the managed worktree names on stderr.
|
|
176
|
+
unknown_worktree() {
|
|
177
|
+
local i=0 name
|
|
178
|
+
printf 'error: unknown-worktree: no worktree named %s\n' "$1" >&2
|
|
179
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
180
|
+
name=$(worktree_name "${WT_PATHS[$i]}")
|
|
181
|
+
if [ -n "$name" ]; then
|
|
182
|
+
printf ' candidate: %s\n' "$name" >&2
|
|
183
|
+
fi
|
|
184
|
+
i=$((i + 1))
|
|
185
|
+
done
|
|
186
|
+
exit 2
|
|
66
187
|
}
|
|
67
188
|
|
|
68
|
-
#
|
|
69
|
-
# is unavailable (for example in single-branch clones).
|
|
70
|
-
get_default_branch() {
|
|
71
|
-
local head_ref
|
|
72
|
-
head_ref=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null || true)
|
|
189
|
+
# ── Env files and dev tool trust ────────────────────────────────────────────
|
|
73
190
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
191
|
+
# Copy the main checkout's git-ignored .env* files into a worktree. Tracked
|
|
192
|
+
# files are not ignored, so they are skipped. Symlinks and existing
|
|
193
|
+
# destinations are skipped too, and nothing is overwritten or backed up.
|
|
194
|
+
copy_env_files() {
|
|
195
|
+
local dest_dir="$1" f name copied=0
|
|
196
|
+
|
|
197
|
+
for f in "$MAIN_ROOT"/.env*; do
|
|
198
|
+
name=$(basename "$f")
|
|
199
|
+
if [ -L "$f" ] || [ ! -f "$f" ]; then
|
|
200
|
+
continue
|
|
201
|
+
fi
|
|
202
|
+
if ! git -C "$MAIN_ROOT" check-ignore -q -- "$name"; then
|
|
203
|
+
say "skipped $name (not ignored by git)"
|
|
204
|
+
continue
|
|
205
|
+
fi
|
|
206
|
+
if [ -e "$dest_dir/$name" ] || [ -L "$dest_dir/$name" ]; then
|
|
207
|
+
say "skipped $name (already exists in the worktree)"
|
|
208
|
+
continue
|
|
209
|
+
fi
|
|
210
|
+
cp "$f" "$dest_dir/$name"
|
|
211
|
+
say "copied $name"
|
|
212
|
+
copied=$((copied + 1))
|
|
213
|
+
done
|
|
214
|
+
printf 'copied=%s\n' "$copied"
|
|
79
215
|
}
|
|
80
216
|
|
|
81
217
|
# Auto-trust is only safe when the worktree is created from a long-lived branch
|
|
82
|
-
# the developer already controls. Review/PR branches
|
|
83
|
-
#
|
|
218
|
+
# the developer already controls. Review/PR branches fall back to the default
|
|
219
|
+
# branch baseline and require manual direnv approval.
|
|
84
220
|
is_trusted_base_branch() {
|
|
85
|
-
local branch="$1"
|
|
86
|
-
local default_branch="$2"
|
|
87
|
-
|
|
88
|
-
[[ "$branch" == "$default_branch" ]] && return 0
|
|
221
|
+
local branch="$1" default_branch="$2"
|
|
89
222
|
|
|
223
|
+
[ "$branch" = "$default_branch" ] && return 0
|
|
90
224
|
case "$branch" in
|
|
91
|
-
develop|dev|trunk|staging|release/*)
|
|
92
|
-
return 0
|
|
93
|
-
;;
|
|
94
|
-
*)
|
|
95
|
-
return 1
|
|
96
|
-
;;
|
|
225
|
+
develop | dev | trunk | staging | release/*) return 0 ;;
|
|
97
226
|
esac
|
|
227
|
+
return 1
|
|
98
228
|
}
|
|
99
229
|
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
230
|
+
# True when the file is identical to its version at the base ref. Symlinks are
|
|
231
|
+
# rejected. A mismatch (added, modified or filtered) means trust is skipped,
|
|
232
|
+
# which is the safe direction.
|
|
233
|
+
config_unchanged() {
|
|
234
|
+
local file="$1" base_ref="$2" worktree_path="$3" base_hash worktree_hash
|
|
235
|
+
|
|
236
|
+
[ -L "$worktree_path/$file" ] && return 1
|
|
237
|
+
base_hash=$(git -C "$MAIN_ROOT" rev-parse "$base_ref:$file" 2> /dev/null) || return 1
|
|
238
|
+
worktree_hash=$(git hash-object "$worktree_path/$file") || return 1
|
|
239
|
+
[ "$base_hash" = "$worktree_hash" ]
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
# Trust mise and direnv configs in a new worktree: these tools have never seen
|
|
243
|
+
# the new path and would otherwise block on a prompt. Only configs unchanged
|
|
244
|
+
# from the trusted baseline are trusted, and direnv only for trusted bases.
|
|
245
|
+
# Stdin is /dev/null so a tool can never wait for an answer.
|
|
111
246
|
trust_dev_tools() {
|
|
112
|
-
local worktree_path="$1"
|
|
113
|
-
local
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
local skipped_messages=()
|
|
117
|
-
local manual_commands=()
|
|
118
|
-
|
|
119
|
-
# mise: trust the specific config file if present and unchanged
|
|
120
|
-
if command -v mise &>/dev/null; then
|
|
247
|
+
local worktree_path="$1" base_ref="$2" allow_direnv_auto="$3" f
|
|
248
|
+
local manual=()
|
|
249
|
+
|
|
250
|
+
if command -v mise > /dev/null 2>&1; then
|
|
121
251
|
for f in .mise.toml mise.toml .tool-versions; do
|
|
122
|
-
if [
|
|
123
|
-
if
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
else
|
|
127
|
-
echo -e " ${YELLOW}Warning: 'mise trust $f' failed -- run manually in $worktree_path${NC}"
|
|
128
|
-
fi
|
|
252
|
+
if [ -f "$worktree_path/$f" ]; then
|
|
253
|
+
if config_unchanged "$f" "$base_ref" "$worktree_path"; then
|
|
254
|
+
(cd "$worktree_path" && mise trust "$f" --quiet < /dev/null > /dev/null) ||
|
|
255
|
+
say "mise trust $f failed; run it manually in $worktree_path"
|
|
129
256
|
else
|
|
130
|
-
|
|
131
|
-
manual_commands+=("mise trust $f")
|
|
257
|
+
manual+=("mise trust $f")
|
|
132
258
|
fi
|
|
133
259
|
break
|
|
134
260
|
fi
|
|
135
261
|
done
|
|
136
262
|
fi
|
|
137
263
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
elif _config_unchanged ".envrc" "$base_ref" "$worktree_path"; then
|
|
145
|
-
if (cd "$worktree_path" && direnv allow); then
|
|
146
|
-
trusted=$((trusted + 1))
|
|
147
|
-
else
|
|
148
|
-
echo -e " ${YELLOW}Warning: 'direnv allow' failed -- run manually in $worktree_path${NC}"
|
|
149
|
-
fi
|
|
150
|
-
else
|
|
151
|
-
skipped_messages+=("direnv allow (.envrc differs from $base_ref)")
|
|
152
|
-
manual_commands+=("direnv allow")
|
|
153
|
-
fi
|
|
264
|
+
if command -v direnv > /dev/null 2>&1 && [ -f "$worktree_path/.envrc" ]; then
|
|
265
|
+
if [ "$allow_direnv_auto" = "true" ] && config_unchanged ".envrc" "$base_ref" "$worktree_path"; then
|
|
266
|
+
(cd "$worktree_path" && direnv allow < /dev/null > /dev/null) ||
|
|
267
|
+
say "direnv allow failed; run it manually in $worktree_path"
|
|
268
|
+
else
|
|
269
|
+
manual+=("direnv allow")
|
|
154
270
|
fi
|
|
155
271
|
fi
|
|
156
272
|
|
|
157
|
-
if [
|
|
158
|
-
|
|
159
|
-
fi
|
|
160
|
-
|
|
161
|
-
if [[ ${#skipped_messages[@]} -gt 0 ]]; then
|
|
162
|
-
echo -e " ${YELLOW}Skipped auto-trust for config(s) requiring manual review:${NC}"
|
|
163
|
-
for item in "${skipped_messages[@]}"; do
|
|
164
|
-
echo -e " - $item"
|
|
165
|
-
done
|
|
166
|
-
if [[ ${#manual_commands[@]} -gt 0 ]]; then
|
|
167
|
-
local joined
|
|
168
|
-
joined=$(printf ' && %s' "${manual_commands[@]}")
|
|
169
|
-
echo -e " ${BLUE}Review the diff, then run manually: cd $worktree_path${joined}${NC}"
|
|
170
|
-
fi
|
|
273
|
+
if [ "${#manual[@]}" -gt 0 ]; then
|
|
274
|
+
say "not auto-trusted (differs from $base_ref or untrusted base); review, then run in $worktree_path: ${manual[*]}"
|
|
171
275
|
fi
|
|
172
276
|
}
|
|
173
277
|
|
|
174
|
-
#
|
|
175
|
-
# Returns 0 (true) if the file is identical to the base branch version.
|
|
176
|
-
# Returns 1 (false) if the file was added or modified by this branch.
|
|
177
|
-
#
|
|
178
|
-
# Note: rev-parse returns the stored blob hash; hash-object on a path applies
|
|
179
|
-
# gitattributes filters. A mismatch causes a false negative (trust skipped),
|
|
180
|
-
# which is the safe direction.
|
|
181
|
-
_config_unchanged() {
|
|
182
|
-
local file="$1"
|
|
183
|
-
local base_ref="$2"
|
|
184
|
-
local worktree_path="$3"
|
|
278
|
+
# ── create ──────────────────────────────────────────────────────────────────
|
|
185
279
|
|
|
186
|
-
|
|
187
|
-
|
|
280
|
+
# Sets DEFAULT_BRANCH from origin/HEAD, else from the remote. Never guesses.
|
|
281
|
+
DEFAULT_BRANCH=""
|
|
188
282
|
|
|
189
|
-
|
|
190
|
-
local
|
|
191
|
-
base_hash=$(git rev-parse "$base_ref:$file" 2>/dev/null) || return 1
|
|
283
|
+
resolve_default_branch() {
|
|
284
|
+
local symref line rest
|
|
192
285
|
|
|
193
|
-
|
|
194
|
-
|
|
286
|
+
symref=$(git -C "$MAIN_ROOT" symbolic-ref -q refs/remotes/origin/HEAD 2> /dev/null) || symref=""
|
|
287
|
+
if [ -n "$symref" ]; then
|
|
288
|
+
DEFAULT_BRANCH="${symref#refs/remotes/origin/}"
|
|
289
|
+
return 0
|
|
290
|
+
fi
|
|
195
291
|
|
|
196
|
-
|
|
292
|
+
line=$(git -C "$MAIN_ROOT" ls-remote --symref origin HEAD < /dev/null 2> /dev/null) || line=""
|
|
293
|
+
while IFS= read -r line; do
|
|
294
|
+
case "$line" in
|
|
295
|
+
"ref: refs/heads/"*)
|
|
296
|
+
rest="${line#ref: refs/heads/}"
|
|
297
|
+
DEFAULT_BRANCH="${rest%%"$TAB"*}"
|
|
298
|
+
break
|
|
299
|
+
;;
|
|
300
|
+
esac
|
|
301
|
+
done <<< "$line"
|
|
302
|
+
[ -n "$DEFAULT_BRANCH" ] ||
|
|
303
|
+
die 3 base-unresolved "cannot tell origin's default branch; pass --base <ref>"
|
|
197
304
|
}
|
|
198
305
|
|
|
199
|
-
#
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
local
|
|
306
|
+
# .worktrees/.gitignore holds `*`, so the directory never shows up in status and
|
|
307
|
+
# no tracked file is edited. An existing file is left as it is.
|
|
308
|
+
ensure_ignore_file() {
|
|
309
|
+
local ignore="$WORKTREE_DIR/.gitignore"
|
|
203
310
|
|
|
204
|
-
if
|
|
205
|
-
|
|
206
|
-
|
|
311
|
+
if git -C "$MAIN_ROOT" ls-files --error-unmatch -- ".worktrees/.gitignore" > /dev/null 2>&1; then
|
|
312
|
+
say "warning: .worktrees/.gitignore is tracked; leaving it as it is"
|
|
313
|
+
return 0
|
|
314
|
+
fi
|
|
315
|
+
if [ -e "$ignore" ] || [ -L "$ignore" ]; then
|
|
316
|
+
return 0
|
|
207
317
|
fi
|
|
318
|
+
mkdir -p "$WORKTREE_DIR" || die 1 environment "cannot create $WORKTREE_DIR"
|
|
319
|
+
printf '*\n' > "$ignore" || die 1 environment "cannot write $ignore"
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
create_worktree() {
|
|
323
|
+
local branch="" base_arg="" base_ref base_commit worktree_path status_out i
|
|
324
|
+
|
|
325
|
+
while [ $# -gt 0 ]; do
|
|
326
|
+
case "$1" in
|
|
327
|
+
--base)
|
|
328
|
+
[ $# -ge 2 ] || die 2 usage "--base needs a ref"
|
|
329
|
+
base_arg="$2"
|
|
330
|
+
shift 2
|
|
331
|
+
;;
|
|
332
|
+
-*) die 2 usage "unknown option for create: $1" ;;
|
|
333
|
+
*)
|
|
334
|
+
[ -z "$branch" ] || die 2 usage "unexpected argument for create: $1"
|
|
335
|
+
branch="$1"
|
|
336
|
+
shift
|
|
337
|
+
;;
|
|
338
|
+
esac
|
|
339
|
+
done
|
|
340
|
+
[ -n "$branch" ] || die 2 usage "create needs a branch name"
|
|
341
|
+
git check-ref-format "refs/heads/$branch" 2> /dev/null ||
|
|
342
|
+
die 2 usage "not a valid branch name: $branch"
|
|
208
343
|
|
|
209
|
-
|
|
344
|
+
resolve_main_root
|
|
345
|
+
worktree_path="$WORKTREE_DIR/$branch"
|
|
210
346
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
347
|
+
if git -C "$MAIN_ROOT" show-ref --verify --quiet "refs/heads/$branch"; then
|
|
348
|
+
die 3 branch-exists "branch $branch already exists"
|
|
349
|
+
fi
|
|
350
|
+
load_worktrees
|
|
351
|
+
i=0
|
|
352
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
353
|
+
if [ "${WT_PATHS[$i]}" = "$worktree_path" ]; then
|
|
354
|
+
die 3 target-exists "$worktree_path is already a registered worktree"
|
|
218
355
|
fi
|
|
219
|
-
|
|
356
|
+
i=$((i + 1))
|
|
357
|
+
done
|
|
358
|
+
if [ -L "$worktree_path" ] || { [ -e "$worktree_path" ] && [ ! -d "$worktree_path" ]; } ||
|
|
359
|
+
{ [ -d "$worktree_path" ] && [ -n "$(ls -A "$worktree_path")" ]; }; then
|
|
360
|
+
die 3 target-exists "$worktree_path already exists and is not empty"
|
|
220
361
|
fi
|
|
221
362
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
copy_env_files "$worktree_path"
|
|
240
|
-
|
|
241
|
-
# Trust dev tool configs (mise, direnv) so hooks and scripts work immediately.
|
|
242
|
-
# Long-lived integration branches can use themselves as the trust baseline,
|
|
243
|
-
# while review/PR branches fall back to the default branch and require manual
|
|
244
|
-
# direnv approval.
|
|
245
|
-
local default_branch
|
|
246
|
-
default_branch=$(get_default_branch)
|
|
247
|
-
local trust_branch="$default_branch"
|
|
248
|
-
local allow_direnv_auto="false"
|
|
249
|
-
if is_trusted_base_branch "$from_branch" "$default_branch"; then
|
|
250
|
-
trust_branch="$from_branch"
|
|
251
|
-
allow_direnv_auto="true"
|
|
363
|
+
if [ -n "$base_arg" ]; then
|
|
364
|
+
# Resolved in the caller's checkout, so HEAD means the caller's HEAD.
|
|
365
|
+
base_commit=$(git rev-parse --verify --quiet "$base_arg^{commit}" 2> /dev/null) ||
|
|
366
|
+
die 3 base-unresolved "--base $base_arg does not resolve locally; fetch it first"
|
|
367
|
+
base_ref="$base_arg"
|
|
368
|
+
# Local only: the default branch is needed just to pick the trust baseline.
|
|
369
|
+
DEFAULT_BRANCH=$(git -C "$MAIN_ROOT" symbolic-ref -q refs/remotes/origin/HEAD 2> /dev/null) || DEFAULT_BRANCH=""
|
|
370
|
+
DEFAULT_BRANCH="${DEFAULT_BRANCH#refs/remotes/origin/}"
|
|
371
|
+
else
|
|
372
|
+
resolve_default_branch
|
|
373
|
+
say "fetching origin $DEFAULT_BRANCH"
|
|
374
|
+
if ! status_out=$(git -C "$MAIN_ROOT" fetch --quiet --no-tags origin \
|
|
375
|
+
"+refs/heads/$DEFAULT_BRANCH:refs/remotes/origin/$DEFAULT_BRANCH" 2>&1 < /dev/null); then
|
|
376
|
+
die 1 fetch-failed "$(printf '%s\n' "$status_out" | head -n 1); pass --base <ref> to skip the fetch"
|
|
377
|
+
fi
|
|
378
|
+
base_ref="origin/$DEFAULT_BRANCH"
|
|
379
|
+
base_commit="$base_ref"
|
|
252
380
|
fi
|
|
253
381
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
382
|
+
ensure_ignore_file
|
|
383
|
+
say "creating $worktree_path from $base_ref"
|
|
384
|
+
git -C "$MAIN_ROOT" worktree add --quiet --no-track -b "$branch" "$worktree_path" "$base_commit" ||
|
|
385
|
+
die 1 git-failure "git worktree add failed"
|
|
386
|
+
|
|
387
|
+
status_out=$(git -C "$MAIN_ROOT" status --porcelain -- .worktrees) ||
|
|
388
|
+
die 1 git-failure "git status failed in $MAIN_ROOT"
|
|
389
|
+
if [ -n "$status_out" ]; then
|
|
390
|
+
die 1 not-ignored ".worktrees shows up in git status after create (worktree left at $worktree_path): $(printf '%s' "$status_out" | head -n 1)"
|
|
262
391
|
fi
|
|
263
392
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
393
|
+
copy_env_files "$worktree_path" > /dev/null
|
|
394
|
+
trust_after_create "$worktree_path" "$base_ref"
|
|
395
|
+
|
|
396
|
+
printf 'branch=%s\n' "$branch"
|
|
397
|
+
printf 'base=%s\n' "$base_ref"
|
|
398
|
+
printf 'path=%s\n' "$worktree_path"
|
|
269
399
|
}
|
|
270
400
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
echo -e "${BLUE}Available worktrees:${NC}"
|
|
274
|
-
echo ""
|
|
401
|
+
trust_after_create() {
|
|
402
|
+
local worktree_path="$1" base_ref="$2" base_name trust_branch allow="false"
|
|
275
403
|
|
|
276
|
-
if [
|
|
277
|
-
|
|
278
|
-
return
|
|
404
|
+
if [ -z "$DEFAULT_BRANCH" ]; then
|
|
405
|
+
say "skipping dev tool trust: default branch unknown"
|
|
406
|
+
return 0
|
|
279
407
|
fi
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
if [[ "$PWD" == "$worktree_path" ]]; then
|
|
289
|
-
echo -e "${GREEN}✓ $worktree_name${NC} (current) → branch: $branch"
|
|
290
|
-
else
|
|
291
|
-
echo -e " $worktree_name → branch: $branch"
|
|
292
|
-
fi
|
|
293
|
-
fi
|
|
294
|
-
done
|
|
295
|
-
|
|
296
|
-
if [[ $count -eq 0 ]]; then
|
|
297
|
-
echo -e "${YELLOW}No worktrees found${NC}"
|
|
408
|
+
base_name="${base_ref#origin/}"
|
|
409
|
+
trust_branch="$DEFAULT_BRANCH"
|
|
410
|
+
if is_trusted_base_branch "$base_name" "$DEFAULT_BRANCH"; then
|
|
411
|
+
trust_branch="$base_name"
|
|
412
|
+
allow="true"
|
|
413
|
+
fi
|
|
414
|
+
if git -C "$MAIN_ROOT" rev-parse --verify --quiet "origin/$trust_branch" > /dev/null 2>&1; then
|
|
415
|
+
trust_dev_tools "$worktree_path" "origin/$trust_branch" "$allow"
|
|
298
416
|
else
|
|
299
|
-
|
|
300
|
-
echo -e "${BLUE}Total: $count worktree(s)${NC}"
|
|
417
|
+
say "skipping dev tool trust: origin/$trust_branch not found locally"
|
|
301
418
|
fi
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
# ── list, switch, copy-env ──────────────────────────────────────────────────
|
|
302
422
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
423
|
+
# One line per worktree, tab-separated key=value fields. The main checkout is
|
|
424
|
+
# listed with name=(main).
|
|
425
|
+
list_worktrees() {
|
|
426
|
+
local i=0 name cwd current best=-1 best_len=0
|
|
427
|
+
resolve_main_root
|
|
428
|
+
load_worktrees
|
|
429
|
+
cwd=$(pwd -P)
|
|
430
|
+
# Every linked worktree under .worktrees is also inside the main root, so
|
|
431
|
+
# only the longest path containing the caller is current.
|
|
432
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
433
|
+
if path_within "$cwd" "${WT_PATHS[$i]}" && [ "${#WT_PATHS[$i]}" -gt "$best_len" ]; then
|
|
434
|
+
best="$i"
|
|
435
|
+
best_len="${#WT_PATHS[$i]}"
|
|
436
|
+
fi
|
|
437
|
+
i=$((i + 1))
|
|
438
|
+
done
|
|
439
|
+
i=0
|
|
440
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
441
|
+
name=$(worktree_name "${WT_PATHS[$i]}")
|
|
442
|
+
[ "$i" -eq 0 ] && name="(main)"
|
|
443
|
+
current="no"
|
|
444
|
+
[ "$i" -eq "$best" ] && current="yes"
|
|
445
|
+
printf 'name=%s\tbranch=%s\tcurrent=%s\tpath=%s\n' \
|
|
446
|
+
"${name:--}" "${WT_BRANCHES[$i]}" "$current" "${WT_PATHS[$i]}"
|
|
447
|
+
i=$((i + 1))
|
|
448
|
+
done
|
|
308
449
|
}
|
|
309
450
|
|
|
310
|
-
#
|
|
451
|
+
# Prints the path only. A script cannot change its caller's directory.
|
|
311
452
|
switch_worktree() {
|
|
312
|
-
|
|
453
|
+
[ -n "$1" ] || die 2 usage "switch needs a worktree name"
|
|
454
|
+
resolve_main_root
|
|
455
|
+
load_worktrees
|
|
456
|
+
find_by_name "$1"
|
|
457
|
+
[ "$FOUND_IDX" -ge 0 ] || unknown_worktree "$1"
|
|
458
|
+
[ -d "${WT_PATHS[$FOUND_IDX]}" ] ||
|
|
459
|
+
die 3 worktree-missing "${WT_PATHS[$FOUND_IDX]} is registered but its directory is gone"
|
|
460
|
+
printf '%s\n' "${WT_PATHS[$FOUND_IDX]}"
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
copy_env_to_worktree() {
|
|
464
|
+
local i=0 cwd best=-1 best_len=0
|
|
313
465
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
466
|
+
resolve_main_root
|
|
467
|
+
load_worktrees
|
|
468
|
+
if [ -n "$1" ]; then
|
|
469
|
+
find_by_name "$1"
|
|
470
|
+
[ "$FOUND_IDX" -ge 0 ] || unknown_worktree "$1"
|
|
471
|
+
else
|
|
472
|
+
cwd=$(pwd -P)
|
|
473
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
474
|
+
if [ -n "$(worktree_name "${WT_PATHS[$i]}")" ] &&
|
|
475
|
+
path_within "$cwd" "${WT_PATHS[$i]}" && [ "${#WT_PATHS[$i]}" -gt "$best_len" ]; then
|
|
476
|
+
best="$i"
|
|
477
|
+
best_len="${#WT_PATHS[$i]}"
|
|
478
|
+
fi
|
|
479
|
+
i=$((i + 1))
|
|
480
|
+
done
|
|
481
|
+
[ "$best" -ge 0 ] || die 2 usage "copy-env needs a worktree name unless run inside one"
|
|
482
|
+
FOUND_IDX="$best"
|
|
318
483
|
fi
|
|
484
|
+
[ -d "${WT_PATHS[$FOUND_IDX]}" ] ||
|
|
485
|
+
die 3 worktree-missing "${WT_PATHS[$FOUND_IDX]} is registered but its directory is gone"
|
|
486
|
+
copy_env_files "${WT_PATHS[$FOUND_IDX]}"
|
|
487
|
+
}
|
|
319
488
|
|
|
320
|
-
|
|
489
|
+
# ── cleanup ─────────────────────────────────────────────────────────────────
|
|
321
490
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
fi
|
|
491
|
+
# Sets C_STATE, C_MOD, C_UNT, C_IGN and C_REASON for the worktree at index $1.
|
|
492
|
+
# The first match wins: prunable, current, locked, contains-worktree, dirty,
|
|
493
|
+
# detached, clean.
|
|
494
|
+
classify() {
|
|
495
|
+
local idx="$1" p="${WT_PATHS[$1]}" status_out row j rc=0
|
|
328
496
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
497
|
+
C_MOD="n/a"
|
|
498
|
+
C_UNT="n/a"
|
|
499
|
+
C_IGN="n/a"
|
|
500
|
+
C_REASON=""
|
|
333
501
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if [[ -z "$worktree_name" ]]; then
|
|
340
|
-
# Check if we're currently in a worktree
|
|
341
|
-
local current_dir=$(pwd)
|
|
342
|
-
if [[ "$current_dir" == "$WORKTREE_DIR"/* ]]; then
|
|
343
|
-
worktree_path="$current_dir"
|
|
344
|
-
worktree_name=$(basename "$worktree_path")
|
|
345
|
-
echo -e "${BLUE}Detected current worktree: $worktree_name${NC}"
|
|
346
|
-
else
|
|
347
|
-
echo -e "${YELLOW}Usage: worktree-manager.sh copy-env [worktree-name]${NC}"
|
|
348
|
-
echo "Or run from within a worktree to copy to current directory"
|
|
349
|
-
list_worktrees
|
|
350
|
-
return 1
|
|
351
|
-
fi
|
|
352
|
-
else
|
|
353
|
-
worktree_path="$WORKTREE_DIR/$worktree_name"
|
|
502
|
+
if [ ! -d "$p" ]; then
|
|
503
|
+
C_STATE="prunable"
|
|
504
|
+
C_REASON="directory is missing; run git worktree prune yourself"
|
|
505
|
+
return 0
|
|
506
|
+
fi
|
|
354
507
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
508
|
+
status_out=$(git -C "$p" status --porcelain=v1 --untracked-files=all 2> /dev/null) || rc=$?
|
|
509
|
+
C_MOD=0
|
|
510
|
+
C_UNT=0
|
|
511
|
+
while IFS= read -r row; do
|
|
512
|
+
case "$row" in
|
|
513
|
+
"") ;;
|
|
514
|
+
"??"*) C_UNT=$((C_UNT + 1)) ;;
|
|
515
|
+
*) C_MOD=$((C_MOD + 1)) ;;
|
|
516
|
+
esac
|
|
517
|
+
done <<< "$status_out"
|
|
518
|
+
C_IGN=$(git -C "$p" ls-files -o -i --exclude-standard --directory 2> /dev/null | wc -l) || C_IGN=0
|
|
519
|
+
C_IGN=$((C_IGN + 0))
|
|
520
|
+
|
|
521
|
+
if path_within "$CLEANUP_CWD" "$p"; then
|
|
522
|
+
C_STATE="current"
|
|
523
|
+
C_REASON="your working directory is inside it"
|
|
524
|
+
return 0
|
|
525
|
+
fi
|
|
526
|
+
if [ "${WT_LOCKED[$idx]}" = "yes" ]; then
|
|
527
|
+
C_STATE="locked"
|
|
528
|
+
C_REASON="locked; unlock it first"
|
|
529
|
+
return 0
|
|
530
|
+
fi
|
|
531
|
+
j=0
|
|
532
|
+
while [ "$j" -lt "${#WT_PATHS[@]}" ]; do
|
|
533
|
+
if [ "$j" -ne "$idx" ] && [ "${WT_PATHS[$j]}" != "$p" ] && path_within "${WT_PATHS[$j]}" "$p"; then
|
|
534
|
+
C_STATE="contains-worktree"
|
|
535
|
+
C_REASON="contains another worktree: ${WT_PATHS[$j]}"
|
|
536
|
+
return 0
|
|
359
537
|
fi
|
|
538
|
+
j=$((j + 1))
|
|
539
|
+
done
|
|
540
|
+
if [ "$rc" -ne 0 ] || [ -n "$status_out" ]; then
|
|
541
|
+
C_STATE="dirty"
|
|
542
|
+
C_REASON="modified or untracked files"
|
|
543
|
+
return 0
|
|
544
|
+
fi
|
|
545
|
+
if [ "${WT_BRANCHES[$idx]}" = "(detached)" ]; then
|
|
546
|
+
C_STATE="detached"
|
|
547
|
+
C_REASON="detached HEAD; its commits may be unreachable"
|
|
548
|
+
return 0
|
|
360
549
|
fi
|
|
550
|
+
C_STATE="clean"
|
|
551
|
+
}
|
|
361
552
|
|
|
362
|
-
|
|
363
|
-
|
|
553
|
+
print_state() {
|
|
554
|
+
local idx="$1"
|
|
555
|
+
printf 'name=%s\tpath=%s\tbranch=%s\tstate=%s\tmodified=%s\tuntracked=%s\tignored=%s\n' \
|
|
556
|
+
"$(worktree_name "${WT_PATHS[$idx]}")" "${WT_PATHS[$idx]}" "${WT_BRANCHES[$idx]}" \
|
|
557
|
+
"$C_STATE" "$C_MOD" "$C_UNT" "$C_IGN"
|
|
364
558
|
}
|
|
365
559
|
|
|
366
|
-
#
|
|
560
|
+
# Report only, unless --remove names worktrees. Only clean ones are removed,
|
|
561
|
+
# with plain `git worktree remove`; branches are never deleted.
|
|
367
562
|
cleanup_worktrees() {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
563
|
+
local remove_mode=0 names=() name i refused=0 out
|
|
564
|
+
|
|
565
|
+
while [ $# -gt 0 ]; do
|
|
566
|
+
case "$1" in
|
|
567
|
+
--remove)
|
|
568
|
+
remove_mode=1
|
|
569
|
+
shift
|
|
570
|
+
while [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; do
|
|
571
|
+
names+=("$1")
|
|
572
|
+
shift
|
|
573
|
+
done
|
|
574
|
+
;;
|
|
575
|
+
*) die 2 usage "unknown argument for cleanup: $1" ;;
|
|
576
|
+
esac
|
|
577
|
+
done
|
|
578
|
+
if [ "$remove_mode" -eq 1 ] && [ "${#names[@]}" -eq 0 ]; then
|
|
579
|
+
die 2 usage "--remove needs at least one worktree name"
|
|
371
580
|
fi
|
|
372
581
|
|
|
373
|
-
|
|
374
|
-
|
|
582
|
+
resolve_main_root
|
|
583
|
+
load_worktrees
|
|
584
|
+
CLEANUP_CWD=$(pwd -P)
|
|
375
585
|
|
|
376
|
-
|
|
377
|
-
|
|
586
|
+
if [ "$remove_mode" -eq 0 ]; then
|
|
587
|
+
i=1
|
|
588
|
+
while [ "$i" -lt "${#WT_PATHS[@]}" ]; do
|
|
589
|
+
if [ -n "$(worktree_name "${WT_PATHS[$i]}")" ]; then
|
|
590
|
+
classify "$i"
|
|
591
|
+
print_state "$i"
|
|
592
|
+
fi
|
|
593
|
+
i=$((i + 1))
|
|
594
|
+
done
|
|
595
|
+
return 0
|
|
596
|
+
fi
|
|
378
597
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
598
|
+
# Unknown names fail before anything is removed.
|
|
599
|
+
for name in "${names[@]}"; do
|
|
600
|
+
find_by_name "$name"
|
|
601
|
+
[ "$FOUND_IDX" -ge 0 ] || unknown_worktree "$name"
|
|
602
|
+
done
|
|
382
603
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
604
|
+
for name in "${names[@]}"; do
|
|
605
|
+
find_by_name "$name"
|
|
606
|
+
i="$FOUND_IDX"
|
|
607
|
+
classify "$i"
|
|
608
|
+
if [ "$C_STATE" = "clean" ]; then
|
|
609
|
+
if out=$(git -C "$MAIN_ROOT" worktree remove "${WT_PATHS[$i]}" 2>&1 < /dev/null); then
|
|
610
|
+
printf 'result=removed\tname=%s\tpath=%s\n' "$name" "${WT_PATHS[$i]}"
|
|
386
611
|
continue
|
|
387
612
|
fi
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
to_remove+=("$worktree_path")
|
|
391
|
-
echo -e "${YELLOW}• $worktree_name${NC}"
|
|
613
|
+
C_STATE="refused-by-git"
|
|
614
|
+
C_REASON=$(printf '%s\n' "$out" | head -n 1)
|
|
392
615
|
fi
|
|
616
|
+
refused=1
|
|
617
|
+
printf 'result=refused\tname=%s\tstate=%s\tignored=%s\treason=%s\n' "$name" "$C_STATE" "$C_IGN" "$C_REASON"
|
|
393
618
|
done
|
|
619
|
+
[ "$refused" -eq 0 ] || exit 3
|
|
620
|
+
}
|
|
394
621
|
|
|
395
|
-
|
|
396
|
-
echo -e "${GREEN}No inactive worktrees to clean up${NC}"
|
|
397
|
-
return
|
|
398
|
-
fi
|
|
399
|
-
|
|
400
|
-
echo ""
|
|
401
|
-
echo -e "Remove $found worktree(s)? (y/n)"
|
|
402
|
-
read -r response
|
|
622
|
+
# ── Entry point ─────────────────────────────────────────────────────────────
|
|
403
623
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
fi
|
|
624
|
+
show_help() {
|
|
625
|
+
cat << 'EOF'
|
|
626
|
+
Git Worktree Manager
|
|
408
627
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
local worktree_name=$(basename "$worktree_path")
|
|
412
|
-
git worktree remove "$worktree_path" --force 2>/dev/null || true
|
|
413
|
-
echo -e "${GREEN}✓ Removed: $worktree_name${NC}"
|
|
414
|
-
done
|
|
628
|
+
Worktrees live at <main repository root>/.worktrees/<branch>. The script never
|
|
629
|
+
reads stdin and never changes your directory.
|
|
415
630
|
|
|
416
|
-
|
|
417
|
-
if [[ -z "$(ls -A "$WORKTREE_DIR" 2>/dev/null)" ]]; then
|
|
418
|
-
rmdir "$WORKTREE_DIR" 2>/dev/null || true
|
|
419
|
-
fi
|
|
631
|
+
Usage: worktree-manager.sh <command> [options]
|
|
420
632
|
|
|
421
|
-
|
|
633
|
+
Commands:
|
|
634
|
+
create <branch> [--base <ref>] New branch and worktree. Without --base it
|
|
635
|
+
fetches origin's default branch and branches
|
|
636
|
+
from origin/<default>. --base uses a local
|
|
637
|
+
ref and does not fetch. Last stdout lines:
|
|
638
|
+
branch=, base=, path=.
|
|
639
|
+
list | ls One line per worktree.
|
|
640
|
+
switch | go <name> Print the worktree's path. It cannot change
|
|
641
|
+
the caller's directory: run commands with
|
|
642
|
+
that path as the working directory.
|
|
643
|
+
copy-env | env [name] Copy git-ignored .env* files from the main
|
|
644
|
+
checkout. Never overwrites. Without a name,
|
|
645
|
+
uses the worktree you are inside.
|
|
646
|
+
cleanup | clean Report each worktree under .worktrees with
|
|
647
|
+
state, modified, untracked and ignored counts.
|
|
648
|
+
cleanup --remove <name>... Remove the named worktrees that are clean.
|
|
649
|
+
Removal deletes their ignored files (.env,
|
|
650
|
+
node_modules). Other states are refused.
|
|
651
|
+
Branches are never deleted.
|
|
652
|
+
help Show this help.
|
|
653
|
+
|
|
654
|
+
Exit codes: 0 ok, 1 error, 2 usage, 3 refused (needs a decision).
|
|
655
|
+
Errors print `error: <code>: <message>` on stderr.
|
|
656
|
+
EOF
|
|
422
657
|
}
|
|
423
658
|
|
|
424
|
-
# Main command handler
|
|
425
659
|
main() {
|
|
426
660
|
local command="${1:-list}"
|
|
661
|
+
[ $# -eq 0 ] || shift
|
|
427
662
|
|
|
428
663
|
case "$command" in
|
|
429
|
-
create)
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
switch|go)
|
|
436
|
-
switch_worktree "$2"
|
|
437
|
-
;;
|
|
438
|
-
copy-env|env)
|
|
439
|
-
copy_env_to_worktree "$2"
|
|
440
|
-
;;
|
|
441
|
-
cleanup|clean)
|
|
442
|
-
cleanup_worktrees
|
|
443
|
-
;;
|
|
444
|
-
help)
|
|
445
|
-
show_help
|
|
446
|
-
;;
|
|
664
|
+
create) create_worktree "$@" ;;
|
|
665
|
+
list | ls) list_worktrees ;;
|
|
666
|
+
switch | go) switch_worktree "$1" ;;
|
|
667
|
+
copy-env | env) copy_env_to_worktree "$1" ;;
|
|
668
|
+
cleanup | clean) cleanup_worktrees "$@" ;;
|
|
669
|
+
help | -h | --help) show_help ;;
|
|
447
670
|
*)
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
show_help
|
|
451
|
-
exit 1
|
|
671
|
+
show_help >&2
|
|
672
|
+
die 2 usage "unknown command: $command"
|
|
452
673
|
;;
|
|
453
674
|
esac
|
|
454
675
|
}
|
|
455
676
|
|
|
456
|
-
show_help() {
|
|
457
|
-
cat << EOF
|
|
458
|
-
Git Worktree Manager
|
|
459
|
-
|
|
460
|
-
Usage: worktree-manager.sh <command> [options]
|
|
461
|
-
|
|
462
|
-
Commands:
|
|
463
|
-
create <branch-name> [from-branch] Create new worktree (copies .env files automatically)
|
|
464
|
-
(from-branch defaults to main)
|
|
465
|
-
list | ls List all worktrees
|
|
466
|
-
switch | go [name] Switch to worktree
|
|
467
|
-
copy-env | env [name] Copy .env files from main repo to worktree
|
|
468
|
-
(if name omitted, uses current worktree)
|
|
469
|
-
cleanup | clean Clean up inactive worktrees
|
|
470
|
-
help Show this help message
|
|
471
|
-
|
|
472
|
-
Environment Files:
|
|
473
|
-
- Automatically copies .env, .env.local, .env.test, etc. on create
|
|
474
|
-
- Skips .env.example (should be in git)
|
|
475
|
-
- Creates .backup files if destination already exists
|
|
476
|
-
- Use 'copy-env' to refresh env files after main repo changes
|
|
477
|
-
|
|
478
|
-
Dev Tool Trust:
|
|
479
|
-
- Trusts mise config (.mise.toml, mise.toml, .tool-versions) and direnv (.envrc)
|
|
480
|
-
- Uses trusted base branches directly (main, develop, dev, trunk, staging, release/*)
|
|
481
|
-
- Other branches fall back to the default branch as the trust baseline
|
|
482
|
-
- direnv auto-allow is skipped on non-trusted base branches; review manually first
|
|
483
|
-
- Modified configs are flagged for manual review
|
|
484
|
-
- Only runs if the tool is installed and config exists
|
|
485
|
-
- Prevents hooks/scripts from hanging on interactive trust prompts
|
|
486
|
-
|
|
487
|
-
Examples:
|
|
488
|
-
worktree-manager.sh create feature-login
|
|
489
|
-
worktree-manager.sh create feature-auth develop
|
|
490
|
-
worktree-manager.sh switch feature-login
|
|
491
|
-
worktree-manager.sh copy-env feature-login
|
|
492
|
-
worktree-manager.sh copy-env # copies to current worktree
|
|
493
|
-
worktree-manager.sh cleanup
|
|
494
|
-
worktree-manager.sh list
|
|
495
|
-
|
|
496
|
-
EOF
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
# Run
|
|
500
677
|
main "$@"
|