You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

180 lines
6.4 KiB

return {
{
"tpope/vim-fugitive",
event = "VeryLazy",
dependencies = {
"shumphrey/fugitive-gitlab.vim",
},
config = function()
-- Git diff settings
vim.opt.diffopt:append("vertical")
vim.opt.display:append("lastline")
-- Helper function for consistent keymap creation
local function map(mode, lhs, rhs, desc, opts)
opts = opts or {}
opts.desc = desc
opts.noremap = true
opts.silent = true
vim.keymap.set(mode, lhs, rhs, opts)
end
-- Buffer-local mappings for fugitive buffers
local function set_fugitive_buffer_mappings()
local buf_opts = { buffer = 0, noremap = true, silent = true }
-- Git operations
map("n", "<leader>gp", "<cmd>Git push<CR>", "Git push", buf_opts)
map("n", "<leader>gP", "<cmd>Git pull<CR>", "Git pull", buf_opts)
map("n", "<leader>gc", "<cmd>Git commit<CR>", "Git commit", buf_opts)
-- Additional useful fugitive buffer mappings
map("n", "q", "<cmd>close<CR>", "Close fugitive buffer", buf_opts)
map("n", "<leader>gd", "<cmd>Gvdiffsplit<CR>", "Git diff split", buf_opts)
map("n", "<leader>gb", "<cmd>Git blame<CR>", "Git blame", buf_opts)
end
-- Auto-command for fugitive buffer mappings
local fugitive_augroup = vim.api.nvim_create_augroup("fugitive_mappings", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
group = fugitive_augroup,
pattern = "fugitive",
callback = set_fugitive_buffer_mappings,
desc = "Set fugitive buffer-local mappings",
})
-- Enhanced line-based git log function
local function git_log_range()
local mode = vim.fn.mode()
local start_line, end_line
if mode == 'V' or mode == 'v' then
-- Visual mode - get selected range
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>")
start_line = start_pos[2]
end_line = end_pos[2]
else
-- Normal mode - use current line
start_line = vim.fn.line(".")
end_line = start_line
end
-- Validate line numbers
if start_line <= 0 or end_line <= 0 then
vim.notify("Invalid line range selected", vim.log.levels.WARN)
return
end
local filepath = vim.fn.expand("%:.")
if filepath == "" then
vim.notify("No file in current buffer", vim.log.levels.WARN)
return
end
-- Ensure proper order
if start_line > end_line then
start_line, end_line = end_line, start_line
end
local cmd = string.format("Git log -L %d,%d:%s", start_line, end_line, filepath)
vim.cmd(cmd)
end
-- Global git mappings
map("n", "<leader>gg", function()
vim.cmd("Git")
end, "Open git status")
map("n", "<leader>gl", function()
vim.cmd("Git log --oneline -20")
end, "Git log (last 20)")
map("n", "<leader>gL", function()
vim.cmd("Git log")
end, "Git log (detailed)")
map({ "n", "v" }, "<leader>gr", git_log_range, "Git log for range/line")
map("n", "<leader>gs", function()
vim.cmd("Git status")
end, "Git status")
map("n", "<leader>gd", function()
vim.cmd("Gvdiffsplit")
end, "Git diff split")
map("n", "<leader>gb", function()
vim.cmd("Git blame")
end, "Git blame")
map("n", "<leader>ga", function()
vim.cmd("Git add %")
end, "Git add current file")
map("n", "<leader>gA", function()
vim.cmd("Git add .")
end, "Git add all")
-- Git stash operations
map("n", "<leader>gss", function()
vim.cmd("Git stash")
end, "Git stash")
map("n", "<leader>gsp", function()
vim.cmd("Git stash pop")
end, "Git stash pop")
map("n", "<leader>gsl", function()
vim.cmd("Git stash list")
end, "Git stash list")
-- Branch operations
map("n", "<leader>gco", function()
local branch = vim.fn.input("Branch name: ")
if branch ~= "" then
vim.cmd("Git checkout " .. branch)
end
end, "Git checkout branch")
map("n", "<leader>gcb", function()
local branch = vim.fn.input("New branch name: ")
if branch ~= "" then
vim.cmd("Git checkout -b " .. branch)
end
end, "Git create and checkout branch")
-- Additional autocmds for better fugitive experience
vim.api.nvim_create_autocmd("FileType", {
group = fugitive_augroup,
pattern = "gitcommit",
callback = function()
-- Enable spell checking for commit messages
vim.opt_local.spell = true
vim.opt_local.spelllang = "en_us"
-- Position cursor at the beginning
vim.cmd("startinsert")
end,
desc = "Git commit buffer settings",
})
-- Auto-close fugitive buffers when done
vim.api.nvim_create_autocmd("BufReadPost", {
group = fugitive_augroup,
pattern = "fugitive:*",
callback = function()
vim.opt_local.bufhidden = "delete"
end,
desc = "Auto-delete fugitive buffers",
})
end,
},
{
"rbong/vim-flog",
lazy = true,
cmd = { "Flog", "Flogsplit", "Floggit" },
dependencies = {
"tpope/vim-fugitive",
},
},
}