commit:     9946517f1e0ee35b2a6eed1ff6c1fd10acf8fd45
Author:     Felix Bier <flx.bier <AT> gmail <DOT> com>
AuthorDate: Sun May  9 23:27:19 2021 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Mon May 24 06:22:30 2021 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=9946517f

Sort emerge --unmerge order for determinism

This commit changes the order in which packages are uninstalled
with --unmerge to be deterministic.

Before this commit, when an atom specified with --unmerge matches
multiple versions, these versions were uninstalled in a random order.
For example, when some-pkg-1.0.0 and some-pkg-2.0.0 are installed,
then running emerge --unmerge some-pkg will unmerge 1.0.0 and 2.0.0
in a random order.

With this commit, when an atom matches multiple versions,
they are uninstalled in a sorted order. So in the above example,
1.0.0 will be unmerged before 2.0.0.

This does not affect the order specified on the command-line,
for example when running:
emerge --unmerge =some-pkg-2.0.0 =some-pkg-1.0.0
that order is respected, as before this commit.

Similarly, when uninstalling multiple different packages, e.g.:
emerge --unmerge some-pkg some-other-pkg
the sorting only applies to the versions matching each atom,
so the versions matching some-pkg are sorted separately from
the versions matching some-other-pkg, and since some-pkg
is specified before some-other-pkg, its versions will be
uninstalled first.

Motivation: When a package has a custom pkg_postrm hook, uninstalling
multiple versions in a random order can leave the filesystem in a
different state depending on which version is uninstalled last.
When running emerge as part of a larger build system such as catalyst,
this is an obstacle towards reproducible builds.

Bug: https://bugs.gentoo.org/782724
Signed-off-by: Felix Bier <felix.bier <AT> rohde-schwarz.com>
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/_emerge/PackageUninstall.py | 2 +-
 lib/_emerge/unmerge.py          | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/_emerge/PackageUninstall.py b/lib/_emerge/PackageUninstall.py
index 43210b4bc..8786c2cd1 100644
--- a/lib/_emerge/PackageUninstall.py
+++ b/lib/_emerge/PackageUninstall.py
@@ -71,7 +71,7 @@ class PackageUninstall(CompositeTask):
 
                # Output only gets logged if it comes after prepare_build_dirs()
                # which initializes PORTAGE_LOG_FILE.
-               retval, pkgmap = _unmerge_display(self.pkg.root_config,
+               retval, _ = _unmerge_display(self.pkg.root_config,
                        self.opts, "unmerge", [self.pkg.cpv], clean_delay=0,
                        writemsg_level=self._writemsg_level)
 

diff --git a/lib/_emerge/unmerge.py b/lib/_emerge/unmerge.py
index e8b7c9aaa..09e155de3 100644
--- a/lib/_emerge/unmerge.py
+++ b/lib/_emerge/unmerge.py
@@ -432,6 +432,11 @@ def _unmerge_display(root_config, myopts, unmerge_action,
                                cp_dict[k].update(v)
                pkgmap = [unordered[cp] for cp in sorted(unordered)]
 
+       # Sort each set of selected packages
+       if ordered:
+               for pkg in pkgmap:
+                       pkg["selected"] = sorted(pkg["selected"], 
key=cpv_sort_key())
+
        for x in range(len(pkgmap)):
                selected = pkgmap[x]["selected"]
                if not selected:

Reply via email to