Patchwork commit: abort when a committemplate is not changed

login
register
mail settings
Submitter Tony Tung
Date Oct. 16, 2015, 9:35 p.m.
Message ID <ceaeafe3716104d2eeb5.1445031307@andromeda.local>
Download mbox | patch
Permalink /patch/11151/
State Accepted
Headers show

Comments

Tony Tung - Oct. 16, 2015, 9:35 p.m.
# HG changeset patch
# User Tony Tung <tonytung@fb.com>
# Date 1444452294 25200
#      Fri Oct 09 21:44:54 2015 -0700
# Node ID ceaeafe3716104d2eeb583027cf1a7942682ac00
# Parent  6e715040c1725b5debce888c4f7d3fdbf55cc900
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.
Matt Mackall - Oct. 16, 2015, 10:25 p.m.
On Fri, 2015-10-16 at 14:35 -0700, Tony Tung wrote:
> # HG changeset patch
> # User Tony Tung <tonytung@fb.com>
> # Date 1444452294 25200
> #      Fri Oct 09 21:44:54 2015 -0700
> # Node ID ceaeafe3716104d2eeb583027cf1a7942682ac00
> # Parent  6e715040c1725b5debce888c4f7d3fdbf55cc900
> commit: abort when a committemplate is not changed.

Queued for default, thanks.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -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 util.Abort(_("empty commit message"))
+    if unchangedmessagedetection and editortext == templatetext:
+        raise util.Abort(_("commit message unchanged"))
 
     return text
 
diff --git a/tests/test-commit.t b/tests/test-commit.t
--- a/tests/test-commit.t
+++ b/tests/test-commit.t
@@ -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 ..