annotate hgext3rd/topic/common.py @ 6911:e14ae43aed88 mercurial-5.9

test-compat: merge mercurial-6.0 into mercurial-5.9
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 25 Oct 2024 16:09:37 +0400
parents 963471ebe26a
children f8ea46c21b56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4531
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
1 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net>
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
2 #
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
3 # This software may be used and distributed according to the terms of the
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
4 # GNU General Public License version 2 or any later version.
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
5
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
6 def hastopicext(repo):
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
7 """True if the repo use the topic extension"""
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
8 return getattr(repo, 'hastopicext', False)
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
9
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
10 def parsefqbn(string):
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
11 """parse branch//namespace/topic string into branch, namespace and topic
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
12
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
13 >>> parsefqbn(b'branch//topic')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
14 ('branch', 'none', 'topic')
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
15 >>> parsefqbn(b'//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
16 ('default', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
17 >>> parsefqbn(b'branch//')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
18 ('branch', 'none', '')
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
19 >>> parsefqbn(b'//namespace/')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
20 ('default', 'namespace', '')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
21 >>> parsefqbn(b'/topic')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
22 ('/topic', 'none', '')
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
23 >>> parsefqbn(b'//topic')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
24 ('default', 'none', 'topic')
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
25 >>> parsefqbn(b'branch//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
26 ('branch', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
27 >>> parsefqbn(b'file:///tmp/branch//')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
28 ('file:///tmp/branch', 'none', '')
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
29 >>> parsefqbn(b'http://example.com/branch//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
30 ('http://example.com/branch', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
31 """
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
32 branch, sep, other = string.rpartition(b'//')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
33 if not sep:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
34 # when there's no // anywhere in the string, rpartition returns
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
35 # untouched string as the 3rd element, and the first two are empty
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
36 branch, other = other, b''
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
37 if not branch:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
38 branch = b'default'
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
39 tns, sep, topic = other.partition(b'/')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
40 if not sep:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
41 # when there's no / in the rest of the string, there can only be topic
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
42 tns, topic = b'none', tns
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
43 return branch, tns, topic
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
44
6267
1eb543272657 topic: switch to the short fqbn format by default
Anton Shestakov <av6@dwimlabs.net>
parents: 6266
diff changeset
45 def formatfqbn(branch=b'', namespace=b'', topic=b'', short=True):
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
46 """format branch, namespace and topic into branch//namespace/topic string
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
47
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
48 >>> formatfqbn(branch=b'branch', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
49 'branch//topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
50 >>> formatfqbn(namespace=b'namespace', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
51 '//namespace/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
52 >>> formatfqbn(branch=b'branch')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
53 'branch'
6244
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
54 >>> formatfqbn(branch=b'branch//')
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
55 'branch////'
6335
394c795d7ba4 tests: add "double//slash" example to topic.common.formatfqbn() docstring
Anton Shestakov <av6@dwimlabs.net>
parents: 6296
diff changeset
56 >>> formatfqbn(branch=b'double//slash')
394c795d7ba4 tests: add "double//slash" example to topic.common.formatfqbn() docstring
Anton Shestakov <av6@dwimlabs.net>
parents: 6296
diff changeset
57 'double//slash//'
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
58 >>> formatfqbn(namespace=b'namespace')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
59 '//namespace/'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
60 >>> formatfqbn(branch=b'/topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
61 '/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
62 >>> formatfqbn(topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
63 '//topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
64 >>> formatfqbn(branch=b'branch', namespace=b'namespace', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
65 'branch//namespace/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
66 >>> formatfqbn(branch=b'foo/bar', namespace=b'user26', topic=b'feature')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
67 'foo/bar//user26/feature'
6244
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
68 >>> formatfqbn(branch=b'http://example.com/branch', namespace=b'namespace', topic=b'topic')
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
69 'http://example.com/branch//namespace/topic'
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
70 """
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
71 result = b''
6266
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
72 showbranch = True # branch and not (short and branch == b'default')
6487
963471ebe26a topic: make topic namespace use string "none" as the default/empty value
Anton Shestakov <av6@dwimlabs.net>
parents: 6335
diff changeset
73 shownamespace = namespace and not (short and namespace == b'none')
6266
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
74 if short and not showbranch and not shownamespace and not topic:
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
75 # if there's nothing to show, show at least branch
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
76 showbranch = True
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
77 if showbranch:
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
78 result += branch
6266
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
79 if shownamespace or topic or b'//' in branch:
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
80 result += b'//'
6266
213db29a19e9 topic: ability to shorten branch//namespace/topic strings when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 6244
diff changeset
81 if shownamespace:
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
82 result += namespace + b'/'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
83 result += topic
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
84 return result
6296
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
85
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
86 def upgradeformat(branch):
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
87 """take branch and topic in ":" format and return fqbn in "//" format
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
88
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
89 This function can be used for transforming branchmap contents of peers that
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
90 don't support topic namespaces yet to work with peers with topic namespaces
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
91 support.
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
92
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
93 >>> upgradeformat(b'branch')
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
94 'branch'
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
95 >>> upgradeformat(b'branch:topic')
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
96 'branch//topic'
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
97 >>> upgradeformat(b'branch//')
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
98 'branch////'
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
99 >>> upgradeformat(b'branch//:topic')
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
100 'branch////topic'
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
101 """
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
102 if b':' not in branch:
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
103 # formatting anyway, because named branch could contain "//"
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
104 return formatfqbn(branch=branch)
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
105 # topic namespace cannot be extracted from ":" format
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
106 branch, topic = branch.split(b':', 1)
a2855aff1268 topic: call a different wire protocol command to get tns-aware branchmap
Anton Shestakov <av6@dwimlabs.net>
parents: 6267
diff changeset
107 return formatfqbn(branch=branch, topic=topic)