[PATCH 3 of 7] templater: add experimental support for extdata

Yuya Nishihara yuya at tcha.org
Sun Oct 1 08:00:14 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1506852789 -3600
#      Sun Oct 01 11:13:09 2017 +0100
# Node ID 2b81a9a4752373722a9a568c7031dcf53171cc6b
# Parent  2c9054ec242d781c0bda1d0d0ab0b26b5b93f012
templater: add experimental support for extdata

This is minimal and non-controversial implementation of extdata() template
function. Originally extdata sources were exposed to the keyword namespace,
but I've changed it to a plain function for simplicity.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -24,6 +24,7 @@ from . import (
     registrar,
     revset as revsetmod,
     revsetlang,
+    scmutil,
     templatefilters,
     templatekw,
     util,
@@ -629,6 +630,22 @@ def diff(context, mapping, args):
 
     return ''.join(chunks)
 
+ at templatefunc('extdata(source)', argspec='source')
+def extdata(context, mapping, args):
+    """Show a text read from the specified extdata source. (EXPERIMENTAL)"""
+    if 'source' not in args:
+        # i18n: "extdata" is a keyword
+        raise error.ParseError(_('extdata expects one argument'))
+
+    source = evalstring(context, mapping, args['source'])
+    cache = mapping['cache'].setdefault('extdata', {})
+    ctx = mapping['ctx']
+    if source in cache:
+        data = cache[source]
+    else:
+        data = cache[source] = scmutil.extdatasource(ctx.repo(), source)
+    return data.get(ctx.rev(), '')
+
 @templatefunc('files(pattern)')
 def files(context, mapping, args):
     """All files of the current changeset matching the pattern. See
diff --git a/tests/test-extdata.t b/tests/test-extdata.t
--- a/tests/test-extdata.t
+++ b/tests/test-extdata.t
@@ -10,12 +10,19 @@ test revset support
   $ cat <<'EOF' >> .hg/hgrc
   > [extdata]
   > filedata = file:extdata.txt
+  > notes = notes.txt
   > shelldata = shell:cat extdata.txt | grep 2
   > EOF
   $ cat <<'EOF' > extdata.txt
-  > 2
+  > 2 another comment on 2
   > 3
   > EOF
+  $ cat <<'EOF' > notes.txt
+  > f6ed this change is great!
+  > e834 this is buggy :(
+  > 0625 first post
+  > bogusnode gives no error
+  > EOF
 
   $ hg log -qr "extdata(filedata)"
   2:f6ed99a58333
@@ -43,6 +50,31 @@ test bad extdata() revset source
   abort: unknown extdata source 'unknown'
   [255]
 
+test template support:
+
+  $ hg log -r:3 -T "{node|short}{if(extdata('notes'), ' # {extdata('notes')}')}\n"
+  06254b906311 # first post
+  e8342c9a2ed1 # this is buggy :(
+  f6ed99a58333 # this change is great!
+  9de260b1e88e
+
+test template cache:
+
+  $ hg log -r:3 -T '{rev} "{extdata("notes")}" "{extdata("shelldata")}"\n'
+  0 "first post" ""
+  1 "this is buggy :(" ""
+  2 "this change is great!" "another comment on 2"
+  3 "" ""
+
+test bad extdata() template source
+
+  $ hg log -T "{extdata()}\n"
+  hg: parse error: extdata expects one argument
+  [255]
+  $ hg log -T "{extdata('unknown')}\n"
+  abort: unknown extdata source 'unknown'
+  [255]
+
 we don't fix up relative file URLs, but we do run shell commands in repo root
 
   $ mkdir sub


More information about the Mercurial-devel mailing list