[PATCH hglib] client: add pathto helper (issue4510)
Axel Hecht
axel at mozilla.com
Wed Apr 6 13:51:14 UTC 2016
# HG changeset patch
# User Axel Hecht <axel at pike.org>
# Date 1432910925 -7200
# Fri May 29 16:48:45 2015 +0200
# Node ID 0850d9353245231ad335d89b9753b42cca933e33
# Parent 98efa416f763d5f408244baeaf6ab5e2fc4fd756
client: add pathto helper (issue4510)
Reimplement what localrepo.pathto does via util.pathto for hglib.
diff --git a/hglib/client.py b/hglib/client.py
--- a/hglib/client.py
+++ b/hglib/client.py
@@ -1164,6 +1164,18 @@
return self._parserevs(out)
+ def pathto(self, f, cwd=b('.')):
+ """
+ Return relative path to f. If cwd is given, use it as current
+ working directory.
+ The returned path uses os.sep as separator.
+
+ f - file path with / as separator
+ cwd - working directory with os.sep as separator
+ """
+ return os.path.relpath(os.path.join(self.root(), *(f.split(b('/')))),
+ start=cwd)
+
def paths(self, name=None):
"""
Return the definition of given symbolic path name. If no name is given,
diff --git a/tests/test-pathto.py b/tests/test-pathto.py
new file mode 100644
--- /dev/null
+++ b/tests/test-pathto.py
@@ -0,0 +1,26 @@
+from tests import common
+from hglib.util import b
+import os.path
+
+class test_pathto(common.basetest):
+ def test_inside(self):
+ self.assertEquals(self.client.pathto(b('foo/bar')),
+ b(os.path.join('foo', 'bar')))
+ self.assertEquals(self.client.pathto(b('foo/bar'), b('foo')),
+ b(os.path.join('bar')))
+
+ def test_parent(self):
+ self.assertEquals(self.client.pathto(b('foo/bar'), b(os.path.pardir)),
+ b(os.path.join('test_pathto', 'foo', 'bar')))
+ self.assertEquals(self.client.pathto(b('foo/bar'),
+ b(os.path.abspath(os.path.pardir))),
+ b(os.path.join('test_pathto', 'foo', 'bar')))
+
+ def test_sibling(self):
+ sibling = b(os.path.join(os.path.abspath(os.path.pardir), 'sibling'))
+ reference = b(os.path.join(os.path.pardir, 'test_pathto', 'foo', 'bar'))
+ self.assertEquals(self.client.pathto(b('foo/bar'), sibling),
+ reference)
+ self.assertEquals(self.client.pathto(b('foo/bar'),
+ os.path.abspath(sibling)),
+ reference)
More information about the Mercurial-devel
mailing list