changeset 6877:f8ea46c21b56

topic: make formatfqbn() able to produce FQBN of 3 different lengths
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 26 Sep 2024 17:09:11 +0400
parents 2edffcd94850
children 2fbe91d762ef
files hgext3rd/topic/__init__.py hgext3rd/topic/common.py tests/test-namespaces.t
diffstat 3 files changed, 57 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Thu Sep 19 16:50:21 2024 +0400
+++ b/hgext3rd/topic/__init__.py	Thu Sep 26 17:09:11 2024 +0400
@@ -380,12 +380,12 @@
         return None
 context.basectx.topicidx = _contexttopicidx
 
-def _contextfqbn(self, short=True):
+def _contextfqbn(self, length=common.FQBN_NORMAL):
     """return branch//namespace/topic of the changeset, also known as fully
     qualified branch name
     """
     branch = encoding.tolocal(self.extra()[b'branch'])
-    return common.formatfqbn(branch, self.topic_namespace(), self.topic(), short=short)
+    return common.formatfqbn(branch, self.topic_namespace(), self.topic(), length=length)
 
 context.basectx.fqbn = _contextfqbn
 
@@ -974,11 +974,11 @@
             if ce:
                 ce.refresh()
 
-        def fqbn(self, short=True):
+        def fqbn(self, length=common.FQBN_NORMAL):
             branch = encoding.tolocal(self._branch)
             tns = encoding.tolocal(self._tns)
             topic = encoding.tolocal(self._topic)
-            return common.formatfqbn(branch, tns, topic, short=short)
+            return common.formatfqbn(branch, tns, topic, length=length)
 
     dirstate.dirstate = topicdirstate
 
@@ -1983,12 +1983,21 @@
     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'),
-        (b's', b'short', True, b'short format'),
+        (b'b', b'branch', b'', b'branch', b'BRANCH'),
+        (b'n', b'topic-namespace', b'', b'topic namespace', b'TNS'),
+        (b't', b'topic', b'', b'topic', b'TOPIC'),
+        (b'l', b'length', b'normal', b'length (short, normal, or full)', b'LENGTH'),
     ], 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'), opts.get('short'))
-    ui.write(b'%s\n' % short)
+    length = opts['length']
+    if length == b'short':
+        length = common.FQBN_SHORT
+    elif length == b'normal':
+        length = common.FQBN_NORMAL
+    elif length == b'full':
+        length = common.FQBN_FULL
+    else:
+        raise compat.InputError(b"invalid --length value: %s" % length)
+    fqbn = common.formatfqbn(opts.get('branch'), opts.get('topic_namespace'), opts.get('topic'), length=length)
+    ui.write(b'%s\n' % fqbn)
--- a/hgext3rd/topic/common.py	Thu Sep 19 16:50:21 2024 +0400
+++ b/hgext3rd/topic/common.py	Thu Sep 26 17:09:11 2024 +0400
@@ -42,7 +42,29 @@
         tns, topic = b'none', tns
     return branch, tns, topic
 
-def formatfqbn(branch=b'', namespace=b'', topic=b'', short=True):
+# Fully qualified branch name length constants for formatfqbn() etc
+#
+# Selecting different length only matters when at least one FQBN component is
+# empty/default. The general idea is: short length will skip all empty
+# components (except for branch if there's nothing else to show), normal length
+# will only skip default topic namespace and empty topic, full length will not
+# skip anything and will show everything. For example:
+#
+# - formatfqbn(topic=foo, length=FQBN_SHORT) gives:
+#   //foo
+#
+# - formatfqbn(topic=foo, length=FQBN_NORMAL) gives:
+#   default//foo
+#
+# - formatfqbn(topic=foo, length=FQBN_FULL) gives:
+#   default//none/foo
+#
+# See also: tests for debug-format-fqbn command.
+FQBN_SHORT = 1
+FQBN_NORMAL = 2
+FQBN_FULL = 3
+
+def formatfqbn(branch=b'', namespace=b'', topic=b'', length=FQBN_NORMAL):
     """format branch, namespace and topic into branch//namespace/topic string
 
     >>> formatfqbn(branch=b'branch', topic=b'topic')
@@ -69,9 +91,9 @@
     'http://example.com/branch//namespace/topic'
     """
     result = b''
-    showbranch = True # branch and not (short and branch == b'default')
-    shownamespace = namespace and not (short and namespace == b'none')
-    if short and not showbranch and not shownamespace and not topic:
+    showbranch = branch and not (length == FQBN_SHORT and branch == b'default')
+    shownamespace = namespace and not (length != FQBN_FULL and namespace == b'none')
+    if not shownamespace and not topic:
         # if there's nothing to show, show at least branch
         showbranch = True
     if showbranch:
--- a/tests/test-namespaces.t	Thu Sep 19 16:50:21 2024 +0400
+++ b/tests/test-namespaces.t	Thu Sep 26 17:09:11 2024 +0400
@@ -400,19 +400,25 @@
 
 default values
 
-  $ hg debug-format-fqbn -b default -n none -t '' --no-short
+  $ hg debug-format-fqbn -b default -n none -t '' --length=full
   default//none/
-  $ hg debug-format-fqbn -b default -n none -t '' --short
+  $ hg debug-format-fqbn -b default -n none -t '' --length=normal
+  default
+  $ hg debug-format-fqbn -b default -n none -t '' --length=short
   default
 
-  $ hg debug-format-fqbn -b default -n namespace -t '' --no-short
+  $ hg debug-format-fqbn -b default -n namespace -t '' --length=full
   default//namespace/
-  $ hg debug-format-fqbn -b default -n namespace -t '' --short
+  $ hg debug-format-fqbn -b default -n namespace -t '' --length=normal
   default//namespace/
+  $ hg debug-format-fqbn -b default -n namespace -t '' --length=short
+  //namespace/
 
-  $ hg debug-format-fqbn -b default -n none -t topic --no-short
+  $ hg debug-format-fqbn -b default -n none -t topic --length=full
   default//none/topic
-  $ hg debug-format-fqbn -b default -n none -t topic --short
+  $ hg debug-format-fqbn -b default -n none -t topic --length=normal
   default//topic
+  $ hg debug-format-fqbn -b default -n none -t topic --length=short
+  //topic
 
   $ cd ..