[PATCH V2] log: add an extension hook-point in changeset_printer

Boris Feld boris.feld at octobus.net
Thu Jun 29 17:40:16 UTC 2017


# HG changeset patch
# User Boris Feld <boris.feld at octobus.net>
# Date 1498484784 -7200
#      Mon Jun 26 15:46:24 2017 +0200
# Node ID 3a063ab1f1baf9a74bad7c163293dd2611025195
# Parent  176030f695cade8520c29d3a1df339ddf00d984b
# EXP-Topic changesetprinterhook
log: add an extension hook-point in changeset_printer

Extensions sometimes wants to add other information in the default log output
format (when no templating is used).

Add an empty function named '_exthook' for easing the extension life.
Extensions will be able to wrap this function and collaborate to display
additional information.

Exthook is called after displaying troubles and just before displaying the
files, extra and description.

Add a new test file to test it and not pollute other test files.

diff -r 176030f695ca -r 3a063ab1f1ba mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Wed Jun 28 10:50:37 2017 -0700
+++ b/mercurial/cmdutil.py	Mon Jun 26 15:46:24 2017 +0200
@@ -1411,6 +1411,8 @@
             self.ui.write(_("trouble:     %s\n") % ', '.join(ctx.troubles()),
                           label='log.trouble')
 
+        self._exthook(ctx)
+
         if self.ui.debugflag:
             files = ctx.p1().status(ctx)[:3]
             for key, value in zip([# i18n: column positioning for "hg log"
@@ -1457,6 +1459,11 @@
 
         self.showpatch(ctx, matchfn)
 
+    def _exthook(self, ctx):
+        '''empty method used by extension as a hook point
+        '''
+        pass
+
     def showpatch(self, ctx, matchfn):
         if not matchfn:
             matchfn = self.matchfn
diff -r 176030f695ca -r 3a063ab1f1ba tests/test-log-exthook.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-log-exthook.t	Mon Jun 26 15:46:24 2017 +0200
@@ -0,0 +1,56 @@
+Test hg log changeset printer external hook
+-------------------------------------------
+
+  $ cat > $TESTTMP/logexthook.py <<EOF
+  > from mercurial import repair, commands
+  > from mercurial import cmdutil
+  > def rot13description(self, ctx):     
+  >     summary = "summary".encode('rot13')
+  >     description = ctx.description().strip().splitlines()[0].encode('rot13')
+  >     self.ui.write("%s:     %s\n" % (summary, description))
+  > def reposetup(ui, repo):
+  >     cmdutil.changeset_printer._exthook = rot13description
+  > EOF
+
+Prepare the repository
+
+  $ hg init empty
+  $ cd empty
+  $ touch ROOT
+  $ hg commit -A -m "Root" ROOT
+
+  $ touch a b c
+  $ hg commit -A -m "Add A, B, C" a b c
+
+Check the log
+
+  $ hg log --config extensions.t=$TESTTMP/logexthook.py
+  changeset:   1:70fc82b23320
+  tag:         tip
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  fhzznel:     Nqq N, O, P
+  summary:     Add A, B, C
+  
+  changeset:   0:b00443a54871
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  fhzznel:     Ebbg
+  summary:     Root
+  
+Check that exthook is working with graph log too
+
+  $ hg log -G --config extensions.t=$TESTTMP/logexthook.py
+  @  changeset:   1:70fc82b23320
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  fhzznel:     Nqq N, O, P
+  |  summary:     Add A, B, C
+  |
+  o  changeset:   0:b00443a54871
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     fhzznel:     Ebbg
+     summary:     Root
+  


More information about the Mercurial-devel mailing list