|
| 1 | +set -g git_worktrees_root ~/projects/git-worktrees |
| 2 | +mkdir -p $git_worktrees_root |
| 3 | + |
| 4 | +function gw -a name |
| 5 | + if test -z "$name" |
| 6 | + gwm # switch to main or latest worktree |
| 7 | + return 0 |
| 8 | + end |
| 9 | + |
| 10 | + set -l repo (basename (git rev-parse --show-toplevel)) |
| 11 | + set -l path $git_worktrees_root/$repo-$name |
| 12 | + |
| 13 | + if not test -d $path |
| 14 | + git worktree add $path -b $name |
| 15 | + echo "Created worktree '$name' at $path" |
| 16 | + end |
| 17 | + cd $path |
| 18 | +end |
| 19 | + |
| 20 | +function gpr -a pr |
| 21 | + if test -z "$pr" |
| 22 | + echo "Usage: gpr <pr-number-or-branch>" |
| 23 | + return 1 |
| 24 | + end |
| 25 | + |
| 26 | + set -l branch (gh pr view "$pr" --json headRefName -q .headRefName) |
| 27 | + set -l repo (basename (git rev-parse --show-toplevel)) |
| 28 | + set -l path $git_worktrees_root/$repo-$pr-$branch |
| 29 | + |
| 30 | + if not test -d $path |
| 31 | + git worktree add $path || return 1 |
| 32 | + cd $path # needed for gh to work correctly |
| 33 | + gh pr checkout $pr || return 1 |
| 34 | + end |
| 35 | + cd $path # switch to the worktree |
| 36 | +end |
| 37 | + |
| 38 | +function gwl |
| 39 | + set -l current (pwd) |
| 40 | + set -l worktrees (git worktree list | awk '{print $1}') |
| 41 | + set -l filtered |
| 42 | + for wt in $worktrees |
| 43 | + if test "$wt" != "$current" |
| 44 | + set -a filtered $wt |
| 45 | + end |
| 46 | + end |
| 47 | + set -l selected (printf '%s\n' $filtered | fzf) |
| 48 | + if test -n "$selected" |
| 49 | + cd $selected |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +function gwr |
| 54 | + set -l selected (git worktree list | tail -n +2 | fzf | awk '{print $1}') |
| 55 | + if test -n "$selected" |
| 56 | + set -l main (git worktree list | head -n 1 | awk '{print $1}') |
| 57 | + cd $main |
| 58 | + git worktree remove $selected |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +function gwm |
| 63 | + set -l current (pwd) |
| 64 | + set -l main (git worktree list | head -n 1 | awk '{print $1}') |
| 65 | + |
| 66 | + if test "$current" = "$main" |
| 67 | + # We're in main, go to most recent worktree |
| 68 | + set -l latest (git worktree list | tail -n 1 | awk '{print $1}') |
| 69 | + if test "$latest" != "$main" |
| 70 | + cd $latest |
| 71 | + end |
| 72 | + else |
| 73 | + # We're in a worktree, go to main |
| 74 | + cd $main |
| 75 | + end |
| 76 | +end |
0 commit comments