mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00

Several markup formats, such as Markdown or Re(Structured)Text can format titles as text with '=' characters as double underlining, like this: ``` My Page Title ============= Lorem ipsum... ``` Rather that considering any line starting with seven '=' as a conflict marker, require a space (or line-ending newline) after the equals. This could still create a false positive for a seven character title, like "Problem", but the markup formats generally allow extra '=' characters, so by formatting the text like this: ``` Problem ======== Not... ``` these pre-commit warnings can be avoided. Also updates the tests to add newlines for more realistic conflict files (while a file might not end with a newline, conflict markers will). Prevent false negative on test_does_not_care_when_not_in_a_conflict() by making sure that README.md contains a line identical to a conflict string (exactly seven '=' followed by a newline).
44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
from __future__ import print_function
|
|
|
|
import argparse
|
|
import os.path
|
|
import sys
|
|
|
|
CONFLICT_PATTERNS = [
|
|
'<<<<<<< ',
|
|
'======= ',
|
|
'=======\n',
|
|
'>>>>>>> '
|
|
]
|
|
WARNING_MSG = 'Merge conflict string "{0}" found in {1}:{2}'
|
|
|
|
|
|
def is_in_merge_conflict():
|
|
return (
|
|
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
|
|
os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
|
|
)
|
|
|
|
|
|
def detect_merge_conflict(argv=None):
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('filenames', nargs='*')
|
|
args = parser.parse_args(argv)
|
|
|
|
if not is_in_merge_conflict():
|
|
return 0
|
|
|
|
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 + 1))
|
|
retcode = 1
|
|
|
|
return retcode
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(detect_merge_conflict())
|