repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

git: better config for diff+merge and review

e1327307939004cd295e167e42d6fbbe59475f74

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

Notes

Modified
1 -Subproject commit bb91b3fdd57d9d1be71281dc5d06887594528167
1 +Subproject commit c820b31e9b54ae9a90749a916e518dd8d8bf5532

git/gitconfig

Modified
9 9 rc = rebase --continue
10 10 st = status --short
11 11 unstage = reset HEAD --
12 + up = push
13 + tracked = rev-parse --abbrev-ref --symbolic-full-name @{upstream}
12 14 last = log -1 HEAD
13 15 ui = !gitu
14 16 down = "!sh -c 'git pull --recurse-submodules \"$@\" && git submodule sync --recursive && git submodule update --init --recursive' -"
17 + diffz = difftool --tool zed
18 + rv = "!f() { base=origin/trunk; if [ \"$1\" != \"\" ] && [ \"$1\" != \"--\" ]; then base=$1; shift; fi; if [ \"$1\" = \"--\" ]; then shift; fi; next() { printf '\\n[Enter] %s / [Ctrl-C] stop ' \"$1\"; read -r _; }; git fetch --all --prune || return; echo \"STEP 1/4 COMMITS $base..HEAD\"; git log --oneline --decorate --graph \"$base..HEAD\"; next \"STEP 2/4 FILES\"; echo \"STEP 2/4 FILES $base...HEAD\"; git diff --name-status \"$base...HEAD\" -- \"$@\"; git diff --stat \"$base...HEAD\" -- \"$@\"; next \"STEP 3/4 DELTA DIFF\"; echo \"STEP 3/4 DELTA DIFF $base...HEAD\"; git diff \"$base...HEAD\" -- \"$@\"; next \"STEP 4/4 DIFFTASTIC\"; echo \"STEP 4/4 DIFFTASTIC $base...HEAD\"; git difftool -d \"$base...HEAD\" -- \"$@\"; }; f"
16 20 [core]
17 21 attributesfile = .gitattributes
89 93 enabled = true
90 94 autoupdate = true
92 -[diff]
93 - context = 3
94 - renames = copies
95 - interHunkContext = 10
96 - colorMoved = default
97 - tool = zed
99 96 [difftool]
100 97 prompt = false
105 102 [difftool "difftastic"]
106 103 cmd = "difft \"$LOCAL\" \"$REMOTE\""
105 +[diff]
106 + # Difftastic as default `git difftool` target.
107 + tool = difftastic
108 + algorithm = histogram
109 + colorMoved = plain
110 + context = 3
111 + interHunkContext = 10
112 + mnemonicPrefix = true
113 + renames = true
114 + submodule = log
108 116 [merge]
109 - tool = vim
117 + tool = nvimdiff2
110 118 autostash = true
111 119 conflictstyle = zdiff3
118 126 name = mergiraf
119 127 driver = mergiraf merge --git %O %A %B -s %S -x %X -y %Y -p %P -l %L
121 -[mergetool "vim"]
122 - cmd = "nvim -d \"$LOCAL\" \"$MERGED\" \"$REMOTE\" "
124 129 [branch]
125 130 sort = committerdate
126 131 autoSetupMerge = inherit
164 169 [submodule]
165 170 recurse = true
167 -[difftool "difftastic"]
168 - # See `man git-difftool` for a description of MERGED, LOCAL and REMOTE.
169 - cmd = difft "$MERGED" "$LOCAL" "abcdef1" "100644" "$REMOTE" "abcdef2" "100644"
171 -[difftool]
172 - # Run the difftool immediately, don't ask 'are you sure' each time.
173 - prompt = false
175 172 [pager]
176 173 # Use a pager if the difftool output is larger than one screenful,
177 174 # consistent with the behaviour of `git diff`.
178 175 difftool = true
180 -[diff]
181 - # Set difftastic as the default difftool, so we don't need to specify
182 - # `-t difftastic` every time.
183 - tool = difftastic
184 - algorithm = histogram
185 - colorMoved = plain
186 - mnemonicPrefix = true
187 - renames = true
188 - submodule = log
190 177 [worktree]
191 178 guessRemote = true

nushell/modules/git.nu

