changeset 6237:0b9042408809

topic: formatting branch, namespace and topic into fully qualified branch name
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 03 May 2022 21:46:23 +0400
parents 7ad8107d953a
children 6bbd16579f0f
files hgext3rd/topic/__init__.py hgext3rd/topic/common.py tests/test-namespaces.t
diffstat 3 files changed, 53 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Tue May 03 21:35:28 2022 +0400
+++ b/hgext3rd/topic/__init__.py	Tue May 03 21:46:23 2022 +0400
@@ -1512,3 +1512,13 @@
     fm.write(b'topic_namespace', b'namespace: %s\n', tns)
     fm.write(b'topic', b'topic:     %s\n', topic)
     fm.end()
+
+@command(b'debug-format-fqbn', [
+        (b'b', b'branch', b'', b'branch'),
+        (b'n', b'topic-namespace', b'', b'topic namespace'),
+        (b't', b'topic', b'', b'topic'),
+    ], optionalrepo=True)
+def debugformatfqbn(ui, repo, **opts):
+    """format branch, namespace and topic into branch//namespace/topic string"""
+    short = common.formatfqbn(opts.get('branch'), opts.get('topic_namespace'), opts.get('topic'))
+    ui.write(b'%s\n' % short)
--- a/hgext3rd/topic/common.py	Tue May 03 21:35:28 2022 +0400
+++ b/hgext3rd/topic/common.py	Tue May 03 21:46:23 2022 +0400
@@ -41,3 +41,33 @@
         # when there's no / in the rest of the string, there can only be topic
         tns, topic = b'default', tns
     return branch, tns, topic
+
+def formatfqbn(branch=b'', namespace=b'', topic=b''):
+    """format branch, namespace and topic into branch//namespace/topic string
+
+    >>> formatfqbn(branch=b'branch', topic=b'topic')
+    'branch//topic'
+    >>> formatfqbn(namespace=b'namespace', topic=b'topic')
+    '//namespace/topic'
+    >>> formatfqbn(branch=b'branch')
+    'branch'
+    >>> formatfqbn(namespace=b'namespace')
+    '//namespace/'
+    >>> formatfqbn(branch=b'/topic')
+    '/topic'
+    >>> formatfqbn(topic=b'topic')
+    '//topic'
+    >>> formatfqbn(branch=b'branch', namespace=b'namespace', topic=b'topic')
+    'branch//namespace/topic'
+    >>> formatfqbn(branch=b'foo/bar', namespace=b'user26', topic=b'feature')
+    'foo/bar//user26/feature'
+    """
+    result = b''
+    if branch:
+        result += branch
+    if namespace or topic:
+        result += b'//'
+    if namespace:
+        result += namespace + b'/'
+    result += topic
+    return result
--- a/tests/test-namespaces.t	Tue May 03 21:35:28 2022 +0400
+++ b/tests/test-namespaces.t	Tue May 03 21:46:23 2022 +0400
@@ -12,3 +12,16 @@
   branch:    foo/bar
   namespace: default
   topic:     
+
+Formatting
+
+  $ hg debugformatfqbn -b branch -n namespace -t topic
+  branch//namespace/topic
+
+  $ hg debug-format-fqbn -n namespace
+  //namespace/
+
+  $ hg debug-format-fqbn -b foo/bar -n user26 -t feature
+  foo/bar//user26/feature
+
+  $ cd ..