changeset 6240:2686ecc90791

topic: store topic namespace in commit extras
author Anton Shestakov <av6@dwimlabs.net>
date Wed, 04 May 2022 15:53:57 +0400
parents 8861e33f3bb2
children 85abf58c3d8c
files hgext3rd/topic/__init__.py tests/test-namespaces.t
diffstat 2 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Wed May 04 15:38:24 2022 +0400
+++ b/hgext3rd/topic/__init__.py	Wed May 04 15:53:57 2022 +0400
@@ -291,6 +291,24 @@
                       default=None,
             )
 
+def _contexttns(self, force=False):
+    if not force and not self.mutable():
+        return b'default'
+    cache = getattr(self._repo, '_tnscache', None)
+    # topic loaded, but not enabled (eg: multiple repo in the same process)
+    if cache is None:
+        return b'default'
+    if self.rev() is None:
+        # don't cache volatile ctx instances that aren't stored on-disk yet
+        return self.extra().get(b'topic-namespace', b'default')
+    tns = cache.get(self.rev())
+    if tns is None:
+        tns = self.extra().get(b'topic-namespace', b'default')
+        self._repo._tnscache[self.rev()] = tns
+    return tns
+
+context.basectx.topic_namespace = _contexttns
+
 def _contexttopic(self, force=False):
     if not (force or self.mutable()):
         return b''
@@ -735,6 +753,12 @@
     orig(self, repo, *args, **kwargs)
     if not hastopicext(repo):
         return
+    if b'topic-namespace' not in self._extra:
+        if getattr(repo, 'currenttns', b''):
+            self._extra[b'topic-namespace'] = repo.currenttns
+        else:
+            # Default value will be dropped from extra by another hack at the changegroup level
+            self._extra[b'topic-namespace'] = b'default'
     if constants.extrakey not in self._extra:
         if getattr(repo, 'currenttopic', b''):
             self._extra[constants.extrakey] = repo.currenttopic
@@ -745,6 +769,9 @@
 def wrapadd(orig, cl, manifest, files, desc, transaction, p1, p2, user,
             date=None, extra=None, p1copies=None, p2copies=None,
             filesadded=None, filesremoved=None):
+    if b'topic-namespace' in extra and extra[b'topic-namespace'] == b'default':
+        extra = extra.copy()
+        del extra[b'topic-namespace']
     if constants.extrakey in extra and not extra[constants.extrakey]:
         extra = extra.copy()
         del extra[constants.extrakey]
--- a/tests/test-namespaces.t	Wed May 04 15:38:24 2022 +0400
+++ b/tests/test-namespaces.t	Wed May 04 15:53:57 2022 +0400
@@ -20,6 +20,19 @@
   abort: cannot use --clear when setting a topic namespace
   [255]
 
+  $ hg branch stable
+  marked working directory as branch stable
+  (branches are permanent and global, did you want a bookmark?)
+  $ hg debug-topic-namespace alice
+  marked working directory as topic namespace: alice
+  $ hg topic feature
+  marked working directory as topic: feature
+  $ echo a > a
+  $ hg ci -qAm a
+
+  $ hg debug-topic-namespaces
+  alice
+
 Parsing
 
   $ hg debugparsefqbn foo/bar//user26/feature -T '[{branch}] <{topic_namespace}> ({topic})\n'