Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/utils/urlutil.py @ 47068:c029b35565dd
url_util: introduce a `try_path` function
That function? try a build a path, returning None on failure. This helps us to simplify various part of the existing code.
Differential Revision: https://phab.mercurial-scm.org/D10437
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Thu, 15 Apr 2021 09:50:56 +0200 |
parents | 221f8585e985 |
children | d69a9628eba7 |
comparison
equal
deleted
inserted
replaced
47067:20eba5cef2e0 | 47068:c029b35565dd |
---|---|
443 u = url(u) | 443 u = url(u) |
444 u.user = u.passwd = None | 444 u.user = u.passwd = None |
445 return bytes(u) | 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 def get_push_paths(repo, ui, dests): | 460 def get_push_paths(repo, ui, dests): |
449 """yields all the `path` selected as push destination by `dests`""" | 461 """yields all the `path` selected as push destination by `dests`""" |
450 if not dests: | 462 if not dests: |
451 if b'default-push' in ui.paths: | 463 if b'default-push' in ui.paths: |
452 yield ui.paths[b'default-push'] | 464 yield ui.paths[b'default-push'] |
469 for source in sources: | 481 for source in sources: |
470 if source in ui.paths: | 482 if source in ui.paths: |
471 url = ui.paths[source].rawloc | 483 url = ui.paths[source].rawloc |
472 else: | 484 else: |
473 # Try to resolve as a local path or URI. | 485 # Try to resolve as a local path or URI. |
474 try: | 486 path = try_path(ui, source) |
475 # we pass the ui instance are warning might need to be issued | 487 if path is not None: |
476 url = path(ui, None, rawloc=source).rawloc | 488 url = path.rawloc |
477 except ValueError: | 489 else: |
478 url = source | 490 url = source |
479 yield parseurl(url, default_branches) | 491 yield parseurl(url, default_branches) |
480 | 492 |
481 | 493 |
482 def get_unique_push_path(action, repo, ui, dest=None): | 494 def get_unique_push_path(action, repo, ui, dest=None): |
518 else: | 530 else: |
519 if source in ui.paths: | 531 if source in ui.paths: |
520 url = ui.paths[source].rawloc | 532 url = ui.paths[source].rawloc |
521 else: | 533 else: |
522 # Try to resolve as a local path or URI. | 534 # Try to resolve as a local path or URI. |
523 try: | 535 path = try_path(ui, source) |
524 # we pass the ui instance are warning might need to be issued | 536 if path is not None: |
525 url = path(ui, None, rawloc=source).rawloc | 537 url = path.rawloc |
526 except ValueError: | 538 else: |
527 url = source | 539 url = source |
528 return parseurl(url, default_branches) | 540 return parseurl(url, default_branches) |
529 | 541 |
530 | 542 |
531 def get_clone_path(ui, source, default_branches=()): | 543 def get_clone_path(ui, source, default_branches=()): |
540 else: | 552 else: |
541 if source in ui.paths: | 553 if source in ui.paths: |
542 url = ui.paths[source].rawloc | 554 url = ui.paths[source].rawloc |
543 else: | 555 else: |
544 # Try to resolve as a local path or URI. | 556 # Try to resolve as a local path or URI. |
545 try: | 557 path = try_path(ui, source) |
546 # we pass the ui instance are warning might need to be issued | 558 if path is not None: |
547 url = path(ui, None, rawloc=source).rawloc | 559 url = path.rawloc |
548 except ValueError: | 560 else: |
549 url = source | 561 url = source |
550 clone_path, branch = parseurl(url, default_branches) | 562 clone_path, branch = parseurl(url, default_branches) |
551 return url, clone_path, branch | 563 return url, clone_path, branch |
552 | 564 |
553 | 565 |
605 | 617 |
606 # Most likely empty string. | 618 # Most likely empty string. |
607 # This may need to raise in the future. | 619 # This may need to raise in the future. |
608 if not name: | 620 if not name: |
609 return None | 621 return None |
610 | 622 if name in self: |
611 try: | |
612 return self[name] | 623 return self[name] |
613 except KeyError: | 624 else: |
614 # Try to resolve as a local path or URI. | 625 # Try to resolve as a local path or URI. |
615 try: | 626 path = try_path(ui, name) |
616 # we pass the ui instance are warning might need to be issued | 627 if path is None: |
617 return path(ui, None, rawloc=name) | |
618 except ValueError: | |
619 raise error.RepoError(_(b'repository %s does not exist') % name) | 628 raise error.RepoError(_(b'repository %s does not exist') % name) |
629 return path.rawloc | |
620 | 630 |
621 | 631 |
622 _pathsuboptions = {} | 632 _pathsuboptions = {} |
623 | 633 |
624 | 634 |