repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(nushell): fast wt add by default (--pr opt-in) and hierarchical worktree paths

6783dcce912710ba9154158267994e36f6ecb27c

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

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (1)

+35 / −18 text lines

Diff layout

nushell/modules/git.nu

Modified

+35 / −18 text lines

134134 }
137# Branch name -> path-safe suffix.
137# Branch name -> path-safe suffix (supports nested paths).
138# Each path segment is normalized for win32/linx compatibility.
138139def branch-slug [branch: string] {
140 let illegal = '[<>:"|?*\\ -]'
141 let reserved = ["NUL" "CON" "PRN" "AUX" "COM1" "COM2" "COM3" "COM4" "COM5" "COM6" "COM7" "COM8" "COM9" "LPT1" "LPT2" "LPT3" "LPT4" "LPT5" "LPT6" "LPT7" "LPT8" "LPT9"]
139143 $branch
140 | str replace --all --regex '[^A-Za-z0-9._-]+' '-'
141 | str trim --char '-'
144 | split row "/"
145 | 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 }
150 }
151 | where {|seg| $seg != null }
152 | str join "/"
144155# URL/path -> repo name.
327338def dedupe-add-candidates [] {
328 reduce --fold [] {|candidate, acc|
329 if ($candidate.branch in ($acc | get --optional branch)) { $acc } else { $acc | append $candidate }
330 }
339 $in | sort-by branch | uniq-by branch
333def raw-add-candidates [] {
342def raw-add-candidates [include-prs: bool] {
334343 let l = (layout)
335344 let occupied = (worktrees-basic | where branch != null | get --optional branch)
336345 let remotes = (git-remotes $l.git_dir)
338347 local-add-candidates $l.git_dir
339348 | append (remote-add-candidates $l.git_dir)
340 | append (gh-pr-add-candidates $remotes)
341 | append (fj-pr-add-candidates $remotes)
349 | if $include-prs {
350 append (gh-pr-add-candidates $remotes)
351 | append (fj-pr-add-candidates $remotes)
352 } else { $in }
342353 | where {|candidate|
343354 (not ($candidate.branch in $occupied)) and (not ((wt-path $candidate.branch) | path exists))
344355 }
347def available-add-candidates [] {
348 raw-add-candidates | dedupe-add-candidates
358def available-add-candidates [include-prs: bool = false] {
359 raw-add-candidates $include-prs | dedupe-add-candidates
351362def add-candidate-matches [candidate: record, query: string] {
355366 $branch or $base or $display
358def pick-add-candidate [query?: string] {
369def pick-add-candidate [query?: string, include-prs: bool = false] {
359370 let matches = if $query == null {
360 available-add-candidates
371 available-add-candidates $include-prs
361372 } else {
362 raw-add-candidates | where {|candidate| add-candidate-matches $candidate $query }
373 raw-add-candidates $include-prs | where {|candidate| add-candidate-matches $candidate $query }
363374 }
365376 if ($matches | is-empty) {
549560 rm -rf $path
550561 }
563 # Create parent directories for nested worktree paths (e.g. features/make-it-good).
564 let parent = ($path | path dirname)
565 if ($parent != $path) {
566 mkdir $parent | ignore
567 }
552569 add-worktree $l.git_dir $path $branch $base $submodules
553570 configure-upstream-from-base $l.git_dir $branch $base
554571 $path
939956 --switch (-s) # cd into the created or existing worktree
940957 --no-submodules # Skip `git submodule update --init --recursive`
941958] {
942 let path = (create-worktree $branch $base (not $no_submodules))
943 if $switch { cd $path }
944 $path
959 create-worktree $branch $base (not $no_submodules)
960 | if $switch { cd $in; $in } else { $in }
947963export def --env "wt go" [
10061022 --base (-b): string = $HEAD # Explicitly create a new branch from this base instead of picking
10071023 --switch (-s) # cd into the created worktree
10081024 --no-submodules # Skip submodule init in the created worktree
1025 --pr # Only show PR candidates (gh/fj); local/remote branches excluded
10091026] {
10101027 if $branch != null and $base != $HEAD {
10111028 return (wt new $branch --base $base --switch=$switch --no-submodules=$no_submodules)
1014 let selected = (pick-add-candidate $branch)
1031 let selected = (pick-add-candidate $branch $pr)
10151032 let path = (create-picked-worktree $selected (not $no_submodules))
10161033 if $switch { cd $path }
10171034 $path