[PATCH 4 of 4] templatekw: add experimental {status} keyword

Yuya Nishihara yuya at tcha.org
Thu Sep 13 10:10:31 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1532869441 -32400
#      Sun Jul 29 22:04:01 2018 +0900
# Node ID c39d94dc731620467b4a4be531e739efd3ee2733
# Parent  8d875b086b8c80c7c8047b0700cd05e2520c1971
templatekw: add experimental {status} keyword

This is another example of fctx-based keywords. I think this is somewhat
useful in log templates.

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -301,6 +301,15 @@ def _getfilestatus(context, mapping, lis
         revcache['filestatusall'] = listall
     return revcache['filestatus']
 
+def _getfilestatusmap(context, mapping, listall=False):
+    revcache = context.resource(mapping, 'revcache')
+    if 'filestatusmap' not in revcache or revcache['filestatusall'] < listall:
+        stat = _getfilestatus(context, mapping, listall=listall)
+        revcache['filestatusmap'] = statmap = {}
+        for char, files in zip(pycompat.iterbytestr('MAR!?IC'), stat):
+            statmap.update((f, char) for f in files)
+    return revcache['filestatusmap']  # {path: statchar}
+
 def _showfilesbystat(context, mapping, name, index):
     stat = _getfilestatus(context, mapping)
     files = stat[index]
@@ -595,6 +604,19 @@ def showsize(context, mapping):
     fctx = context.resource(mapping, 'fctx')
     return fctx.size()
 
+# requires 'fctx' to denote {status} depends on (ctx, path) pair
+ at templatekeyword('status', requires={'ctx', 'fctx', 'revcache'})
+def showstatus(context, mapping):
+    """String. Status code of the current file. (EXPERIMENTAL)"""
+    path = templateutil.runsymbol(context, mapping, 'path')
+    path = templateutil.stringify(context, mapping, path)
+    if not path:
+        return
+    statmap = _getfilestatusmap(context, mapping)
+    if path not in statmap:
+        statmap = _getfilestatusmap(context, mapping, listall=True)
+    return statmap.get(path)
+
 @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
@@ -789,12 +789,19 @@ Test file copies dict:
 
 Test file attributes:
 
-  $ hg log -l1 -T '{files % "{pad(size, 3, left=True)} {path}\n"}'
-      a
-    0 b
-    7 fifth
-      fourth
-   13 third
+  $ hg log -l1 -T '{files % "{status} {pad(size, 3, left=True)} {path}\n"}'
+  R     a
+  A   0 b
+  A   7 fifth
+  R     fourth
+  M  13 third
+
+Test file status including clean ones:
+
+  $ hg log -r9 -T '{files("**") % "{status} {path}\n"}'
+  A a
+  C fourth
+  C third
 
 Test index keyword:
 


More information about the Mercurial-devel mailing list