[PATCH 9 of 9] template: use template-engine for obsfate

Boris Feld boris.feld at octobus.net
Thu Jul 6 17:50:17 EDT 2017


# HG changeset patch
# User Boris Feld <boris.feld at octobus.net>
# Date 1499085172 -7200
#      Mon Jul 03 14:32:52 2017 +0200
# Node ID e18d8e61b7260e246a82727c8cde01f936692cff
# Parent  098585d4fbc88dc54513e12fa306d0e52ea2b323
# EXP-Topic obsfatetemplate
template: use template-engine for obsfate

Try to replace the obsfateprinter part by "sub-templates".

The code is hacky, I've tried to use at maximum the template engine but the
raw data-structure doesn't seems well supported:

    [{'markers': [{}, {}, ...], 'successors': [A, ...], 'verb': '', ...}, ...]

I've put this changeset at the end so the beginning of the serie might be
accepted without it. Even without this changeset, we already have good tests
and the right structure for the computation of obsfate data.

diff -r 098585d4fbc8 -r e18d8e61b726 mercurial/templatekw.py
--- a/mercurial/templatekw.py	Mon Jul 03 17:38:56 2017 +0200
+++ b/mercurial/templatekw.py	Mon Jul 03 14:32:52 2017 +0200
@@ -699,6 +699,32 @@
 
     return "; ".join(lines)
 
