Files
nvim/lua/git/log.lua
T

63 lines
1.4 KiB
Lua

local git = require("git")
local log = require("log")
local repo = require("git.repo")
local util = require("util")
local M = {}
local LOG_FORMAT = "%h %ad {%an}%d %s"
local DEFAULT_MAX_COUNT = 1000
---@class ow.Git.LogOpts
---@field max_count integer? cap on commits to show. Nil uses the default, <= 0 means "all"
---@param opts ow.Git.LogOpts?
function M.show(opts)
opts = opts or {}
local max_count = opts.max_count
if max_count == nil then
max_count = DEFAULT_MAX_COUNT
end
local _, worktree = repo.resolve_cwd()
if not worktree then
log.warning("not in a git repository")
return
end
local cmd = {
"git",
"log",
"--graph",
"--all",
"--decorate",
"--date=short",
"--format=format:" .. LOG_FORMAT,
}
if max_count > 0 then
table.insert(cmd, "--max-count=" .. max_count)
end
local result = vim.system(cmd, { cwd = worktree, text = true }):wait()
if result.code ~= 0 then
log.error("git log failed: %s", vim.trim(result.stderr or ""))
return
end
local buf = git.new_scratch()
vim.b[buf].git_worktree = worktree
vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(
buf,
0,
-1,
false,
util.split_lines(result.stdout or "")
)
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
vim.bo[buf].filetype = "gitlog"
end
return M