dotfiles/.config/nvim/lua/plugins/git-worktree.lua

52 lines
1.8 KiB
Lua

return {
"polarmutex/git-worktree.nvim",
name = "git-worktree",
config = function()
require("telescope").load_extension("git_worktree")
vim.keymap.set(
"n",
"<leader>gw",
"<CMD>lua require('telescope').extensions.git_worktree.git_worktrees()<CR>",
{ desc = "worktrees" }
)
vim.keymap.set(
"n",
"<leader>gW",
"<CMD>lua require('telescope').extensions.git_worktree.create_git_worktree()<CR>",
{ desc = "worktree create" }
)
end,
opts = function()
local Worktree = require("git-worktree")
Worktree.on_tree_change(function(op, metadata)
if op == Worktree.Operations.Create then
local Path = require("plenary.path")
local Job = require("plenary.job")
-- If we're dealing with create, the path is relative to the worktree and not absolute
-- so we need to convert it to an absolute path.
local git_path = metadata.path
if not Path:new(git_path):is_absolute() then
git_path = Path:new():absolute()
if git_path:sub(-#"/") == "/" then
git_path = string.sub(git_path, 1, string.len(git_path) - 1)
end
end
print(git_path)
-- git_path = string.match(git_path, "(.-)%.git") .. ".git"
-- bare_repo_path =
if Path:new(git_path .. "/.symlink"):exists() then
local worktree_path = git_path .. "/" .. metadata.path .. "/"
local link_gitignored_cmd = "ln -s " .. git_path .. "/.symlink/* " .. worktree_path
os.execute(link_gitignored_cmd)
end
-- Job:new({
-- "git",
-- "branch",
-- "-u origin/" .. metadata.branch,
-- }):start()
--TODO setup branch tracking via `git branch -u origin/<branch-name>`
end
end)
end,
}