Bug 1270872 - add a commit hook to reject commit messages containing diffstats r?gps draft
authorWes Kocher <wkocher@mozilla.com>
Tue, 19 Sep 2017 00:04:02 -0700
changeset 11713 07f014fc0c6772a93b944f7180af30c48f474673
parent 11679 f254a80cbc809b48a48e034c9bdf7703d2868e0b
push id1802
push userbmo:wkocher@mozilla.com
push dateTue, 26 Sep 2017 17:30:49 +0000
reviewersgps
bugs1270872
Bug 1270872 - add a commit hook to reject commit messages containing diffstats r?gps MozReview-Commit-ID: Aolva6wbT98
hghooks/mozhghooks/commit-message.py
hghooks/tests/test-commit-messages.t
--- a/hghooks/mozhghooks/commit-message.py
+++ b/hghooks/mozhghooks/commit-message.py
@@ -16,16 +16,18 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
 import re
 from mercurial.node import short
 from mozautomation import commitparser
 
 INVALID_REVIEW_FLAG_RE = re.compile(r'[\s.;]r\?(?:\w|$)')
 
+DIFFSTATS_RE = re.compile(r'^ [0-9]+ files changed(, [0-9]+ insertions\(\+\))?(, [0-9]+ deletions\(\-\))?', re.M)
+
 goodMessage = [re.compile(x, re.I) for x in [
     r'bug [0-9]+',
     r'no bug',
 
     r'^(back(ing|ed)?\s+out|backout).*(\s+|\:)[0-9a-f]{12}',
     r'^(revert(ed|ing)?).*(\s+|\:)[0-9a-f]{12}',
     r'^add(ed|ing)? tag'
 ]]
@@ -95,16 +97,21 @@ def is_good_message(ui, c):
                 'git-format-patch -k to avoid this.')
         return False
 
     if INVALID_REVIEW_FLAG_RE.search(firstline):
         message("Rev {rev} contains 'r?' in the commit message. Please use "
                 "'r=' instead.")
         return False
 
+    if DIFFSTATS_RE.search(desc):
+        message("Rev {rev} contains diff stats in the commit message. Please "
+                "remove these from the commit message before pushing.")
+        return False
+
     for r in goodMessage:
         if r.search(firstline):
             return True
 
     desc_lower = desc.lower()
     if desc_lower.startswith(('merge', 'merging', 'automated merge')):
         if len(c.parents()) == 2:
             return True
@@ -146,9 +153,9 @@ def hook(ui, repo, node, hooktype, sourc
     if not rejecting:
         return 0
 
     # We want to allow using this hook locally
     if hooktype == "pretxnchangegroup":
         return 1
 
     ui.write('This changeset would have been rejected!\n')
-    return 0  # to fail not warn change to 1
+    return 0  # to fail not warn change to 1
\ No newline at end of file
--- a/hghooks/tests/test-commit-messages.t
+++ b/hghooks/tests/test-commit-messages.t
@@ -689,8 +689,100 @@ Change a random file and make sure someo
   Source-Revision: 287b02e21fa2c81d58b070be36add5e951512679
   *************************************************************
   
   
   transaction abort!
   rollback completed
   abort: pretxnchangegroup.commit_message hook failed
   [255]
+
+Reject commit messages containing diffstats.
+
+  $ cat >> .hg/hgrc << EOF
+  > [extensions]
+  > strip=
+  > 
+  > [hooks]
+  > pretxnchangegroup.commit_message = python:mozhghooks.commit-message.hook
+  > EOF
+
+  $ hg -q strip -r . --no-backup
+  $ touch foo.bar
+  $ hg -q commit -A -l - << EOF
+  > Bug 12345 - Some commit message r=foo
+  > 
+  >  accessible/tests/browser/events.js | 100 +++++
+  >  accessible/tests/browser/head.js | 297 ++++++++++++++
+  >  accessible/tests/mochitest/common.js | 41 +-
+  >  38 files changed, 3176 insertions(+), 7 deletions(-)
+  > EOF
+  $ touch foo.baz
+  $ hg -q commit -A -l - << EOF
+  > Bug 12345 - Some commit message r=foo
+  > 
+  >  accessible/tests/browser/events.js | 100 +++++
+  >  accessible/tests/browser/head.js | 297 ++++++++++++++
+  >  accessible/tests/mochitest/common.js | 41 +-
+  >  38 files changed, 7 deletions(-)
+  > EOF
+  $ touch foo.ban
+  $ hg -q commit -A -l - << EOF
+  > Bug 12345 - Some commit message r=foo
+  > 
+  >  accessible/tests/browser/events.js | 100 +++++
+  >  accessible/tests/browser/head.js | 297 ++++++++++++++
+  >  accessible/tests/mochitest/common.js | 41 +-
+  >  38 files changed, 3176 insertions(+)
+  > EOF
+  $ hg push
+  pushing to $TESTTMP/striptest/server-vendor
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  
+  
+  ************************** ERROR ****************************
+  Rev f0d6ec39e891 contains diff stats in the commit message. Please remove these from the commit message before pushing.
+  test
+  Bug 12345 - Some commit message r=foo
+  
+   accessible/tests/browser/events.js | 100 +++++
+   accessible/tests/browser/head.js | 297 ++++++++++++++
+   accessible/tests/mochitest/common.js | 41 +-
+   38 files changed, 3176 insertions(+)
+  *************************************************************
+  
+  
+  
+  
+  ************************** ERROR ****************************
+  Rev 5aa22858c44d contains diff stats in the commit message. Please remove these from the commit message before pushing.
+  test
+  Bug 12345 - Some commit message r=foo
+  
+   accessible/tests/browser/events.js | 100 +++++
+   accessible/tests/browser/head.js | 297 ++++++++++++++
+   accessible/tests/mochitest/common.js | 41 +-
+   38 files changed, 7 deletions(-)
+  *************************************************************
+  
+  
+  
+  
+  ************************** ERROR ****************************
+  Rev fc08240976ea contains diff stats in the commit message. Please remove these from the commit message before pushing.
+  test
+  Bug 12345 - Some commit message r=foo
+  
+   accessible/tests/browser/events.js | 100 +++++
+   accessible/tests/browser/head.js | 297 ++++++++++++++
+   accessible/tests/mochitest/common.js | 41 +-
+   38 files changed, 3176 insertions(+), 7 deletions(-)
+  *************************************************************
+  
+  
+  transaction abort!
+  rollback completed
+  abort: pretxnchangegroup.commit_message hook failed
+  [255]