Files
nvim/lua/git/commit.lua
T

81 lines
2.3 KiB
Lua

local log = require("log")
local repo = require("git.repo")
local M = {}
---@param opts { amend: boolean? }?
function M.commit(opts)
local amend = opts and opts.amend or false
local path = vim.api.nvim_buf_get_name(0)
if path == "" then
path = vim.fn.getcwd()
end
local gitdir, worktree = repo.resolve(path)
if not gitdir or not worktree then
log.warning("not in a git repository")
return
end
local msg_path = vim.fs.joinpath(gitdir, "COMMIT_EDITMSG")
local initial = ""
if amend then
local result = vim.system(
{ "git", "log", "-1", "--pretty=%B" },
{ cwd = worktree, text = true }
):wait()
if result.code == 0 then
initial = (result.stdout or ""):gsub("\n+$", "")
else
log.warning("git log -1 failed: %s", vim.trim(result.stderr or ""))
end
end
local f, err = io.open(msg_path, "w")
if not f then
log.error("failed to open %s: %s", msg_path, err or "")
return
end
f:write(initial)
f:close()
vim.cmd("edit " .. vim.fn.fnameescape(msg_path))
local buf = vim.api.nvim_get_current_buf()
vim.bo[buf].filetype = "gitcommit"
vim.api.nvim_create_autocmd("BufWriteCmd", {
buffer = buf,
callback = function()
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local fw, werr = io.open(msg_path, "w")
if not fw then
log.error("failed to write %s: %s", msg_path, werr or "")
return
end
fw:write(table.concat(lines, "\n"))
fw:close()
vim.bo[buf].modified = false
local cmd = { "git", "commit", "-F", msg_path }
if amend then
table.insert(cmd, "--amend")
end
local result = vim.system(cmd, { cwd = worktree, text = true })
:wait()
if result.code ~= 0 then
log.error(
"git commit failed: %s",
vim.trim(result.stderr or "")
)
return
end
local out = vim.trim(result.stdout or "")
if out ~= "" then
log.info("%s", out)
end
vim.api.nvim_buf_delete(buf, { force = true })
end,
})
end
return M