[PATCH 1 of 2] revset: delay showing parse error for the revset alias until it is referred

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Mon Jan 5 02:05:39 UTC 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1420423324 -32400
#      Mon Jan 05 11:02:04 2015 +0900
# Node ID 59c2ca97eb6fb2f44c98affbdecac5bd98347bfb
# Parent  42908c3275c63b7d2d6c871094c1e2c8f7ba31c8
revset: delay showing parse error for the revset alias until it is referred

Before this patch, a problematic revset alias aborts execution
immediately, even if it isn't referred in the specified revset.

If old "hg" may be used too (for example, bisecting Mercurial itself),
it is also difficult to write alias definitions using features newly
introduced by newer "hg" into configuration files, because such alias
definitions cause unexpected abortion at parsing revset aliases with
old "hg".

This patch delays showing parse error for the revset alias until it is
actually referred at runtime.

This patch detects referring problematic aliases in "_expandaliases"
by examination of "revsetalias.error", which is initialized with the
error message only when parsing fails.

For usability, this patch also warns about problematic aliases, even
if they aren't referred at runtime. This should help users to know
potential problems in their alias definitions earlier.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1956,6 +1956,12 @@
     funcre = re.compile('^([^(]+)\(([^)]+)\)$')
     args = None
 
+    # error message at parsing, or None
+    error = None
+    # whether own `error` information is already shown or not.
+    # this avoids showing same warning multiple times at each `findaliases`.
+    warned = False
+
     def __init__(self, name, value):
         '''Aliases like:
 
@@ -1975,11 +1981,17 @@
             self.name = name
             self.tree = ('symbol', name)
 
-        self.replacement, pos = parse(value)
-        if pos != len(value):
-            raise error.ParseError(_('invalid token'), pos)
-        # Check for placeholder injection
-        _checkaliasarg(self.replacement, self.args)
+        try:
+            self.replacement, pos = parse(value)
+            if pos != len(value):
+                raise error.ParseError(_('invalid token'), pos)
+            # Check for placeholder injection
+            _checkaliasarg(self.replacement, self.args)
+        except error.ParseError, inst:
+            if len(inst.args) > 1:
+                self.error = _('at %s: %s') % (inst.args[1], inst.args[0])
+            else:
+                self.error = inst.args[0]
 
 def _getalias(aliases, tree):
     """If tree looks like an unexpanded alias, return it. Return None
@@ -2021,6 +2033,9 @@
         return tree
     alias = _getalias(aliases, tree)
     if alias is not None:
+        if alias.error:
+            raise util.Abort(_('failed to parse revset alias "%s": %s') %
+                             (alias.name, alias.error))
         if alias in expanding:
             raise error.ParseError(_('infinite expansion of revset alias "%s" '
                                      'detected') % alias.name)
@@ -2042,13 +2057,22 @@
                        for t in tree)
     return result
 
-def findaliases(ui, tree):
+def findaliases(ui, tree, showwarning=None):
     _checkaliasarg(tree)
     aliases = {}
     for k, v in ui.configitems('revsetalias'):
         alias = revsetalias(k, v)
         aliases[alias.name] = alias
-    return _expandaliases(aliases, tree, [], {})
+    tree = _expandaliases(aliases, tree, [], {})
+    if showwarning:
+        # warn about problematic (but not referred) aliases
+        for name, alias in sorted(aliases.iteritems()):
+            if alias.error and not alias.warned:
+                msg = _('failed to parse revset alias "%s": %s'
+                        ) % (name, alias.error)
+                showwarning(_('warning: %s\n') % (msg))
+                alias.warned = True
+    return tree
 
 def parse(spec, lookup=None):
     p = parser.parser(tokenize, elements)
@@ -2064,7 +2088,7 @@
     if (pos != len(spec)):
         raise error.ParseError(_("invalid token"), pos)
     if ui:
-        tree = findaliases(ui, tree)
+        tree = findaliases(ui, tree, showwarning=ui.warn)
     weight, tree = optimize(tree, True)
     def mfunc(repo, subset):
         if util.safehasattr(subset, 'isascending'):
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -956,11 +956,19 @@
     (range
       ('symbol', '2')
       ('symbol', '5')))
-  hg: parse error: not a function: _aliasarg
-  [255]
+  abort: failed to parse revset alias "injectparamasstring2": not a function: _aliasarg
+  [255]
+  $ hg debugrevspec --debug --config revsetalias.anotherbadone='branch(' "tip"
+  ('symbol', 'tip')
+  warning: failed to parse revset alias "anotherbadone": at 7: not a prefix: end
+  warning: failed to parse revset alias "injectparamasstring2": not a function: _aliasarg
+  9
   >>> data = file('.hg/hgrc', 'rb').read()
   >>> file('.hg/hgrc', 'wb').write(data.replace('_aliasarg', ''))
 
+  $ try 'tip'
+  ('symbol', 'tip')
+  9
   $ try 'd(2:5)'
   (func
     ('symbol', 'd')


More information about the Mercurial-devel mailing list