6 +const WT_REPO_NAME =
"wt.repo-name" 7 +const WT_MAIN_BRANCH =
"wt.main-branch" 8 +const WT_SOURCE_PATH =
"wt.source-path" 11 +def gitx [args: list<any>, cwd?: path] {
12 + let res =
if (
$cwd == null) {
13 + ^git ...
$args | complete
15 + ^git -C
$cwd ...
$args | complete
18 + if $res.exit_code !=
0 {
19 + let msg = (
$res.stderr | str trim)
20 + error make {msg: (
if (
$msg | is-empty) { $
"git failed: git ($args | str join ' ')" }
else {
$msg })}
23 + $res.stdout | str trim
27 +def gitc [args: list<any>, cwd?: path] {
29 + ^git ...
$args | complete
31 + ^git -C
$cwd ...
$args | complete
36 +def branch-slug [branch: string] {
38 + | str replace --all --regex
'[^A-Za-z0-9._-]+' '-' 39 + | str trim --char
'-' 43 +def repo-name-from [
source: string] {
44 + let base = (
$source | path basename)
45 + $base | str replace --regex
'\.git$' '' 50 + let common = (gitx [
"rev-parse" "--path-format=absolute" "--git-common-dir"])
51 + let bare = (gitc [
"--git-dir" $common "config" "--bool" "--get" "core.bare"])
53 + if ((
$bare.stdout | str trim) !=
"true") {
54 + error make {msg:
"not inside co/wt layout: git common dir is not bare"}
57 + let root = (
$common | path dirname)
58 + let repo_res = (gitc [
"--git-dir" $common "config" "--get" $WT_REPO_NAME])
59 + let repo =
if $repo_res.exit_code ==
0 {
60 + $repo_res.stdout | str trim
62 + $root | path basename | str replace --regex
'\.wt$' '' 65 + let main_res = (gitc [
"--git-dir" $common "config" "--get" $WT_MAIN_BRANCH])
66 + let main =
if $main_res.exit_code ==
0 {
$main_res.stdout | str trim }
else {
"main" }
68 + {root:
$root, git_dir:
$common, repo:
$repo, main:
$main}
72 +def wt-path [branch: string] {
74 + [
$l.root $
"($l.repo).(branch-slug $branch)"] | path join
78 +def parse-worktree-list [text: string] {
81 + |
where {|block| not ((
$block | str trim) | is-empty) }
83 + mut rec = {path: null, head: null, branch: null, detached: false, bare: false, locked: false, lock_reason: null, prunable: false}
85 + for line
in (
$block | lines) {
86 + if (
$line | str starts-with
"worktree ") {
87 + $rec = (
$rec | upsert path (
$line | str replace
"worktree " ""))
88 + }
else if (
$line | str starts-with
"HEAD ") {
89 + $rec = (
$rec | upsert head (
$line | str replace
"HEAD " ""))
90 + }
else if (
$line | str starts-with
"branch ") {
91 + let b = (
$line | str replace
"branch " "" | str replace
"refs/heads/" "")
92 + $rec = (
$rec | upsert branch
$b)
93 + }
else if $line ==
"detached" {
94 + $rec = (
$rec | upsert detached true)
95 + }
else if $line ==
"bare" {
96 + $rec = (
$rec | upsert bare true)
97 + }
else if (
$line | str starts-with
"locked") {
98 + let reason = (
$line | str replace
"locked" "" | str trim)
99 + $rec = (
$rec | upsert locked true | upsert lock_reason (
if (
$reason | is-empty) { null }
else {
$reason }))
100 + }
else if (
$line | str starts-with
"prunable") {
101 + $rec = (
$rec | upsert prunable true)
110 +def worktrees-basic [] {
112 + parse-worktree-list (gitx [
"--git-dir" $l.git_dir "worktree" "list" "--porcelain"])
116 +def path-
for-branch [branch: string] {
117 + let found = (worktrees-basic |
where branch ==
$branch | first)
118 + if (
$found == null) { wt-path
$branch }
else {
$found.path }
122 +def create-worktree [branch: string, base: string] {
124 + let path = (wt-path
$branch)
126 + if (
$path | path exists) {
127 + error make {msg: $
"path exists: ($path)"}
130 + let exists = ((gitc [
"--git-dir" $l.git_dir "show-ref" "--verify" "--quiet" $
"refs/heads/($branch)"]).exit_code ==
0)
133 + gitx [
"--git-dir" $l.git_dir "worktree" "add" $path $branch] | ignore
135 + gitx [
"--git-dir" $l.git_dir "worktree" "add" "-b" $branch $path $base] | ignore
142 +def current-branch [] {
143 + let b = (gitx [
"rev-parse" "--abbrev-ref" "HEAD"])
144 + if $b ==
"HEAD" { error make {msg:
"detached HEAD is not supported"} }
149 +def --env remove-worktree [branch: string, delete_branch: bool, force: bool] {
151 + let target = (path-
for-branch
$branch | path expand)
152 + let cwd_top = (try { gitx [
"rev-parse" "--show-toplevel"] } catch {
"" } | path expand)
154 + if $target ==
$cwd_top {
155 + if $branch ==
$l.main {
158 + let main_path = (path-
for-branch
$l.main)
159 + if (
$main_path | path exists) { cd
$main_path }
else { cd
$l.root }
163 + let remove_args =
if $force {
164 + [
"--git-dir" $l.git_dir "worktree" "remove" "--force" $target]
166 + [
"--git-dir" $l.git_dir "worktree" "remove" $target]
168 + gitx
$remove_args | ignore
170 + if $delete_branch {
171 + let branch_flag =
if $force {
"-D" }
else {
"-d" }
172 + gitx [
"--git-dir" $l.git_dir "branch" $branch_flag $branch] | ignore
179 +def --env co-fresh [
source: string, name?: string, branch?: string, dir?: path] {
180 + let repo =
if (
$name == null) { repo-name-from
$source }
else {
$name }
181 + let parent =
if (
$dir == null) {
"." }
else {
$dir }
182 + let root = ([
$parent $repo] | path join | path expand)
183 + let git_dir = ([
$root ".git"] | path join)
185 + if (
$root | path exists) {
186 + error make {msg: $
"target exists: ($root)"}
190 + gitx [
"clone" "--bare" $source $git_dir] | ignore
192 + let chosen_branch =
if ((
$branch == null) or (
$branch | is-empty)) {
193 + gitx [
"--git-dir" $git_dir "symbolic-ref" "--short" "HEAD"]
198 + gitx [
"--git-dir" $git_dir "config" $WT_REPO_NAME $repo] | ignore
199 + gitx [
"--git-dir" $git_dir "config" $WT_MAIN_BRANCH $chosen_branch] | ignore
201 + let worktree = ([
$root $
"($repo).(branch-slug $chosen_branch)"] | path join)
202 + gitx [
"--git-dir" $git_dir "worktree" "add" $worktree $chosen_branch] | ignore
205 + {root:
$root, worktree:
$worktree, branch:
$chosen_branch}
209 +def --env co-current [name?: string, branch?: string, plan: bool = false, dirty: string =
"refuse"] {
210 + let src = (gitx [
"rev-parse" "--show-toplevel"] | path expand)
211 + let src_name = (
$src | path basename)
212 + let repo =
if (
$name == null) {
$src_name }
else {
$name }
213 + let parent = (
$src | path dirname)
214 + let root = ([
$parent $
"($repo).wt"] | path join | path expand)
215 + let git_dir = ([
$root ".git"] | path join)
216 + let current = (current-branch)
217 + let chosen_branch =
if ((
$branch == null) or (
$branch | is-empty)) {
$current }
else {
$branch }
218 + let target = ([
$root $
"($repo).(branch-slug $chosen_branch)"] | path join)
219 + let status_before = (gitx [
"status" "--porcelain=v1"]
$src)
220 + let is_dirty = (not (
$status_before | is-empty))
221 + let stash_res = (gitc [
"stash" "list"]
$src)
222 + let stash_count =
if $stash_res.exit_code ==
0 { (
$stash_res.stdout | lines | length) }
else {
0 }
224 + if not (
$dirty in [
"refuse" "copy"]) {
225 + error make {msg:
"--dirty must be one of: refuse, copy"}
229 + mode:
"current-checkout" 232 + target_git_dir:
$git_dir 233 + target_worktree:
$target 235 + branch:
$chosen_branch 238 + stashes:
$stash_count 242 + if $plan { return
$migration }
244 + if (
$root | path exists) {
245 + error make {msg: $
"target exists: ($root)"}
248 + if $is_dirty and
$dirty ==
"refuse" {
249 + error make {msg:
"working tree has changes; rerun with `co . --dirty copy` or commit/stash first"}
252 + if $stash_count >
0 {
253 + print -e $
"warning: ($stash_count) stash(es) remain only in original checkout: ($src)" 256 + let tmp = (mktemp -d)
257 + let staged_patch = ([
$tmp "staged.patch"] | path join)
258 + let unstaged_patch = ([
$tmp "unstaged.patch"] | path join)
261 + ^git -C
$src diff --binary --cached | save --force
$staged_patch 262 + ^git -C
$src diff --binary | save --force
$unstaged_patch 266 + gitx [
"clone" "--bare" $src $git_dir] | ignore
267 + gitx [
"--git-dir" $git_dir "config" $WT_REPO_NAME $repo] | ignore
268 + gitx [
"--git-dir" $git_dir "config" $WT_MAIN_BRANCH $chosen_branch] | ignore
269 + gitx [
"--git-dir" $git_dir "config" $WT_SOURCE_PATH $src] | ignore
270 + gitx [
"--git-dir" $git_dir "worktree" "add" $target $chosen_branch] | ignore
273 + if (not ((open --raw
$staged_patch) | is-empty)) {
274 + gitx [
"apply" "--index" $staged_patch]
$target | ignore
276 + if (not ((open --raw
$unstaged_patch) | is-empty)) {
277 + gitx [
"apply" $unstaged_patch]
$target | ignore
280 + let rsync = (which rsync | length) >
0 282 + gitx [
"-C" $src "status"] | ignore
283 + ^rsync -a --ignore-existing --exclude .git $
"($src)/" $
"($target)/" | complete | ignore
285 + ^cp -a -n $
"($src)/." $target | complete | ignore
288 + let status_after = (gitx [
"status" "--porcelain=v1"]
$target)
289 + if $status_before !=
$status_after {
290 + error make {msg: $
"dirty-copy verification failed; original untouched at ($src), target left for inspection at ($root)"}
301 +export def --env co [
302 + source: string =
"." 304 + --branch (-b): string =
"" 305 + --dir (-d): path =
"." 307 + --dirty: string =
"refuse" 309 + if $source ==
"." {
310 + co-current
$name $branch $plan $dirty 312 + if $plan { error make {msg:
"--plan only applies to `co .`"} }
313 + if $dirty !=
"refuse" { error make {msg:
"--dirty only applies to `co .`"} }
314 + co-fresh
$source $name $branch $dir 319 +export def wt [] { wt ls }
321 +export def "wt root" [] { (layout).root }
322 +export def "wt repo" [] { (layout).repo }
323 +export def "wt path" [branch: string] { wt-path
$branch }
325 +export def "wt ls" [] {
326 + let cwd_top = (try { gitx [
"rev-parse" "--show-toplevel"] } catch {
"" } | path expand)
328 + worktrees-basic |
where bare == false |
each {|row|
329 + let p = (
$row.path | path expand)
330 + let dirty =
if ((
$p | path exists) and (not
$row.bare) and (
$row.branch != null)) {
331 + not ((gitx [
"status" "--porcelain=v1"]
$p) | is-empty)
337 + | upsert current (
$p ==
$cwd_top)
338 + | upsert dirty
$dirty 342 +export def --env
"wt new" [
344 + --base (-b): string =
"HEAD" 347 + let p = (create-worktree
$branch $base)
348 + if $switch { cd
$p }
352 +export def --env
"wt go" [
355 + --base (-b): string =
"HEAD" 357 + let p = (path-
for-branch
$branch)
358 + if (not (
$p | path exists)) {
360 + create-worktree
$branch $base | ignore
362 + error make {msg: $
"no worktree for branch `($branch)`; rerun with --create"}
369 +export def --env
"wt rm" [
374 + let b =
if (
$name == null) { current-branch }
else {
$name }
375 + remove-worktree
$b $branch $force 378 +export def --env
"wt done" [
382 + let b =
if (
$branch == null) { current-branch }
else {
$branch }
383 + remove-worktree
$b true
$force 386 +export def "wt prune" [] {
388 + gitx [
"--git-dir" $l.git_dir "worktree" "prune"] | ignore
391 +export def "wt repair" [] {
393 + gitx [
"--git-dir" $l.git_dir "worktree" "repair"] | ignore
397 +export def --env
"wt add" [branch: string, --base (-b): string =
"HEAD", --switch (-s)] {
398 + wt new
$branch --base
$base --switch=
$switch 401 +export def "wt list" [] { wt ls }
403 +export def "wt lock" [branch?: string, reason?: string] {
404 + let b =
if (
$branch == null) { current-branch }
else {
$branch }
405 + let p = (path-
for-branch
$b)
407 + if (
$reason == null) {
408 + gitx [
"--git-dir" $l.git_dir "worktree" "lock" $p] | ignore
410 + gitx [
"--git-dir" $l.git_dir "worktree" "lock" "--reason" $reason $p] | ignore
414 +export def "wt unlock" [branch?: string] {
415 + let b =
if (
$branch == null) { current-branch }
else {
$branch }
416 + let p = (path-
for-branch
$b)
418 + gitx [
"--git-dir" $l.git_dir "worktree" "unlock" $p] | ignore
421 +export def "wt move" [branch: string, dest: path] {
422 + let p = (path-
for-branch
$branch)
424 + gitx [
"--git-dir" $l.git_dir "worktree" "move" $p $dest] | ignore
428 +export def --env
"wt remove" [name?: string, --branch (-b), --force (-f)] {
429 + wt rm
$name --branch=
$branch --force=
$force