changeset 21924:5375ba75df40

cmdutil: make commit message shown in text editor customizable by template This patch makes commit message shown in text editor customizable by template. For example, this can advertise: - sample commit messages for routine works, - points to call attention before commit, - message of the day, and so on To show commit message correctly even in problematic encoding, this patch chooses the latter below: - replace "buildcommittext" with "buildcommittemplate" completely - invoke "buildcommittemplate" only if '[committemplate] changeset' is configured explicitly For example, if multibyte character ending with backslash (0x5c) is followed by ASCII character 'n' in the customized template, sequence of backslash and 'n' is treated as line-feed unexpectedly (and multibyte character is broken, too). This corruption occurs in 'decode("string-escape")' while parsing template string.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Tue, 15 Jul 2014 23:34:13 +0900
parents e582e20cd3e6
children 7142e04b438e
files mercurial/cmdutil.py mercurial/help/config.txt tests/test-commit.t tests/test-mq-qrefresh-replace-log-message.t
diffstat 4 files changed, 146 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Fri Jul 18 23:15:28 2014 -0500
+++ b/mercurial/cmdutil.py	Tue Jul 15 23:34:13 2014 +0900
@@ -2173,7 +2173,11 @@
 def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None):
     if not extramsg:
         extramsg = _("Leave message empty to abort commit.")
-    committext = buildcommittext(repo, ctx, subs, extramsg)
+    tmpl = repo.ui.config('committemplate', 'changeset', '').strip()
+    if tmpl:
+        committext = buildcommittemplate(repo, ctx, subs, extramsg, tmpl)
+    else:
+        committext = buildcommittext(repo, ctx, subs, extramsg)
 
     # run editor in the repository root
     olddir = os.getcwd()
@@ -2189,6 +2193,22 @@
 
     return text
 
+def buildcommittemplate(repo, ctx, subs, extramsg, tmpl):
+    ui = repo.ui
+    tmpl, mapfile = gettemplate(ui, tmpl, None)
+
+    try:
+        t = changeset_templater(ui, repo, None, {}, tmpl, mapfile, False)
+    except SyntaxError, inst:
+        raise util.Abort(inst.args[0])
+
+    if not extramsg:
+        extramsg = '' # ensure that extramsg is string
+
+    ui.pushbuffer()
+    t.show(ctx, extramsg=extramsg)
+    return ui.popbuffer()
+
 def buildcommittext(repo, ctx, subs, extramsg):
     edittext = []
     modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
--- a/mercurial/help/config.txt	Fri Jul 18 23:15:28 2014 -0500
+++ b/mercurial/help/config.txt	Tue Jul 15 23:34:13 2014 +0900
@@ -330,6 +330,64 @@
 for credentials as usual if required by the remote.
 
 
