forked from pre-commit/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_merge_conflict.py
More file actions
61 lines (49 loc) · 1.81 KB
/
check_merge_conflict.py
File metadata and controls
61 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations
import argparse
import os.path
from collections.abc import Sequence
from pre_commit_hooks.util import cmd_output
CONFLICT_PATTERNS = [
b'<<<<<<< ',
b'=======',
b'>>>>>>> ',
]
N = len(CONFLICT_PATTERNS)
def is_in_merge() -> bool:
git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip()
return (
os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
(
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'))
)
)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument('--assume-in-merge', action='store_true')
args = parser.parse_args(argv)
if not is_in_merge() and not args.assume_in_merge:
return 0
retcode = 0
for filename in args.filenames:
with open(filename, 'rb') as inputfile:
expected_conflict_pattern_index = 0
for i, line in enumerate(inputfile, start=1):
# Look for conflict patterns in order
if line.startswith(
CONFLICT_PATTERNS[expected_conflict_pattern_index],
):
expected_conflict_pattern_index += 1
if expected_conflict_pattern_index == N:
print(
f"{filename}:{i}: Merge conflict string "
f"{CONFLICT_PATTERNS[-1].strip().decode()!r} "
f"found",
)
retcode = 1
break
return retcode
if __name__ == '__main__':
raise SystemExit(main())