# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1657264470 -7200
#      Fri Jul 08 09:14:30 2022 +0200
# Node ID 55e9bbc4e17ffb2aa9d9bde0bb13c21f185079e0
# Parent  51d4b32639f58a3c81b529c631b645adecfe34fa
auto-cases: do not schedule benchmark for incompatible data-env

If the repository use requirements not understood by the revision we benchmark,
we can skip it.

diff --git a/scm-perf/poulpe-scheduler-bin/auto-cases b/scm-perf/poulpe-scheduler-bin/auto-cases
--- a/scm-perf/poulpe-scheduler-bin/auto-cases
+++ b/scm-perf/poulpe-scheduler-bin/auto-cases
@@ -5,6 +5,8 @@
 import subprocess
 import sys
 
+import poulpe_helper as poulpe
+
 TARGET_PYTHON_VERSIONS = [
     "3.7"
 ]
@@ -75,6 +77,24 @@
     raws = [Path(l) for l in p.stdout.splitlines()]
     return [Path(*p.parts[:-1]) for p in raws]
 
+
+compat_cache = {}
+
+
+def compatible_with(base_dir, data_env, changeset):
+    """check if a data_env is compatible with the selected revision"""
+    path = base_dir / "data-envs" / data_env / "data-env.poulpe"
+    data_args = poulpe.get_data(path)
+    assert data_args is not None, path
+    ds2_key = "data-env-vars.mercurial.repo.format.dirstate-v2"
+    if poulpe.get_one_value(data_args, ds2_key) == "yes":
+        if "dirstate-v2" not in compat_cache:
+            revs = list_mercurial_hashes(base_dir, 'tagged("6.0")::')
+            compat_cache['dirstate-v2'] = set(revs)
+        return changeset in compat_cache['dirstate-v2']
+    return True
+
+
 def list_benchmarks(base_dir):
     """return a list of possible benchmarks"""
     bench_dir =  base_dir / "benchmarks"
@@ -100,10 +120,12 @@
     datas = list_data_envs(base_dir)
     benchmarks = list_benchmarks(base_dir)
     for i in range(size):
+        h = random.choice(hashes)
+        compatible_datas = [d for d in datas if compatible_with(base_dir, d, h)]
         yield Case(
-            random.choice(hashes),
+            h,
             random.choice(variants),
-            random.choice(datas),
+            random.choice(compatible_datas),
             random.choice(benchmarks),
         )