Skip to content

Commit 964948b

Browse files
committed
Fix non-ascii merge commit messages in python2
1 parent e3b14c3 commit 964948b

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

pre_commit/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def is_in_merge_conflict():
4848
def parse_merge_msg_for_conflicts(merge_msg):
4949
# Conflicted files start with tabs
5050
return [
51-
line.lstrip('#').strip()
51+
line.lstrip(b'#').strip().decode('UTF-8')
5252
for line in merge_msg.splitlines()
5353
# '#\t' for git 2.4.1
54-
if line.startswith(('\t', '#\t'))
54+
if line.startswith((b'\t', b'#\t'))
5555
]
5656

5757

@@ -60,7 +60,7 @@ def get_conflicted_files():
6060
logger.info('Checking merge-conflict files only.')
6161
# Need to get the conflicted files from the MERGE_MSG because they could
6262
# have resolved the conflict by choosing one side or the other
63-
merge_msg = open(os.path.join(get_git_dir('.'), 'MERGE_MSG')).read()
63+
merge_msg = open(os.path.join(get_git_dir('.'), 'MERGE_MSG'), 'rb').read()
6464
merge_conflict_filenames = parse_merge_msg_for_conflicts(merge_msg)
6565

6666
# This will get the rest of the changes made after the merge.

tests/commands/install_uninstall_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: UTF-8 -*-
12
from __future__ import absolute_import
23
from __future__ import unicode_literals
34

@@ -190,6 +191,18 @@ def test_commit_am(tempdir_factory):
190191
assert ret == 0
191192

192193

194+
def test_unicode_merge_commit_message(tempdir_factory):
195+
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
196+
with cwd(path):
197+
assert install(Runner(path, C.CONFIG_FILE)) == 0
198+
cmd_output('git', 'checkout', 'master', '-b', 'foo')
199+
cmd_output('git', 'commit', '--allow-empty', '-m', 'branch2')
200+
cmd_output('git', 'checkout', 'master')
201+
cmd_output('git', 'merge', 'foo', '--no-ff', '--no-commit', '-m', '☃')
202+
# Used to crash
203+
cmd_output('git', 'commit', '--no-edit')
204+
205+
193206
def test_install_idempotent(tempdir_factory):
194207
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
195208
with cwd(path):

tests/git_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def test_get_conflicted_files_unstaged_files(in_merge_conflict):
142142
assert ret == {'conflict_file'}
143143

144144

145-
MERGE_MSG = "Merge branch 'foo' into bar\n\nConflicts:\n\tconflict_file\n"
146-
OTHER_MERGE_MSG = MERGE_MSG + '\tother_conflict_file\n'
145+
MERGE_MSG = b"Merge branch 'foo' into bar\n\nConflicts:\n\tconflict_file\n"
146+
OTHER_MERGE_MSG = MERGE_MSG + b'\tother_conflict_file\n'
147147

148148

149149
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)