Skip to content

Commit 8d38634

Browse files
committed
feat(lockfile): Add commit date to lockfile
This change adds `date` info for the commit. Git can now use `--shallow-since` if lockfile is enabled and plugin is in lockfile.
1 parent 0041d74 commit 8d38634

3 files changed

Lines changed: 58 additions & 31 deletions

File tree

lua/packer.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ local config_defaults = {
2727
cmd = 'git',
2828
subcommands = {
2929
update = 'pull --ff-only --progress --rebase=false',
30-
install = 'clone --depth %i --no-single-branch --progress',
31-
fetch = 'fetch --depth 999999 --progress',
30+
install = 'clone --no-single-branch --progress',
31+
fetch = 'fetch --progress',
3232
checkout = 'checkout %s --',
3333
update_branch = 'merge --ff-only @{u}',
3434
current_branch = 'rev-parse --abbrev-ref HEAD',
@@ -38,6 +38,7 @@ local config_defaults = {
3838
get_rev = 'rev-parse --short HEAD',
3939
get_header = 'log --color=never --pretty=format:FMT --no-show-signature HEAD -n 1',
4040
get_bodies = 'log --color=never --pretty=format:"===COMMIT_START===%h%n%s===BODY_START===%b" --no-show-signature HEAD@{1}...HEAD',
41+
get_date = 'show -s --format="%ct"',
4142
submodules = 'submodule update --init --recursive --progress',
4243
revert = 'reset --hard HEAD@{1}',
4344
revert_to = 'reset --hard %s --',

lua/packer/lockfile.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ local function collect_commits(plugins)
5151
end
5252
else
5353
local rev = await(plugin.get_rev())
54+
local date = await(plugin.get_date())
5455
if rev.err then
5556
failed[name] = fmt("Getting rev for '%s' failed because of error '%s'", name, vim.inspect(rev.err))
57+
elseif date.err then
58+
failed[name] = fmt("Getting date for '%s' failed because of error '%s'", name, vim.inspect(date.err))
5659
else
57-
completed[name] = { commit = rev.ok }
60+
completed[name] = { commit = rev.ok, date = date.ok }
5861
end
5962
end
6063
end
@@ -79,8 +82,7 @@ lockfile.load = function()
7982
end
8083

8184
lockfile.get = function(name)
82-
local res = data[name]
83-
return res and res.commit
85+
return data[name] or {}
8486
end
8587

8688
lockfile.update = function(plugins)
@@ -89,7 +91,7 @@ lockfile.update = function(plugins)
8991
local commits = await(collect_commits(plugins))
9092

9193
for name, commit in pairs(commits.ok.completed) do
92-
lines[#lines + 1] = fmt([[ ["%s"] = { commit = "%s" },]], name, commit.commit)
94+
lines[#lines + 1] = fmt([[ ["%s"] = { commit = "%s", date = %s },]], name, commit.commit, commit.date)
9395
end
9496

9597
-- Lines are sorted so that the diff will only contain changes not random re-ordering

lua/packer/plugin_types/git.lua

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ git.cfg = function(_config)
8686
ensure_git_env()
8787
end
8888

89-
---Get lockfile hash if lockfile should be applied
90-
---@param name string
91-
---@return string|nil
92-
local function get_lockfile_hash(name)
93-
if config.is_lockfile and not lockfile.is_updating then
94-
return lockfile.get(name)
95-
end
89+
---Get lockfile info if lockfile should be applied
90+
---@param plugin table @ plugin being applied
91+
---@return table @ either table with lockfile info or an empty table
92+
local function get_lockfile_info(plugin)
93+
local use_lockfile = config.is_lockfile and not lockfile.is_updating
94+
return use_lockfile and lockfile.get(plugin.short_name) or {}
9695
end
9796

9897
---Resets a git repo `dest` to `commit`
@@ -159,7 +158,7 @@ local handle_checkouts = function(plugin, dest, disp)
159158
end)
160159
end
161160

162-
local commit = plugin.commit or get_lockfile_hash(plugin.short_name)
161+
local commit = plugin.commit or get_lockfile_info(plugin).commit
163162
if commit then
164163
if disp ~= nil then
165164
disp:task_update(plugin_name, fmt('checking out %s...', commit))
@@ -212,30 +211,49 @@ local get_rev = function(plugin)
212211
end)
213212
end
214213

215-
git.setup = function(plugin)
214+
local get_date = function(plugin)
216215
local plugin_name = util.get_plugin_full_name(plugin)
217-
local install_to = plugin.install_path
218-
local install_cmd
216+
217+
local rev_cmd = config.exec_cmd .. config.subcommands.get_date
218+
219+
return async(function()
220+
local rev = await(jobs.run(rev_cmd, { cwd = plugin.install_path, options = { env = git.job_env }, capture_output = true }))
221+
:map_ok(function(ok)
222+
local _, r = next(ok.output.data.stdout)
223+
return r
224+
end)
225+
:map_err(function(err)
226+
local _, msg = fmt('%s: %s', plugin_name, next(err.output.data.stderr))
227+
return msg
228+
end)
229+
230+
return rev
231+
end)
232+
end
233+
234+
local get_depth = function(plugin)
219235
if config.is_lockfile then
220-
install_cmd = vim.split(config.exec_cmd .. fmt(config.subcommands.install, 999999), '%s+')
236+
local info = lockfile.get(plugin.short_name)
237+
return info.date and fmt('--shallow-since="%s"', info.date) or '--depth=999999'
221238
else
222-
install_cmd =
223-
vim.split(config.exec_cmd .. fmt(config.subcommands.install, plugin.commit and 999999 or config.depth), '%s+')
239+
local depth = plugin.commit and 999999 or config.depth
240+
return fmt('--depth="%s"', depth)
224241
end
242+
end
243+
244+
git.setup = function(plugin)
245+
local plugin_name = util.get_plugin_full_name(plugin)
246+
local install_to = plugin.install_path
247+
local install_cmd = vim.split(config.exec_cmd .. config.subcommands.install, '%s+')
248+
install_cmd[#install_cmd + 1] = get_depth(plugin)
225249

226250
local submodule_cmd = config.exec_cmd .. config.subcommands.submodules
227251
local rev_cmd = config.exec_cmd .. config.subcommands.get_rev
228252

229-
local update_cmd = config.exec_cmd
230-
if config.is_lockfile then
231-
update_cmd = update_cmd .. config.subcommands.fetch
232-
else
233-
if plugin.commit or plugin.tag then
234-
update_cmd = update_cmd .. config.subcommands.fetch
235-
else
236-
update_cmd = update_cmd .. config.subcommands.update
237-
end
238-
end
253+
local use_fetch = config.is_lockfile or plugin.commit or plugin.tag
254+
local update_subcmd = use_fetch and config.subcommands.fetch or config.subcommands.update
255+
local update_cmd = vim.split(config.exec_cmd .. update_subcmd, '%s+')
256+
update_cmd[#update_cmd + 1] = get_depth(plugin)
239257

240258
local branch_cmd = config.exec_cmd .. config.subcommands.current_branch
241259
local current_commit_cmd = vim.split(config.exec_cmd .. config.subcommands.get_header, '%s+')
@@ -280,7 +298,7 @@ git.setup = function(plugin)
280298
installer_opts.cwd = install_to
281299
r:and_then(await, jobs.run(submodule_cmd, installer_opts))
282300

283-
local commit = plugin.commit or get_lockfile_hash(plugin.short_name)
301+
local commit = plugin.commit or get_lockfile_info(plugin).commit
284302
if commit then
285303
disp:task_update(plugin_name, fmt('checking out %s...', commit))
286304
r
@@ -565,6 +583,12 @@ git.setup = function(plugin)
565583
plugin.get_rev = function()
566584
return get_rev(plugin)
567585
end
586+
587+
---Returns HEAD's date
588+
---@return string
589+
plugin.get_date = function()
590+
return get_date(plugin)
591+
end
568592
end
569593

570594
return git

0 commit comments

Comments
 (0)