aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lua/dep.lua172
-rw-r--r--lua/dep/proc.lua62
2 files changed, 152 insertions, 82 deletions
diff --git a/lua/dep.lua b/lua/dep.lua
index 1c143c8..2198863 100644
--- a/lua/dep.lua
+++ b/lua/dep.lua
@@ -479,97 +479,105 @@ local function sync(package, cb)
return
end
- if package.exists then
- if package.pin then
- cb()
- return
- end
+ local function log_err(err)
+ logger:log("error", string.format("failed to update %s; reason: %s", package.id, err))
+ end
- local function log_err(err)
- logger:log("error", string.format("failed to update %s; reason: %s", package.id, err))
+ proc.git_resolve_branch(package.url, package.branch, function(err, branch)
+ if err then
+ log_err(err)
+ cb(err)
end
+ package.branch = branch
- 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", string.format("skipped %s", package.id))
- cb(err)
- else
- mark_reconfigure(package)
- logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after))
- 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", function(err, after)
- if err then
- log_err(after)
- cb(err)
- elseif before == after then
- logger:log("skip", string.format("skipped %s", package.id))
- cb(err)
- else
- proc.git_reset(package.dir, after, function(err, message)
- if err then
- log_err(message)
- else
- mark_reconfigure(package)
- logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after))
- end
+ if package.exists then
+ 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", string.format("skipped %s", package.id))
+ cb(err)
+ else
+ mark_reconfigure(package)
+ logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after))
+ 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", function(err, after)
+ if err then
+ log_err(after)
+ cb(err)
+ elseif before == after then
+ logger:log("skip", string.format("skipped %s", package.id))
cb(err)
- end)
- end
- end)
- end
- end)
+ else
+ proc.git_reset(package.dir, after, function(err, message)
+ if err then
+ log_err(message)
+ else
+ mark_reconfigure(package)
+ logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after))
+ end
+
+ cb(err)
+ end)
+ end
+ end)
+ end
+ end)
+ end
end
- end
- end)
- else
- proc.git_clone(package.dir, package.url, package.branch, function(err, message)
- if err then
- logger:log("error", string.format("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", string.format("failed to checkout %s; reason: %s", package.id, message))
- else
- package.exists = true
- mark_reconfigure(package)
- logger:log("install", string.format("installed %s", package.id))
- end
- end)
+ end)
+ else
+ proc.git_clone(package.dir, package.url, package.branch, function(err, message)
+ if err then
+ logger:log("error", string.format("failed to install %s; reason: %s", package.id, message))
else
- package.exists = true
- mark_reconfigure(package)
- logger:log("install", string.format("installed %s", package.id))
+ if package.commit then
+ proc.git_checkout(package.dir, package.branch, package.commit, function(err, message)
+ if err then
+ logger:log("error", string.format("failed to checkout %s; reason: %s", package.id, message))
+ else
+ package.exists = true
+ mark_reconfigure(package)
+ logger:log("install", string.format("installed %s", package.id))
+ end
+ end)
+ else
+ package.exists = true
+ mark_reconfigure(package)
+ logger:log("install", string.format("installed %s", package.id))
+ end
end
- end
- cb(err)
- end)
- end
+ cb(err)
+ end)
+ end
+ end)
end
local function sync_list(list, on_complete)
diff --git a/lua/dep/proc.lua b/lua/dep/proc.lua
index cf3aa9c..c8272e5 100644
--- a/lua/dep/proc.lua
+++ b/lua/dep/proc.lua
@@ -71,4 +71,66 @@ function proc.git_checkout(dir, branch, commit, cb)
end)
end
+function proc.git_resolve_branch(url, branch, cb)
+ if string.match(branch or "", "*") ~= "*" then
+ cb(false, branch)
+ return
+ end
+ local buffer = {}
+
+ local function cb_output(_, data, _)
+ if data[1] ~= "" then
+ buffer = data
+ end
+ end
+
+ vim.fn.jobstart({
+ "git",
+ "ls-remote",
+ "--tags",
+ "--sort",
+ "v:refname",
+ url
+ }, {
+ cwd = nil,
+ env = { GIT_TERMINAL_PROMPT = 0 },
+ stdin = nil,
+ on_stdout = cb_output,
+ on_stderr = cb_output,
+ on_exit = function(_, exit_code, _)
+ if exit_code == 0 then
+ -- get a list of all versions
+ local versions = {}
+ for _, v in pairs(buffer) do
+ local s, e = string.find(v, "refs/tags/.+")
+ if not s or not e then
+ goto continue
+ end
+
+ local tag = string.sub(v, s, e)
+ tag = string.gsub(tag, "refs/tags/", "")
+ tag = string.gsub(tag, "%^{}", "")
+
+ table.insert(versions, tag)
+ ::continue::
+ end
+
+ -- match the chosen version against all versions
+ for i = #versions, 1, -1 do
+ if branch == "*" then
+ cb(false, versions[i])
+ return
+ else
+ local r = string.match(versions[i], branch)
+ if r then
+ cb(false, r)
+ return
+ end
+ end
+ end
+ end
+ end
+ })
+end
+
return proc