Skip to content

Commit

Permalink
feat: add vim.g.git_worktrees to enable usage of git worktrees (#2092)
Browse files Browse the repository at this point in the history
* feat(options): add setting to enable git integration for custom worktrees

* fix(autocmds): add additional check for AstroGitFile to see if file is in custom worktree

* refactor(autocmds): extract `in_worktree()` to custom utility function `find_worktree()`

* feat(mappings): allow toggle lazygit to automatically load git worktrees if available

* chore(mappings): cleanup redundant toggle lazygit mapping
  • Loading branch information
Subjective authored and mehalter committed Jul 5, 2023
1 parent 068c626 commit eb2d350
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lua/astronvim/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ autocmd({ "BufReadPost", "BufNewFile", "BufWritePost" }, {
callback = function(args)
if not (vim.fn.expand "%" == "" or vim.api.nvim_get_option_value("buftype", { buf = args.buf }) == "nofile") then
astroevent "File"
if utils.cmd({ "git", "-C", vim.fn.expand "%:p:h", "rev-parse" }, false) then
if
utils.cmd({ "git", "-C", vim.fn.expand "%:p:h", "rev-parse" }, false)
or vim.g.git_worktrees
and require("astronvim.utils.git").file_worktree(vim.fn.expand "%", vim.g.git_worktrees)
then
astroevent "GitFile"
vim.api.nvim_del_augroup_by_name "file_user_events"
end
Expand Down
14 changes: 12 additions & 2 deletions lua/astronvim/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,18 @@ if is_available "toggleterm.nvim" then
maps.n["<leader>t"] = sections.t
if vim.fn.executable "lazygit" == 1 then
maps.n["<leader>g"] = sections.g
maps.n["<leader>gg"] = { function() utils.toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" }
maps.n["<leader>tl"] = { function() utils.toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" }
maps.n["<leader>gg"] = {
function()
local worktree = vim.g.git_worktrees
and require("astronvim.utils.git").file_worktree(vim.fn.expand "%", vim.g.git_worktrees)
local lazygit_cmd = worktree
and ("lazygit --work-tree=" .. worktree.toplevel .. " --git-dir=" .. worktree.gitdir)
or "lazygit"
utils.toggle_term_cmd(lazygit_cmd)
end,
desc = "ToggleTerm lazygit",
}
maps.n["<leader>tl"] = maps.n["<leader>gg"]
end
if vim.fn.executable "node" == 1 then
maps.n["<leader>tn"] = { function() utils.toggle_term_cmd "node" end, desc = "ToggleTerm node" }
Expand Down
1 change: 1 addition & 0 deletions lua/astronvim/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ local options = astronvim.user_opts("options", {
lsp_handlers_enabled = true, -- enable or disable default vim.lsp.handlers (hover and signatureHelp)
semantic_tokens_enabled = true, -- enable or disable LSP semantic tokens on startup
ui_notifications_enabled = true, -- disable notifications (TODO: rename to notifications_enabled in AstroNvim v4)
git_worktrees = nil, -- enable git integration for custom worktrees (specify a table where each entry is of the form { toplevel = vim.env.HOME, gitdir=vim.env.HOME .. "/.dotfiles" })
},
t = vim.t.bufs and vim.t.bufs or { bufs = vim.api.nvim_list_bufs() }, -- initialize buffers for the current tab
})
Expand Down
23 changes: 23 additions & 0 deletions lua/astronvim/utils/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,27 @@ function git.pretty_changelog(commits)
return changelog
end

--- Get the first worktree that a file belongs to
---@param file string the file to check
---@param worktrees table<string, string>[] an array like table of worktrees with entries `toplevel` and `gitdir`
---@return table<string, string>|nil # a table specifying the `toplevel` and `gitdir` of a worktree or nil if not found
function git.file_worktree(file, worktrees)
for _, worktree in ipairs(worktrees) do
if
require("astronvim.utils").cmd({
"git",
"--work-tree",
worktree.toplevel,
"--git-dir",
worktree.gitdir,
"ls-files",
"--error-unmatch",
file,
}, false)
then
return worktree
end
end
end

return git
1 change: 1 addition & 0 deletions lua/plugins/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ return {
changedelete = { text = get_icon "GitSign" },
untracked = { text = get_icon "GitSign" },
},
worktrees = vim.g.git_worktrees,
},
}

0 comments on commit eb2d350

Please sign in to comment.