2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-03-14 07:30:14 +08:00
|
|
|
import argparse
|
2015-03-21 07:15:09 +08:00
|
|
|
import os.path
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2019-02-01 11:19:10 +08:00
|
|
|
|
2021-10-05 03:50:38 +08:00
|
|
|
from pre_commit_hooks.util import cmd_output
|
|
|
|
|
2015-03-14 07:30:14 +08:00
|
|
|
|
|
|
|
CONFLICT_PATTERNS = [
|
2016-05-27 02:20:32 +08:00
|
|
|
b'<<<<<<< ',
|
|
|
|
b'======= ',
|
2022-04-07 04:55:26 +08:00
|
|
|
b'=======\r\n',
|
2016-05-27 02:20:32 +08:00
|
|
|
b'=======\n',
|
2017-07-13 09:35:24 +08:00
|
|
|
b'>>>>>>> ',
|
2015-03-14 07:30:14 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-10-05 03:50:38 +08:00
|
|
|
def is_in_merge() -> bool:
|
|
|
|
git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip()
|
2015-03-21 07:15:09 +08:00
|
|
|
return (
|
2021-10-05 03:50:38 +08:00
|
|
|
os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
|
2017-06-13 01:39:07 +08:00
|
|
|
(
|
2021-10-05 03:50:38 +08:00
|
|
|
os.path.exists(os.path.join(git_dir, 'MERGE_HEAD')) or
|
|
|
|
os.path.exists(os.path.join(git_dir, 'rebase-apply')) or
|
|
|
|
os.path.exists(os.path.join(git_dir, 'rebase-merge'))
|
2017-06-13 01:39:07 +08:00
|
|
|
)
|
2015-03-21 07:15:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2015-03-14 07:30:14 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('filenames', nargs='*')
|
2018-06-27 02:04:04 +08:00
|
|
|
parser.add_argument('--assume-in-merge', action='store_true')
|
2015-03-14 07:30:14 +08:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
2018-06-27 02:04:04 +08:00
|
|
|
if not is_in_merge() and not args.assume_in_merge:
|
2015-03-21 07:15:09 +08:00
|
|
|
return 0
|
|
|
|
|
2015-03-14 07:30:14 +08:00
|
|
|
retcode = 0
|
|
|
|
for filename in args.filenames:
|
2016-05-27 02:20:32 +08:00
|
|
|
with open(filename, 'rb') as inputfile:
|
2022-04-07 04:55:26 +08:00
|
|
|
for i, line in enumerate(inputfile, start=1):
|
2015-03-14 07:30:14 +08:00
|
|
|
for pattern in CONFLICT_PATTERNS:
|
|
|
|
if line.startswith(pattern):
|
2020-02-04 00:41:48 +08:00
|
|
|
print(
|
2022-04-07 04:55:26 +08:00
|
|
|
f'{filename}:{i}: Merge conflict string '
|
|
|
|
f'{pattern.strip().decode()!r} found',
|
2020-02-04 00:41:48 +08:00
|
|
|
)
|
2015-03-14 07:30:14 +08:00
|
|
|
retcode = 1
|
|
|
|
|
|
|
|
return retcode
|
|
|
|
|
2016-12-01 01:56:42 +08:00
|
|
|
|
2015-03-14 07:30:14 +08:00
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|