repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

fix(nushell): make wt actually load, join nested worktree paths natively, dedupe filtered candidates

3f69953e1210f1d12461b2144596555f72c2d985

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (2)

nushell/modules/git.nu

Modified
137 -# Branch name -> path-safe suffix (supports nested paths).
138 -# Each path segment is normalized for win32/linx compatibility.
137 +# Branch name -> path-safe suffix.
138 +# Slashes stay slashes, so `features/make-it-good` keeps its folder hierarchy.
139 +# Every segment is normalized on its own: characters that win32 rejects in file
140 +# names become `-`, and DOS device names get a trailing `-` so the path stays
141 +# usable on both win32 and linux.
139 142 def branch-slug [branch: string] {
140 - let illegal = '[<>:"|?*\\ -]'
143 + let illegal = '[<>:"|?*\\\s-]+'
141 144 let reserved = ["NUL" "CON" "PRN" "AUX" "COM1" "COM2" "COM3" "COM4" "COM5" "COM6" "COM7" "COM8" "COM9" "LPT1" "LPT2" "LPT3" "LPT4" "LPT5" "LPT6" "LPT7" "LPT8" "LPT9"]
143 146 $branch
144 147 | split row "/"
145 148 | each {|segment|
146 - let cleaned = ($segment | str replace --all --regex $illegal '-' | str replace --all --regex '(-)+' '-' | str trim --char '-')
147 - if ($cleaned | is-empty) { null }
148 - else if ($cleaned | str uppercase) in $reserved { $"($cleaned)-" }
149 - else { $cleaned }
149 + let cleaned = ($segment | str replace --all --regex $illegal '-' | str trim --char '-')
150 + if ($cleaned | is-empty) {
151 + null
152 + } else if (($cleaned | str uppercase) in $reserved) {
153 + $"($cleaned)-"
154 + } else {
155 + $cleaned
151 - | where {|seg| $seg != null }
158 + | where {|segment| $segment != null }
152 159 | str join "/"
172 179 {root: $root, git_dir: $git_dir, repo: $repo, main: $main}
182 +# <root>/<branch-slug> as a native path.
183 +# The slug may contain `/`, so join it segment-wise instead of embedding a
184 +# foreign separator into a win32 path.
185 +def branch-path [root: path, branch: string] {
186 + [$root] | append (branch-slug $branch | split row "/") | path join
189 +# Top-level entry that a branch's worktree occupies inside the layout root.
190 +def branch-root-entry [branch: string] {
191 + branch-slug $branch | split row "/" | first
175 194 # Computed conventional worktree path for branch.
176 195 def wt-path [branch: string] {
177 - let l = (layout)
178 - [$l.root (branch-slug $branch)] | path join
196 + branch-path (layout).root $branch
181 199 def empty-worktree-record [] {
339 357 $in | sort-by branch | uniq-by branch
342 -def raw-add-candidates [include-prs: bool] {
360 +# Candidate branches for `wt add`.
361 +# PR lookups spawn `gh`/`fj` with network round trips, so they stay opt-in.
362 +def raw-add-candidates [include_prs: bool] {
343 363 let l = (layout)
344 364 let occupied = (worktrees-basic | where branch != null | get --optional branch)
345 - let remotes = (git-remotes $l.git_dir)
347 - local-add-candidates $l.git_dir
348 - | append (remote-add-candidates $l.git_dir)
349 - | if $include-prs {
350 - append (gh-pr-add-candidates $remotes)
351 - | append (fj-pr-add-candidates $remotes)
352 - } else { $in }
353 - | where {|candidate|
366 + let candidates = if $include_prs {
367 + let remotes = (git-remotes $l.git_dir)
368 + gh-pr-add-candidates $remotes | append (fj-pr-add-candidates $remotes)
369 + } else {
370 + local-add-candidates $l.git_dir | append (remote-add-candidates $l.git_dir)
373 + $candidates | where {|candidate|
354 374 (not ($candidate.branch in $occupied)) and (not ((wt-path $candidate.branch) | path exists))
358 -def available-add-candidates [include-prs: bool = false] {
359 - raw-add-candidates $include-prs | dedupe-add-candidates
378 +def available-add-candidates [include_prs: bool] {
379 + raw-add-candidates $include_prs | dedupe-add-candidates
362 382 def add-candidate-matches [candidate: record, query: string] {
366 386 $branch or $base or $display
369 -def pick-add-candidate [query?: string, include-prs: bool = false] {
389 +def pick-add-candidate [query: any, include_prs: bool] {
390 + let candidates = (available-add-candidates $include_prs)
370 391 let matches = if $query == null {
371 - available-add-candidates $include-prs
392 + $candidates
372 393 } else {
373 - raw-add-candidates $include-prs | where {|candidate| add-candidate-matches $candidate $query }
394 + $candidates | where {|candidate| add-candidate-matches $candidate $query }
376 397 if ($matches | is-empty) {
695 716 ensure-upstream $git_dir $chosen_branch
696 717 git-dir-complete $git_dir ["fetch" "--all" "--prune"] | ignore
698 - let worktree = ([$root (branch-slug $chosen_branch)] | path join)
719 + let worktree = (branch-path $root $chosen_branch)
699 720 git-dir-output $git_dir ["worktree" "add" $worktree $chosen_branch] | ignore
700 721 init-submodules $worktree $submodules
701 722 cd $worktree
724 745 let src = (git-output ["rev-parse" "--show-toplevel"] | path expand)
725 746 let chosen_branch = (selected-branch $branch)
726 - let target = ([$src (branch-slug $chosen_branch)] | path join)
747 + let target = (branch-path $src $chosen_branch)
727 748 let status_before = (status-porcelain $src)
728 749 let is_dirty = (not ($status_before | is-empty))
880 901 copy-dirty-state $migration $patches
883 - clear-stale-files $src [$DOT_GIT (branch-slug $migration.branch) ".git.wt-backup"]
904 + clear-stale-files $src [$DOT_GIT (branch-root-entry $migration.branch) ".git.wt-backup"]
884 905 init-submodules $migration.target_worktree $submodules
886 907 rm -rf $old_git_backup
956 977 --switch (-s) # cd into the created or existing worktree
957 978 --no-submodules # Skip `git submodule update --init --recursive`
959 - create-worktree $branch $base (not $no_submodules)
960 - | if $switch { cd $in; $in } else { $in }
980 + let path = (create-worktree $branch $base (not $no_submodules))
981 + if $switch { cd $path }
982 + $path
963 985 export def --env "wt go" [

nushell/tests/wt-smoke.nu

Added
new file mode 100644
1 +# End-to-end smoke test for the `wt` worktree helpers.
2 +# Builds a throwaway source repo, converts it with `wt clone`, then exercises
3 +# path mapping, creation, listing and `wt add` without PR lookups.
4 +# Run: nu nushell/tests/wt-smoke.nu
6 +use ../modules/git.nu *
8 +def fail [message: string] {
9 + print $"FAIL ($message)"
10 + exit 1
13 +def check [label: string, actual: any, expected: any] {
14 + if $actual == $expected {
15 + print $"ok ($label)"
16 + } else {
17 + fail $"($label): got ($actual), want ($expected)"
18 + }
21 +let tmp = (mktemp -d)
22 +let src = ([$tmp "source"] | path join)
24 +mkdir $src
25 +cd $src
26 +^git init --initial-branch trunk . | ignore
27 +^git config user.email test@example.com
28 +^git config user.name test
29 +"seed" | save --force seed.txt
30 +^git add seed.txt
31 +^git commit -m seed | ignore
33 +# Note: Windows cannot store loose refs named `CON` or containing `<`, so those
34 +# names are only exercised through the path mapping, never as real branches here.
35 +for branch in ["features/make-it-good" "release/1.0"] {
36 + ^git branch $branch trunk
39 +cd $tmp
40 +wt clone $src repo --no-submodules | ignore
42 +let root = (wt root)
43 +check "root" ($root | path basename) "repo"
44 +check "main worktree is nested-free" (pwd | path basename) "trunk"
46 +# Branch name -> path mapping.
47 +check "flat branch" (wt path "trunk") ([$root "trunk"] | path join)
48 +check "nested branch keeps hierarchy" (wt path "features/make-it-good") ([$root "features" "make-it-good"] | path join)
49 +check "win32-illegal chars normalized" (wt path "weird<name>") ([$root "weird-name"] | path join)
50 +check "DOS reserved name escaped" (wt path "CON") ([$root "CON-"] | path join)
52 +# Creating a nested worktree must create parent directories.
53 +let nested = (wt new "features/make-it-good" --no-submodules)
54 +check "nested worktree path" $nested ([$root "features" "make-it-good"] | path join)
55 +check "nested worktree exists" ($nested | path exists) true
57 +check "wt ls sees two worktrees" (wt ls | length) 2
58 +check "wt ls marks current" (wt ls | where current == true | get branch | first) "trunk"
60 +# `wt add` without --pr must not touch gh/fj and must resolve a local branch.
61 +let added = (wt add "release/1.0" --no-submodules)
62 +check "wt add nested path" $added ([$root "release" "1.0"] | path join)
63 +check "wt add created worktree" ($added | path exists) true
64 +check "wt ls sees three worktrees" (wt ls | length) 3
66 +# `--pr` only looks at PRs; this fixture has none, so it must fail cleanly
67 +# instead of falling back to local or remote branches.
68 +let pr_error = (try { wt add --pr --no-submodules; "no error" } catch {|e| $e.msg })
69 +check "wt add --pr ignores branches" ($pr_error | str contains "no branches or PRs available") true
71 +cd ($tmp | path dirname)
72 +rm -rf $tmp
73 +print "all wt smoke checks passed"