2022-11-28 10:36:12 +00:00
|
|
|
local Git = require("lazy.manage.git")
|
2022-11-28 23:15:13 +00:00
|
|
|
local Lock = require("lazy.manage.lock")
|
2022-11-29 14:25:09 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-28 10:04:32 +00:00
|
|
|
|
|
|
|
---@type table<string, LazyTaskDef>
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.log = {
|
2022-11-29 14:25:09 +00:00
|
|
|
---@param opts {updated?:boolean, check?: boolean}
|
2022-11-28 12:10:52 +00:00
|
|
|
skip = function(plugin, opts)
|
2022-12-21 13:39:08 +00:00
|
|
|
if opts.check and plugin.pin then
|
|
|
|
return true
|
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
if opts.updated and not (plugin._.updated and plugin._.updated.from ~= plugin._.updated.to) then
|
|
|
|
return true
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
2022-12-20 18:58:48 +00:00
|
|
|
local stat = vim.loop.fs_stat(plugin.dir .. "/.git")
|
|
|
|
return stat and stat.type ~= "directory"
|
2022-11-28 10:04:32 +00:00
|
|
|
end,
|
2022-11-29 14:25:09 +00:00
|
|
|
---@param opts {args?: string[], updated?:boolean, check?:boolean}
|
2022-11-28 21:03:44 +00:00
|
|
|
run = function(self, opts)
|
2022-11-28 10:04:32 +00:00
|
|
|
local args = {
|
|
|
|
"log",
|
|
|
|
"--pretty=format:%h %s (%cr)",
|
|
|
|
"--abbrev-commit",
|
|
|
|
"--decorate",
|
|
|
|
"--date=short",
|
|
|
|
"--color=never",
|
|
|
|
}
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
if opts.updated then
|
2022-11-28 10:19:50 +00:00
|
|
|
table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD"))
|
2022-11-29 07:23:23 +00:00
|
|
|
elseif opts.check then
|
|
|
|
local info = assert(Git.info(self.plugin.dir))
|
|
|
|
local target = assert(Git.get_target(self.plugin))
|
2022-12-02 10:26:07 +00:00
|
|
|
assert(target.commit, self.plugin.name .. " " .. target.branch)
|
2022-12-05 19:49:23 +00:00
|
|
|
self.plugin._.has_updates = target.commit ~= info.commit
|
2022-11-29 07:23:23 +00:00
|
|
|
table.insert(args, info.commit .. ".." .. target.commit)
|
2022-11-29 09:55:49 +00:00
|
|
|
else
|
2022-11-29 14:25:09 +00:00
|
|
|
vim.list_extend(args, opts.args or Config.options.git.log)
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self:spawn("git", {
|
|
|
|
args = args,
|
|
|
|
cwd = self.plugin.dir,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
M.clone = {
|
|
|
|
skip = function(plugin)
|
|
|
|
return plugin._.installed or plugin._.is_local
|
|
|
|
end,
|
2022-11-28 10:04:32 +00:00
|
|
|
run = function(self)
|
2022-11-28 21:03:44 +00:00
|
|
|
local args = {
|
|
|
|
"clone",
|
2022-12-06 10:12:54 +00:00
|
|
|
self.plugin.url,
|
2022-11-28 21:03:44 +00:00
|
|
|
"--filter=blob:none",
|
|
|
|
"--recurse-submodules",
|
2022-12-21 22:13:18 +00:00
|
|
|
"--also-filter-submodules",
|
2022-11-28 21:03:44 +00:00
|
|
|
"--single-branch",
|
|
|
|
"--progress",
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.plugin.branch then
|
|
|
|
vim.list_extend(args, { "-b", self.plugin.branch })
|
2022-11-28 12:10:52 +00:00
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
|
|
|
|
table.insert(args, self.plugin.dir)
|
|
|
|
self:spawn("git", {
|
|
|
|
args = args,
|
|
|
|
on_exit = function(ok)
|
|
|
|
if ok then
|
|
|
|
self.plugin._.cloned = true
|
|
|
|
self.plugin._.installed = true
|
|
|
|
self.plugin._.dirty = true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:26:07 +00:00
|
|
|
-- setup origin branches if needed
|
|
|
|
-- fetch will retrieve the data
|
2022-11-28 21:03:44 +00:00
|
|
|
M.branch = {
|
|
|
|
skip = function(plugin)
|
|
|
|
if not plugin._.installed or plugin._.is_local then
|
|
|
|
return true
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
local branch = assert(Git.get_branch(plugin))
|
2022-12-02 10:26:07 +00:00
|
|
|
return Git.get_commit(plugin.dir, branch)
|
2022-11-28 21:03:44 +00:00
|
|
|
end,
|
|
|
|
run = function(self)
|
|
|
|
local args = {
|
|
|
|
"remote",
|
|
|
|
"set-branches",
|
|
|
|
"--add",
|
|
|
|
"origin",
|
2022-12-02 10:26:07 +00:00
|
|
|
assert(Git.get_branch(self.plugin)),
|
2022-11-28 21:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self:spawn("git", {
|
|
|
|
args = args,
|
|
|
|
cwd = self.plugin.dir,
|
|
|
|
})
|
2022-11-28 10:04:32 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:26:07 +00:00
|
|
|
-- fetches all needed origin branches
|
2022-11-28 21:03:44 +00:00
|
|
|
M.fetch = {
|
|
|
|
skip = function(plugin)
|
|
|
|
return not plugin._.installed or plugin._.is_local
|
|
|
|
end,
|
2022-11-30 22:05:51 +00:00
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
run = function(self)
|
2022-11-28 21:03:44 +00:00
|
|
|
local args = {
|
|
|
|
"fetch",
|
|
|
|
"--recurse-submodules",
|
|
|
|
"--progress",
|
|
|
|
}
|
|
|
|
|
|
|
|
self:spawn("git", {
|
|
|
|
args = args,
|
|
|
|
cwd = self.plugin.dir,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:26:07 +00:00
|
|
|
-- checkout to the target commit
|
|
|
|
-- branches will exists at this point, so so will the commit
|
2022-11-28 21:03:44 +00:00
|
|
|
M.checkout = {
|
|
|
|
skip = function(plugin)
|
|
|
|
return not plugin._.installed or plugin._.is_local
|
|
|
|
end,
|
2022-11-30 22:05:51 +00:00
|
|
|
|
2022-11-28 23:15:13 +00:00
|
|
|
---@param opts {lockfile?:boolean}
|
|
|
|
run = function(self, opts)
|
2022-11-28 21:03:44 +00:00
|
|
|
local info = assert(Git.info(self.plugin.dir))
|
|
|
|
local target = assert(Git.get_target(self.plugin))
|
|
|
|
|
2022-12-02 10:26:07 +00:00
|
|
|
-- if the plugin is pinned and we did not just clone it,
|
2022-11-29 13:27:32 +00:00
|
|
|
-- then don't update
|
2022-11-29 19:19:07 +00:00
|
|
|
if self.plugin.pin and not self.plugin._.cloned then
|
2022-11-29 11:36:07 +00:00
|
|
|
target = info
|
|
|
|
end
|
|
|
|
|
2022-11-28 23:15:13 +00:00
|
|
|
local lock
|
|
|
|
if opts.lockfile then
|
2022-12-02 10:26:07 +00:00
|
|
|
-- restore to the lock if it exists
|
2022-11-28 23:15:13 +00:00
|
|
|
lock = Lock.get(self.plugin)
|
|
|
|
if lock then
|
|
|
|
---@diagnostic disable-next-line: cast-local-type
|
|
|
|
target = lock
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-02 10:26:07 +00:00
|
|
|
-- dont run checkout if target is already reached.
|
2022-12-21 22:13:18 +00:00
|
|
|
-- unless we just cloned, since then we won't have any data yet
|
|
|
|
if info.commit == target.commit and info.branch == target.branch then
|
2022-11-29 09:30:45 +00:00
|
|
|
self.plugin._.updated = {
|
|
|
|
from = info.commit,
|
|
|
|
to = info.commit,
|
|
|
|
}
|
2022-11-28 21:03:44 +00:00
|
|
|
return
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
|
|
|
|
local args = {
|
|
|
|
"checkout",
|
|
|
|
"--progress",
|
2022-12-21 22:13:18 +00:00
|
|
|
"--recurse-submodules",
|
2022-11-28 21:03:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 23:15:13 +00:00
|
|
|
if lock then
|
|
|
|
table.insert(args, lock.commit)
|
|
|
|
elseif target.tag then
|
2022-11-28 21:03:44 +00:00
|
|
|
table.insert(args, "tags/" .. target.tag)
|
|
|
|
elseif self.plugin.commit then
|
|
|
|
table.insert(args, self.plugin.commit)
|
2022-12-02 10:26:07 +00:00
|
|
|
else
|
|
|
|
table.insert(args, target.commit)
|
2022-11-28 21:03:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self:spawn("git", {
|
|
|
|
args = args,
|
|
|
|
cwd = self.plugin.dir,
|
|
|
|
on_exit = function(ok)
|
|
|
|
if ok then
|
|
|
|
local new_info = assert(Git.info(self.plugin.dir))
|
|
|
|
if not self.plugin._.cloned then
|
|
|
|
self.plugin._.updated = {
|
|
|
|
from = info.commit,
|
|
|
|
to = new_info.commit,
|
|
|
|
}
|
2022-12-21 13:45:32 +00:00
|
|
|
if self.plugin._.updated.from ~= self.plugin._.updated.to then
|
|
|
|
self.plugin._.dirty = true
|
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-28 10:04:32 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
return M
|