Skip to content

Commit 95e7736

Browse files
committed
refactor: simplify conflict resolution
1 parent 898b0e7 commit 95e7736

File tree

1 file changed

+26
-58
lines changed

1 file changed

+26
-58
lines changed

‎lua/pack.lua

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ M.status = {
2222
UPDATED = 2,
2323
REMOVED = 3,
2424
TO_INSTALL = 4,
25-
TO_MOVE = 5,
26-
TO_RECLONE = 6,
2725
}
2826

2927
--- List of packages to build
@@ -81,8 +79,6 @@ local Filter = {
8179
to_update = function(p)
8280
return p.status ~= M.status.REMOVED and p.status ~= M.status.TO_INSTALL and not p.pin
8381
end,
84-
to_move = function(p) return p.status == M.status.TO_MOVE end,
85-
to_reclone = function(p) return p.status == M.status.TO_RECLONE end,
8682
}
8783

8884
--- @param path string
@@ -281,17 +277,6 @@ local function clone_or_pull(pkg, counter)
281277
end
282278
end
283279

284-
--- Move package to wanted location.
285-
--- @param src pack.Package
286-
--- @param dst pack.Package
287-
local function move(src, dst)
288-
local ok = uv.fs_rename(src.dir, dst.dir)
289-
if ok then
290-
dst.status = M.status.INSTALLED
291-
lock_write()
292-
end
293-
end
294-
295280
local function process_build_queue()
296281
local failed = {}
297282

@@ -328,33 +313,13 @@ end
328313
--- @param pkg pack.Package
329314
local function reclone(pkg)
330315
local ok = pcall(vim.fs.rm, pkg.dir, { recursive = true })
331-
if not ok then
332-
return
333-
end
334-
local args = vim.list_extend({ 'git', 'clone', pkg.url }, Config.clone_args)
335-
if pkg.branch then
336-
vim.list_extend(args, { '-b', pkg.branch })
316+
if ok then
317+
clone(pkg, function() end)
337318
end
338-
table.insert(args, pkg.dir)
339-
vim.system(args, {}, function(obj)
340-
if obj.code == 0 then
341-
pkg.status = M.status.INSTALLED
342-
pkg.hash = get_git_hash(pkg.dir)
343-
lock_write()
344-
if pkg.build then
345-
table.insert(BuildQueue, pkg)
346-
end
347-
end
348-
end)
349319
end
350320

351-
local function resolve(pkg, counter)
352-
if Filter.to_move(pkg) then
353-
move(pkg, Packages[pkg.name])
354-
elseif Filter.to_reclone(pkg) then
355-
reclone(Packages[pkg.name], counter)
356-
end
357-
end
321+
--- @param conflict pack.Conflict
322+
local function resolve(conflict) reclone(conflict.curr) end
358323

359324
--- @param pkg string|pack.Package
360325
--- @return pack.Package?
@@ -431,9 +396,7 @@ local function exe_op(op, fn, pkgs)
431396
process_build_queue()
432397
end
433398

434-
vim.api.nvim_exec_autocmds('User', {
435-
pattern = 'PackDone' .. op:gsub('^%l', string.upper),
436-
})
399+
vim.api.nvim_exec_autocmds('User', { pattern = 'PackDone' .. op:gsub('^%l', string.upper) })
437400

438401
-- This makes the logfile reload if there were changes while the job was running
439402
vim.cmd('silent! checktime ' .. vim.fn.fnameescape(Path.log))
@@ -446,23 +409,21 @@ local function exe_op(op, fn, pkgs)
446409
end
447410
end
448411

449-
local function calculate_diffs()
450-
local diffs = {}
451-
for name, lock_pkg in pairs(Lock) do
452-
local pack_pkg = Packages[name]
453-
if pack_pkg and Filter.not_removed(lock_pkg) and not vim.deep_equal(lock_pkg, pack_pkg) then
454-
for k, v in pairs {
455-
branch = M.status.TO_RECLONE,
456-
url = M.status.TO_RECLONE,
457-
} do
458-
if lock_pkg[k] ~= pack_pkg[k] then
459-
lock_pkg.status = v
460-
table.insert(diffs, lock_pkg)
461-
end
462-
end
412+
--- @nodoc
413+
--- @class pack.Conflict
414+
--- @field prev pack.Package
415+
--- @field curr pack.Package
416+
---
417+
--- @return pack.Conflict[]
418+
local function calculate_conflicts()
419+
local conflicts = {}
420+
for name, lock in pairs(Lock) do
421+
local pkg = Packages[name]
422+
if pkg and Filter.not_removed(lock) and (lock.branch ~= pkg.branch or lock.url ~= lock.url) then
423+
table.insert(conflicts, { prev = lock, curr = pkg })
463424
end
464425
end
465-
return diffs
426+
return conflicts
466427
end
467428

468429
---@param opts pack.Opts? When omitted or `nil`, retrieve the current
@@ -495,17 +456,24 @@ end
495456
--- @param pkgs pack.Package[]
496457
function M.register(pkgs)
497458
vim.validate('pkgs', pkgs, 'table', true)
459+
498460
Packages = {}
499461
pkgs = vim.tbl_map(register, pkgs)
500462

463+
-- Load packages into runtimepath
501464
for _, pkg in ipairs(pkgs) do
502465
Packages[pkg.name] = pkg
503466
vim.opt.runtimepath:prepend(pkg.dir)
504467
end
505468

469+
-- Resolve conflict between user configuration and lockfile
506470
lock_load()
507-
exe_op('resolve', resolve, calculate_diffs())
471+
for _, conflict in ipairs(calculate_conflicts()) do
472+
resolve(conflict)
473+
end
474+
lock_write()
508475

476+
-- Unload unlisted packages from runtimepath
509477
for _, pkg in ipairs(find_unlisted()) do
510478
vim.opt.runtimepath:remove(pkg.dir)
511479
end

0 commit comments

Comments
 (0)