Patchwork D10437: url_util: introduce a `try_path` function

login
register
mail settings
Submitter phabricator
Date April 15, 2021, 8:52 a.m.
Message ID <differential-rev-PHID-DREV-cg6xn5uqpupdr7nafrj2-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48752/
State Superseded
Headers show

Comments

phabricator - April 15, 2021, 8:52 a.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  That function… try a build a path, returning None on failure. This helps us to simplify various part of the existing code.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10437

AFFECTED FILES
  mercurial/utils/urlutil.py

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/utils/urlutil.py b/mercurial/utils/urlutil.py
--- a/mercurial/utils/urlutil.py
+++ b/mercurial/utils/urlutil.py
@@ -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 = {}