templatekw: fix shownames() to check if namespace exists in repo (issue6301)
authorYuya Nishihara <yuya@tcha.org>
Thu, 16 Apr 2020 22:30:11 +0900
changeset 44728 59ad165f6cdb
parent 44727 694d40416d62
child 44729 26ce8e751503
child 44732 f44a7d8660ea
templatekw: fix shownames() to check if namespace exists in repo (issue6301) Namespace registration is dynamic, but the corresponding template keyword is registered statically.
mercurial/namespaces.py
mercurial/templatekw.py
tests/test-log.t
--- a/mercurial/namespaces.py	Wed Apr 15 20:10:35 2020 +0200
+++ b/mercurial/namespaces.py	Thu Apr 16 22:30:11 2020 +0900
@@ -83,6 +83,9 @@
     def __iter__(self):
         return self._names.__iter__()
 
+    def get(self, namespace, default=None):
+        return self._names.get(namespace, default)
+
     def items(self):
         return pycompat.iteritems(self._names)
 
--- a/mercurial/templatekw.py	Wed Apr 15 20:10:35 2020 +0200
+++ b/mercurial/templatekw.py	Thu Apr 16 22:30:11 2020 +0900
@@ -562,7 +562,11 @@
     """helper method to generate a template keyword for a namespace"""
     repo = context.resource(mapping, b'repo')
     ctx = context.resource(mapping, b'ctx')
-    ns = repo.names[namespace]
+    ns = repo.names.get(namespace)
+    if ns is None:
+        # namespaces.addnamespace() registers new template keyword, but
+        # the registered namespace might not exist in the current repo.
+        return
     names = ns.names(repo, ctx.node())
     return compatlist(
         context, mapping, ns.templatename, names, plural=namespace
--- a/tests/test-log.t	Wed Apr 15 20:10:35 2020 +0200
+++ b/tests/test-log.t	Thu Apr 16 22:30:11 2020 +0900
@@ -2273,6 +2273,8 @@
   > from mercurial import namespaces
   > 
   > def reposetup(ui, repo):
+  >     if not repo.local():
+  >         return
   >     foo = {b'foo': repo[0].node()}
   >     names = lambda r: foo.keys()
   >     namemap = lambda r, name: foo.get(name)
@@ -2328,6 +2330,18 @@
 
   $ cd ..
 
+New namespace is registered per repo instance, but the template keyword
+is global. So we shouldn't expect the namespace always exists. Using
+ssh:// makes sure a bundle repository is created from scratch. (issue6301)
+
+  $ hg clone -e "'$PYTHON' '$TESTDIR/dummyssh'" \
+  >          -qr0 "ssh://user@dummy/`pwd`/a" a-clone
+  $ hg incoming --config extensions.names=names.py -R a-clone \
+  >             -e "'$PYTHON' '$TESTDIR/dummyssh'" -T '{bars}\n' -l1
+  comparing with ssh://user@dummy/$TESTTMP/a
+  searching for changes
+  
+
 hg log -f dir across branches
 
   $ hg init acrossbranches