Modified
1 1 # Git worktree helpers.
2 2 # Layout:
3 -# <root>/.git bare repository
4 -# <root>/<repo>.<branch> worktrees
3 +# <root>/.git bare repository
4 +# <root>/<branch-slug> worktrees
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} }
50 + print -e ""
51 + print -e $message
52 + let selected = ($choices | input list --display label "choose")
53 + if $selected == null or $selected.id == "cancel" { error make {msg: "cancelled"} }
54 + $selected.id
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
49 65 # Run git and return trimmed stdout; fail with stderr as Nushell error.
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
95 137 # Branch name -> path-safe suffix.
96 138 def branch-slug [branch: string] {
97 139 $branch
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}
122 164 # Computed conventional worktree path for branch.
123 165 def wt-path [branch: string] {
124 166 let l = (layout)
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"]
244 + | lines
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"]
251 + | lines
252 + | where {|ref| not (($ref | str trim) | is-empty) }
253 + | where {|ref| not ($ref | str ends-with "/HEAD") }
254 + | each {|ref|
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"]
262 + | lines
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 [] }
271 + $remotes
272 + | each {|remote|
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) {
275 + []
276 + } else {
277 + $res.stdout
278 + | from json
279 + | each {|pr|
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)}
286 + | flatten
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 }
294 + let title = (
295 + $trimmed
296 + | str replace --regex '^#?\d+\s+' ''
297 + | str replace --regex '\s+\(by .+\)$' ''
298 + | str trim
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 [] }
309 + $remotes
310 + | each {|remote|
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) {
313 + []
314 + } else {
315 + $res.stdout
316 + | lines
317 + | each {|line|
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 }
324 + | flatten
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 [] {
334 + let l = (layout)
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
362 + } else {
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"} }
385 + $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)"
421 + return
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] {
445 + let l = (layout)
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
450 + return $path
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"
461 + return $path
464 + let path = (create-worktree $candidate.branch $candidate.base $submodules)
465 + if $candidate.source == "local" { configure-upstream-matching-remote $l.git_dir $candidate.branch }
466 + $path
196 469 # Init + checkout submodules inside a freshly added worktree.
197 470 # No-op when disabled or the worktree has no .gitmodules. Best-effort:
198 471 # failure (e.g. no network/auth) warns but leaves the worktree intact.
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
222 521 # Create branch + worktree, or add existing branch.
223 522 def create-worktree [branch: string, base: string, submodules: bool] {
224 523 let l = (layout)
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 }
550 + rm -rf $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 }
270 626 # Remove worktree, maybe branch, and cd away if removing current worktree.
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] {
272 628 let l = (layout)
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
282 638 $target
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 }
318 674 mkdir $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
328 685 cd $worktree
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"
363 716 source: $src
364 - target_root: $root
365 - target_git_dir: $git_dir
717 + target_root: $src
718 + target_git_dir: ([$src $DOT_GIT] | path join)
366 719 target_worktree: $target
367 - repo: $repo
720 + repo: ($src | path basename)
368 721 branch: $chosen_branch
369 722 dirty: $is_dirty
370 723 dirty_mode: $dirty_mode
371 724 stashes: (stash-count $src)
372 - destructive: false
725 + destructive: true
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)"}
384 - }
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"}
388 - }
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) }
746 + $plan
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 })}
427 - }
430 -def copy-untracked [source: path, target: path] {
431 - git-output ["status"] $source | ignore # sanity: source still repo
433 - if (which rsync | is-not-empty) {
434 - assert-external-ok "rsync" (^rsync -a --ignore-existing --exclude .git $"($source)/" $"($target)/" | complete)
435 - } else {
436 - assert-external-ok "cp" (^cp -a -n $"($source)/." $target | complete)
781 +def copy-untracked [source: path, target: path, --exclude: list<string> = []] {
782 + mkdir $target
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
800 +# Copy stash refs from old .git to new bare .git.
801 +# `git clone --bare` does not transport refs/stash.
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 }
458 -# Migrate current checkout into sibling .wt layout; original untouched.
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
819 +# Remove top-level entries from `root` except those in `keep`.
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) {
824 + rm -rf $entry.name
829 +# Convert current checkout in place to bare+worktree layout.
830 +# Original tracked files and untracked files are removed; dirty state is
831 +# restored from patches, untracked files are copied, stashes are preserved.
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 }
850 + # Keep .git backup on same filesystem: cross-device mv copies sockets and can ENXIO.
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 }
475 - } catch {|err|
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
871 + rm -rf $tmp
872 + } catch {|e|
873 + if ($new_git | path exists) { rm -rf $new_git }
874 + if ($old_git_backup | path exists) {
875 + rm -rf $old_git
876 + mv $old_git_backup $old_git
878 + rm -rf $tmp
879 + error make {msg: ($e | get msg)}
480 882 cd $migration.target_worktree
484 886 # Bootstrap a new bare+worktree layout from a URL/path.
485 887 export def --env "wt clone" [
486 - source: string # URL/path
487 - name?: string # repo/root name
488 - --branch (-b): string = "" # branch to check out; default remote HEAD
489 - --dir (-d): path = "." # parent dir
490 - --no-submodules # skip submodule init in the new worktree
888 + source: string # Git URL/path to clone into bare+worktree layout
889 + name?: string # Root directory name; default derives from source basename
890 + --branch (-b): string = "" # Initial branch to check out; default remote HEAD
891 + --dir (-d): path = "." # Parent directory for the new layout root
892 + --no-submodules # Skip submodule init in the initial worktree
492 894 co-fresh $source $name $branch $dir (not $no_submodules)
495 -# Migrate the current checkout into a sibling bare+worktree layout.
897 +# Migrate the current checkout in place to a bare+worktree layout.
898 +# Destructive: original tracked files are removed; dirty state, untracked
899 +# files, and stashes are preserved.
496 900 export def --env "wt migrate" [
497 - name?: string # repo/root name
498 - --branch (-b): string = "" # branch to check out; default current branch
499 - --plan # preview only
500 - --dirty: string = $DIRTY_REFUSE # `refuse` | `copy`
501 - --no-submodules # skip submodule init in the new worktree
901 + --branch (-b): string = "" # Branch to make the first worktree; default current branch
902 + --plan # Print migration plan only; do not modify files
903 + --dirty: string = $DIRTY_REFUSE # Dirty handling: `refuse` or `copy`; prompt if dirty and omitted
904 + --no-submodules # Skip submodule init in the migrated worktree
503 - co-current $name $branch $plan $dirty (not $no_submodules)
906 + co-current $branch $plan $dirty (not $no_submodules)
506 909 # Summary.
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" [
915 + branch: string # Branch name to map to its conventional worktree 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" [
533 - branch: string
534 - --base (-b): string = $HEAD
535 - --switch (-s)
536 - --no-submodules # skip submodule init in the new worktree
938 + branch: string # New or existing local branch to create a worktree for
939 + --base (-b): string = $HEAD # Base ref for a new branch; ignored when branch already exists
940 + --switch (-s) # cd into the created or existing worktree
941 + --no-submodules # Skip `git submodule update --init --recursive`
538 943 let path = (create-worktree $branch $base (not $no_submodules))
539 944 if $switch { cd $path }
543 948 export def --env "wt go" [
544 - branch: string
545 - --create (-c)
546 - --base (-b): string = $HEAD
547 - --no-submodules # with --create: skip submodule init
949 + branch: string # Branch/worktree to cd into
950 + --create (-c) # Create missing worktree without prompting
951 + --base (-b): string = $HEAD # Base ref when creating a missing branch/worktree
952 + --no-submodules # With create: skip submodule init
549 954 let path = (path-for-branch $branch)
550 955 if not ($path | path exists) {
551 956 if not $create {
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" [
561 - name?: string
562 - --branch (-b) # also delete git branch
563 - --force (-f)
974 + name?: string # Branch/worktree to remove; default current branch
975 + --branch (-b) # Also delete the local branch after removing the worktree
976 + --force (-f) # Skip prompts and force removal/deletion where Git allows
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" [
570 - branch?: string
571 - --force (-f)
983 + branch?: string # Branch/worktree to finish; default current branch
984 + --force (-f) # Skip prompts and force worktree removal + branch deletion
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
991 + let l = (layout)
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
998 + let l = (layout)
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)}
585 1004 # Thin native aliases for muscle memory / git parity.
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" [
1006 + branch?: string # Filter local/remote/PR picker; omitted opens full picker
1007 + --base (-b): string = $HEAD # Explicitly create a new branch from this base instead of picking
1008 + --switch (-s) # cd into the created worktree
1009 + --no-submodules # Skip submodule init in the created worktree
1010 +] {
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 }
1018 + $path
590 1021 export def "wt list" [] { wt ls }
592 -export def "wt lock" [branch?: string, reason?: string] {
1023 +export def "wt lock" [
1024 + branch?: string # Worktree/branch to lock; default current branch
1025 + reason?: string # Optional lock reason stored by Git
1026 +] {
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
1060 + return
1064 + error make {msg: $msg}
600 -export def "wt unlock" [branch?: string] {
1067 +export def "wt unlock" [
1068 + branch?: string # Worktree/branch to unlock; default current branch
1069 +] {
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)
1072 + let l = (layout)
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." }
1079 + return
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
607 - $dest
1084 +export def "wt move" [
1085 + branch: string # Worktree/branch to move
1086 + dest: path # New worktree path
1087 +] {
1088 + let l = (layout)
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" {
1117 + mv $source $dest
1118 + git-dir-output $l.git_dir ["worktree" "repair" $dest] | ignore
1119 + return $dest
1123 + error make {msg: $msg}
610 -export def --env "wt remove" [name?: string, --branch (-b), --force (-f)] {
1126 +export def --env "wt remove" [
1127 + name?: string # Branch/worktree to remove; default current branch
1128 + --branch (-b) # Also delete the local branch after removing the worktree
1129 + --force (-f) # Skip prompts and force removal/deletion where Git allows
1130 +] {
611 1131 wt rm $name --branch=$branch --force=$force

pi/agent/settings.json

Modified
14 14 "openai-codex/gpt-5.5",
15 15 "minimax/MiniMax-M3"
17 - "defaultProvider": "minimax",
18 - "defaultModel": "MiniMax-M3",
17 + "defaultProvider": "openai-codex",
18 + "defaultModel": "gpt-5.5",
19 19 "defaultThinkingLevel": "high",
20 20 "quietStartup": false,
21 21 "collapseChangelog": true,