subrepo: introduce a `repo_rel_or_abs_source` function
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 15 Apr 2021 09:23:19 +0200
changeset 46929 5a59a0ed0a37
parent 46928 93c224dc099b
child 46930 0afe96e374a7
subrepo: introduce a `repo_rel_or_abs_source` function The `subrepoutil` module has various function to compute the path of a sub-repository compared to the root of the top repository. However, they was no function available to compute the relative path of the repository "source". And we need this information for exchange operation (in our case, `hg outgoing`). The information is currently build using the `repo._subtoppath` hack. We reuse the same logic but in a dedicated function independent of the `repo._subtoppath` hack. Differential Revision: https://phab.mercurial-scm.org/D10434
mercurial/subrepoutil.py
--- a/mercurial/subrepoutil.py	Wed Apr 14 22:50:41 2021 -0400
+++ b/mercurial/subrepoutil.py	Thu Apr 15 09:23:19 2021 +0200
@@ -383,6 +383,24 @@
     return subs, commitsubs, newstate
 
 
+def repo_rel_or_abs_source(repo):
+    """return the source of this repo
+
+    Either absolute or relative the outermost repo"""
+    parent = repo
+    chunks = []
+    while util.safehasattr(parent, b'_subparent'):
+        source = urlutil.url(parent._subsource)
+        chunks.append(bytes(source))
+        if source.isabs():
+            break
+        parent = parent._subparent
+
+    chunks.reverse()
+    path = posixpath.join(*chunks)
+    return posixpath.normpath(path)
+
+
 def reporelpath(repo):
     # type: (localrepo.localrepository) -> bytes
     """return path to this (sub)repo as seen from outermost repo"""