Skip to content

Commit 79f2af8

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 94a9d5e commit 79f2af8

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

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
@@ -77,13 +77,12 @@ git.cfg = function(_config)
7777
ensure_git_env()
7878
end
7979

80-
---Get lockfile hash if lockfile should be applied
81-
---@param name string
82-
---@return string|nil
83-
local function get_lockfile_hash(name)
84-
if config.is_lockfile and not lockfile.is_updating then
85-
return lockfile.get(name)
86-
end
80+
---Get lockfile info if lockfile should be applied
81+
---@param plugin table @ plugin being applied
82+
---@return table @ either table with lockfile info or an empty table
83+
local function get_lockfile_info(plugin)
84+
local use_lockfile = config.is_lockfile and not lockfile.is_updating
85+
return use_lockfile and lockfile.get(plugin.short_name) or {}
8786
end
8887

8988
---Resets a git repo `dest` to `commit`
@@ -150,7 +149,7 @@ local handle_checkouts = function(plugin, dest, disp)
150149
end)
151150
end
152151

153-
local commit = plugin.commit or get_lockfile_hash(plugin.short_name)
152+
local commit = plugin.commit or get_lockfile_info(plugin).commit
154153
if commit then
155154
if disp ~= nil then
156155
disp:task_update(plugin_name, fmt('checking out %s...', commit))
@@ -203,30 +202,49 @@ local get_rev = function(plugin)
203202
end)
204203
end
205204

206-
git.setup = function(plugin)
205+
local get_date = function(plugin)
207206
local plugin_name = util.get_plugin_full_name(plugin)
208-
local install_to = plugin.install_path
209-
local install_cmd
207+
208+
local rev_cmd = config.exec_cmd .. config.subcommands.get_date
209+
210+
return async(function()
211+
local rev = await(jobs.run(rev_cmd, { cwd = plugin.install_path, options = { env = git.job_env }, capture_output = true }))
212+
:map_ok(function(ok)
213+
local _, r = next(ok.output.data.stdout)
214+
return r
215+
end)
216+
:map_err(function(err)
217+
local _, msg = fmt('%s: %s', plugin_name, next(err.output.data.stderr))
218+
return msg
219+
end)
220+
221+
return rev
222+
end)
223+
end
224+
225+
local get_depth = function(plugin)
210226
if config.is_lockfile then
211-
install_cmd = vim.split(config.exec_cmd .. fmt(config.subcommands.install, 999999), '%s+')
227+
local info = lockfile.get(plugin.short_name)
228+
return info.date and fmt('--shallow-since="%s"', info.date) or '--depth=999999'
212229
else
213-
install_cmd =
214-
vim.split(config.exec_cmd .. fmt(config.subcommands.install, plugin.commit and 999999 or config.depth), '%s+')
230+
local depth = plugin.commit and 999999 or config.depth
231+
return fmt('--depth="%s"', depth)
215232
end
233+
end
234+
235+
git.setup = function(plugin)
236+
local plugin_name = util.get_plugin_full_name(plugin)
237+
local install_to = plugin.install_path
238+
local install_cmd = vim.split(config.exec_cmd .. config.subcommands.install, '%s+')
239+
install_cmd[#install_cmd + 1] = get_depth(plugin)
216240

217241
local submodule_cmd = config.exec_cmd .. config.subcommands.submodules
218242
local rev_cmd = config.exec_cmd .. config.subcommands.get_rev
219243

220-
local update_cmd = config.exec_cmd
221-
if config.is_lockfile then
222-
update_cmd = update_cmd .. config.subcommands.fetch
223-
else
224-
if plugin.commit or plugin.tag then
225-
update_cmd = update_cmd .. config.subcommands.fetch
226-
else
227-
update_cmd = update_cmd .. config.subcommands.update
228-
end
229-
end
244+
local use_fetch = config.is_lockfile or plugin.commit or plugin.tag
245+
local update_subcmd = use_fetch and config.subcommands.fetch or config.subcommands.update
246+
local update_cmd = vim.split(config.exec_cmd .. update_subcmd, '%s+')
247+
update_cmd[#update_cmd + 1] = get_depth(plugin)
230248

231249
local branch_cmd = config.exec_cmd .. config.subcommands.current_branch
232250
local current_commit_cmd = vim.split(config.exec_cmd .. config.subcommands.get_header, '%s+')
@@ -271,7 +289,7 @@ git.setup = function(plugin)
271289
installer_opts.cwd = install_to
272290
r:and_then(await, jobs.run(submodule_cmd, installer_opts))
273291

274-
local commit = plugin.commit or get_lockfile_hash(plugin.short_name)
292+
local commit = plugin.commit or get_lockfile_info(plugin).commit
275293
if commit then
276294
disp:task_update(plugin_name, fmt('checking out %s...', commit))
277295
r
@@ -556,6 +574,12 @@ git.setup = function(plugin)
556574
plugin.get_rev = function()
557575
return get_rev(plugin)
558576
end
577+
578+
---Returns HEAD's date
579+
---@return string
580+
plugin.get_date = function()
581+
return get_date(plugin)
582+
end
559583
end
560584

561585
return git

0 commit comments

Comments
 (0)