Patchwork [4,of,9,paths,v2] ui: change how default is handled by getpath()

login
register
mail settings
Submitter Gregory Szorc
Date March 1, 2015, 9:50 p.m.
Message ID <4d9ba0789bf42ad77c40.1425246643@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/7865/
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 1423509114 28800
#      Mon Feb 09 11:11:54 2015 -0800
# Node ID 4d9ba0789bf42ad77c407c84b19a6486652730ed
# Parent  ab2f54a886bbe13db2854bdf750e32560e16c1e1
ui: change how default is handled by getpath()

Callers of ui.expandpath perform a pattern like
|ui.expandpath(dest or 'default-push', dest or 'default')|. The
intent of this pattern is somewhat difficult to read and requires
callers contain logic for path fallback.

Since we now have a proper paths API, we now have the opportunity
to move this logic from the caller into the new API so callers
aren't burdened with it.

This patch changes getpath() to perform default fallback internally,
rather than burden callers.

The new code is a bit more complicated than I would like, especially
the new implementation of ui.expandpath. However, this complication
will eventually absorb complication in callers of ui.expandpath and
will making calling code simpler. This will be addressed in subsequent
patches.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -531,9 +531,16 @@  class ui(object):
         """Return repository location relative to cwd or from [paths]"""
         if util.hasscheme(loc) or os.path.isdir(os.path.join(loc, '.hg')):
             return loc
 
-        p = self.paths.getpath(loc, default=default)
+        if loc == 'default-push':
+            p = self.paths.getpath(loc, default=True)
+        elif loc == 'default':
+            p = self.paths.getpath(loc)
+        else:
+            assert default != 'default-push'
+            p = self.paths.getpath(loc, default=default=='default')
+
         if p:
             return p.loc
         return loc
 
@@ -959,19 +966,32 @@  class paths(object):
 
     def getpath(self, name, default=None):
         """Return a ``path`` for the specified name.
 
+        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.
         """
         try:
             return self[name]
         except KeyError:
-            if default is not None:
-                try:
-                    return self[default]
-                except KeyError:
-                    pass
+            pass
+
+        if default == 'push':
+            try:
+                return self['default-push']
+            except KeyError:
+                # Fall through to "default"
+                pass
+
+        if default:
+            try:
+                return self['default']
+            except KeyError:
+                pass
 
         return None
 
 class path(object):