Patchwork [2,of,3] fileset: reorder 'or' expression by weight

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 4, 2018, 12:14 p.m.
Message ID <db488b9c65596088550b.1533384891@mimosa>
Download mbox | patch
Permalink /patch/33236/
State New
Headers show

Comments

Yuya Nishihara - Aug. 4, 2018, 12:14 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1532160814 -32400
#      Sat Jul 21 17:13:34 2018 +0900
# Node ID db488b9c65596088550b2b96ed25aff60a938219
# Parent  edc1dd4b31467ccff6d50435cca4f29840032331
fileset: reorder 'or' expression by weight

Patch

diff --git a/mercurial/filesetlang.py b/mercurial/filesetlang.py
--- a/mercurial/filesetlang.py
+++ b/mercurial/filesetlang.py
@@ -207,6 +207,8 @@  def _optimize(x):
             return wb, _optimizeandops(op, tb, ta)
     if op == 'or':
         ws, ts = zip(*(_optimize(y) for y in x[1:]))
+        ts = tuple(it[1] for it in sorted(enumerate(ts),
+                                          key=lambda it: ws[it[0]]))
         return max(ws), (op,) + ts
     if op == 'list':
         ws, ts = zip(*(_optimize(y) for y in x[1:]))
diff --git a/tests/test-fileset.t b/tests/test-fileset.t
--- a/tests/test-fileset.t
+++ b/tests/test-fileset.t
@@ -203,6 +203,30 @@  Show parsed tree at stages:
   b1
   b2
 
+OR expression should be reordered by weight:
+
+  $ fileset -p optimized -s -r. 'grep("a") or a1 or grep("b") or b2'
+  * optimized:
+  (or
+    (symbol 'a1')
+    (symbol 'b2')
+    (func
+      (symbol 'grep')
+      (string 'a'))
+    (func
+      (symbol 'grep')
+      (string 'b')))
+  * matcher:
+  <unionmatcher matchers=[
+    <patternmatcher patterns='(?:a1$)'>,
+    <patternmatcher patterns='(?:b2$)'>,
+    <predicatenmatcher pred=grep('a')>,
+    <predicatenmatcher pred=grep('b')>]>
+  a1
+  a2
+  b1
+  b2
+
 Use differencematcher for 'x and not y':
 
   $ fileset -p optimized -s 'a* and not a1'