Patchwork [1,of,2,V3] fileset: split the logic for matching a size expression to a separate method

login
register
mail settings
Submitter Matt Harbison
Date Jan. 13, 2018, 3:21 a.m.
Message ID <2201fc4fd6d3d9dda724.1515813718@Envy>
Download mbox | patch
Permalink /patch/26712/
State Accepted
Headers show

Comments

Matt Harbison - Jan. 13, 2018, 3:21 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1515641708 18000
#      Wed Jan 10 22:35:08 2018 -0500
# Node ID 2201fc4fd6d3d9dda72459c6642c16acbe6fc0a4
# Parent  24b5106e3e1e265cb60258b314ae2b774610de81
fileset: split the logic for matching a size expression to a separate method

This will be used in the next patch to build a simple filtering language, but
where we won't have an mctx.

Patch

diff --git a/mercurial/fileset.py b/mercurial/fileset.py
--- a/mercurial/fileset.py
+++ b/mercurial/fileset.py
@@ -344,6 +344,35 @@ 
     except ValueError:
         raise error.ParseError(_("couldn't parse size: %s") % s)
 
+def sizematcher(x):
+    """Return a function(size) -> bool from the ``size()`` expression"""
+
+    # i18n: "size" is a keyword
+    expr = getstring(x, _("size requires an expression")).strip()
+    if '-' in expr: # do we have a range?
+        a, b = expr.split('-', 1)
+        a = util.sizetoint(a)
+        b = util.sizetoint(b)
+        return lambda x: x >= a and x <= b
+    elif expr.startswith("<="):
+        a = util.sizetoint(expr[2:])
+        return lambda x: x <= a
+    elif expr.startswith("<"):
+        a = util.sizetoint(expr[1:])
+        return lambda x: x < a
+    elif expr.startswith(">="):
+        a = util.sizetoint(expr[2:])
+        return lambda x: x >= a
+    elif expr.startswith(">"):
+        a = util.sizetoint(expr[1:])
+        return lambda x: x > a
+    elif expr[0].isdigit or expr[0] == '.':
+        a = util.sizetoint(expr)
+        b = _sizetomax(expr)
+        return lambda x: x >= a and x <= b
+    else:
+        raise error.ParseError(_("couldn't parse size: %s") % expr)
+
 @predicate('size(expression)', callexisting=True)
 def size(mctx, x):
     """File size matches the given expression. Examples:
@@ -353,33 +382,7 @@ 
     - size('>= .5MB') - files at least 524288 bytes
     - size('4k - 1MB') - files from 4096 bytes to 1048576 bytes
     """
-
-    # i18n: "size" is a keyword
-    expr = getstring(x, _("size requires an expression")).strip()
-    if '-' in expr: # do we have a range?
-        a, b = expr.split('-', 1)
-        a = util.sizetoint(a)
-        b = util.sizetoint(b)
-        m = lambda x: x >= a and x <= b
-    elif expr.startswith("<="):
-        a = util.sizetoint(expr[2:])
-        m = lambda x: x <= a
-    elif expr.startswith("<"):
-        a = util.sizetoint(expr[1:])
-        m = lambda x: x < a
-    elif expr.startswith(">="):
-        a = util.sizetoint(expr[2:])
-        m = lambda x: x >= a
-    elif expr.startswith(">"):
-        a = util.sizetoint(expr[1:])
-        m = lambda x: x > a
-    elif expr[0].isdigit or expr[0] == '.':
-        a = util.sizetoint(expr)
-        b = _sizetomax(expr)
-        m = lambda x: x >= a and x <= b
-    else:
-        raise error.ParseError(_("couldn't parse size: %s") % expr)
-
+    m = sizematcher(x)
     return [f for f in mctx.existing() if m(mctx.ctx[f].size())]
 
 @predicate('encoding(name)', callexisting=True)