local repo = require("git.repo") local util = require("git.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.open(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 util.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 stdout = util.exec(cmd, { cwd = worktree }) if not stdout then return end -- `hide` keeps the log around when the user navigates into a commit -- via `` (which replaces the current buffer); `` jumps back. local buf = util.new_scratch({ bufhidden = "hide" }) vim.b[buf].git_worktree = worktree vim.bo[buf].modifiable = true vim.api.nvim_buf_set_lines(buf, 0, -1, false, util.split_lines(stdout)) vim.bo[buf].modifiable = false vim.bo[buf].modified = false vim.bo[buf].filetype = "gitlog" end return M