urlutil: extract `chain_path` in a function
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 15 Apr 2021 17:15:43 +0200
changeset 47186 26b3953ba1b0
parent 47185 a2632ce1f15b
child 47187 7531cc34713c
urlutil: extract `chain_path` in a function This will no longer modify `path` inplace so it does not make much sense as a method. Differential Revision: https://phab.mercurial-scm.org/D10446
mercurial/utils/urlutil.py
--- a/mercurial/utils/urlutil.py	Thu Apr 15 17:12:25 2021 +0200
+++ b/mercurial/utils/urlutil.py	Thu Apr 15 17:15:43 2021 +0200
@@ -611,7 +611,7 @@
             self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts)
 
         for name, p in sorted(self.items()):
-            p.chain_path(ui, self)
+            self[name] = _chain_path(p, ui, self)
 
     def getpath(self, ui, name, default=None):
         """Return a ``path`` from a string, falling back to default.
@@ -704,6 +704,33 @@
     return value
 
 
+def _chain_path(path, ui, paths):
+    """return the result of "path://" logic applied on a given path"""
+    if path.url.scheme == b'path':
+        assert path.url.path is None
+        subpath = paths.get(path.url.host)
+        if subpath is None:
+            m = _(b'cannot use `%s`, "%s" is not a known path')
+            m %= (path.rawloc, path.url.host)
+            raise error.Abort(m)
+        if subpath.raw_url.scheme == b'path':
+            m = _(b'cannot use `%s`, "%s" is also defined as a `path://`')
+            m %= (path.rawloc, path.url.host)
+            raise error.Abort(m)
+        path.url = subpath.url
+        path.rawloc = subpath.rawloc
+        path.loc = subpath.loc
+        if path.branch is None:
+            path.branch = subpath.branch
+        else:
+            base = path.rawloc.rsplit(b'#', 1)[0]
+            path.rawloc = b'%s#%s' % (base, path.branch)
+        suboptions = subpath._all_sub_opts.copy()
+        suboptions.update(path._own_sub_opts)
+        path._apply_suboptions(ui, suboptions)
+    return path
+
+
 class path(object):
     """Represents an individual path and its configuration."""
 
@@ -756,31 +783,6 @@
 
         self._apply_suboptions(ui, sub_opts)
 
-    def chain_path(self, ui, paths):
-        if self.url.scheme == b'path':
-            assert self.url.path is None
-            try:
-                subpath = paths[self.url.host]
-            except KeyError:
-                m = _(b'cannot use `%s`, "%s" is not a known path')
-                m %= (self.rawloc, self.url.host)
-                raise error.Abort(m)
-            if subpath.raw_url.scheme == b'path':
-                m = _(b'cannot use `%s`, "%s" is also defined as a `path://`')
-                m %= (self.rawloc, self.url.host)
-                raise error.Abort(m)
-            self.url = subpath.url
-            self.rawloc = subpath.rawloc
-            self.loc = subpath.loc
-            if self.branch is None:
-                self.branch = subpath.branch
-            else:
-                base = self.rawloc.rsplit(b'#', 1)[0]
-                self.rawloc = b'%s#%s' % (base, self.branch)
-            suboptions = subpath._all_sub_opts.copy()
-            suboptions.update(self._own_sub_opts)
-            self._apply_suboptions(ui, suboptions)
-
     def copy(self):
         """make a copy of this path object"""
         new = self.__class__()