Patchwork [6,of,9,paths,v2] ui.paths: teach getpath() about required resolution

login
register
mail settings
Submitter Gregory Szorc
Date March 1, 2015, 9:50 p.m.
Message ID <b8d39c692feb69e2560a.1425246645@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/7867/
State Changes Requested
Headers show

Comments

Gregory Szorc - March 1, 2015, 9:50 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# 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.
Yuya Nishihara - March 2, 2015, 1:41 p.m.
On Sun, 01 Mar 2015 13:50:45 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # 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

> @@ -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)

Just a nitpick: it should be error.RepoError, though it seems a bit strange
to me that ui raises RepoError.

Regards,

Patch

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."""