From 4a01f64c8f67ec74edd2c6c501bce8bf61ad3494 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 26 May 2016 11:20:32 -0700 Subject: [PATCH] Fix check-merge-conflict against binary files --- pre_commit_hooks/check_merge_conflict.py | 14 ++++++++------ testing/resources/img1.jpg | Bin 0 -> 843 bytes tests/check_merge_conflict_test.py | 8 ++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 testing/resources/img1.jpg diff --git a/pre_commit_hooks/check_merge_conflict.py b/pre_commit_hooks/check_merge_conflict.py index ec09473..4a98843 100644 --- a/pre_commit_hooks/check_merge_conflict.py +++ b/pre_commit_hooks/check_merge_conflict.py @@ -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 diff --git a/testing/resources/img1.jpg b/testing/resources/img1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..dea42627ad9f5eb8061bf5068117faa3ba22591c GIT binary patch literal 843 zcmex=_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%*;qrN1?DZF(6Oj-S5fuR$!pIEN!@|nR z%E~Fi%grl7GWdUhL6Cz%fI)znQHg;`kdaxC@&6G9d7vj*8Nq-73K*GyZe(NU;N;>4 zD%dK(z{JSR%*4VBay3wOEl{3;MUYiU(a@1iI53f2sZhkIapFP_Wv7h?MT0JWP%%y_ zYU1P)6PJ*bQdLve(9|+9H8Z!cv~qTFb#wRd^a>6M4GWKmj7m;PO-s+n%qlJ^Ei136 ztZHs)ZENr7?3y%r%G7DoXUv?nXz`Mz%a*TLxoXqqEnBy3-?4Mop~FXx9y@;G&P778mFHFAhJO^@zmKAh{eZ~f|M{x9gm=v-l>-n?(>?Mmen~E6EX;=IIShdgcqTAxqLYrGVE+%f;HR<7| pm^B+2ZXP)2P=A