Patchwork [2,of,7] parser: extract helper that creates a dict of aliases

login
register
mail settings
Submitter Yuya Nishihara
Date April 13, 2016, 3:55 p.m.
Message ID <4edd04c9b8c3a00bab9b.1460562914@mimosa>
Download mbox | patch
Permalink /patch/14581/
State Accepted
Commit ee11167fe1da9ea8d931f703a8255251d5723434
Headers show

Comments

Yuya Nishihara - April 13, 2016, 3:55 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1456741455 -32400
#      Mon Feb 29 19:24:15 2016 +0900
# Node ID 4edd04c9b8c3a00bab9b11e77b32792dfe38c6c8
# Parent  ed89253c74dde41715fb40384e1212995be5a264
parser: extract helper that creates a dict of aliases

This will be common between revset and templater.

The local variable 'alias' is renamed to 'a' to avoid shadowing the global
'alias' class.

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -462,3 +462,13 @@  class basealiasrules(object):
         if err:
             err = efmt % {'section': cls._section, 'name': name, 'error': err}
         return alias(name, tree, args, err, repl)
+
+    @classmethod
+    def buildmap(cls, items):
+        """Parse a list of alias (name, replacement) pairs into a dict of
+        alias objects"""
+        aliases = {}
+        for decl, defn in items:
+            a = cls.build(decl, defn)
+            aliases[a.name] = a
+        return aliases
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2320,10 +2320,7 @@  def _expandaliases(aliases, tree, expand
     return result
 
 def findaliases(ui, tree, showwarning=None):
-    aliases = {}
-    for k, v in ui.configitems('revsetalias'):
-        alias = _aliasrules.build(k, v)
-        aliases[alias.name] = alias
+    aliases = _aliasrules.buildmap(ui.configitems('revsetalias'))
     tree = _expandaliases(aliases, tree, [], {})
     if showwarning:
         # warn about problematic (but not referred) aliases