comparison mercurial/ui.py @ 26189:663fbc336e22

ui: change default path fallback mechanism (issue4796) The previous paths API code always fell back to the default path. This was wrong because if a requested path doesn't exist, that should error. Only if no path was requested should we fall back to the default. As part of implementing the test case for issue 4796, it was discovered that the "repository does not exist" error message raised by localrepository.__init__ wasn't being seen because the paths API validates paths before localrepository.__init__ was being called. The exception and error message from localrepository.__init__ has been introduced to getpath(). This necessitated rewriting expandpath() both to catch the exception and to have proper default fallback. This code is more complicated than I'd like. But making all tests pass was a big chore. As more code moves to getpath(), there will likely be opportunities to improve things a bit.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 06 Sep 2015 11:28:48 -0700
parents cebf7e48365e
children 6c962145f523
comparison
equal deleted inserted replaced
26188:662ea52d5dca 26189:663fbc336e22
548 user = util.shortuser(user) 548 user = util.shortuser(user)
549 return user 549 return user
550 550
551 def expandpath(self, loc, default=None): 551 def expandpath(self, loc, default=None):
552 """Return repository location relative to cwd or from [paths]""" 552 """Return repository location relative to cwd or from [paths]"""
553 p = self.paths.getpath(loc, default=default) 553 try:
554 if p: 554 p = self.paths.getpath(loc)
555 return p.rawloc 555 if p:
556 return p.rawloc
557 except error.RepoError:
558 pass
559
560 if default:
561 try:
562 p = self.paths.getpath(default)
563 if p:
564 return p.rawloc
565 except error.RepoError:
566 pass
567
556 return loc 568 return loc
557 569
558 @util.propertycache 570 @util.propertycache
559 def paths(self): 571 def paths(self):
560 return paths(self) 572 return paths(self)
1012 """Return a ``path`` from a string, falling back to a default. 1024 """Return a ``path`` from a string, falling back to a default.
1013 1025
1014 ``name`` can be a named path or locations. Locations are filesystem 1026 ``name`` can be a named path or locations. Locations are filesystem
1015 paths or URIs. 1027 paths or URIs.
1016 1028
1017 Returns the first of ``name`` or ``default`` that is present, or None 1029 Returns None if ``name`` is not a registered path, a URI, or a local
1018 if neither is present. 1030 path to a repo.
1019 """ 1031 """
1032 # Only fall back to default if no path was requested.
1033 if name is None:
1034 if default:
1035 try:
1036 return self[default]
1037 except KeyError:
1038 return None
1039 else:
1040 return None
1041
1042 # Most likely empty string.
1043 # This may need to raise in the future.
1044 if not name:
1045 return None
1046
1020 try: 1047 try:
1021 return self[name] 1048 return self[name]
1022 except KeyError: 1049 except KeyError:
1023 # Try to resolve as a local path or URI. 1050 # Try to resolve as a local path or URI.
1024 try: 1051 try:
1025 return path(None, rawloc=name) 1052 return path(None, rawloc=name)
1026 except ValueError: 1053 except ValueError:
1027 pass 1054 raise error.RepoError(_('repository %s does not exist') %
1028 1055 name)
1029 if default is not None: 1056
1030 try: 1057 assert False
1031 return self[default]
1032 except KeyError:
1033 pass
1034
1035 return None
1036 1058
1037 class path(object): 1059 class path(object):
1038 """Represents an individual path and its configuration.""" 1060 """Represents an individual path and its configuration."""
1039 1061
1040 def __init__(self, name, rawloc=None, pushloc=None): 1062 def __init__(self, name, rawloc=None, pushloc=None):