changeset 6044:b47a952b244a

evolve: refactor _pick_latest_divergent(), rename to _sorted_divergents()
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 05 Oct 2021 09:29:36 +0300
parents b1a04ff0f99e
children 2da56c12e70f
files hgext3rd/evolve/evolvecmd.py
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/evolve/evolvecmd.py	Wed Sep 22 02:33:50 2021 +0300
+++ b/hgext3rd/evolve/evolvecmd.py	Tue Oct 05 09:29:36 2021 +0300
@@ -1127,31 +1127,26 @@
         base, others = divergentdata(divergent)
         othersrevs = [o.rev() for o in others]
         all_divergents = othersrevs + [divergent.rev()]
-        pick, discard = _pick_latest_divergent(repo, all_divergents)
-        res.add(pick)
-        discarded.update(discard)
+        all_divergents = _sorted_divergents(repo, all_divergents)
+        res.update(all_divergents[:1])
+        discarded.update(all_divergents[1:])
     return res
 
-def _pick_latest_divergent(repo, divergent_revs):
-    """On the basis of evolution date, pick out the latest evolved revision
-    from all the `divergent_revs` (which are divergent with each other)
+def _sorted_divergents(repo, divergent_revs):
+    """Sort divergent_revs by latest evolution date, from newest to oldest.
 
-    Return a tuple (latest_evolved, others)
+    Fall back to rev number, but in ascending order, for historical reasons.
     """
-    mapping = []
-    for rev in divergent_revs:
-        led = latest_evolution_date(repo, repo[rev])
+    ledmap = {
         # For purpose of comparing, from `led` which is (unixtime, offset)
         # we only need `unixtime`.
-        mapping.append((led[0], rev))
+        rev: latest_evolution_date(repo, repo[rev])[0]
+        for rev in divergent_revs
+    }
     # Sorting by negating the `rev` in key func, to fallback to the old way
     # of selecting revision, in case when `led` is same while comparing.
     # Old way: was to select the one with minimum revision number
-    mapping = sorted(mapping, key=lambda v: (v[0], -v[1]))
-    latest_evolved = mapping[-1][1]
-    others = mapping[:-1]
-    others = [val[1] for val in others[:]]
-    return latest_evolved, others
+    return sorted(divergent_revs, key=lambda rev: (-ledmap[rev], rev))
 
 def latest_evolution_date(repo, ctx):
     """Return latest evolution date of a changeset `ctx`"""