[PATCH 3 of 3 RFC] templater: add 'transplant_source' keyword

Dan Villiom Podlaski Christiansen danchr at gmail.com
Sun Sep 19 14:08:30 CDT 2010


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1283432861 -7200
# Node ID 5697656e9aaba1374d3e1c7bb79dfa15c7e2cb70
# Parent  2be04fa93c4d8804814df7143c4f9864f60eb6bf
templater: add 'transplant_source' keyword.
* * *
templater: add 'convert_source' keyword.

diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt
--- a/mercurial/help/templates.txt
+++ b/mercurial/help/templates.txt
@@ -66,6 +66,10 @@ keywords are usually available for templ
 
 :latesttagdistance: Integer. Longest path to the latest tag.
 
+:extra: Mapping. The extra metadata of the changeset. Use "." to get the
+    individual items. For instance, {extra.branch} gets the branch name,
+    but does not filter out default.
+
 The "date" keyword does not produce human-readable output. If you
 want to use a date in your output, you can use a filter to process
 it. Filters are functions which return a string based on the input
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -239,6 +239,44 @@ def showrev(repo, ctx, templ, **args):
 def showtags(**args):
     return showlist('tag', args['ctx'].tags(), **args)
 
+def showextra(repo, ctx, templ, **args):
+    def isbinary(s):
+        '''
+        Improved, UTF-8-aware heuristic for short strings.
+
+        The usual heuristic (does the string contain '\0'?) is insufficient for
+        short strings. Instead, we use the following:
+
+        - Is the string valid UTF-8?
+        - If so, does it contain any of the 32 ASCII control characters?
+
+        Combined, the two make false negatives reasonably unlikely.
+        '''
+        # ' ', or space, is the first printable character in ASCII
+        unprintable = set(unichr(c) for c in xrange(ord(' ')))
+
+        try:
+            return not unprintable.isdisjoint(s.decode('utf-8'))
+        except UnicodeDecodeError:
+            return True
+
+    def unbin(s):
+        '''
+        ASCIIfy binary node identifiers, such as those stored by transplant.
+
+        Ideally, we wouldn't to do this here. On the one hand, we'd prefer to
+        have users use a filter to get the hexadecimal revision identifier. On
+        the other hand, binary data is - by definition - not text and therefore
+        not printable. Requiring the user to always specify a filter is not
+        a good UI; if possible we should handle that internally.
+        '''
+        if len(s) == 20 and isbinary(s):
+            return hex(s)
+        else:
+            return s
+
+    return dict((k, unbin(v)) for k, v in ctx.extra().iteritems())
+
 # keywords are callables like:
 # fn(repo, ctx, templ, cache, revcache, **args)
 # with:
