Page MenuHomePhabricator

D1419.largetrue.id.diff
No OneTemporary

File Metadata

Created
Sat, Apr 16, 6:11 PM

D1419.largetrue.id.diff

diff --git a/hgext3rd/checkmessagehook.py b/hgext3rd/checkmessagehook.py
--- a/hgext3rd/checkmessagehook.py
+++ b/hgext3rd/checkmessagehook.py
@@ -17,10 +17,17 @@
return True
printable = set(string.printable)
- for c in hg_commit_message:
- if ord(c) < 128 and c not in printable:
- ui.warn(_('non-printable characters in commit message\n'))
- return True
+ badlines = []
+ for lnum, line in enumerate(hg_commit_message.splitlines()):
+ for c in line:
+ if ord(c) < 128 and c not in printable:
+ badlines.append((lnum + 1, line))
+ break
+
+ if badlines:
+ ui.warn(_('non-printable characters in commit message\n'))
+ for num, l in badlines:
+ ui.warn(_('Line {}: {!r}\n'.format(num, l)))
# False means success
- return False
+ return bool(badlines)
diff --git a/tests/ctrlchar-msg.txt b/tests/ctrlchar-msg.txt
new file mode 100644
--- /dev/null
+++ b/tests/ctrlchar-msg.txt
@@ -0,0 +1,8 @@
+This is a commit with bad chars in the message - but this one is OK
+
+That was a blank line
+
+This has a sneaky ctrl-A: 
+And this has esc: 
+
+Finish off with an OK line
diff --git a/tests/perfectlyok-msg.txt b/tests/perfectlyok-msg.txt
new file mode 100644
--- /dev/null
+++ b/tests/perfectlyok-msg.txt
@@ -0,0 +1 @@
+This commit message is perfectly OK, and has no sneaky control characters.
diff --git a/tests/test-checkmessagehook.t b/tests/test-checkmessagehook.t
new file mode 100644
--- /dev/null
+++ b/tests/test-checkmessagehook.t
@@ -0,0 +1,28 @@
+ $ cat >> $HGRCPATH <<EOF
+ > [extensions]
+ > checkmessagehook = $TESTDIR/../hgext3rd/checkmessagehook.py
+ > EOF
+
+Build up a repo
+
+ $ hg init repo
+ $ cd repo
+ $ touch a
+ $ hg commit -A -l $TESTDIR/ctrlchar-msg.txt
+ adding a
+ non-printable characters in commit message
+ Line 5: 'This has a sneaky ctrl-A: \x01'
+ Line 6: 'And this has esc: \x1b'
+ transaction abort!
+ rollback completed
+ abort: pretxncommit.checkmessage hook failed
+ [255]
+ $ hg commit -A -l $TESTDIR/perfectlyok-msg.txt
+ adding a
+ $ hg log -r .
+ changeset: 0:d9cf9881be7b
+ tag: tip
+ user: test
+ date: Thu Jan 01 00:00:00 1970 +0000
+ summary: This commit message is perfectly OK, and has no sneaky control characters.
+

Event Timeline