6 -const WT_REPO_NAME =
"wt.repo-name" 7 6 const WT_MAIN_BRANCH =
"wt.main-branch" 8 -const WT_SOURCE_PATH =
"wt.source-path" 10 8 const DOT_GIT =
".git" 11 -const WT_ROOT_SUFFIX =
".wt" 12 10 const DEFAULT_MAIN_BRANCH =
"main" 13 11 const HEAD =
"HEAD" 14 12 const REF_HEADS =
"refs/heads/" 47 +def choose-recovery [message: string, choices: list<record>, non_interactive: string] {
48 + if not
$nu.is-interactive { error make {msg:
$non_interactive} }
52 + let selected = (
$choices | input list --display label
"choose")
53 + if $selected == null or
$selected.id ==
"cancel" { error make {msg:
"cancelled"} }
57 +def remove-needs-force [message: string] {
58 + let modified = (
$message | str contains
"contains modified or untracked files")
59 + let submodules = (
$message | str contains
"working trees containing submodules")
60 + let dirty = (
$message | str contains
"is dirty")
61 + let not_clean = (
$message | str contains
"not clean")
62 + $modified or
$submodules or
$dirty or
$not_clean 50 66 def git-output [args: list<string>, cwd?: path] {
51 67 let res =
if $cwd == null { git-complete
$args }
else { git-complete
$args $cwd }
83 99 if $res.exit_code ==
0 {
$res.stdout | str trim }
else { null }
102 +def git-config-values [git_dir: path, key: string] {
103 + let res = (git-dir-complete
$git_dir [
"config" "--get-all" $key])
104 + if $res.exit_code ==
0 {
$res.stdout | lines }
else { [] }
86 107 def git-config-bool [git_dir: path, key: string] {
87 108 let res = (git-dir-complete
$git_dir [
"config" "--bool" "--get" $key])
88 109 if $res.exit_code ==
0 {
$res.stdout | str trim }
else { null }
92 113 git-dir-output
$git_dir [
"config" $key $value] | ignore
116 +def git-config-unset [git_dir: path, key: string] {
117 + git-dir-complete
$git_dir [
"config" "--unset" $key] | ignore
120 +def ensure-upstream [git_dir: path, branch: string] {
121 + if (git-config
$git_dir "remote.origin.url") == null { return }
123 + if (git-config
$git_dir "remote.origin.fetch") == null {
124 + git-config-set
$git_dir "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" 127 + if (git-config
$git_dir $
"branch.($branch).remote") == null {
128 + git-config-set
$git_dir $
"branch.($branch).remote" "origin" 129 + git-config-set
$git_dir $
"branch.($branch).merge" $
"refs/heads/($branch)" 132 + if (git-dir-complete
$git_dir [
"show-ref" "--verify" "--quiet" $
"refs/heads/($branch)"]).exit_code ==
0 {
133 + git-dir-output
$git_dir [
"update-ref" $
"refs/remotes/origin/($branch)" $
"refs/heads/($branch)"] | ignore
96 138 def branch-slug [branch: string] {
115 157 let root = (
$git_dir | path dirname)
116 - let repo = (git-config
$git_dir $WT_REPO_NAME | default (
$root | path basename | str replace --regex $
"($WT_ROOT_SUFFIX)$" ""))
158 + let repo = (
$root | path basename)
117 159 let main = (git-config
$git_dir $WT_MAIN_BRANCH | default
$DEFAULT_MAIN_BRANCH)
119 161 {root:
$root, git_dir:
$git_dir, repo:
$repo, main:
$main}
123 165 def wt-path [branch: string] {
125 - [
$l.root $
"($l.repo).(branch-slug $branch)"] | path join
167 + [
$l.root (branch-slug
$branch)] | path join
128 170 def empty-worktree-record [] {
193 235 (git-dir-complete
$git_dir [
"show-ref" "--verify" "--quiet" $
"($REF_HEADS)($branch)"]).exit_code ==
0 238 +def add-candidate-display [branch: string,
source: string, base: string] {
239 + $
"($branch) ($source) ($base)" 242 +def local-add-candidates [git_dir: path] {
243 + git-dir-output
$git_dir [
"for-each-ref" "--format=%(refname:short)" "refs/heads"]
245 + |
where {|branch| not ((
$branch | str trim) | is-empty) }
246 + |
each {|branch| {branch:
$branch, base:
$branch,
source:
"local", display: (add-candidate-display
$branch "local" $branch)} }
249 +def remote-add-candidates [git_dir: path] {
250 + git-dir-output
$git_dir [
"for-each-ref" "--format=%(refname:short)" "refs/remotes"]
252 + |
where {|ref| not ((
$ref | str trim) | is-empty) }
253 + |
where {|ref| not (
$ref | str ends-with
"/HEAD") }
255 + let branch = (
$ref | split row
"/" | skip
1 | str join
"/")
256 + {branch:
$branch, base:
$ref,
source:
"remote", display: (add-candidate-display
$branch "remote" $ref)}
260 +def git-remotes [git_dir: path] {
261 + git-dir-output
$git_dir [
"remote"]
263 + |
where {|name| not ((
$name | str trim) | is-empty) }
264 + |
each {|name| {name:
$name, url: (git-dir-output
$git_dir [
"remote" "get-url" $name])} }
267 +def gh-pr-add-candidates [remotes: list<record>] {
268 + if (which gh | is-empty) { return [] }
269 + if (^gh auth status | complete).exit_code !=
0 { return [] }
273 + let res = (^gh pr list --repo
$remote.url --json number,headRefName,title --limit
100 | complete)
274 + if $res.exit_code !=
0 or ((
$res.stdout | str trim) | is-empty) {
280 + let branch =
$pr.headRefName 281 + let base = $
"pull/($pr.number)/head" 282 + {branch:
$branch, base:
$base,
source:
"gh-pr", remote:
$remote.name, number:
$pr.number, display: (add-candidate-display
$branch $
"gh ($remote.name) #($pr.number)" $pr.title)}
289 +def parse-fj-pr-line [line: string] {
290 + let trimmed = (
$line | str trim)
291 + let first = (
$trimmed | split row
" " | first | str replace
"#" "")
292 + if not (
$first =~
'^\d+$') { return null }
296 + | str replace --regex
'^#?\d+\s+' '' 297 + | str replace --regex
'\s+\(by .+\)$' '' 301 + let number = (
$first | into int)
302 + {branch: $
"pr-($number)", base: $
"pull/($number)/head",
source:
"fj-pr", number:
$number, title:
$title, display: (add-candidate-display $
"pr-($number)" $
"fj #($number)" $title)}
305 +def fj-pr-add-candidates [remotes: list<record>] {
306 + if (which fj | is-empty) { return [] }
307 + if (^fj auth list | complete).exit_code !=
0 { return [] }
311 + let res = (^fj --style minimal pr -R
$remote.name search --state open | complete)
312 + if $res.exit_code !=
0 or ((
$res.stdout | str trim) | is-empty) {
318 + let parsed = (parse-fj-pr-line
$line)
319 + if $parsed == null { null }
else {
$parsed | upsert remote
$remote.name | upsert display (add-candidate-display
$parsed.branch $
"fj ($remote.name) #($parsed.number)" $parsed.title) }
321 + |
where {|candidate|
$candidate != null }
327 +def dedupe-add-candidates [] {
328 + reduce --fold [] {|candidate, acc|
329 + if (
$candidate.branch in (
$acc | get --optional branch)) {
$acc }
else {
$acc | append
$candidate }
333 +def raw-add-candidates [] {
335 + let occupied = (worktrees-basic |
where branch != null | get --optional branch)
336 + let remotes = (git-remotes
$l.git_dir)
338 + local-add-candidates
$l.git_dir 339 + | append (remote-add-candidates
$l.git_dir)
340 + | append (gh-pr-add-candidates
$remotes)
341 + | append (fj-pr-add-candidates
$remotes)
342 + |
where {|candidate|
343 + (not (
$candidate.branch in $occupied)) and (not ((wt-path
$candidate.branch) | path exists))
347 +def available-add-candidates [] {
348 + raw-add-candidates | dedupe-add-candidates
351 +def add-candidate-matches [candidate: record, query: string] {
352 + let q = (
$query | str lowercase)
353 + let branch = (
$candidate.branch | str lowercase | str contains
$q)
354 + let base = (
$candidate.base | str lowercase | str contains
$q)
355 + let display = (
$candidate.display | str lowercase | str contains
$q)
356 + $branch or
$base or
$display 359 +def pick-add-candidate [query?: string] {
360 + let matches =
if $query == null {
361 + available-add-candidates
363 + raw-add-candidates |
where {|candidate| add-candidate-matches
$candidate $query }
366 + if (
$matches | is-empty) {
367 + if $query != null {
368 + let action = (choose-recovery
369 + $
"No local branch, remote branch, or PR matched `($query)`." 371 + {id:
"create", label: $
"Create new branch `($query)` from HEAD"}
372 + {id:
"cancel", label:
"Cancel"}
374 + $
"no branch/remote/PR matching `($query)`. Non-interactive: create explicitly with `git wt new ($query)` or pass `git wt add ($query) --base <base>`." 376 + if $action ==
"create" { return {branch:
$query, base:
$HEAD,
source:
"new", display: $
"($query) new HEAD"} }
378 + error make {msg:
"no branches or PRs available; use `wt new` to create a branch"}
381 + if (
$matches | length) ==
1 { return (
$matches | first) }
383 + let selected = (
$matches | input list --fuzzy --display display
"branch")
384 + if $selected == null { error make {msg:
"no branch selected"} }
388 +def ensure-fetch-refspec [git_dir: path, remote: string, refspec: string] {
389 + let key = $
"remote.($remote).fetch" 390 + if not (
$refspec in (git-config-values
$git_dir $key)) {
391 + git-dir-output
$git_dir [
"config" "--add" $key $refspec] | ignore
395 +def split-remote-branch [git_dir: path, ref: string] {
396 + let parts = (
$ref | split row
"/")
397 + if (
$parts | length) <
2 { return null }
399 + let remote = (
$parts | first)
400 + let branch = (
$parts | skip
1 | str join
"/")
401 + if (git-dir-complete
$git_dir [
"show-ref" "--verify" "--quiet" $
"refs/remotes/($remote)/($branch)"]).exit_code !=
0 { return null }
402 + {remote:
$remote, branch:
$branch}
405 +def configure-branch-upstream [git_dir: path, branch: string, remote: string, merge_ref: string] {
406 + git-config-set
$git_dir $
"branch.($branch).remote" $remote 407 + git-config-set
$git_dir $
"branch.($branch).merge" $merge_ref 410 +def configure-upstream-from-base [git_dir: path, branch: string, base: string] {
411 + let remote_branch = (split-remote-branch
$git_dir $base)
412 + if $remote_branch != null {
413 + configure-branch-upstream
$git_dir $branch $remote_branch.remote $
"refs/heads/($remote_branch.branch)" 417 +def configure-upstream-matching-remote [git_dir: path, branch: string] {
418 + for remote
in (git-remotes
$git_dir) {
419 + if (git-dir-complete
$git_dir [
"show-ref" "--verify" "--quiet" $
"refs/remotes/($remote.name)/($branch)"]).exit_code ==
0 {
420 + configure-branch-upstream
$git_dir $branch $remote.name $
"refs/heads/($branch)" 426 +def ensure-branch-points-at [git_dir: path, branch: string, target_ref: string, label: string] {
427 + if not (branch-exists
$git_dir $branch) { return }
429 + let local = (git-dir-output
$git_dir [
"rev-parse" $branch])
430 + let target = (git-dir-output
$git_dir [
"rev-parse" $target_ref])
431 + if $local ==
$target { return }
433 + let action = (choose-recovery
434 + $
"Local branch `($branch)` already exists but does not point at selected ($label)." 436 + {id:
"reset", label: $
"Reset `($branch)` to ($label)"}
437 + {id:
"cancel", label:
"Cancel"}
439 + $
"local branch `($branch)` differs from selected ($label). Non-interactive: delete/reset it first, then rerun `git wt add ($branch)`." 441 + if $action ==
"reset" { git-dir-output
$git_dir [
"branch" "-f" $branch $target_ref] | ignore }
444 +def create-picked-worktree [candidate: record, submodules: bool] {
446 + if $candidate.source ==
"remote" {
447 + ensure-branch-points-at
$l.git_dir $candidate.branch $candidate.base $
"remote branch ($candidate.base)" 448 + let path = (create-worktree
$candidate.branch $candidate.base $submodules)
449 + configure-upstream-from-base
$l.git_dir $candidate.branch $candidate.base 453 + if $candidate.source in [
"gh-pr" "fj-pr"] {
454 + let tracking_ref = $
"refs/remotes/($candidate.remote)/pull/($candidate.number)" 455 + let refspec = $
"+refs/pull/($candidate.number)/head:($tracking_ref)" 456 + ensure-fetch-refspec
$l.git_dir $candidate.remote $refspec 457 + git-dir-output
$l.git_dir [
"fetch" $candidate.remote $
"refs/pull/($candidate.number)/head:($tracking_ref)"] | ignore
458 + ensure-branch-points-at
$l.git_dir $candidate.branch $tracking_ref $
"PR #($candidate.number) from ($candidate.remote)" 459 + let path = (create-worktree
$candidate.branch $tracking_ref $submodules)
460 + configure-branch-upstream
$l.git_dir $candidate.branch $candidate.remote $
"refs/pull/($candidate.number)/head" 464 + let path = (create-worktree
$candidate.branch $candidate.base $submodules)
465 + if $candidate.source ==
"local" { configure-upstream-matching-remote
$l.git_dir $candidate.branch }
484 +def add-worktree-needs-fetch [message: string] {
485 + let invalid = (
$message | str contains
"invalid reference")
486 + let unknown = (
$message | str contains
"unknown revision")
487 + let not_commit = (
$message | str contains
"not a commit")
488 + let no_remote = (
$message | str contains
"couldn't find remote ref")
489 + $invalid or
$unknown or
$not_commit or
$no_remote 211 492 def add-worktree [git_dir: path, path: path, branch: string, base: string, submodules: bool] {
212 493 let args =
if (branch-exists
$git_dir $branch) {
213 494 [
"worktree" "add" $path $branch]
215 496 [
"worktree" "add" "-b" $branch $path $base]
218 - git-dir-output
$git_dir $args | ignore
499 + let res = (git-dir-complete
$git_dir $args)
500 + if $res.exit_code !=
0 {
501 + let msg = (git-failure
$args $res.stderr)
502 + if not (add-worktree-needs-fetch
$msg) { error make {msg:
$msg} }
504 + let action = (choose-recovery
505 + $
"Git could not create this worktree.\n\n($msg)\n\nOften the base branch/ref is stale or has not been fetched yet." 507 + {id:
"fetch", label:
"Fetch all remotes, then retry"}
508 + {id:
"cancel", label:
"Cancel"}
510 + $
"($msg)\nNon-interactive: run `git fetch --all --prune`, then retry the `git wt` command." 512 + if $action ==
"fetch" {
513 + git-dir-output
$git_dir [
"fetch" "--all" "--prune"] | ignore
514 + git-dir-output
$git_dir $args | ignore
219 518 init-submodules
$path $submodules 223 522 def create-worktree [branch: string, base: string, submodules: bool] {
524 + let existing = (worktrees-basic |
where branch ==
$branch | first)
525 + if $existing != null {
526 + let action = (choose-recovery
527 + $
"Branch `($branch)` already has a worktree:\n($existing.path)" 529 + {id:
"use", label:
"Use existing worktree"}
530 + {id:
"cancel", label:
"Cancel"}
532 + $
"branch `($branch)` already has worktree ($existing.path). Non-interactive: use `git wt go ($branch)`." 534 + if $action ==
"use" { return
$existing.path }
225 537 let path = (wt-path
$branch)
227 539 if (
$path | path exists) {
228 - error make {msg: $
"path exists: ($path)"}
540 + let action = (choose-recovery
541 + $
"Target path already exists: ($path)\nGit cannot create a worktree on top of it." 543 + {id:
"use", label:
"Use existing path; do not create anything"}
544 + {id:
"remove", label:
"Delete existing path, then create worktree"}
545 + {id:
"cancel", label:
"Cancel"}
547 + $
"target exists: ($path)\nNon-interactive: remove it first (`rm -rf ($path)`) or use it manually." 549 + if $action ==
"use" { return
$path }
231 553 add-worktree
$l.git_dir $path $branch $base $submodules 554 + configure-upstream-from-base
$l.git_dir $branch $base 262 585 if $force { [
"worktree" "remove" "--force" $target] }
else { [
"worktree" "remove" $target] }
265 -def delete-branch [git_dir: path, branch: string, force: bool] {
588 +def remove-worktree-git [git_dir: path, target: path, force: bool, command_hint: string] {
589 + let res = (git-dir-complete
$git_dir (remove-worktree-args
$target $force))
590 + if $res.exit_code ==
0 { return }
592 + let msg = (git-failure (remove-worktree-args
$target $force)
$res.stderr)
593 + if $force or not (remove-needs-force
$msg) { error make {msg:
$msg} }
595 + let action = (choose-recovery
596 + $
"Git refused to remove this worktree.\n\n($msg)\n\nUsually this means dirty files or submodules are present. Force removal deletes that worktree directory." 598 + {id:
"force", label:
"Force remove worktree"}
599 + {id:
"cancel", label:
"Cancel"}
601 + $
"($msg)\nNon-interactive: rerun with `($command_hint) --force`." 603 + if $action ==
"force" { git-dir-output
$git_dir (remove-worktree-args
$target true) | ignore }
606 +def delete-branch [git_dir: path, branch: string, force: bool, command_hint: string] {
266 607 let flag =
if $force {
"-D" }
else {
"-d" }
267 - git-dir-output
$git_dir [
"branch" $flag $branch] | ignore
608 + let res = (git-dir-complete
$git_dir [
"branch" $flag $branch])
609 + if $res.exit_code ==
0 { return }
611 + let msg = (git-failure [
"branch" $flag $branch]
$res.stderr)
612 + if $force { error make {msg:
$msg} }
614 + let action = (choose-recovery
615 + $
"Git refused to delete branch `($branch)`.\n\n($msg)\n\nUsually this means commits are not merged upstream." 617 + {id:
"force", label: $
"Force delete branch `($branch)`"}
618 + {id:
"keep", label:
"Keep branch"}
619 + {id:
"cancel", label:
"Cancel"}
621 + $
"($msg)\nNon-interactive: rerun with `($command_hint) --force` to force-delete, or use `git wt rm ($branch)` to keep the branch." 623 + if $action ==
"force" { git-dir-output
$git_dir [
"branch" "-D" $branch] | ignore }
271 -def --env remove-worktree [branch: string, delete_branch: bool, force: bool] {
627 +def --env remove-worktree [branch: string, delete_branch: bool, force: bool, command_hint: string] {
273 629 let target = (path-
for-branch
$branch | path expand)
275 631 cd-away-from-target
$l $target $branch 276 - git-dir-output
$l.git_dir (remove-worktree-args
$target $force) | ignore
632 + remove-worktree-git
$l.git_dir $target $force $command_hint 278 634 if $delete_branch {
279 - delete-branch
$l.git_dir $branch $force 635 + delete-branch
$l.git_dir $branch $force $command_hint 285 -def init-bare-layout [
source: string, git_dir: path, repo: string, main: string] {
286 - git-output [
"clone" "--bare" $source $git_dir] | ignore
287 - git-config-set
$git_dir $WT_REPO_NAME $repo 288 - git-config-set
$git_dir $WT_MAIN_BRANCH $main 291 -def remember-
source [git_dir: path,
source: path] {
292 - git-config-set
$git_dir $WT_SOURCE_PATH $source 295 643 def choose-fresh-branch [git_dir: path, branch: any] {
296 644 if ((
$branch == null) or (
$branch | is-empty)) {
312 660 let git_dir = ([
$root $DOT_GIT] | path join)
314 662 if (
$root | path exists) {
315 - error make {msg: $
"target exists: ($root)"}
663 + let action = (choose-recovery
664 + $
"Clone target already exists: ($root)" 666 + {id:
"remove", label:
"Delete target directory, then clone"}
667 + {id:
"cancel", label:
"Cancel"}
669 + $
"target exists: ($root). Non-interactive: choose another name/dir or remove it first." 671 + if $action ==
"remove" { rm -rf
$root }
319 675 git-output [
"clone" "--bare" $source $git_dir] | ignore
321 677 let chosen_branch = (choose-fresh-branch
$git_dir $branch)
322 - git-config-set
$git_dir $WT_REPO_NAME $repo 323 678 git-config-set
$git_dir $WT_MAIN_BRANCH $chosen_branch 679 + ensure-upstream
$git_dir $chosen_branch 680 + git-dir-complete
$git_dir [
"fetch" "--all" "--prune"] | ignore
325 - let worktree = ([
$root $
"($repo).(branch-slug $chosen_branch)"] | path join)
682 + let worktree = ([
$root (branch-slug
$chosen_branch)] | path join)
326 683 git-dir-output
$git_dir [
"worktree" "add" $worktree $chosen_branch] | ignore
327 684 init-submodules
$worktree $submodules 348 -def migration-plan [dirty_mode: string, name: any, branch: any] {
705 +def migration-plan [dirty_mode: string, branch: any] {
349 706 assert-dirty-mode
$dirty_mode 351 708 let src = (git-output [
"rev-parse" "--show-toplevel"] | path expand)
352 - let repo =
if (
$name == null) {
$src | path basename }
else {
$name }
353 - let parent = (
$src | path dirname)
354 - let root = ([
$parent $
"($repo)($WT_ROOT_SUFFIX)"] | path join | path expand)
355 - let git_dir = ([
$root $DOT_GIT] | path join)
356 709 let chosen_branch = (selected-branch
$branch)
357 - let target = ([
$root $
"($repo).(branch-slug $chosen_branch)"] | path join)
710 + let target = ([
$src (branch-slug
$chosen_branch)] | path join)
358 711 let status_before = (status-porcelain
$src)
359 712 let is_dirty = (not (
$status_before | is-empty))
362 715 mode:
"current-checkout" 365 - target_git_dir:
$git_dir 718 + target_git_dir: ([
$src $DOT_GIT] | path join)
366 719 target_worktree:
$target 720 + repo: (
$src | path basename)
368 721 branch:
$chosen_branch 370 723 dirty_mode:
$dirty_mode 371 724 stashes: (stash-count
$src)
373 726 status_before:
$status_before 378 731 $plan | reject status_before
381 -def assert-migration-ready [plan: record] {
382 - if (
$plan.target_root | path exists) {
383 - error make {msg: $
"target exists: ($plan.target_root)"}
734 +def resolve-migration-dirty [plan: record] {
735 + if not (
$plan.dirty and
$plan.dirty_mode ==
$DIRTY_REFUSE) { return
$plan }
386 - if $plan.dirty and
$plan.dirty_mode ==
$DIRTY_REFUSE {
387 - error make {msg:
"working tree has changes; rerun with `co . --dirty copy` or commit/stash first"}
737 + let action = (choose-recovery
738 + "Working tree has uncommitted changes. Migration can copy them into the new worktree, or you can cancel and commit/stash manually." 740 + {id:
"copy", label:
"Copy dirty state into migrated worktree"}
741 + {id:
"cancel", label:
"Cancel"}
743 + "working tree has changes. Non-interactive: rerun with `git wt migrate --dirty copy`, or commit/stash first." 745 + if $action ==
"copy" { return (
$plan | upsert dirty_mode
$DIRTY_COPY) }
391 -def warn-stashes [plan: record] {
392 - if $plan.stashes >
0 {
393 - print -e $
"warning: ($plan.stashes) stash(es) remain only in original checkout: ($plan.source)" 749 +def assert-migration-ready [plan: record] {
750 + if $plan.dirty and
$plan.dirty_mode ==
$DIRTY_REFUSE {
751 + error make {msg:
"working tree has changes; rerun with `--dirty copy` or commit/stash first"}
420 778 apply-patch
$target []
$patches.unstaged 423 -def assert-external-ok [name: string, res: record] {
424 - if $res.exit_code !=
0 {
425 - let msg = (
$res.stderr | str trim)
426 - error make {msg: (
if (
$msg | is-empty) { $
"($name) failed" }
else {
$msg })}
430 -def copy-untracked [
source: path, target: path] {
431 - git-output [
"status"]
$source | ignore
433 - if (which rsync | is-not-empty) {
434 - assert-external-ok
"rsync" (^rsync -a --ignore-existing --exclude .git $
"($source)/" $
"($target)/" | complete)
436 - assert-external-ok
"cp" (^cp -a -n $
"($source)/." $target | complete)
781 +def copy-untracked [
source: path, target: path, --exclude: list<string> = []] {
783 + let skip = ([
$DOT_GIT] | append
$exclude)
784 + for entry
in (ls -a
$source | get name) {
785 + if ((
$entry | path basename)
in $skip) { continue }
786 + cp -r --no-dereference --no-clobber
$entry $target 440 790 def copy-dirty-state [plan: record, patches: record] {
441 791 apply-dirty-patches
$plan.target_worktree $patches 442 - copy-untracked
$plan.source $plan.target_worktree 792 + copy-untracked
$plan.source $plan.target_worktree --exclude [(
$plan.target_worktree | path basename)]
444 794 let status_after = (status-porcelain
$plan.target_worktree)
445 795 if $plan.status_before !=
$status_after {
446 - error make {msg: $
"dirty-copy verification failed; original untouched at ($plan.source), target left for inspection at ($plan.target_root)"}
796 + error make {msg: $
"dirty-copy verification failed; target left for inspection at ($plan.target_root)"}
450 -def create-migration-layout [plan: record, submodules: bool] {
451 - mkdir
$plan.target_root 452 - init-bare-layout
$plan.source $plan.target_git_dir $plan.repo $plan.branch 453 - remember-
source $plan.target_git_dir $plan.source 454 - git-dir-output
$plan.target_git_dir [
"worktree" "add" $plan.target_worktree $plan.branch] | ignore
455 - init-submodules
$plan.target_worktree $submodules 802 +def copy-git-config [old_git: path, new_git: path] {
803 + let src = ([
$old_git "config"] | path join)
804 + let dst = ([
$new_git "config"] | path join)
805 + if (
$src | path exists) { cp --force
$src $dst }
459 -def --env co-current [name?: string, branch?: string, plan: bool = false, dirty: string =
$DIRTY_REFUSE, submodules: bool = true] {
460 - let migration = (migration-plan
$dirty $name $branch)
808 +def preserve-stash-refs [old_git: path, new_git: path] {
809 + for sub
in [
"refs/stash" "logs/refs/stash"] {
810 + let src = ([
$old_git $sub] | path join)
811 + let dst = ([
$new_git $sub] | path join)
812 + if (
$src | path exists) {
813 + mkdir (
$dst | path dirname)
814 + cp -r --no-dereference --no-clobber
$src $dst 820 +def clear-stale-files [root: path, keep: list<string>] {
821 + let keep_set = (
$keep | append [
".",
".."])
822 + for entry
in (ls -a
$root) {
823 + if ((
$entry.name | path basename) not-
in $keep_set) {
832 +def --env co-current [branch?: string, plan: bool = false, dirty: string =
$DIRTY_REFUSE, submodules: bool = true] {
833 + let migration = (resolve-migration-dirty (migration-plan
$dirty $branch))
461 834 if $plan { return (public-migration
$migration) }
463 836 assert-migration-ready
$migration 464 - warn-stashes
$migration 466 - let tmp =
if $migration.dirty { mktemp -d }
else { null }
838 + let src =
$migration.source 839 + let old_git = ([
$src $DOT_GIT] | path join)
840 + let old_git_backup = ([
$src ".git.wt-backup"] | path join)
841 + let new_git = ([
$src ".git.wt-new"] | path join)
842 + let tmp = (mktemp -d)
844 + if (
$old_git_backup | path exists) { error make {msg: $
"backup exists: ($old_git_backup)"} }
845 + if (
$new_git | path exists) { error make {msg: $
"temporary git dir exists: ($new_git)"} }
469 - let patches =
if $migration.dirty { write-dirty-patches
$migration.source $tmp }
else { null }
470 - create-migration-layout
$migration $submodules 848 + let patches =
if $migration.dirty { write-dirty-patches
$src $tmp }
else { null }
851 + git-output [
"clone" "--bare" $src $new_git] | ignore
852 + mv
$old_git $old_git_backup 853 + mv
$new_git $old_git 855 + copy-git-config
$old_git_backup $migration.target_git_dir 856 + git-config-set
$migration.target_git_dir "core.bare" "true" 857 + git-config-unset
$migration.target_git_dir "core.worktree" 858 + preserve-stash-refs
$old_git_backup $migration.target_git_dir 859 + git-config-set
$migration.target_git_dir $WT_MAIN_BRANCH $migration.branch 860 + ensure-upstream
$migration.target_git_dir $migration.branch 861 + git-dir-output
$migration.target_git_dir [
"worktree" "add" $migration.target_worktree $migration.branch] | ignore
471 863 if $migration.dirty {
472 864 copy-dirty-state
$migration $patches 474 - if $tmp != null { rm -rf
$tmp }
476 - if $tmp != null { rm -rf
$tmp }
477 - error make {msg:
$err.msg}
867 + clear-stale-files
$src [
$DOT_GIT (branch-slug
$migration.branch)
".git.wt-backup"]
868 + init-submodules
$migration.target_worktree $submodules 870 + rm -rf
$old_git_backup 873 + if (
$new_git | path exists) { rm -rf
$new_git }
874 + if (
$old_git_backup | path exists) {
876 + mv
$old_git_backup $old_git 879 + error make {msg: (
$e | get msg)}
480 882 cd
$migration.target_worktree 485 887 export def --env
"wt clone" [
488 - --branch (-b): string =
"" 489 - --dir (-d): path =
"." 890 + --branch (-b): string =
"" 891 + --dir (-d): path =
"." 492 894 co-fresh
$source $name $branch $dir (not
$no_submodules)
496 900 export def --env
"wt migrate" [
498 - --branch (-b): string =
"" 500 - --dirty: string =
$DIRTY_REFUSE 901 + --branch (-b): string =
"" 903 + --dirty: string =
$DIRTY_REFUSE 503 - co-current
$name $branch $plan $dirty (not
$no_submodules)
906 + co-current
$branch $plan $dirty (not
$no_submodules)
509 912 export def "wt root" [] { (layout).root }
510 913 export def "wt repo" [] { (layout).repo }
511 -export def "wt path" [branch: string] { wt-path
$branch }
914 +export def "wt path" [
916 +] { wt-path
$branch }
513 918 def worktree-dirty [row: record] {
514 919 if ((
$row.path | path exists) and (not
$row.bare) and (
$row.branch != null)) {
532 937 export def --env
"wt new" [
534 - --base (-b): string =
$HEAD 939 + --base (-b): string =
$HEAD 538 943 let path = (create-worktree
$branch $base (not
$no_submodules))
539 944 if $switch { cd
$path }
543 948 export def --env
"wt go" [
546 - --base (-b): string =
$HEAD 951 + --base (-b): string =
$HEAD 549 954 let path = (path-
for-branch
$branch)
550 955 if not (
$path | path exists) {
552 - error make {msg: $
"no worktree for branch `($branch)`; rerun with --create"}
957 + let action = (choose-recovery
958 + $
"No worktree exists for branch `($branch)`." 960 + {id:
"create", label: $
"Create worktree for `($branch)`"}
961 + {id:
"cancel", label:
"Cancel"}
963 + $
"no worktree for branch `($branch)`. Non-interactive: rerun with `git wt go ($branch) --create`." 965 + if $action !=
"create" { error make {msg:
"cancelled"} }
554 967 create-worktree
$branch $base (not
$no_submodules) | ignore
560 973 export def --env
"wt rm" [
565 978 let b =
if (
$name == null) { current-branch }
else {
$name }
566 - remove-worktree
$b $branch $force 979 + remove-worktree
$b $branch $force $
"git wt rm ($b)" 569 982 export def --env
"wt done" [
573 986 let b =
if (
$branch == null) { current-branch }
else {
$branch }
574 - remove-worktree
$b true
$force 987 + remove-worktree
$b true
$force $
"git wt done ($b)" 577 990 export def "wt prune" [] {
578 - git-dir-output (layout).git_dir [
"worktree" "prune"] | ignore
992 + let res = (git-dir-complete
$l.git_dir [
"worktree" "prune"])
993 + if $res.exit_code ==
0 { return }
994 + error make {msg: (git-failure [
"worktree" "prune"]
$res.stderr)}
581 997 export def "wt repair" [] {
582 - git-dir-output (layout).git_dir [
"worktree" "repair"] | ignore
999 + let res = (git-dir-complete
$l.git_dir [
"worktree" "repair"])
1000 + if $res.exit_code ==
0 { return }
1001 + error make {msg: (git-failure [
"worktree" "repair"]
$res.stderr)}
586 -export def --env
"wt add" [branch: string, --base (-b): string =
$HEAD, --switch (-s), --no-submodules] {
587 - wt new
$branch --base
$base --switch=
$switch --no-submodules=
$no_submodules 1005 +export def --env
"wt add" [
1007 + --base (-b): string =
$HEAD 1011 + if $branch != null and
$base !=
$HEAD {
1012 + return (wt new
$branch --base
$base --switch=
$switch --no-submodules=
$no_submodules)
1015 + let selected = (pick-add-candidate
$branch)
1016 + let path = (create-picked-worktree
$selected (not
$no_submodules))
1017 + if $switch { cd
$path }
590 1021 export def "wt list" [] { wt ls }
592 -export def "wt lock" [branch?: string, reason?: string] {
1023 +export def "wt lock" [
593 1027 let b =
if (
$branch == null) { current-branch }
else {
$branch }
594 1028 let path = (path-
for-branch
$b)
595 1029 let l = (layout)
1031 + if not (
$path | path exists) {
1032 + let action = (choose-recovery
1033 + $
"No worktree path exists for `($b)`: ($path)" 1035 + {id:
"create", label:
"Create worktree, then lock it"}
1036 + {id:
"cancel", label:
"Cancel"}
1038 + $
"no worktree path for `($b)`. Non-interactive: run `git wt go ($b) --create`, then `git wt lock ($b)`." 1040 + if $action ==
"create" { create-worktree
$b $HEAD true | ignore }
596 1043 let args =
if (
$reason == null) { [
"worktree" "lock" $path] }
else { [
"worktree" "lock" "--reason" $reason $path] }
597 - git-dir-output
$l.git_dir $args | ignore
1044 + let res = (git-dir-complete
$l.git_dir $args)
1045 + if $res.exit_code ==
0 { return }
1047 + let msg = (git-failure
$args $res.stderr)
1048 + if (
$msg | str contains
"already locked") {
1049 + let action = (choose-recovery
1050 + $
"Worktree `($b)` is already locked.\n\n($msg)" 1052 + {id:
"relock", label:
"Unlock, then lock again"}
1053 + {id:
"cancel", label:
"Cancel"}
1055 + $
"($msg)\nNon-interactive: run `git wt unlock ($b)`, then retry `git wt lock ($b)`." 1057 + if $action ==
"relock" {
1058 + git-dir-output
$l.git_dir [
"worktree" "unlock" $path] | ignore
1059 + git-dir-output
$l.git_dir $args | ignore
1064 + error make {msg:
$msg}
600 -export def "wt unlock" [branch?: string] {
1067 +export def "wt unlock" [
601 1070 let b =
if (
$branch == null) { current-branch }
else {
$branch }
602 - git-dir-output (layout).git_dir [
"worktree" "unlock" (path-
for-branch
$b)] | ignore
1071 + let path = (path-
for-branch
$b)
1073 + let res = (git-dir-complete
$l.git_dir [
"worktree" "unlock" $path])
1074 + if $res.exit_code ==
0 { return }
1076 + let msg = (git-failure [
"worktree" "unlock" $path]
$res.stderr)
1077 + if (
$msg | str contains
"not locked") {
1078 + if $nu.is-interactive { print -e $
"Worktree `($b)` is not locked. Nothing to do." }
1081 + error make {msg:
$msg}
605 -export def "wt move" [branch: string, dest: path] {
606 - git-dir-output (layout).git_dir [
"worktree" "move" (path-
for-branch
$branch)
$dest] | ignore
1084 +export def "wt move" [
1089 + let source = (path-
for-branch
$branch)
1091 + if (
$dest | path exists) {
1092 + let action = (choose-recovery
1093 + $
"Destination already exists: ($dest)" 1095 + {id:
"remove", label:
"Delete destination, then move"}
1096 + {id:
"cancel", label:
"Cancel"}
1098 + $
"destination exists: ($dest). Non-interactive: remove it first, then rerun `git wt move ($branch) ($dest)`." 1100 + if $action ==
"remove" { rm -rf
$dest }
1103 + let res = (git-dir-complete
$l.git_dir [
"worktree" "move" $source $dest])
1104 + if $res.exit_code ==
0 { return
$dest }
1106 + let msg = (git-failure [
"worktree" "move" $source $dest]
$res.stderr)
1107 + if (
$msg | str contains
"working trees containing submodules") {
1108 + let action = (choose-recovery
1109 + $
"Git refuses to move worktrees containing submodules.\n\n($msg)\n\nSafe workaround: filesystem move, then `git worktree repair`." 1111 + {id:
"repair", label:
"Move directory, then repair worktree metadata"}
1112 + {id:
"cancel", label:
"Cancel"}
1114 + $
"($msg)\nNon-interactive: run `mv ($source) ($dest)` then `git wt repair`." 1116 + if $action ==
"repair" {
1118 + git-dir-output
$l.git_dir [
"worktree" "repair" $dest] | ignore
1123 + error make {msg:
$msg}
610 -export def --env
"wt remove" [name?: string, --branch (-b), --force (-f)] {
1126 +export def --env
"wt remove" [
611 1131 wt rm
$name --branch=
$branch --force=
$force