diff mercurial/templatekw.py @ 33414:16ed67164002

templatekw: export ui.paths as {peerpaths} It's sometimes useful to show hyperlinks in log output. "{get(peerpaths, "default")}/rev/{node}" Since each path may have sub options, "{peerpaths}" is structured as a dict of dicts, but the inner dict is rendered as if it were a string URL. The implementation is ad-hoc, so there are some weird behaviors described in the test. We might need to introduce a proper way of handling a hybrid scalar object. This patch adds _hybrid.__getitem__() so d['path']['url'] works. The keyword is named as "peerpaths" since "paths" seemed too generic in log context.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 13 Jul 2017 00:35:54 +0900
parents 89796a25d4bb
children c0d8de2724ce
line wrap: on
line diff
--- a/mercurial/templatekw.py	Fri Jul 07 23:13:04 2017 +0900
+++ b/mercurial/templatekw.py	Thu Jul 13 00:35:54 2017 +0900
@@ -59,6 +59,8 @@
             yield makemap(x)
     def __contains__(self, x):
         return x in self._values
+    def __getitem__(self, key):
+        return self._values[key]
     def __len__(self):
         return len(self._values)
     def __iter__(self):
@@ -591,6 +593,25 @@
         return 'obsolete'
     return ''
 
+@templatekeyword('peerpaths')
+def showpeerpaths(repo, **args):
+    """A dictionary of repository locations defined in the [paths] section
+    of your configuration file."""
+    # see commands.paths() for naming of dictionary keys
+    paths = util.sortdict()
+    for k, p in sorted(repo.ui.paths.iteritems()):
+        d = util.sortdict()
+        d['url'] = p.rawloc
+        d.update((o, v) for o, v in sorted(p.suboptions.iteritems()))
+        def f():
+            yield d['url']
+        paths[k] = hybriddict(d, gen=f())
+
+    # no hybriddict() since d['path'] can't be formatted as a string. perhaps
+    # hybriddict() should call templatefilters.stringify(d[value]).
+    return _hybrid(None, paths, lambda k: {'name': k, 'path': paths[k]},
+                   lambda d: '%s=%s' % (d['name'], d['path']['url']))
+
 @templatekeyword("predecessors")
 def showpredecessors(repo, ctx, **args):
     """Returns the list if the closest visible successors