changeset 6236:7ad8107d953a

topic: introduce topic namespaces concept starting with simple parsing
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 03 May 2022 21:35:28 +0400
parents 318b81560f8c
children 0b9042408809
files hgext3rd/topic/__init__.py hgext3rd/topic/common.py tests/test-check-sdist.t tests/test-doctest.py tests/test-namespaces.t
diffstat 5 files changed, 63 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Thu May 12 20:02:40 2022 +0400
+++ b/hgext3rd/topic/__init__.py	Tue May 03 21:35:28 2022 +0400
@@ -1500,3 +1500,15 @@
     # Restore the topic if need
     if topic:
         _changecurrenttopic(repo, topic)
+
+@command(b'debug-parse-fqbn', commands.formatteropts, _(b'FQBN'), optionalrepo=True)
+def debugparsefqbn(ui, repo, fqbn, **opts):
+    """parse branch//namespace/topic string into its components"""
+    branch, tns, topic = common.parsefqbn(fqbn)
+    opts = pycompat.byteskwargs(opts)
+    fm = ui.formatter(b'debug-parse-namespace', opts)
+    fm.startitem()
+    fm.write(b'branch', b'branch:    %s\n', branch)
+    fm.write(b'topic_namespace', b'namespace: %s\n', tns)
+    fm.write(b'topic', b'topic:     %s\n', topic)
+    fm.end()
--- a/hgext3rd/topic/common.py	Thu May 12 20:02:40 2022 +0400
+++ b/hgext3rd/topic/common.py	Tue May 03 21:35:28 2022 +0400
@@ -6,3 +6,38 @@
 def hastopicext(repo):
     """True if the repo use the topic extension"""
     return getattr(repo, 'hastopicext', False)
+
+def parsefqbn(string):
+    """parse branch//namespace/topic string into branch, namespace and topic
+
+    >>> parsefqbn(b'branch//topic')
+    ('branch', 'default', 'topic')
+    >>> parsefqbn(b'//namespace/topic')
+    ('default', 'namespace', 'topic')
+    >>> parsefqbn(b'branch//')
+    ('branch', 'default', '')
+    >>> parsefqbn(b'//namespace/')
+    ('default', 'namespace', '')
+    >>> parsefqbn(b'/topic')
+    ('/topic', 'default', '')
+    >>> parsefqbn(b'//topic')
+    ('default', 'default', 'topic')
+    >>> parsefqbn(b'branch//namespace/topic')
+    ('branch', 'namespace', 'topic')
+    >>> parsefqbn(b'file:///tmp/branch//')
+    ('file:///tmp/branch', 'default', '')
+    >>> parsefqbn(b'http://example.com/branch//namespace/topic')
+    ('http://example.com/branch', 'namespace', 'topic')
+    """
+    branch, sep, other = string.rpartition(b'//')
+    if not sep:
+        # when there's no // anywhere in the string, rpartition returns
+        # untouched string as the 3rd element, and the first two are empty
+        branch, other = other, b''
+    if not branch:
+        branch = b'default'
+    tns, sep, topic = other.partition(b'/')
+    if not sep:
+        # when there's no / in the rest of the string, there can only be topic
+        tns, topic = b'default', tns
+    return branch, tns, topic
--- a/tests/test-check-sdist.t	Thu May 12 20:02:40 2022 +0400
+++ b/tests/test-check-sdist.t	Tue May 03 21:35:28 2022 +0400
@@ -35,7 +35,7 @@
 
   $ tar -tzf hg-evolve-*.tar.gz | sed 's|^hg-evolve-[^/]*/||' | sort > files
   $ wc -l files
-  358 files
+  359 files
   $ fgrep debian files
   tests/test-check-debian.t
   $ fgrep __init__.py files
--- a/tests/test-doctest.py	Thu May 12 20:02:40 2022 +0400
+++ b/tests/test-doctest.py	Tue May 03 21:35:28 2022 +0400
@@ -110,6 +110,7 @@
 expected_mods_tested = set(
     [
         ('hgext3rd.evolve.obshistory', '{}'),
+        ('hgext3rd.topic.common', '{}'),
     ]
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-namespaces.t	Tue May 03 21:35:28 2022 +0400
@@ -0,0 +1,14 @@
+https://www.mercurial-scm.org/wiki/TopicPlan#sub_branches.2C_namespacing_and_representation
+
+  $ . "$TESTDIR/testlib/topic_setup.sh"
+
+Parsing
+
+  $ hg debugparsefqbn foo/bar//user26/feature -T '[{branch}] <{topic_namespace}> ({topic})\n'
+  [foo/bar] <user26> (feature)
+
+no double slashes means it's a named branch
+  $ hg debug-parse-fqbn foo/bar
+  branch:    foo/bar
+  namespace: default
+  topic: