[PATCH 5 of 9 paths v2] ui: move URL and path detection into getpath()

Gregory Szorc gregory.szorc at gmail.com
Sun Mar 1 15:50:44 CST 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1423427350 28800
#      Sun Feb 08 12:29:10 2015 -0800
# Node ID 0aaf05f23aae7fdc440412af2f9815daaa9c4123
# Parent  4d9ba0789bf42ad77c407c84b19a6486652730ed
ui: move URL and path detection into getpath()

ui.expandpath() has code for recognizing URLs or local filesystem
paths. Our goal is to use path instances in more locations. Since many
call sites pass in arguments that could be URLs or filesystem paths,
we move this code into paths.getpath() so that more callers can be
switched to using the new API directly instead of ui.expandpath().

The added check for whether ``name`` is defined is added because
future callers may pass in empty values (expandpath doesn't
appear to receive empty values today).

Upcoming patches will perform additional processing on the "local"
argument to path.__init__. For now, we don't distinguish between the
origin and shoehorn everything into "loc."

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -528,11 +528,8 @@ class ui(object):
         return user
 
     def expandpath(self, loc, default=None):
         """Return repository location relative to cwd or from [paths]"""
-        if util.hasscheme(loc) or os.path.isdir(os.path.join(loc, '.hg')):
-            return loc
-
         if loc == 'default-push':
             p = self.paths.getpath(loc, default=True)
         elif loc == 'default':
             p = self.paths.getpath(loc)
@@ -955,26 +952,37 @@ class paths(object):
             # per-path attributes.
             if '.' in name:
                 continue
 
-            yield path(name, loc=loc)
+            yield path(name, url=loc)
 
     def __getitem__(self, key):
         for path in self:
             if path.name == key:
                 return path
         raise KeyError('path not known: %s' % key)
 
     def getpath(self, name, default=None):
-        """Return a ``path`` for the specified name.
+        """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``
+        directories.
 
         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 name:
+            if util.hasscheme(name):
+                return path(None, url=name)
+
+            if os.path.isdir(os.path.join(name, '.hg')):
+                return path(name, local=name)
+
         try:
             return self[name]
         except KeyError:
             pass
@@ -996,10 +1004,14 @@ class paths(object):
 
 class path(object):
     """Represents an individual path and its configuration."""
 
-    def __init__(self, name, loc=None):
-        """Construct a path from its config options."""
+    def __init__(self, name, url=None, local=None):
+        """Construct a path from its config options.
+
+        The primary URL for the path is defined as either a URL via ``url``
+        (preferred) or from a local, relative filesystem path (``local``).
+        """
         self.name = name
-        # We will eventually parse loc into URLs. For now, we carry the
-        # existing API forward to make the transition simpler.
-        self.loc = loc
+        # We will eventually do intelligent things depending on the
+        # source type. Until then, store a single variable for simplicity.
+        self.loc = url or local


More information about the Mercurial-devel mailing list