mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 11:16:53 +00:00
Add check-merge-conflict hook
This commit is contained in:
parent
70a319aea3
commit
779a42919a
5 changed files with 66 additions and 0 deletions
31
pre_commit_hooks/check_merge_conflict.py
Normal file
31
pre_commit_hooks/check_merge_conflict.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
CONFLICT_PATTERNS = [
|
||||
'<<<<<<< ',
|
||||
'=======',
|
||||
'>>>>>>> '
|
||||
]
|
||||
WARNING_MSG = 'Merge conflict string "{0}" found in {1}:{2}'
|
||||
|
||||
|
||||
def detect_merge_conflict(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retcode = 0
|
||||
for filename in args.filenames:
|
||||
with open(filename) as inputfile:
|
||||
for i, line in enumerate(inputfile):
|
||||
for pattern in CONFLICT_PATTERNS:
|
||||
if line.startswith(pattern):
|
||||
print(WARNING_MSG.format(pattern, filename, i))
|
||||
retcode = 1
|
||||
|
||||
return retcode
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(detect_merge_conflict())
|
||||
Loading…
Add table
Add a link
Reference in a new issue