feat: add vim.g.git_worktrees to enable usage of detached git working trees (#2092)
* 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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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 detached 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
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -12,5 +12,6 @@ return {
|
||||
changedelete = { text = get_icon "GitSign" },
|
||||
untracked = { text = get_icon "GitSign" },
|
||||
},
|
||||
worktrees = vim.g.git_worktrees,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user