Patchwork [6,of,6] templatekw: add {size} keyword as an example of fctx-based keyword

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 2, 2018, 7:43 a.m.
Message ID <fabfd409466f6320b99a.1535874183@mimosa>
Download mbox | patch
Permalink /patch/34251/
State New
Headers show

Comments

Yuya Nishihara - Sept. 2, 2018, 7:43 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1532867331 -32400
#      Sun Jul 29 21:28:51 2018 +0900
# Node ID fabfd409466f6320b99a38a1c3e9ca5fe6f22a6d
# Parent  699afc590069f20c593f3f45c0fdc7395d567a19
templatekw: add {size} keyword as an example of fctx-based keyword

I'll add {status}, and I think some lfs keywords can be migrated to this.
I'm not certain how many fctx-based keywords will be introduced into the
global space, but if there are a couple more, we'll probably need to sort
them out to the "File Keywords" section in the templater help. Until then,
fctx keywords are hidden as experimental.
Augie Fackler - Sept. 12, 2018, 9:37 p.m.
On Sun, Sep 02, 2018 at 04:43:03PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1532867331 -32400
> #      Sun Jul 29 21:28:51 2018 +0900
> # Node ID fabfd409466f6320b99a38a1c3e9ca5fe6f22a6d
> # Parent  699afc590069f20c593f3f45c0fdc7395d567a19
> templatekw: add {size} keyword as an example of fctx-based keyword

queued, thanks

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -582,6 +582,12 @@  def showreporoot(context, mapping):
     repo = context.resource(mapping, 'repo')
     return repo.root
 
+@templatekeyword('size', requires={'fctx'})
+def showsize(context, mapping):
+    """Integer. Size of the current file in bytes. (EXPERIMENTAL)"""
+    fctx = context.resource(mapping, 'fctx')
+    return fctx.size()
+
 @templatekeyword("successorssets", requires={'repo', 'ctx'})
 def showsuccessorssets(context, mapping):
     """Returns a string of sets of successors for a changectx. Format used
diff --git a/tests/test-template-keywords.t b/tests/test-template-keywords.t
--- a/tests/test-template-keywords.t
+++ b/tests/test-template-keywords.t
@@ -787,6 +787,15 @@  Test file copies dict:
   $ hg log -r8 -C -T '{file_copies_switch % "{path} <- {source}\n"}'
   fourth <- second
 
+Test file attributes:
+
+  $ hg log -l1 -T '{files % "{pad(size, 3, left=True)} {path}\n"}'
+      a
+    0 b
+    7 fifth
+      fourth
+   13 third
+
 Test index keyword:
 
   $ hg log -l 2 -T '{index + 10}{files % " {index}:{file}"}\n'