Patchwork [4,of,6] templatefilters: add dirname() filter

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 15, 2018, 1:18 p.m.
Message ID <dd83fddddb7ec9991355.1518700722@mimosa>
Download mbox | patch
Permalink /patch/27971/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 15, 2018, 1:18 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1515294573 -32400
#      Sun Jan 07 12:09:33 2018 +0900
# Node ID dd83fddddb7ec9991355495a72c15b718561780b
# Parent  5a8caa27847f9b8f040589ed182522904f8a9574
templatefilters: add dirname() filter

This is different from stripdir() in that the last component is always
removed.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -100,6 +100,13 @@  def count(i):
     """List or text. Returns the length as an integer."""
     return len(i)
 
+@templatefilter('dirname')
+def dirname(path):
+    """Any text. Treats the text as a path, and strips the last
+    component of the path after splitting by the path separator.
+    """
+    return os.path.dirname(path)
+
 @templatefilter('domain')
 def domain(author):
     """Any text. Finds the first string that looks like an email
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
@@ -2232,6 +2232,10 @@  Filename filters:
 
   $ hg debugtemplate '{"foo/bar"|basename}|{"foo/"|basename}|{"foo"|basename}|\n'
   bar||foo|
+  $ hg debugtemplate '{"foo/bar"|dirname}|{"foo/"|dirname}|{"foo"|dirname}|\n'
+  foo|foo||
+  $ hg debugtemplate '{"foo/bar"|stripdir}|{"foo/"|stripdir}|{"foo"|stripdir}|\n'
+  foo|foo|foo|
 
 Add a dummy commit to make up for the instability of the above: