mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 18:16:52 +00:00
Merge pull request #116 from pre-commit/check_merge_conflict_binary_files
Fix check-merge-conflict against binary files
This commit is contained in:
commit
b64436cdda
4 changed files with 17 additions and 7 deletions
|
|
@ -5,10 +5,10 @@ import os.path
|
|||
import sys
|
||||
|
||||
CONFLICT_PATTERNS = [
|
||||
'<<<<<<< ',
|
||||
'======= ',
|
||||
'=======\n',
|
||||
'>>>>>>> '
|
||||
b'<<<<<<< ',
|
||||
b'======= ',
|
||||
b'=======\n',
|
||||
b'>>>>>>> '
|
||||
]
|
||||
WARNING_MSG = 'Merge conflict string "{0}" found in {1}:{2}'
|
||||
|
||||
|
|
@ -30,11 +30,13 @@ def detect_merge_conflict(argv=None):
|
|||
|
||||
retcode = 0
|
||||
for filename in args.filenames:
|
||||
with open(filename) as inputfile:
|
||||
with open(filename, 'rb') as inputfile:
|
||||
for i, line in enumerate(inputfile):
|
||||
for pattern in CONFLICT_PATTERNS:
|
||||
if line.startswith(pattern):
|
||||
print(WARNING_MSG.format(pattern, filename, i + 1))
|
||||
print(WARNING_MSG.format(
|
||||
pattern.decode(), filename, i + 1,
|
||||
))
|
||||
retcode = 1
|
||||
|
||||
return retcode
|
||||
|
|
|
|||
BIN
testing/resources/img1.jpg
Normal file
BIN
testing/resources/img1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 843 B |
|
|
@ -25,5 +25,5 @@ def get_resource_path(path):
|
|||
|
||||
def write_file(filename, contents):
|
||||
"""Hax because coveragepy chokes on nested context managers."""
|
||||
with io.open(filename, 'w') as file_obj:
|
||||
with io.open(filename, 'w', newline='') as file_obj:
|
||||
file_obj.write(contents)
|
||||
|
|
|
|||
|
|
@ -3,12 +3,14 @@ from __future__ import unicode_literals
|
|||
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.check_merge_conflict import detect_merge_conflict
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
from testing.util import cwd
|
||||
from testing.util import get_resource_path
|
||||
from testing.util import write_file
|
||||
|
||||
|
||||
|
|
@ -109,6 +111,12 @@ def test_merge_conflicts_ok(ok_contents):
|
|||
assert detect_merge_conflict(['f1']) == 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('f1_is_a_conflict_file')
|
||||
def test_ignores_binary_files():
|
||||
shutil.copy(get_resource_path('img1.jpg'), 'f1')
|
||||
assert detect_merge_conflict(['f1']) == 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('in_tmpdir')
|
||||
def test_does_not_care_when_not_in_a_merge():
|
||||
with io.open('README.md', 'w') as readme_file:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue