From 699abc334895b5e737976d7aa6c1982a692d623b Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Mon, 16 May 2016 11:03:19 +0300 Subject: Refactor match_dep() * Instead of applying blacklist and sorting versions in separate steps do a single pass over the list. * On success return just the version instead of {name = name, version = version} since rock name is stored inside dependency table already. --- src/luarocks/deps.lua | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 3c580623..536b6c51 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -322,48 +322,32 @@ end -- @param dep table: A dependency parsed in table format. -- @param blacklist table: Versions that can't be accepted. Table where keys -- are program versions and values are 'true'. --- @return table or nil: A table containing fields 'name' and 'version' --- representing an installed rock which matches the given dependency, +-- @return string or nil: latest installed version of the rock matching the dependency -- or nil if it could not be matched. local function match_dep(dep, blacklist, deps_mode) assert(type(dep) == "table") - local versions = cfg.rocks_provided[dep.name] + local versions if cfg.rocks_provided[dep.name] then -- provided rocks have higher priority than manifest's rocks versions = { cfg.rocks_provided[dep.name] } else versions = manif_core.get_versions(dep.name, deps_mode) end - if not versions then - return nil - end - if blacklist then - local i = 1 - while versions[i] do - if blacklist[versions[i]] then - table.remove(versions, i) - else - i = i + 1 - end - end - end - local candidates = {} + + local latest_version for _, vstring in ipairs(versions) do - local version = deps.parse_version(vstring) - if deps.match_constraints(version, dep.constraints) then - table.insert(candidates, version) + if not blacklist or not blacklist[vstring] then + local version = deps.parse_version(vstring) + if deps.match_constraints(version, dep.constraints) then + if not latest_version or version > latest_version then + latest_version = version + end + end end end - if #candidates == 0 then - return nil - else - table.sort(candidates) - return { - name = dep.name, - version = candidates[#candidates].string - } - end + + return latest_version and latest_version.string end --- Attempt to match dependencies of a rockspec to installed rocks. @@ -386,7 +370,7 @@ function deps.match_deps(rockspec, blacklist, deps_mode) local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode) if found then if not cfg.rocks_provided[dep.name] then - matched[dep] = found + matched[dep] = {name = dep.name, version = found} end else if dep.constraints[1] and dep.constraints[1].no_upgrade then @@ -414,7 +398,7 @@ local function rock_status(name, deps_mode) local search = require("luarocks.search") local installed = match_dep(search.make_query(name), nil, deps_mode) local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed" - return installed and installed.version.." "..installation_type or "not installed" + return installed and installed.." "..installation_type or "not installed" end --- Check dependencies of a rock and attempt to install any missing ones. -- cgit v1.2.3-55-g6feb