From 2903ad8d65a76e9ae6da9a8c4ac13591aeb879d8 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 30 Jan 2023 16:45:00 +0300 Subject: Fix resulting names ordering for system_package_names() --- bpkg/system-package-manager.cxx | 44 +++++++++++++++++++++++------ bpkg/system-package-manager.test.testscript | 3 +- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bpkg/system-package-manager.cxx b/bpkg/system-package-manager.cxx index 3585ba3..b5fa333 100644 --- a/bpkg/system-package-manager.cxx +++ b/bpkg/system-package-manager.cxx @@ -197,27 +197,53 @@ namespace bpkg auto name_values = [&aps] (const string& n, const semantic_version& v) { strings r; + + // For each available package sort the system package names in the + // distribution version descending order and then append them to the + // resulting list, keeping this order and suppressing duplicates. + // + using name_version = pair; + vector nvs; // Reuse the buffer. + for (const auto& a: aps) { + nvs.clear (); + const shared_ptr& ap (a.first); - for (const distribution_name_value& nv: ap->distribution_values) + for (const distribution_name_value& dv: ap->distribution_values) { - if (optional d = nv.distribution ("-name")) + if (optional d = dv.distribution ("-name")) { pair dnv ( - parse_distribution (move (*d), nv.name, ap, a.second)); + parse_distribution (move (*d), dv.name, ap, a.second)); - if (dnv.first == n && - dnv.second <= v && - find_if (r.begin (), r.end (), - [&nv] (const string& v) {return nv.value == v;}) == - r.end ()) + if (dnv.first == n && dnv.second <= v) { - r.push_back (nv.value); + // Add the name/version pair to the sorted vector. + // + name_version nv (make_pair (dv.value, move (dnv.second))); + + nvs.insert (upper_bound (nvs.begin (), nvs.end (), nv, + [] (const name_version& x, + const name_version& y) + {return x.second > y.second;}), + move (nv)); } } } + + // Append the sorted names to the resulting list. + // + for (name_version& nv: nvs) + { + if (find_if (r.begin (), r.end (), + [&nv] (const string& n) {return nv.first == n;}) == + r.end ()) + { + r.push_back (move (nv.first)); + } + } } return r; diff --git a/bpkg/system-package-manager.test.testscript b/bpkg/system-package-manager.test.testscript index b993793..dc672f5 100644 --- a/bpkg/system-package-manager.test.testscript +++ b/bpkg/system-package-manager.test.testscript @@ -26,14 +26,13 @@ summary: curl license: curl EOI -#\ + $* debian 10 -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO; libcurl4 libcurl4-openssl-dev libcurl3-gnutls libcurl4-gnutls-dev libcurl2 libcurl2-dev libcurl2-doc libcurl2 libcurl2-dev EOO -#\ $* debian 9 -- libcurl libcurl7.64.manifest libcurl7.84.manifest >>EOO; libcurl2 libcurl2-dev libcurl2-doc libcurl2 libcurl2-dev -- cgit v1.1