+``committemplate``
+------------------
+
+``changeset`` configuration in this section is used as the template to
+customize the text shown in the editor when committing.
+
+In addition to pre-defined template keywords, commit log specific one
+below can be used for customization:
+
+``extramsg``
+    String: Extra message (typically 'Leave message empty to abort
+    commit.'). This may be changed by some commands or extensions.
+
+For example, the template configuration below shows as same text as
+one shown by default::
+
+    [committemplate]
+    changeset = {desc}\n\n
+        HG: Enter commit message.  Lines beginning with 'HG:' are removed.
+        HG: {extramsg}
+        HG: --
+        HG: user: {author}\n{ifeq(p2rev, "-1", "",
+       "HG: branch merge\n")
+       }HG: branch '{branch}'\n{if(currentbookmark,
+       "HG: bookmark '{currentbookmark}'\n")  }{subrepos %
+       "HG: subrepo {subrepo}\n"              }{file_adds %
+       "HG: added {file}\n"                   }{file_mods %
+       "HG: changed {file}\n"                 }{file_dels %
+       "HG: removed {file}\n"                 }{if(files, "",
+       "HG: no files changed\n")}
+
+.. note::
+
+   For some problematic encodings (see :hg:`help win32mbcs` for
+   detail), this customization should be configured carefully, to
+   avoid showing broken characters.
+
+   For example, if multibyte character ending with backslash (0x5c) is
+   followed by ASCII character 'n' in the customized template,
+   sequence of backslash and 'n' is treated as line-feed unexpectedly
+   (and multibyte character is broken, too).
+
+Customized template is used for commands below (``--edit`` may be
+required):
+
+- :hg:`backout`
+- :hg:`commit`
+- :hg:`fetch` (for merge commit only)
+- :hg:`graft`
+- :hg:`histedit`
+- :hg:`import`
+- :hg:`qfold`, :hg:`qnew` and :hg:`qrefresh`
+- :hg:`rebase`
+- :hg:`shelve`
+- :hg:`sign`
+- :hg:`tag`
+- :hg:`transplant`
+
 ``decode/encode``
 -----------------
 
--- a/tests/test-commit.t	Fri Jul 18 23:15:28 2014 -0500
+++ b/tests/test-commit.t	Tue Jul 15 23:34:13 2014 +0900
@@ -354,6 +354,48 @@
   
   test saving last-message.txt
 
+test that '[committemplate] changeset' definition and commit log
+specific template keywords work well
+
+  $ cat >> .hg/hgrc <<EOF
+  > [committemplate]
+  > changeset = HG: this is customized commit template
+  >     HG: {extramsg}
+  >     {if(currentbookmark,
+  >    "HG: bookmark '{currentbookmark}' is activated\n",
+  >    "HG: no bookmark is activated\n")}{subrepos %
+  >    "HG: subrepo '{subrepo}' is changed\n"}
+  > EOF
+
+  $ hg init sub2
+  $ echo a > sub2/a
+  $ hg -R sub2 add sub2/a
+  $ echo 'sub2 = sub2' >> .hgsub
+
+  $ HGEDITOR=cat hg commit -S -q
+  HG: this is customized commit template
+  HG: Leave message empty to abort commit.
+  HG: bookmark 'currentbookmark' is activated
+  HG: subrepo 'sub' is changed
+  HG: subrepo 'sub2' is changed
+  abort: empty commit message
+  [255]
+
+  $ hg bookmark --inactive currentbookmark
+  $ hg forget .hgsub
+  $ HGEDITOR=cat hg commit -q
+  HG: this is customized commit template
+  HG: Leave message empty to abort commit.
+  HG: no bookmark is activated
+  abort: empty commit message
+  [255]
+
+  $ cat >> .hg/hgrc <<EOF
+  > # disable customizing for subsequent tests
+  > [committemplate]
+  > changeset =
+  > EOF
+
   $ cd ..
 
 
--- a/tests/test-mq-qrefresh-replace-log-message.t	Fri Jul 18 23:15:28 2014 -0500
+++ b/tests/test-mq-qrefresh-replace-log-message.t	Tue Jul 15 23:34:13 2014 +0900
@@ -26,10 +26,28 @@
   First commit message
 
 Testing changing message with -m
-(this tests also that '--edit' can be used with '--message')
+(this tests also that '--edit' can be used with '--message', and
+that '[committemplate] changeset' definition and commit log specific
+template keyword 'extramsg' work well)
+
+  $ cat >> .hg/hgrc <<EOF
+  > [committemplate]
+  > changeset = HG: this is customized commit template
+  >     {desc}\n\n
+  >     HG: Enter commit message.  Lines beginning with 'HG:' are removed.
+  >     HG: {extramsg}
+  >     HG: --
+  >     HG: user: {author}
+  >     HG: branch '{branch}'\n{file_adds %
+  >    "HG: added {file}\n"     }{file_mods %
+  >    "HG: changed {file}\n"   }{file_dels %
+  >    "HG: removed {file}\n"   }{if(files, "",
+  >    "HG: no files changed\n")}
+  > EOF
 
   $ echo bbbb > file
   $ HGEDITOR=cat hg qrefresh -m "Second commit message" -e
+  HG: this is customized commit template
   Second commit message
   
   
@@ -40,6 +58,12 @@
   HG: branch 'default'
   HG: added file
 
+  $ cat >> .hg/hgrc <<EOF
+  > # disable customizing for subsequent tests
+  > [committemplate]
+  > changeset =
+  > EOF
+
 Should display 'Second commit message'
 
   $ hg log -l1 --template "{desc}\n"