changeset 47027:c029b35565dd

url_util: introduce a `try_path` function That function… try a build a path, returning None on failure. This helps us to simplify various part of the existing code. Differential Revision: https://phab.mercurial-scm.org/D10437
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 15 Apr 2021 09:50:56 +0200
parents 20eba5cef2e0
children d69a9628eba7
files mercurial/utils/urlutil.py
diffstat 1 files changed, 29 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/urlutil.py	Tue Apr 20 11:22:35 2021 -0700
+++ b/mercurial/utils/urlutil.py	Thu Apr 15 09:50:56 2021 +0200
@@ -445,6 +445,18 @@
     return bytes(u)
 
 
+def try_path(ui, url):
+    """try to build a path from a url
+
+    Return None if no Path could built.
+    """
+    try:
+        # we pass the ui instance are warning might need to be issued
+        return path(ui, None, rawloc=url)
+    except ValueError:
+        return None
+
+
 def get_push_paths(repo, ui, dests):
     """yields all the `path` selected as push destination by `dests`"""
     if not dests:
@@ -471,10 +483,10 @@
             url = ui.paths[source].rawloc
         else:
             # Try to resolve as a local path or URI.
-            try:
-                # we pass the ui instance are warning might need to be issued
-                url = path(ui, None, rawloc=source).rawloc
-            except ValueError:
+            path = try_path(ui, source)
+            if path is not None:
+                url = path.rawloc
+            else:
                 url = source
         yield parseurl(url, default_branches)
 
@@ -520,10 +532,10 @@
             url = ui.paths[source].rawloc
         else:
             # Try to resolve as a local path or URI.
-            try:
-                # we pass the ui instance are warning might need to be issued
-                url = path(ui, None, rawloc=source).rawloc
-            except ValueError:
+            path = try_path(ui, source)
+            if path is not None:
+                url = path.rawloc
+            else:
                 url = source
     return parseurl(url, default_branches)
 
@@ -542,10 +554,10 @@
             url = ui.paths[source].rawloc
         else:
             # Try to resolve as a local path or URI.
-            try:
-                # we pass the ui instance are warning might need to be issued
-                url = path(ui, None, rawloc=source).rawloc
-            except ValueError:
+            path = try_path(ui, source)
+            if path is not None:
+                url = path.rawloc
+            else:
                 url = source
     clone_path, branch = parseurl(url, default_branches)
     return url, clone_path, branch
@@ -607,16 +619,14 @@
         # This may need to raise in the future.
         if not name:
             return None
-
-        try:
+        if name in self:
             return self[name]
-        except KeyError:
+        else:
             # Try to resolve as a local path or URI.
-            try:
-                # we pass the ui instance are warning might need to be issued
-                return path(ui, None, rawloc=name)
-            except ValueError:
+            path = try_path(ui, name)
+            if path is None:
                 raise error.RepoError(_(b'repository %s does not exist') % name)
+            return path.rawloc
 
 
 _pathsuboptions = {}