[add] git helper to open all changed git files at once

This commit is contained in:
2026-03-30 17:00:58 -04:00
parent 07c630b2dd
commit d24dacb952

View File

@@ -1,3 +1,38 @@
-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here
vim.keymap.set("n", "<leader>gO", function()
local git_root = vim.fn.systemlist("git rev-parse --show-toplevel")[1]
if vim.v.shell_error ~= 0 or not git_root then
vim.notify("Not in a git repository", vim.log.levels.WARN)
return
end
local output = vim.fn.systemlist("git status --porcelain")
local files = {}
for _, line in ipairs(output) do
if line ~= "" then
local status = line:sub(1, 2)
-- skip deleted files
if not status:match("D") then
local filepath = line:sub(4)
-- handle renames: "old -> new"
local arrow_pos = filepath:find(" -> ")
if arrow_pos then
filepath = filepath:sub(arrow_pos + 4)
end
if filepath ~= "" then
table.insert(files, git_root .. "/" .. filepath)
end
end
end
end
if #files == 0 then
vim.notify("No modified files found", vim.log.levels.INFO)
return
end
for _, file in ipairs(files) do
vim.cmd.edit(vim.fn.fnameescape(file))
end
vim.notify("Opened " .. #files .. " modified file(s)", vim.log.levels.INFO)
end, { desc = "Open all git modified files" })