sapling/eden/scm/edenscm/hgext/checkmessagehook.py
Jun Wu 07e043045c transaction: ensure metalog message is valid utf8
Summary:
The metalog message is just for display purpose so it does not have to be
byte-to-byte accurate.  This solves potential crashes on Python 2 Windows.

    File "c:\Tools\hg\python27.zip\edenscm\mercurial\transaction.py", line 587, in _writemetalog
      " ".join(map(util.shellquote, pycompat.sysargv[1:])), int(util.timer())
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: invalid utf-8

The logic will become more "correct" when we migrate to Python 3.

Reported by: markbt

Reviewed By: markbt

Differential Revision: D20422747

fbshipit-source-id: 41123d132a1e545db77d7321099da611668174f4
2020-03-12 14:26:07 -07:00

46 lines
1.2 KiB
Python

# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
import string
from edenscm.mercurial import encoding, registrar
from edenscm.mercurial.i18n import _
configtable = {}
configitem = registrar.configitem(configtable)
configitem("checkmessage", "allownonprintable", default=False)
def reposetup(ui, repo):
ui.setconfig("hooks", "pretxncommit.checkmessage", checkcommitmessage)
def checkcommitmessage(ui, repo, **kwargs):
"""
Checks a single commit message for adherence to commit message rules.
"""
message = encoding.fromlocal(repo["tip"].description())
if ui.configbool("checkmessage", "allownonprintable"):
return False
printable = set(string.printable)
badlines = []
for lnum, line in enumerate(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 bool(badlines)