reviewboard: move commit message rewriting function into library (bug 1243486); r=dminor
authorGregory Szorc <gps@mozilla.com>
Tue, 02 Feb 2016 13:01:51 -0800
changeset 7110 eb9e43ce04570bc08cfb4bd08f0b9ddbe0fe79c9
parent 7109 7a58b51b386c82e76304ff9bba093b7ec86e3bd9
child 7111 1af421167a356a8d008529139ad791dd00a3655a
push id594
push usergszorc@mozilla.com
push dateFri, 05 Feb 2016 20:03:35 +0000
reviewersdminor
bugs1243486
reviewboard: move commit message rewriting function into library (bug 1243486); r=dminor This will enable us to use the function from the Mercurial extension. MozReview-Commit-ID: 4Lggkt0kaJL
git/commands/git-mozreview
hgext/reviewboard/hgrb/util.py
--- a/git/commands/git-mozreview
+++ b/git/commands/git-mozreview
@@ -29,16 +29,17 @@ from requests.auth import (
     AuthBase,
     _basic_auth_str,
 )
 
 from mozautomation.commitparser import (
     parse_bugs,
 )
 from hgrb.util import (
+    addcommitid,
     genid,
     ReviewID,
 )
 
 
 # Endpoints that we implicitly trust to send our Bugzilla API key to.
 TRUSTED_API_KEY_SERVICES = {
     'https://reviewboard-hg.mozilla.org',
@@ -657,40 +658,31 @@ def commit_msg_hook(args):
                 continue
 
             # Ignore the inline git diff.
             if line.startswith('diff --git'):
                 break
 
             lines.append(line)
 
-    # Prune blank lines at end.
-    while lines and not lines[-1].strip():
-        lines.pop()
-
     if not lines:
         return 0
 
     # Ignore temporary commits.
     if lines[0].startswith(('fixup', 'squash')):
         return 0
 
-    # Look for existing review identifier.
-    if any(l.startswith('MozReview-Commit-ID: ') for l in lines):
+    msg, changed = addcommitid('\n'.join(lines),
+                               fakeidpath=os.environ.get('FAKEIDPATH'))
+
+    if not changed:
         return 0
 
-    # Insert empty line between main commit message and metadata.
-    if not re.match('^[a-zA-Z-]+: ', lines[-1]):
-        lines.append('')
-
-    cid = genid(fakeidpath=os.environ.get('FAKEIDPATH'))
-    lines.append('MozReview-Commit-ID: %s' % cid)
-
     with open(args.path, 'wb') as fh:
-        fh.write('\n'.join(lines))
+        fh.write(msg)
 
     return 0
 
 
 def main(args):
     if sys.platform == 'win32':
         import msvcrt
         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
--- a/hgext/reviewboard/hgrb/util.py
+++ b/hgext/reviewboard/hgrb/util.py
@@ -1,13 +1,14 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
 import errno
 import random
+import re
 import time
 
 from mercurial import util
 from mercurial.i18n import _
 
 class ReviewID(object):
     """Represents a parsed review identifier."""
 
@@ -122,8 +123,32 @@ def genid(repo=None, fakeidpath=None):
 
     chars = []
     while value > 0:
         quot, remain = divmod(value, 62)
         chars.append(BASE62_CHARS[remain])
         value = quot
 
     return ''.join(reversed(chars))
+
+
+def addcommitid(msg, repo=None, fakeidpath=None):
+    """Add a commit ID to a commit message."""
+    lines = msg.splitlines()
+
+    # Prune blank lines at the end.
+    while lines and not lines[-1].strip():
+        lines.pop()
+
+    if not lines:
+        return msg, False
+
+    if any(l.startswith('MozReview-Commit-ID: ') for l in lines):
+        return msg, False
+
+    # Insert empty line between main commit message and metadata.
+    if not re.match('^[a-zA-Z-]+: ', lines[-1]):
+        lines.append('')
+
+    cid = genid(repo=repo, fakeidpath=fakeidpath)
+    lines.append('MozReview-Commit-ID: %s' % cid)
+
+    return '\n'.join(lines), True