changeset 26742:bec1a579ebc4

commit: abort when a committemplate is not changed If a committemplate is provided and no message is provided on the command line, and no edits are made to the commit template, then abort the commit.
author Tony Tung <tonytung@fb.com>
date Fri, 09 Oct 2015 21:44:54 -0700
parents e1568d5eb052
children 58b04a538caa
files mercurial/cmdutil.py tests/test-commit.t
diffstat 2 files changed, 39 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Wed Oct 14 16:04:43 2015 -0700
+++ b/mercurial/cmdutil.py	Fri Oct 09 21:44:54 2015 -0700
@@ -2691,19 +2691,22 @@
 def commiteditor(repo, ctx, subs, editform=''):
     if ctx.description():
         return ctx.description()
-    return commitforceeditor(repo, ctx, subs, editform=editform)
+    return commitforceeditor(repo, ctx, subs, editform=editform,
+                             unchangedmessagedetection=True)
 
 def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None,
-                      editform=''):
+                      editform='', unchangedmessagedetection=False):
     if not extramsg:
         extramsg = _("Leave message empty to abort commit.")
 
     forms = [e for e in editform.split('.') if e]
     forms.insert(0, 'changeset')
+    templatetext = None
     while forms:
         tmpl = repo.ui.config('committemplate', '.'.join(forms))
         if tmpl:
-            committext = buildcommittemplate(repo, ctx, subs, extramsg, tmpl)
+            templatetext = committext = buildcommittemplate(
+                repo, ctx, subs, extramsg, tmpl)
             break
         forms.pop()
     else:
@@ -2712,14 +2715,18 @@
     # run editor in the repository root
     olddir = os.getcwd()
     os.chdir(repo.root)
-    text = repo.ui.edit(committext, ctx.user(), ctx.extra(), editform=editform)
-    text = re.sub("(?m)^HG:.*(\n|$)", "", text)
+    editortext = repo.ui.edit(committext, ctx.user(), ctx.extra(),
+                              editform=editform)
+
+    text = re.sub("(?m)^HG:.*(\n|$)", "", editortext)
     os.chdir(olddir)
 
     if finishdesc:
         text = finishdesc(text)
     if not text.strip():
         raise error.Abort(_("empty commit message"))
+    if unchangedmessagedetection and editortext == templatetext:
+        raise error.Abort(_("commit message unchanged"))
 
     return text
 
--- a/tests/test-commit.t	Wed Oct 14 16:04:43 2015 -0700
+++ b/tests/test-commit.t	Fri Oct 09 21:44:54 2015 -0700
@@ -650,3 +650,30 @@
   $ hg co --clean tip
   abort: path contains illegal component: HG8B6C~2/hgrc (glob)
   [255]
+
+# test that an unmodified commit template message aborts
+
+  $ hg init unmodified_commit_template
+  $ cd unmodified_commit_template
+  $ echo foo > foo
+  $ hg add foo
+  $ hg commit -m "foo"
+  $ cat >> .hg/hgrc <<EOF
+  > [committemplate]
+  > changeset.commit = HI THIS IS NOT STRIPPED
+  >     HG: this is customized commit template
+  >     HG: {extramsg}
+  >     {if(activebookmark,
+  >    "HG: bookmark '{activebookmark}' is activated\n",
+  >    "HG: no bookmark is activated\n")}{subrepos %
+  >    "HG: subrepo '{subrepo}' is changed\n"}
+  > EOF
+  $ cat > $TESTTMP/notouching.sh <<EOF
+  > true
+  > EOF
+  $ echo foo2 > foo2
+  $ hg add foo2
+  $ HGEDITOR="sh $TESTTMP/notouching.sh" hg commit
+  abort: commit message unchanged
+  [255]
+  $ cd ..