+def obsfatedefaulttempl(ui):
+    """ Returns a dict with the default templates for obs fate
+    """
+    # Prepare templates
+    verbtempl = '{verb}'
+    usertempl = '{if(users, " by {join(users, ", ")}")}'
+    succtempl = '{if(successors, " as ")}{successors}' # Bypass if limitation
+    datetempleq = ' (at {min_date|isodate})'
+    datetemplnoteq = ' (between {min_date|isodate} and {max_date|isodate})'
+
+    datetempl = '{if(max_date, "{ifeq(min_date, max_date, "%s", "%s")}")}'
+    datetempl = datetempl % (datetempleq, datetemplnoteq)
+
+    optionalusertempl = usertempl
+    username = _getusername(ui)
+    if username is not None:
+        optionalusertempl = ('{ifeq(join(users, "\0"), "%s", "", "%s")}'
+                             % (username, usertempl))
+
+    # Assemble them
+    return {
+        'obsfate_quiet': verbtempl + succtempl,
+        'obsfate': verbtempl + optionalusertempl + succtempl,
+        'obsfate_verbose': verbtempl + usertempl + succtempl + datetempl,
+    }
+
 @templatekeyword("obsfate")
 def showobsfate(repo, ctx, **args):
     """Returns a string describing how an obsolete changeset has evolved in a
@@ -717,7 +743,38 @@
     if values is None:
         return ''
 
-    return _obsfateprinter(values, repo, repo.ui)
+    # Format each successorset successors list
+    for raw in values:
+        # As we can't do something like
+        # "{join(map(nodeshort, successors), ', '}" in template, manually
+        # create a correct textual representation
+        gen = ', '.join(_formatrevnode(repo[n]) for n in raw['successors'])
+
+        makemap = lambda x: {'successor': x}
+        joinfmt = lambda d: "%s" % d['successor']
+        raw['successors'] = _hybrid(gen, raw['successors'], makemap,
+                                    joinfmt)
+
+    # And then format them
+    # Insert default obsfate templates
+    args['templ'].cache.update(obsfatedefaulttempl(repo.ui))
+
+    if repo.ui.quiet:
+        name = "obsfate_quiet"
+    elif repo.ui.verbose:
+        name = "obsfate_verbose"
+    elif repo.ui.debugflag:
+        name = "obsfate_debug"
+    else:
+        name = "obsfate"
+
+    # Format a single value using template
+    def fmt(d):
+        nargs = args.copy()
+        nargs.update(d[name])
+        return args['templ'](name, **nargs)
+
+    return _hybrid(None, values, lambda x: {name: x}, fmt)
 
 @templatekeyword('p1rev')
 def showp1rev(repo, ctx, templ, **args):
diff -r 098585d4fbc8 -r e18d8e61b726 tests/test-obsmarker-template.t
--- a/tests/test-obsmarker-template.t	Mon Jul 03 17:38:56 2017 +0200
+++ b/tests/test-obsmarker-template.t	Mon Jul 03 14:32:52 2017 +0200
@@ -20,7 +20,8 @@
   >     {if(successorssets, "\n  Successors: {successorssets}")}\
   >     {if(successorssets, "\n  multi-line: {join(successorssets, "\n  multi-line: ")}")}\
   >     {if(successorssets, "\n  json: {successorssets|json}")}\n'
-  > fatelog = log -G -T '{node|short}\n{if(obsfate, "  Obsfate: {obsfate}\n")}'
+  > fatelog = log -G -T '{node|short}\n{if(obsfate, "  Obsfate: {join(obsfate, "; ")}\n")}'
+  > fatelogjson = log -G -T '{node|short} {obsfate|json}\n'
   > EOF
 
 Test templates on amended commit
@@ -212,6 +213,19 @@
   |/     Obsfate: rewritten by test1 as 3:a468dc9b3633 (at 2009-02-13 23:31 +0000)
   o  ea207398892e
   
+
+  $ hg fatelogjson --hidden
+  @  d004c8f274b9 ""
+  |
+  | x  a468dc9b3633 [{"markers": [["a468dc9b36338b14fdb7825f55ce3df4e71517ad", ["d004c8f274b9ec480a47a93c10dac5eee63adb78"], 0, [["user", "test2"]], [987654321.0, 0], null]], "max_date": [987654321.0, 0], "min_date": [987654321.0, 0], "successors": ["d004c8f274b9ec480a47a93c10dac5eee63adb78"], "users": ["test2"], "verb": "rewritten"}]
+  |/
+  | x  f137d23bb3e1 [{"markers": [["f137d23bb3e11dc1daeb6264fac9cb2433782e15", [], 0, [["user", "test1"]], [1234567890.0, 0], ["471f378eab4c5e25f6c77f785b27c936efb22874"]]], "max_date": [1234567890.0, 0], "min_date": [1234567890.0, 0], "successors": [], "users": ["test1"], "verb": "pruned"}]
+  | |
+  | x  471f378eab4c [{"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["a468dc9b36338b14fdb7825f55ce3df4e71517ad"], 0, [["user", "test1"]], [1234567890.0, 0], null]], "max_date": [1234567890.0, 0], "min_date": [1234567890.0, 0], "successors": ["a468dc9b36338b14fdb7825f55ce3df4e71517ad"], "users": ["test1"], "verb": "rewritten"}]
+  |/
+  o  ea207398892e ""
+  
+
 Test templates with splitted commit
 ===================================
 
@@ -345,6 +359,15 @@
   |/     Obsfate: split as 2:337fec4d2edc, 3:f257fde29c7a
   o  ea207398892e
   
+  $ hg fatelogjson --hidden
+  @  f257fde29c7a ""
+  |
+  o  337fec4d2edc ""
+  |
+  | x  471597cad322 [{"markers": [["471597cad322d1f659bb169751be9133dad92ef3", ["337fec4d2edcf0e7a467e35f818234bc620068b5", "f257fde29c7a847c9b607f6e958656d0df0fb15c"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["337fec4d2edcf0e7a467e35f818234bc620068b5", "f257fde29c7a847c9b607f6e958656d0df0fb15c"], "users": ["test"], "verb": "split"}]
+  |/
+  o  ea207398892e ""
+  
 Test templates with folded commit
 =================================
 
@@ -504,6 +527,17 @@
   |/     Obsfate: rewritten as 3:eb5a0daa2192
   o  ea207398892e
   
+
+  $ hg fatelogjson --hidden
+  @  eb5a0daa2192 ""
+  |
+  | x  0dec01379d3b [{"markers": [["0dec01379d3be6318c470ead31b1fe7ae7cb53d5", ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], "users": ["test"], "verb": "rewritten"}]
+  | |
+  | x  471f378eab4c [{"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  o  ea207398892e ""
+  
+
 Test templates with divergence
 ==============================
 
@@ -690,6 +724,19 @@
   |/     Obsfate: rewritten as 2:fdf9bde5129a; rewritten as 3:65b757b745b9
   o  ea207398892e
   
+
+  $ hg fatelogjson --hidden
+  o  019fadeab383 ""
+  |
+  | x  65b757b745b9 [{"markers": [["65b757b745b935093c87a2bccd877521cccffcbd", ["019fadeab383f6699fa83ad7bdb4d82ed2c0e5ab"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["019fadeab383f6699fa83ad7bdb4d82ed2c0e5ab"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  | @  fdf9bde5129a ""
+  |/
+  | x  471f378eab4c [{"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["fdf9bde5129a28d4548fadd3f62b265cdd3b7a2e"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["fdf9bde5129a28d4548fadd3f62b265cdd3b7a2e"], "users": ["test"], "verb": "rewritten"}, {"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["65b757b745b935093c87a2bccd877521cccffcbd"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["65b757b745b935093c87a2bccd877521cccffcbd"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  o  ea207398892e ""
+  
+
 Test templates with amended + folded commit
 ===========================================
 
@@ -906,6 +953,19 @@
   |/     Obsfate: rewritten as 4:eb5a0daa2192
   o  ea207398892e
   
+
+  $ hg fatelogjson --hidden
+  @  eb5a0daa2192 ""
+  |
+  | x  b7ea6d14e664 [{"markers": [["b7ea6d14e664bdc8922221f7992631b50da3fb07", ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], "users": ["test"], "verb": "rewritten"}]
+  | |
+  | | x  0dec01379d3b [{"markers": [["0dec01379d3be6318c470ead31b1fe7ae7cb53d5", ["b7ea6d14e664bdc8922221f7992631b50da3fb07"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["b7ea6d14e664bdc8922221f7992631b50da3fb07"], "users": ["test"], "verb": "rewritten"}]
+  | |/
+  | x  471f378eab4c [{"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["eb5a0daa21923bbf8caeb2c42085b9e463861fd0"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  o  ea207398892e ""
+  
+
 Test template with pushed and pulled obs markers
 ================================================
 
@@ -1467,6 +1527,27 @@
   |/     Obsfate: rewritten as 2:0dec01379d3b
   o  ea207398892e
   
+  $ hg fatelogjson --hidden
+  @  0b997eb7ceee ""
+  |
+  | o  b18bc8331526 ""
+  |/
+  | o  ba2ed02b0c9a ""
+  | |
+  | x  4a004186e638 [{"markers": [["4a004186e63889f20cb16434fcbd72220bd1eace", ["b18bc8331526a22cbb1801022bd1555bf291c48b"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["b18bc8331526a22cbb1801022bd1555bf291c48b"], "users": ["test"], "verb": "rewritten"}, {"markers": [["4a004186e63889f20cb16434fcbd72220bd1eace", ["0b997eb7ceeee06200a02f8aab185979092d514e"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["0b997eb7ceeee06200a02f8aab185979092d514e"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  o  dd800401bd8c ""
+  |
+  | x  9bd10a0775e4 [{"markers": [["9bd10a0775e478708cada5f176ec6de654359ce7", ["dd800401bd8c79d815329277739e433e883f784e", "4a004186e63889f20cb16434fcbd72220bd1eace", "ba2ed02b0c9a56b9fdbc4e79c7e57866984d8a1f"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["4a004186e63889f20cb16434fcbd72220bd1eace", "ba2ed02b0c9a56b9fdbc4e79c7e57866984d8a1f", "dd800401bd8c79d815329277739e433e883f784e"], "users": ["test"], "verb": "split"}]
+  |/
+  o  f897c6137566 ""
+  |
+  | x  0dec01379d3b [{"markers": [["0dec01379d3be6318c470ead31b1fe7ae7cb53d5", ["f897c6137566320b081514b4c7227ecc3d384b39"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["f897c6137566320b081514b4c7227ecc3d384b39"], "users": ["test"], "verb": "rewritten"}, {"markers": [["0dec01379d3be6318c470ead31b1fe7ae7cb53d5", ["471f378eab4c5e25f6c77f785b27c936efb22874"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["471f378eab4c5e25f6c77f785b27c936efb22874"], "users": ["test"], "verb": "rewritten"}]
+  | |
+  | x  471f378eab4c [{"markers": [["471f378eab4c5e25f6c77f785b27c936efb22874", ["0dec01379d3be6318c470ead31b1fe7ae7cb53d5"], 0, [["user", "test"]], [0.0, 0], null]], "max_date": [0.0, 0], "min_date": [0.0, 0], "successors": ["0dec01379d3be6318c470ead31b1fe7ae7cb53d5"], "users": ["test"], "verb": "rewritten"}]
+  |/
+  o  ea207398892e ""
+  
   $ hg up --hidden 4
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg rebase -r 7 -d 8 --config extensions.rebase=


More information about the Mercurial-devel mailing list