#!/usr/bin/env nodeimportfsfrom'node:fs';importpathfrom'node:path';import{ spawnSync }from'node:child_process';import{ fileURLToPath }from'node:url';import{ parseArgs, readJson, writeJsonAtomic, withLock, appendEvent, stateDir, fail }from'./lib.mjs';constNAME=/^[a-z0-9]+(?:-_
res args cwd
res
res allowFail
res res res
cwd process
root cwd
registryFile pathroot
registryLock pathroot
baseDir pathroot
registryLock
data registryFile
result data
registryFile data
result
gitDir root
exclude pathroot gitDir
fspathexclude
current fsexclude fsexclude
wanted currentline
wanted fsexclude
name name
name
changed target
changed
src pathroot
dest pathtarget
fssrc
fsdest
fssrc dest process
wt registryFile name
wt
wt
commands
name
base flags flags flags
baseSha base root
target pathbaseDir name
branch
fstarget
branch target baseSha root
dataname target branch base baseSha
deps flags target
root name branch baseSha
name target branch baseSha deps
names
sources names n n
s sources
dirty fss s
dirty
base flags flags flags
baseSha base root
name flags flags flags
target pathbaseDir name
branch
fstarget
branch target baseSha root
merged
conflict
s sources
res s target
res
mergeds
files target
target
conflict s s files merged
deps
lockChanges target baseSha
conflict flags
deps lockChanges target
gates
conflict flags deps
res process pathpath target
gates res res
verdict conflict gates
record name target branch baseSha names merged conflict deps gates verdict
dataname target branch base baseSha names record
pathroot record
root name merged conflictwith verdict
conflict
processverdict verdict
data registryFile
live root
entries data
entries
name wt entries
exists live live
dirty exists wt
ahead exists wt
name
wt registryFile name
wt
wt
dirty wt
name
wt registryFile name
wt
fswt
dirty wt
dirty flags
flags wt root
flags
res wt root
res
dataname
root name flags
root
pruned
gone data fsdatan
n gone datan
gone
pruned
positional flags process
command commandspositional
command
processpositional
positional flags
`# ${name}: committed changes since ${wt.baseSha.slice(0,10)}`
)
;
console
.
log
(
git
(
[
'diff'
,
'--stat'
,
`${wt.baseSha}...HEAD`
]
,
.
path
)
.
out
||
'(none)'
)
;
const
=
git
(
[
'status'
,
'--short'
]
,
.
path
)
.
out
;
console
.
log
(
`\n# uncommitted\n${dirty ||'(clean)'}`
)
;
}
,
remove
(
[name], flags
)
{
requireName
(
)
;
const
=
readJson
(
,
{
worktrees
:
{
}
}
)
.
worktrees
[
]
;
if
(
!
)
fail
(
`Unknown worktree ${name}`
)
;
if
(
.
existsSync
(
.
path
)
)
{
const
=
git
(
[
'status'
,
'--porcelain'
]
,
.
path
)
.
out
;
if
(
&&
!
.
force
)
fail
(
`Worktree ${name} has uncommitted changes. Commit/stash them or pass --force (approval required).`
)
;
git
(
[
'worktree'
,
'remove'
,
...
(
.
force
?
[
'--force'
]
:
[
]
)
,
.
path
]
,
)
;
}
if
(
[
'delete-branch'
]
)
{
const
=
git
(
[
'branch'
,
'-d'
,
.
branch
]
,
,
{
allowFail
:
true
}
)
;
console
.
log
(
.
ok
?
`Deleted merged branch ${wt.branch}`
:
`Kept branch ${wt.branch}: not merged (${res.err})`
)
;
}
registry
(
(
data
)
=>
{
delete
.
worktrees
[
]
;
}
)
;
appendEvent
(
,
{
type
:
'worktree.removed'
,
,
force
:
Boolean
(
.
force
)
}
)
;
console
.
log
(
`Removed worktree ${name}`
)
;
}
,
prune
(
)
{
git
(
[
'worktree'
,
'prune'
]
,
)
;
const
=
registry
(
(
data
)
=>
{
const
=
Object
.
keys
(
.
worktrees
)
.
filter
(
(
n
)
=>
!
.
existsSync
(
.
worktrees
[
]
.
path
)
)
;
for
(
const
of
)
delete
.
worktrees
[
]
;
return
;
}
)
;
console
.
log
(
.
length
?
`Pruned: ${pruned.join(', ')}`
:
'Nothing to prune.'
)
;
}
,
}
;
const
HELP
=
`Usage: node worktree.mjs <command>
create <name> [--base <ref>] [--link-deps]
new branch agent/<name> in .agent-worktrees/<name>;
--link-deps symlinks the root node_modules
integrate <name>... [--base <ref>] [--deps link|none] [--no-gates] [--name <slug>]
merge agent branches into a fresh integration worktree,
stop on first conflict, then run gates there
list status of every agent worktree
diff <name> committed + uncommitted changes vs base
remove <name> [--force] [--delete-branch]
refuses dirty trees without --force; deletes branch only if merged
prune drop registry entries for vanished worktrees
Merging stays a human/reviewer decision: review "diff", then merge the branch with git.`