Patchwork [STABLE] templatekw: fix shownames() to check if namespace exists in repo (issue6301)

login
register
mail settings
Submitter Yuya Nishihara
Date April 16, 2020, 1:46 p.m.
Message ID <7ac0467d2f83793f7f20.1587044794@mimosa>
Download mbox | patch
Permalink /patch/46156/
State Accepted
Headers show

Comments

Yuya Nishihara - April 16, 2020, 1:46 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1587043811 -32400
#      Thu Apr 16 22:30:11 2020 +0900
# Branch stable
# Node ID 7ac0467d2f83793f7f20fb12fa1ef1b4d6599052
# Parent  539490756a72d8b9c8481af3129806072e9a7bb3
templatekw: fix shownames() to check if namespace exists in repo (issue6301)

Namespace registration is dynamic, but the corresponding template keyword
is registered statically.
Augie Fackler - April 16, 2020, 4:58 p.m.
queued, tanks 

> On Apr 16, 2020, at 09:46, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1587043811 -32400
> #      Thu Apr 16 22:30:11 2020 +0900
> # Branch stable
> # Node ID 7ac0467d2f83793f7f20fb12fa1ef1b4d6599052
> # Parent  539490756a72d8b9c8481af3129806072e9a7bb3
> templatekw: fix shownames() to check if namespace exists in repo (issue6301)
> 
> Namespace registration is dynamic, but the corresponding template keyword
> is registered statically.
> 
> diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
> --- a/mercurial/namespaces.py
> +++ b/mercurial/namespaces.py
> @@ -83,6 +83,9 @@ class namespaces(object):
>     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)
> 
> diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
> --- a/mercurial/templatekw.py
> +++ b/mercurial/templatekw.py
> @@ -548,7 +548,11 @@ def shownames(context, mapping, namespac
>     """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
> diff --git a/tests/test-log.t b/tests/test-log.t
> --- a/tests/test-log.t
> +++ b/tests/test-log.t
> @@ -2273,6 +2273,8 @@ Check that adding an arbitrary name show
>> 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 @@ multi-line template with error
> 
>   $ 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
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
--- a/mercurial/namespaces.py
+++ b/mercurial/namespaces.py
@@ -83,6 +83,9 @@  class namespaces(object):
     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)
 
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -548,7 +548,11 @@  def shownames(context, mapping, namespac
     """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
diff --git a/tests/test-log.t b/tests/test-log.t
--- a/tests/test-log.t
+++ b/tests/test-log.t
@@ -2273,6 +2273,8 @@  Check that adding an arbitrary name show
   > 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 @@  multi-line template with error
 
   $ 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