Patchwork [hglib] client: add pathto helper (issue4510)

login
register
mail settings
Submitter Axel Hecht
Date April 6, 2016, 1:51 p.m.
Message ID <0850d9353245231ad335.1459950674@fuchsia.local>
Download mbox | patch
Permalink /patch/14394/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Axel Hecht - April 6, 2016, 1:51 p.m.
# HG changeset patch
# User Axel Hecht <axel@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.
Yuya Nishihara - April 8, 2016, 2:15 p.m.
On Wed, 06 Apr 2016 15:51:14 +0200, Axel Hecht wrote:
> # HG changeset patch
> # User Axel Hecht <axel@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)

If this is intended to be a 100% copy of localrepo.pathto(), "cwd" should be
relative to root, not relative to cwd.

Other than that, this patch seems fine.

Patch

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)