162 lines
4.2 KiB
Lua
162 lines
4.2 KiB
Lua
-- TODO: clean this up, it's a mess
|
|
-- the nesting of all the proc calls is really annoying, and I need to find a
|
|
-- cleaner way to do it
|
|
|
|
local logger = require('dep.log')
|
|
local proc = require('dep.proc')
|
|
|
|
local git = {}
|
|
|
|
--- install or update a given package
|
|
---@param package package package to update/install
|
|
---@param cb function callback
|
|
function git.sync(package, cb)
|
|
local function sync()
|
|
-- update or install
|
|
if package.exists then
|
|
git.update(package, cb)
|
|
else
|
|
git.install(package, cb)
|
|
end
|
|
end
|
|
|
|
-- handle arbitrary branches here
|
|
if package.branch then
|
|
proc.git_resolve_branch(package.url, package.branch, function(err, message)
|
|
if not err then
|
|
package.branch = message
|
|
sync()
|
|
end
|
|
end)
|
|
else
|
|
sync()
|
|
end
|
|
end
|
|
|
|
--- configure a package
|
|
---@param package table package spec
|
|
local function configurepkg(package)
|
|
package:runhooks("on_config")
|
|
|
|
logger:log("config", "package: %s configured", package.id)
|
|
package.configured = true
|
|
end
|
|
|
|
--- install a given package
|
|
---@param package package package to install
|
|
---@param cb function callback
|
|
function git.install(package, cb)
|
|
if not package.enabled then
|
|
cb()
|
|
return
|
|
end
|
|
|
|
proc.git_clone(package.dir, package.url, package.branch, function(err, message)
|
|
if err then
|
|
logger:log("error", "failed to install %s; reason: %s",
|
|
package.id, message)
|
|
else
|
|
if package.commit then
|
|
proc.git_checkout(package.dir, package.branch, package.commit, function(err, message)
|
|
if err then
|
|
logger:log("error", "failed to checkout %s; reason: %s", package.id, message)
|
|
else
|
|
package.exists = true
|
|
package:unconfiguretree()
|
|
logger:log("install", "installed %s", package.id)
|
|
configurepkg(package)
|
|
end
|
|
end)
|
|
else
|
|
package.exists = true
|
|
package:unconfiguretree()
|
|
logger:log("install", "installed %s", package.id)
|
|
configurepkg(package)
|
|
end
|
|
end
|
|
|
|
cb(err)
|
|
end)
|
|
|
|
end
|
|
|
|
--- update a package
|
|
---@param package package package to update
|
|
---@param cb function callback
|
|
function git.update(package, cb)
|
|
if not package.enabled then
|
|
cb()
|
|
return
|
|
end
|
|
|
|
local function log_err(err)
|
|
logger:log("error", "failed to update %s; reason: %s", package.id, err)
|
|
end
|
|
|
|
if package.pin then
|
|
cb()
|
|
return
|
|
end
|
|
|
|
proc.git_rev_parse(package.dir, "HEAD", function(err, before)
|
|
if err then
|
|
log_err(before)
|
|
cb(err)
|
|
else
|
|
if package.commit then
|
|
proc.git_checkout(package.dir, package.branch, package.commit, function(err, message)
|
|
if err then
|
|
log_err(message)
|
|
cb(err)
|
|
else
|
|
proc.git_rev_parse(package.dir, package.commit, function(err, after)
|
|
if err then
|
|
log_err(after)
|
|
cb(err)
|
|
elseif before == after then
|
|
logger:log("skip", "skipped %s", package.id)
|
|
cb(err)
|
|
else
|
|
package:unconfiguretree()
|
|
logger:log("update", "updated %s; %s -> %s", package.id, before, after)
|
|
configurepkg(package)
|
|
end
|
|
end)
|
|
end
|
|
end)
|
|
else
|
|
proc.git_fetch(package.dir, "origin", package.branch or "HEAD", function(err, message)
|
|
if err then
|
|
log_err(message)
|
|
cb(err)
|
|
else
|
|
proc.git_rev_parse(package.dir, "FETCH_HEAD^{commit}", function(err, after)
|
|
if err then
|
|
log_err(after)
|
|
cb(err)
|
|
elseif before == after then
|
|
logger:log("skip", "skipped %s", package.id)
|
|
cb(err)
|
|
else
|
|
proc.git_reset(package.dir, after, function(err, message)
|
|
if err then
|
|
log_err(message)
|
|
else
|
|
package:unconfiguretree()
|
|
logger:log("update", "updated %s; %s -> %s", package.id, before, after)
|
|
configurepkg(package)
|
|
end
|
|
|
|
cb(err)
|
|
end)
|
|
end
|
|
end)
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
return git
|