From 098460199c35218979e207a1709111e477ab9567 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 1 Oct 2015 09:13:37 +0200 Subject: build command genesis, some re-factoring --- bpkg/package.cxx | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'bpkg/package.cxx') diff --git a/bpkg/package.cxx b/bpkg/package.cxx index ad54e51..c6f5dcf 100644 --- a/bpkg/package.cxx +++ b/bpkg/package.cxx @@ -3,9 +3,12 @@ // license : MIT; see accompanying LICENSE file #include +#include #include // invalid_argument +#include + using namespace std; namespace bpkg @@ -34,6 +37,69 @@ namespace bpkg return xv.revision < yv.revision; } + // available_package + // + + // Check if the package is available from the specified repository or + // one of its complements, recursively. Return the first repository + // that contains the package or NULL if none are. + // + static shared_ptr + find (const shared_ptr& r, + const shared_ptr& ap) + { + const auto& cs (r->complements); + + for (const package_location& pl: ap->locations) + { + // First check the repository itself. + // + if (pl.repository.object_id () == r->name) + return r; + + // Then check all the complements without loading them. + // + if (cs.find (pl.repository) != cs.end ()) + return pl.repository.load (); + + // Finally, load the complements and check them recursively. + // + for (const lazy_shared_ptr& cr: cs) + { + if (shared_ptr r = find (cr.load (), ap)) + return r; + } + } + + return nullptr; + } + + vector> + filter (const shared_ptr& r, result&& apr) + { + vector> aps; + + for (shared_ptr ap: pointer_result (apr)) + { + if (find (r, ap) != nullptr) + aps.push_back (move (ap)); + } + + return aps; + } + + shared_ptr + filter_one (const shared_ptr& r, result&& apr) + { + for (shared_ptr ap: pointer_result (apr)) + { + if (find (r, ap) != nullptr) + return ap; + } + + return nullptr; + } + // state // string -- cgit v1.1