From patchwork Sun Mar 1 21:50:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [6, of, 9, paths, v2] ui.paths: teach getpath() about required resolution From: Gregory Szorc X-Patchwork-Id: 7867 Message-Id: To: mercurial-devel@selenic.com Date: Sun, 01 Mar 2015 13:50:45 -0800 # HG changeset patch # User Gregory Szorc # Date 1423507525 28800 # Mon Feb 09 10:45:25 2015 -0800 # Node ID b8d39c692feb69e2560a49abb1b9dcc984b3e1c8 # Parent 0aaf05f23aae7fdc440412af2f9815daaa9c4123 ui.paths: teach getpath() about required resolution It is somewhat common for callers of ui.expandpath to manage their own flow control for cases where the specified location could not be resolved. Sometimes we get as far down as localrepository.__init__ before an exception is raised. Here, we teach paths.getpath() how to raise exceptions in cases where paths could not be resolved. Consumers of this argument will come in subsequent patches. The error messages are identical to messages that already exist. diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -960,9 +960,9 @@ class paths(object): if path.name == key: return path raise KeyError('path not known: %s' % key) - def getpath(self, name, default=None): + def getpath(self, name, default=None, require=False): """Return a ``path`` from a name or location. Arguments can be named paths (from the config) or locations. Locations are URIs or filesystem paths that are directories having ``.hg`` @@ -971,10 +971,11 @@ class paths(object): If ``default`` is True, we attempt to resolve the default path if ``name`` could not be resolved. If ``default`` is the string ``push``, we attempt to resolve the default push path. - Returns None if the specified path or the default path was not - found. + If a path is not found and ``require`` is not defined, we return + None. If ``require`` is defined, we raise an exception appropriate + for the failure type. """ if name: if util.hasscheme(name): return path(None, url=name) @@ -999,9 +1000,15 @@ class paths(object): return self['default'] except KeyError: pass - return None + if not require: + return None + + if default: + raise util.Abort(_('default repository not configured!'), + hint=_('see the "path" section in "hg help config"')) + raise RepoError(_('repository %s not found') % name) class path(object): """Represents an individual path and its configuration."""