@@ -255,6 +293,7 @@ keywords = {
     'desc': showdescription,
     'diffstat': showdiffstat,
     'extras': showextras,
+    'extra': showextra,
     'file_adds': showfileadds,
     'file_copies': showfilecopies,
     'file_copies_switch': showfilecopiesswitch,
diff --git a/tests/test-convert-hg-startrev.t b/tests/test-convert-hg-startrev.t
--- a/tests/test-convert-hg-startrev.t
+++ b/tests/test-convert-hg-startrev.t
@@ -9,7 +9,8 @@
 
   $ glog()
   > {
-  >     hg -R "$1" glog --template '{rev} "{desc}" files: {files}\n'
+  >     hg -R "$1" glog --template \
+  >         '{rev} "{desc}" files: {files}\nsource: {extra.convert_revision}\n'
   > }
 
   $ hg init source
@@ -73,7 +74,7 @@ Convert from zero revision
   o |  1 "1: add c" files: c
   |/
   o  0 "0: add a b" files: a b
-  
+     source: 83b9ec4abfaf46e22378d01514190ddb925f7b62
 Convert from merge parent
 
   $ hg convert --config convert.hg.startrev=1 source conv1
@@ -94,7 +95,7 @@ Convert from merge parent
   o  1 "2: copy e from a, change b" files: b e
   |
   o  0 "1: add c" files: a b c
-  
+     source: d67172394796a5665dee7ed219111c749c4b5135
   $ cd conv1
 
 Check copy preservation
@@ -140,7 +141,7 @@ Convert from merge
   o  1 "5: change a" files: a
   |
   o  0 "4: merge 2 and 3, copy d from b" files: a b c d e
-  
+     source: 20648c59b51f5195515b182aed3d4a36271b750a
   $ cd conv4
   $ hg up -C
   5 files updated, 0 files merged, 0 files removed, 0 files unresolved
diff --git a/tests/test-transplant.t b/tests/test-transplant.t
--- a/tests/test-transplant.t
+++ b/tests/test-transplant.t
@@ -25,12 +25,12 @@
   $ hg ci -Amb3 -d '2 0'
   adding b3
 
-  $ hg log --template '{rev} {parents} {desc}\n'
-  4  b3
-  3  b2
-  2 0:17ab29e464c6  b1
-  1  r2
-  0  r1
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  4   b3
+  3   b2
+  2  0:17ab29e464c6  b1
+  1   r2
+  0   r1
 
   $ hg clone . ../rebase
   updating to branch default
@@ -49,15 +49,15 @@ rebase b onto r1
   722f4667af76 transplanted to 539f377d78df
   applying a53251cdf717
   a53251cdf717 transplanted to ffd6818a3975
-  $ hg log --template '{rev} {parents} {desc}\n'
-  7  b3
-  6  b2
-  5 1:d11e3596cc1a  b1
-  4  b3
-  3  b2
-  2 0:17ab29e464c6  b1
-  1  r2
-  0  r1
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  7 a53251cdf717679d1907b289f991534be05c997a  b3
+  6 722f4667af767100cb15b6a79324bf8abbfe1ef4  b2
+  5 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21 1:d11e3596cc1a  b1
+  4   b3
+  3   b2
+  2  0:17ab29e464c6  b1
+  1   r2
+  0   r1
 
   $ hg clone ../t ../prune
   updating to branch default
@@ -74,14 +74,14 @@ rebase b onto r1, skipping b2
   37a1297eb21b transplanted to e234d668f844
   applying a53251cdf717
   a53251cdf717 transplanted to 7275fda4d04f
-  $ hg log --template '{rev} {parents} {desc}\n'
-  6  b3
-  5 1:d11e3596cc1a  b1
-  4  b3
-  3  b2
-  2 0:17ab29e464c6  b1
-  1  r2
-  0  r1
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  6 a53251cdf717679d1907b289f991534be05c997a  b3
+  5 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21 1:d11e3596cc1a  b1
+  4   b3
+  3   b2
+  2  0:17ab29e464c6  b1
+  1   r2
+  0   r1
 
 
 remote transplant
@@ -101,13 +101,13 @@ remote transplant
   37a1297eb21b transplanted to c19cf0ccb069
   applying a53251cdf717
   a53251cdf717 transplanted to f7fe5bf98525
-  $ hg log --template '{rev} {parents} {desc}\n'
-  3  b3
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  3 a53251cdf717679d1907b289f991534be05c997a  b3
   (transplanted from a53251cdf717679d1907b289f991534be05c997a)
-  2  b1
+  2 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21  b1
   (transplanted from 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21)
-  1  r2
-  0  r1
+  1   r2
+  0   r1
 
 skip previous transplants
 
@@ -115,14 +115,14 @@ skip previous transplants
   searching for changes
   applying 722f4667af76
   722f4667af76 transplanted to 47156cd86c0b
-  $ hg log --template '{rev} {parents} {desc}\n'
-  4  b2
-  3  b3
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  4 722f4667af767100cb15b6a79324bf8abbfe1ef4  b2
+  3 a53251cdf717679d1907b289f991534be05c997a  b3
   (transplanted from a53251cdf717679d1907b289f991534be05c997a)
-  2  b1
+  2 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21  b1
   (transplanted from 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21)
-  1  r2
-  0  r1
+  1   r2
+  0   r1
 
 skip local changes transplanted to the source
 
@@ -162,10 +162,10 @@ remote transplant with pull
   added 1 changesets with 1 changes to 1 files
   applying a53251cdf717
   a53251cdf717 transplanted to 8d9279348abb
-  $ hg log --template '{rev} {parents} {desc}\n'
-  2  b3
-  1  b1
-  0  r1
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  2 a53251cdf717679d1907b289f991534be05c997a  b3
+  1   b1
+  0   r1
 
 transplant --continue
 
@@ -297,11 +297,11 @@ test filter
   filtering .*
   applying a53251cdf717
   a53251cdf717 transplanted to 14f8512272b5
-  $ hg log --template '{rev} {parents} {desc}\n'
-  3  b3
-  2  b2
-  1  b1
-  0  r2
+  $ hg log --template '{rev} {extra.transplant_source} {parents} {desc}\n'
+  3 a53251cdf717679d1907b289f991534be05c997a  b3
+  2 722f4667af767100cb15b6a79324bf8abbfe1ef4  b2
+  1 37a1297eb21b3ef5c5d2ffac22121a0988ed9f21  b1
+  0 17ab29e464c6ca53e329470efe2a9918ac617a6f  r2
   $ cd ..
 
 


More information about the Mercurial-devel mailing list