Patchwork [V2] revset: use '%' as an operator for 'only'

login
register
mail settings
Submitter Sean Farley
Date Jan. 8, 2015, 12:05 a.m.
Message ID <be1b13ac7796d78362a3.1420675506@laptop.local>
Download mbox | patch
Permalink /patch/7367/
State Accepted
Commit 537a2669a1136bff0ced6a9a07b4c5c1a3ae295e
Headers show

Comments

Sean Farley - Jan. 8, 2015, 12:05 a.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1415314518 28800
#      Thu Nov 06 14:55:18 2014 -0800
# Node ID be1b13ac7796d78362a3a2cc8afbe82210e5acb8
# Parent  f82173a90c2c9d0d32216fe7243ec51fc6d44ff7
revset: use '%' as an operator for 'only'

With this patch, we can make it much easier to specify 'only(A,B)' ->
A%B. Similarly, 'only(A)' -> A%.

On Windows, '%' is a semi-reserved symbol in the following way: using non-bash
shells (e.g. cmd.exe but NOT PowerShell, ConEmu, and cmder), %var% is only
expanded when 'var' exists and is surrounded by '%'.

That only leaves batch scripts which could prove to be problematic. I posit
that this isn't a big issue because any developer of batch scripts already
knows that to use '%' one needs to escape it by using a double '%%'.

Alternatives to '%' could be '=' but that might be limiting our future if we
ever decide to use temporary assignments in a revset.
Matt Mackall - Jan. 8, 2015, 6:24 p.m.
On Wed, 2015-01-07 at 16:05 -0800, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1415314518 28800
> #      Thu Nov 06 14:55:18 2014 -0800
> # Node ID be1b13ac7796d78362a3a2cc8afbe82210e5acb8
> # Parent  f82173a90c2c9d0d32216fe7243ec51fc6d44ff7
> revset: use '%' as an operator for 'only'

Queued for default, thanks. Check-code wishes you a happy new year.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -114,10 +114,11 @@  elements = {
     ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
     "not": (10, ("not", 10)),
     "!": (10, ("not", 10)),
     "and": (5, None, ("and", 5)),
     "&": (5, None, ("and", 5)),
+    "%": (5, None, ("only", 5), ("onlypost", 5)),
     "or": (4, None, ("or", 4)),
     "|": (4, None, ("or", 4)),
     "+": (4, None, ("or", 4)),
     ",": (2, None, ("list", 2)),
     ")": (0, None, None),
@@ -150,11 +151,11 @@  def tokenize(program, lookup=None):
             yield ('..', None, pos)
             pos += 1 # skip ahead
         elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
             yield ('##', None, pos)
             pos += 1 # skip ahead
-        elif c in "():,-|&+!~^": # handle simple operators
+        elif c in "():,-|&+!~^%": # handle simple operators
             yield (c, None, pos)
         elif (c in '"\'' or c == 'r' and
               program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
             if c == 'r':
                 pos += 1
@@ -1920,10 +1921,12 @@  methods = {
     "list": listset,
     "func": func,
     "ancestor": ancestorspec,
     "parent": parentspec,
     "parentpost": p1,
+    "only": only,
+    "onlypost": only,
 }
 
 def optimize(x, small):
     if x is None:
         return 0, x
@@ -1933,10 +1936,12 @@  def optimize(x, small):
         smallbonus = .5
 
     op = x[0]
     if op == 'minus':
         return optimize(('and', x[1], ('not', x[2])), small)
+    elif op == 'only':
+        return optimize(('func', ('symbol', 'only'), ('list', x[1], x[2])), small)
     elif op == 'dagrangepre':
         return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
     elif op == 'dagrangepost':
         return optimize(('func', ('symbol', 'descendants'), x[1]), small)
     elif op == 'rangepre':
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -436,10 +436,36 @@  Test empty set input
   2
   4
   8
   9
 
+Test '%' operator
+
+  $ log '9%'
+  8
+  9
+  $ log '9%5'
+  2
+  4
+  8
+  9
+  $ log '(7 + 9)%(5 + 2)'
+  4
+  6
+  7
+  8
+  9
+
+Test the order of operations
+
+  $ log '7 + 9%5 + 2'
+  7
+  2
+  4
+  8
+  9
+
 Test explicit numeric revision
   $ log 'rev(-1)'
   $ log 'rev(0)'
   0
   $ log 'rev(9)'