Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(options): add setting to enable git integration for custom worktrees #2092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
}