Patchwork templater: add relpath() to convert repo path to relative path (issue5394)

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 8, 2016, 5:05 p.m.
Message ID <83e6528647265043ed30.1475946316@mimosa>
Download mbox | patch
Permalink /patch/16953/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 8, 2016, 5:05 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1475933066 -7200
#      Sat Oct 08 15:24:26 2016 +0200
# Node ID 83e6528647265043ed30be784e0db8fd59133230
# Parent  2def3d55b1b9ec2acd53f96ca755d778b5ec865b
templater: add relpath() to convert repo path to relative path (issue5394)

File paths in template are repository-absolute paths. This function can be
used to convert them to filesystem paths relative to cwd. This also converts
'/' to '\\' on Windows.
Jun Wu - Oct. 8, 2016, 9:47 p.m.
This looks good to me.

Excerpts from Yuya Nishihara's message of 2016-10-08 19:05:16 +0200:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1475933066 -7200
> #      Sat Oct 08 15:24:26 2016 +0200
> # Node ID 83e6528647265043ed30be784e0db8fd59133230
> # Parent  2def3d55b1b9ec2acd53f96ca755d778b5ec865b
> templater: add relpath() to convert repo path to relative path (issue5394)
> 
> File paths in template are repository-absolute paths. This function can be
> used to convert them to filesystem paths relative to cwd. This also converts
> '/' to '\\' on Windows.
> 
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -713,6 +713,18 @@ def localdate(context, mapping, args):
>          tzoffset = util.makedate()[1]
>      return (date[0], tzoffset)
>  
> +@templatefunc('relpath(path)')
> +def relpath(context, mapping, args):
> +    """Convert a repository-absolute path into a filesystem path relative to
> +    the current working directory."""
> +    if len(args) != 1:
> +        # i18n: "relpath" is a keyword
> +        raise error.ParseError(_("relpath expects one argument"))
> +
> +    repo = mapping['ctx'].repo()
> +    path = evalstring(context, mapping, args[0])
> +    return repo.pathto(path)
> +
>  @templatefunc('revset(query[, formatargs...])')
>  def revset(context, mapping, args):
>      """Execute a revision set query. See
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -3521,6 +3521,15 @@ Test files function
>    0
>    
>  
> +Test relpath function
> +
> +  $ hg log -r0 -T '{files % "{file|relpath}\n"}'
> +  a
> +  $ cd ..
> +  $ hg log -R r -r0 -T '{files % "{file|relpath}\n"}'
> +  r/a (glob)
> +  $ cd r
> +
>  Test active bookmark templating
>  
>    $ hg book foo
Pierre-Yves David - Oct. 8, 2016, 9:56 p.m.
On 10/08/2016 11:47 PM, Jun Wu wrote:
> This looks good to me.
>
> Excerpts from Yuya Nishihara's message of 2016-10-08 19:05:16 +0200:
>> # HG changeset patch
>> # User Yuya Nishihara <yuya@tcha.org>
>> # Date 1475933066 -7200
>> #      Sat Oct 08 15:24:26 2016 +0200
>> # Node ID 83e6528647265043ed30be784e0db8fd59133230
>> # Parent  2def3d55b1b9ec2acd53f96ca755d778b5ec865b
>> templater: add relpath() to convert repo path to relative path (issue5394)

Pushed, thanks.
(And Thanks Jun for the pre-review)

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -713,6 +713,18 @@  def localdate(context, mapping, args):
         tzoffset = util.makedate()[1]
     return (date[0], tzoffset)
 
+@templatefunc('relpath(path)')
+def relpath(context, mapping, args):
+    """Convert a repository-absolute path into a filesystem path relative to
+    the current working directory."""
+    if len(args) != 1:
+        # i18n: "relpath" is a keyword
+        raise error.ParseError(_("relpath expects one argument"))
+
+    repo = mapping['ctx'].repo()
+    path = evalstring(context, mapping, args[0])
+    return repo.pathto(path)
+
 @templatefunc('revset(query[, formatargs...])')
 def revset(context, mapping, args):
     """Execute a revision set query. See
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -3521,6 +3521,15 @@  Test files function
   0
   
 
+Test relpath function
+
+  $ hg log -r0 -T '{files % "{file|relpath}\n"}'
+  a
+  $ cd ..
+  $ hg log -R r -r0 -T '{files % "{file|relpath}\n"}'
+  r/a (glob)
+  $ cd r
+
 Test active bookmark templating
 
   $ hg book foo