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 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"]
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 + let cleaned = (
$segment | str replace --all --regex
$illegal '-' | str trim --char
'-')
150 + if (
$cleaned | is-empty) {
152 + }
else if ((
$cleaned | str uppercase)
in $reserved) {
151 - |
where {|seg|
$seg != null }
158 + |
where {|segment|
$segment != null }
172 179 {root:
$root, git_dir:
$git_dir, repo:
$repo, main:
$main}
185 +def branch-path [root: path, branch: string] {
186 + [
$root] | append (branch-slug
$branch | split row
"/") | path join
190 +def branch-root-entry [branch: string] {
191 + branch-slug
$branch | split row
"/" | first
176 195 def wt-path [branch: string] {
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] {
362 +def raw-add-candidates [include_prs: bool] {
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)
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)
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 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 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 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 }
963 985 export def --env
"wt go" [
nushell/tests/wt-smoke.nu
Addednew file mode 100644 6 +use ../modules/git.nu *
8 +def fail [message: string] {
9 + print $
"FAIL ($message)" 13 +def check [label: string, actual: any, expected: any] {
14 + if $actual ==
$expected {
15 + print $
"ok ($label)" 17 + fail $
"($label): got ($actual), want ($expected)" 21 +let tmp = (mktemp -d)
22 +let src = ([
$tmp "source"] | path join)
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
31 +^git commit -m seed | ignore
35 +for branch
in [
"features/make-it-good" "release/1.0"] {
36 + ^git branch
$branch trunk
40 +wt clone
$src repo --no-submodules | ignore
43 +check
"root" (
$root | path basename)
"repo" 44 +check
"main worktree is nested-free" (pwd | path basename)
"trunk" 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)
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" 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 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)
73 +print
"all wt smoke checks passed"