feat: git log

This commit is contained in:
Folke Lemaitre 2022-11-22 21:12:50 +01:00
parent 54d5ff18f5
commit 3218c2d9ec
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
7 changed files with 83 additions and 6 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tt.lua

View File

@ -53,6 +53,9 @@ function M.run(operation, opts, filter)
runner:add(Task.new(plugin, "run")) runner:add(Task.new(plugin, "run"))
end end
plugin.dirty = false plugin.dirty = false
if operation == "update" then
runner:add(Task.new(plugin, "log"))
end
end end
-- wait for post-install to finish -- wait for post-install to finish
runner:wait(on_done) runner:wait(on_done)
@ -85,6 +88,14 @@ function M.update(opts)
end) end)
end end
---@param opts? ManagerOpts
function M.log(opts)
---@param plugin LazyPlugin
M.run("log", opts, function(plugin)
return plugin.uri and plugin.installed
end)
end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.clean(opts) function M.clean(opts)
opts = opts or {} opts = opts or {}

View File

@ -8,7 +8,7 @@ local Util = require("lazy.util")
---@field running boolean ---@field running boolean
local Task = {} local Task = {}
---@alias TaskType "update"|"install"|"run"|"clean" ---@alias TaskType "update"|"install"|"run"|"clean"|"log"
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param type TaskType ---@param type TaskType
@ -27,7 +27,6 @@ end
function Task:_done() function Task:_done()
self.running = false self.running = false
vim.cmd("do User LazyRender") vim.cmd("do User LazyRender")
end end
@ -54,7 +53,7 @@ function Task:clean()
end end
self.plugin.installed = false self.plugin.installed = false
self.running = false self:_done()
end end
function Task:install() function Task:install()
@ -161,16 +160,38 @@ function Task:start()
self:run() self:run()
elseif self.type == "clean" then elseif self.type == "clean" then
self:clean() self:clean()
elseif self.type == "log" then
self:log()
end end
end) end)
if not ok then if not ok then
self.error = err or "failed" self.error = err or "failed"
self:_done() self:_done()
end end
end end
function Task:log()
if not Util.file_exists(self.plugin.dir .. "/.git") then
self:_done()
return
end
local args = {
"log",
"--pretty=format:%h %s (%cr)",
"--abbrev-commit",
"--decorate",
"--date=short",
"--color=never",
"--since='7 days ago'",
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end
function Task:update() function Task:update()
if Util.file_exists(self.plugin.uri) then if Util.file_exists(self.plugin.uri) then
if vim.loop.fs_realpath(self.plugin.uri) ~= vim.loop.fs_realpath(self.plugin.dir) then if vim.loop.fs_realpath(self.plugin.uri) ~= vim.loop.fs_realpath(self.plugin.dir) then
@ -180,7 +201,6 @@ function Task:update()
}) })
vim.opt.runtimepath:append(self.plugin.uri) vim.opt.runtimepath:append(self.plugin.uri)
end end
self:_done() self:_done()
else else
local args = { local args = {

View File

@ -21,6 +21,9 @@ M.commands = {
install = function() install = function()
Manager.install({ clear = true, show = true }) Manager.install({ clear = true, show = true })
end, end,
log = function()
Manager.log({ clear = true, show = true })
end,
show = function() show = function()
View.show() View.show()
end, end,

View File

@ -119,6 +119,25 @@ function M:plugin(plugin)
self:nl() self:nl()
end end
end end
elseif task.type == "log" then
local log = vim.trim(task.output)
if log ~= "" then
local lines = vim.split(log, "\n")
for l, line in ipairs(lines) do
if l == 1 then
self:nl()
end
local ref, msg, time = line:match("^(%w+) (.*) (%(.*%))$")
self:append(" " .. ref .. " ", "@variable.builtin")
local col = self:col()
self:append(msg)
-- string.gsub
self:append(" " .. time, "Comment")
if l ~= #lines then
self:nl()
end
end
end
end end
end end
end end

View File

@ -51,6 +51,14 @@ return {
end, end,
title = "Running", title = "Running",
}, },
{
filter = function(plugin)
return has_task(plugin, function(task)
return task.type == "log" and vim.trim(task.output) ~= ""
end)
end,
title = "Log",
},
{ {
filter = function(plugin) filter = function(plugin)
return plugin.installed and not plugin.uri return plugin.installed and not plugin.uri

View File

@ -16,7 +16,7 @@ function Text.new()
end end
---@param str string ---@param str string
---@param hl string|table ---@param hl? string|table
function Text:append(str, hl) function Text:append(str, hl)
if #self._lines == 0 then if #self._lines == 0 then
self:nl() self:nl()
@ -84,4 +84,19 @@ function Text:trim()
end end
end end
function Text:row()
return #self._lines == 0 and 1 or #self._lines
end
function Text:col()
if #self._lines == 0 then
return 0
end
local width = 0
for _, segment in ipairs(self._lines[#self._lines]) do
width = width + vim.fn.strlen(segment.str)
end
return width
end
return Text return Text