28
28
RE_PATH = r"""
29
29
((?P<user>.*)@)?
30
30
(?P<hostname>([^/:]+))
31
- (?P<separator>[:,/])?
31
+ (:(?P<port>\d{1,4}))?
32
+ (?P<separator>/)?
32
33
(?P<path>
33
- (\w[^:.]*) # cut the path at . to negate .hg
34
+ /? (\w[^:.]*)
34
35
)?
35
36
"""
36
37
@@ -151,9 +152,9 @@ class HgURL(URLProtocol, SkipDefaultFieldsReprMixin):
151
152
>>> HgURL(url='ssh://username@machinename/path/to/repo')
152
153
HgURL(url=ssh://username@machinename/path/to/repo,
153
154
scheme=ssh,
155
+ user=username,
154
156
hostname=machinename,
155
157
path=path/to/repo,
156
- user=username,
157
158
matcher=core-hg)
158
159
159
160
- Compatibility checking: :meth:`HgURL.is_valid()`
@@ -162,9 +163,11 @@ class HgURL(URLProtocol, SkipDefaultFieldsReprMixin):
162
163
163
164
url : str
164
165
scheme : Optional [str ] = None
165
- hostname : Optional [str ] = None
166
- path : Optional [str ] = None
167
166
user : Optional [str ] = None
167
+ hostname : str = dataclasses .field (default = "" )
168
+ port : Optional [int ] = None
169
+ separator : str = dataclasses .field (default = "/" )
170
+ path : str = dataclasses .field (default = "" )
168
171
169
172
#
170
173
# commit-ish: tag, branch, ref, revision
@@ -233,23 +236,63 @@ def to_url(self) -> str:
233
236
>>> hg_location.to_url()
234
237
'https://hg.mozilla.org/mobile-browser'
235
238
236
- Switch them to hglab :
239
+ Switch them to localhost :
237
240
238
241
>>> hg_location.hostname = 'localhost'
239
242
>>> hg_location.scheme = 'http'
240
243
241
244
>>> hg_location.to_url()
242
245
'http://localhost/mobile-browser'
243
246
244
- todo
245
- ----
247
+ Another example, `hugin <http://hugin.hg.sourceforge.net>`_:
248
+
249
+ >>> hugin = HgURL(url="http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin")
250
+
251
+ >>> hugin
252
+ HgURL(url=http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin,
253
+ scheme=http,
254
+ hostname=hugin.hg.sourceforge.net,
255
+ port=8000,
256
+ path=hgroot/hugin/hugin,
257
+ matcher=core-hg)
258
+
259
+ >>> hugin.to_url()
260
+ 'http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin'
261
+
262
+ SSH URL with a username, `graphicsmagic <http://graphicsmagick.org/Hg.html>`_:
263
+
264
+ >>> graphicsmagick = HgURL(
265
+ ... url="ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick"
266
+ ... )
267
+
268
+ >>> graphicsmagick
269
+ HgURL(url=ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick,
270
+ scheme=ssh,
271
+ user=yourid,
272
+ hostname=hg.GraphicsMagick.org,
273
+ path=/hg/GraphicsMagick,
274
+ matcher=core-hg)
275
+
276
+ >>> graphicsmagick.to_url()
277
+ 'ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick'
278
+
279
+ Switch the username:
280
+
281
+ >>> graphicsmagick.user = 'lucas'
282
+
283
+ >>> graphicsmagick.to_url()
284
+ 'ssh://lucas@hg.GraphicsMagick.org//hg/GraphicsMagick'
246
285
247
- - Formats: Show an example converting a hghub url from ssh -> https format,
248
- and the other way around.
249
286
"""
250
- if self .scheme is not None :
251
- parts = [self .scheme , "://" , self .hostname , "/" , self .path ]
252
- else :
253
- parts = [self .user or "hg" , "@" , self .hostname , ":" , self .path ]
287
+ parts = [self .scheme or "ssh" , "://" ]
288
+ if self .user :
289
+ parts .extend ([self .user , "@" ])
290
+
291
+ parts .append (self .hostname )
292
+
293
+ if self .port is not None :
294
+ parts .extend ([":" , f"{ self .port } " ])
295
+
296
+ parts .extend ([self .separator , self .path ])
254
297
255
298
return "" .join (part for part in parts if isinstance (part , str ))
0 commit comments