Repository navigation
Expand file tree
/
Copy pathworktree-setup.sh
More file actions
executable file
·124 lines (118 loc) · 6.57 KB
/
Copy pathworktree-setup.sh
File metadata and controls
executable file
·124 lines (118 loc) · 6.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
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
119
120
121
122
123
124
#!/bin/bash
# Sets up a thread's own worktree of Threadbear (docs/thread-worktrees.md):
# node_modules for the repo root (the Convex functions), the runner and
# the mock runner. The runner runs it in
# the new worktree before the thread's first turn
# (.threadbear/patches/checks.ts); it's safe to run again.
#
# Where the main checkout has the same package-lock.json and has installed
# it, node_modules is a symlink to the main checkout's: a moment, rather
# than an npm ci of some 350 MB for every thread. The symlinks are
# gitignored (.gitignore's node_modules matches them). Otherwise, or if the
# main checkout has nothing installed, it's a real npm ci here.
#
# A thread that changes a package.json or package-lock.json replaces the
# link with its own install first: `rm runner/node_modules` (no trailing
# slash: that removes the link, not the main checkout's files), then
# `npm ci` there. Never npm install through the link.
set -euo pipefail
# THREADBARE_* was the old name for these variables: each stands in for its new name.
for v in $(compgen -e | grep '^THREADBARE_' || true); do n="THREADBEAR_${v#THREADBARE_}"; [ -n "${!n+x}" ] || export "$n=${!v}"; done
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Node: THREADBEAR_NODE, else the node on PATH, else nvm's newest
# Node 24 or newer, else Homebrew's (runner/scripts/find-node.sh).
. "$ROOT/runner/scripts/find-node.sh"
NODE="$(threadbear_find_node)" || exit 1
NPM="$(dirname "$NODE")/npm"
[ -x "$NPM" ] || NPM="npm"
# The checkout with main checked out, from git's own list.
MAIN="$(git -C "$ROOT" worktree list --porcelain | awk '
/^worktree / { path = substr($0, 10) }
/^branch refs\/heads\/main$/ && !found { print path; found = 1 }
')"
# Brings the main checkout's install in `$1` up to its package-lock.json
# when git has moved the lock on since (npm writes node_modules/
# .package-lock.json on every install). Nothing else ever does: on 29 Oct
# every thread was linking to a runner/node_modules a day behind its lock,
# without @sentry/node. An install in place, not `npm ci`, as other
# threads' worktrees link to it and ci empties the folder first; a lock
# folder keeps two setups from installing at once. Never with anything
# uncommitted there, as npm runs a package.json's scripts: an agent that may
# only edit files would have this run them, with nobody asked (Codex's review,
# 7 Nov). This thread's worktree gets its own install from what's committed.
refresh_main() {
local there="$1" lock="$MAIN/.git/threadbear-npm.lock" tries=0
[ "$there/package-lock.json" -nt "$there/node_modules/.package-lock.json" ] || return 0
until mkdir "$lock" 2>/dev/null; do
tries=$((tries + 1))
[ "$tries" -lt 300 ] || { echo "$there: waited 5 minutes for another setup's npm install" >&2; return 1; }
sleep 1
done
if [ "$there/package-lock.json" -nt "$there/node_modules/.package-lock.json" ]; then
# The whole checkout, not a list of what npm reads (binding.gyp is on it
# too), and a git that can't say counts as not clean.
local changes
if ! changes="$(git -C "$MAIN" status --porcelain)" || [ -n "$changes" ] \
|| [ -n "$(git -C "$MAIN" status --porcelain --ignored -- .npmrc "$there/.npmrc")" ]; then
echo "${there#"$MAIN"/}: the main checkout has uncommitted changes, so no npm install there"
rmdir "$lock"
return 1
fi
echo "${there#"$MAIN"/}: the main checkout's node_modules is older than its lock; npm install there"
(cd "$there" && "$NPM" install --no-audit --no-fund --loglevel=error) || { rmdir "$lock"; return 1; }
fi
rmdir "$lock"
}
for dir in . runner mock-runner; do
here="$ROOT/$dir"
there="${MAIN:+$MAIN/$dir}"
if [ -e "$here/node_modules" ] || [ -L "$here/node_modules" ]; then
echo "$dir: node_modules is already there"
elif [ -n "$there" ] && [ "$MAIN" != "$ROOT" ] && [ -d "$there/node_modules" ] \
&& cmp -s "$here/package-lock.json" "$there/package-lock.json" && refresh_main "$there"; then
ln -s "$there/node_modules" "$here/node_modules"
echo "$dir: node_modules linked to $there/node_modules"
else
echo "$dir: npm ci (nothing matching in the main checkout to link to)"
(cd "$here" && "$NPM" ci --no-audit --no-fund --loglevel=error)
fi
done
# The Swift app's freeze (scripts/swift-freeze.sh) as git's pre-commit hook,
# so a commit to app/ or dock/ is refused in Mike's copy (and, once the GPUI
# app is out, warned about in other people's) whichever agent makes it and
# however it wrote the files. Hooks live in the shared .git, so this one
# file covers the main checkout and every worktree, each running its own
# copy of the script. Someone else's pre-commit hook, or a core.hooksPath of
# their own, is left alone.
hooks="$(git -C "$ROOT" rev-parse --path-format=absolute --git-common-dir)/hooks"
if [ ! -f "$ROOT/scripts/swift-freeze-pre-commit" ]; then
: # a copy without the freeze (a test's bare worktree): nothing to hook in
elif [ -n "$(git -C "$ROOT" config core.hooksPath || true)" ]; then
echo "pre-commit: core.hooksPath is set, so the Swift app's freeze isn't hooked in"
elif [ -e "$hooks/pre-commit" ] && ! grep -q threadbear-swift-freeze "$hooks/pre-commit"; then
echo "pre-commit: there's already a hook, so the Swift app's freeze isn't hooked in"
else
mkdir -p "$hooks"
# Copied, not linked, as this worktree may go; rewritten each time, so a
# newer stub replaces an older one.
cp "$ROOT/scripts/swift-freeze-pre-commit" "$hooks/pre-commit"
chmod +x "$hooks/pre-commit"
fi
# docs/experiments.md merges line by line (.gitattributes' merge=lines,
# scripts/merge-lines.mjs), so a rebase in `tb land` neither conflicts on
# two threads' new experiments nor brings back one either side took out,
# as union merging did (17 Nov). Git's config is shared by every worktree,
# so this covers them all; rewritten each time, so it follows node moving.
# A relative script path, as git runs a merge driver from the worktree's
# top, and each worktree uses its own copy.
if [ -f "$ROOT/scripts/merge-lines.mjs" ]; then
git -C "$ROOT" config merge.lines.name "one-line entries, in any order (scripts/merge-lines.mjs)"
git -C "$ROOT" config merge.lines.driver "'$NODE' scripts/merge-lines.mjs %O %A %B"
fi
# eslint-suppressions.json merges by count the same way
# (scripts/merge-suppressions.mjs), so two landings that each fix or record
# lint problems don't stop each other on a text conflict.
if [ -f "$ROOT/scripts/merge-suppressions.mjs" ]; then
git -C "$ROOT" config merge.suppressions.name "lint suppressions, by count (scripts/merge-suppressions.mjs)"
git -C "$ROOT" config merge.suppressions.driver "'$NODE' scripts/merge-suppressions.mjs %O %A %B"
fi