changeset 6266:213db29a19e9

topic: ability to shorten branch//namespace/topic strings when possible
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 31 May 2022 12:14:51 +0400
parents 8b3cb6cae4a2
children 1eb543272657
files hgext3rd/topic/__init__.py hgext3rd/topic/common.py tests/test-namespaces.t
diffstat 3 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Thu Jun 23 18:18:55 2022 +0400
+++ b/hgext3rd/topic/__init__.py	Tue May 31 12:14:51 2022 +0400
@@ -1629,8 +1629,9 @@
         (b'b', b'branch', b'', b'branch'),
         (b'n', b'topic-namespace', b'', b'topic namespace'),
         (b't', b'topic', b'', b'topic'),
+        (b's', b'short', False, b'short format'),
     ], 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'))
+    short = common.formatfqbn(opts.get('branch'), opts.get('topic_namespace'), opts.get('topic'), opts.get('short'))
     ui.write(b'%s\n' % short)
--- a/hgext3rd/topic/common.py	Thu Jun 23 18:18:55 2022 +0400
+++ b/hgext3rd/topic/common.py	Tue May 31 12:14:51 2022 +0400
@@ -42,7 +42,7 @@
         tns, topic = b'default', tns
     return branch, tns, topic
 
-def formatfqbn(branch=b'', namespace=b'', topic=b''):
+def formatfqbn(branch=b'', namespace=b'', topic=b'', short=False):
     """format branch, namespace and topic into branch//namespace/topic string
 
     >>> formatfqbn(branch=b'branch', topic=b'topic')
@@ -67,11 +67,16 @@
     'http://example.com/branch//namespace/topic'
     """
     result = b''
-    if branch:
+    showbranch = True # branch and not (short and branch == b'default')
+    shownamespace = namespace and not (short and namespace == b'default')
+    if short and not showbranch and not shownamespace and not topic:
+        # if there's nothing to show, show at least branch
+        showbranch = True
+    if showbranch:
         result += branch
-    if namespace or topic or b'//' in branch:
+    if shownamespace or topic or b'//' in branch:
         result += b'//'
-    if namespace:
+    if shownamespace:
         result += namespace + b'/'
     result += topic
     return result
--- a/tests/test-namespaces.t	Thu Jun 23 18:18:55 2022 +0400
+++ b/tests/test-namespaces.t	Tue May 31 12:14:51 2022 +0400
@@ -109,4 +109,21 @@
   $ hg debug-format-fqbn -b foo/bar -n user26 -t feature
   foo/bar//user26/feature
 
+default values
+
+  $ hg debug-format-fqbn -b default -n default -t ''
+  default//default/
+  $ hg debug-format-fqbn -b default -n default -t '' --short
+  default
+
+  $ hg debug-format-fqbn -b default -n namespace -t ''
+  default//namespace/
+  $ hg debug-format-fqbn -b default -n namespace -t '' --short
+  default//namespace/
+
+  $ hg debug-format-fqbn -b default -n default -t topic
+  default//default/topic
+  $ hg debug-format-fqbn -b default -n default -t topic --short
+  default//topic
+
   $ cd ..