Skip to content

Commit 3fe0fd9

Browse files
committed
feat(nvim/git): easier workflow using git worktrees
1 parent 94ee011 commit 3fe0fd9

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

config/.gitconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
[push]
7979
default = current
8080
autoSetupRemote = true
81-
followTags = true
8281

8382
[fetch]
8483
prune = true

config/fish/conf.d/git.fish

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

config/fish/config.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ alias lazygit "TERM=xterm-256color command lazygit"
109109
abbr gg lazygit
110110
abbr gl 'git l --color | devmoji --log --color | less -rXF'
111111
abbr gs "git st"
112+
abbr gsw "git switch"
112113
abbr gb "git checkout -b"
113114
abbr gc "git commit"
114-
abbr gpr "gh pr checkout"
115115
abbr gm "git branch -l main | rg main > /dev/null 2>&1 && git checkout main || git checkout master"
116116
abbr gcp "git commit -p"
117117
abbr gpp "git push"

nvim/lua/config/lazy.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ vim.opt.rtp:prepend(lazypath)
1212

1313
local M = {}
1414

15+
local override = {} ---@type LazySpec
16+
17+
-- If we are in a git worktree, use this as the plugin dir
18+
local cwd = assert(vim.uv.cwd())
19+
if cwd:find("git-worktrees", 1, true) and vim.fn.isdirectory("lua") == 1 then
20+
local root = vim.fn.systemlist({ "git", "rev-parse", "--path-format=absolute", "--git-common-dir" })[1]
21+
if root and root ~= "" then
22+
root = vim.fs.dirname(root) ---@type string
23+
override = {
24+
name = vim.fs.basename(root),
25+
dir = cwd,
26+
}
27+
end
28+
end
29+
1530
---@param opts LazyConfig
1631
function M.load(opts)
1732
opts = vim.tbl_deep_extend("force", {
1833
spec = {
34+
override,
1935
{
2036
"LazyVim/LazyVim",
2137
import = "lazyvim.plugins",

0 commit comments

Comments
 (0)