[PATCH 1 of 2] setdiscovery: extract sample limitation in a `_limitsample` function

Pierre-Yves David pierre-yves.david at ens-lyon.org
Mon Oct 27 12:23:20 CDT 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1414428032 -3600
#      Mon Oct 27 17:40:32 2014 +0100
# Branch stable
# Node ID 9abdfe27dd006ff12d721fc127d9cea519f1a8b9
# Parent  85e7a464e63979aad2db6a802b3f72b6c889e9d4
setdiscovery: extract sample limitation in a `_limitsample` function

We need to reuse this logic for the initial query. We extract it in a function
to unsure sample limiting is applied consistently in all cases.

diff --git a/mercurial/setdiscovery.py b/mercurial/setdiscovery.py
--- a/mercurial/setdiscovery.py
+++ b/mercurial/setdiscovery.py
@@ -103,18 +103,23 @@ def _takefullsample(dag, nodes, size):
     # update from heads
     _updatesample(dag, nodes, sample, always)
     # update from roots
     _updatesample(dag.inverse(), nodes, sample, always)
     assert sample
-    if len(sample) > desiredlen:
-        sample = set(random.sample(sample, desiredlen))
-    elif len(sample) < desiredlen:
+    sample = _limitsample(sample, desiredlen)
+    if len(sample) < desiredlen:
         more = desiredlen - len(sample)
         sample.update(random.sample(list(nodes - sample - always), more))
     sample.update(always)
     return sample
 
+def _limitsample(sample, desiredlen):
+    """return a random subset of sample of at most desiredlen item"""
+    if len(sample) > desiredlen:
+        sample = set(random.sample(sample, desiredlen))
+    return sample
+
 def findcommonheads(ui, local, remote,
                     initialsamplesize=100,
                     fullsamplesize=200,
                     abortwhenunrelated=True):
     '''Return a tuple (common, anyincoming, remoteheads) used to identify


More information about the Mercurial-devel mailing list