# HG changeset patch # User Sean Farley # Date 2014-11-06 22:55:18 # Node ID 537a2669a1136bff0ced6a9a07b4c5c1a3ae295e # Parent d486e52352e80426db6f3d738e35bb675b3c3fa2 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. diff --git a/mercurial/revset.py b/mercurial/revset.py --- a/mercurial/revset.py +++ b/mercurial/revset.py @@ -116,6 +116,7 @@ elements = { "!": (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)), @@ -152,7 +153,7 @@ def tokenize(program, lookup=None): 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 @@ -1922,6 +1923,8 @@ methods = { "ancestor": ancestorspec, "parent": parentspec, "parentpost": p1, + "only": only, + "onlypost": only, } def optimize(x, small): @@ -1935,6 +1938,9 @@ def optimize(x, small): 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': diff --git a/tests/test-revset.t b/tests/test-revset.t --- a/tests/test-revset.t +++ b/tests/test-revset.t @@ -438,6 +438,32 @@ Test empty set input 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)'