blob: 15ec284a849aee56750cc2ca990b2f05f456ec1d [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Guy Kisel779a4292015-03-13 16:30:14 -07003import argparse
Anthony Sottile635fa7d2015-03-20 16:15:09 -07004import os.path
Anthony Sottile030bfac2019-01-31 19:19:10 -08005from typing import Sequence
6
Ersin Yildirim07af5402021-10-04 21:50:38 +02007from pre_commit_hooks.util import cmd_output
8
Guy Kisel779a4292015-03-13 16:30:14 -07009
10CONFLICT_PATTERNS = [
Anthony Sottile4a01f642016-05-26 11:20:32 -070011 b'<<<<<<< ',
12 b'======= ',
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040013 b'=======\r\n',
Anthony Sottile4a01f642016-05-26 11:20:32 -070014 b'=======\n',
Anthony Sottile2a902e02017-07-12 18:35:24 -070015 b'>>>>>>> ',
Guy Kisel779a4292015-03-13 16:30:14 -070016]
Guy Kisel779a4292015-03-13 16:30:14 -070017
18
Ersin Yildirim07af5402021-10-04 21:50:38 +020019def is_in_merge() -> bool:
20 git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip()
Anthony Sottile635fa7d2015-03-20 16:15:09 -070021 return (
Ersin Yildirim07af5402021-10-04 21:50:38 +020022 os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
Anthony Sottile6076fd12017-06-12 10:39:07 -070023 (
Ersin Yildirim07af5402021-10-04 21:50:38 +020024 os.path.exists(os.path.join(git_dir, 'MERGE_HEAD')) or
25 os.path.exists(os.path.join(git_dir, 'rebase-apply')) or
26 os.path.exists(os.path.join(git_dir, 'rebase-merge'))
Anthony Sottile6076fd12017-06-12 10:39:07 -070027 )
Anthony Sottile635fa7d2015-03-20 16:15:09 -070028 )
29
30
Anthony Sottile8f615292022-01-15 19:24:05 -050031def main(argv: Sequence[str] | None = None) -> int:
Guy Kisel779a4292015-03-13 16:30:14 -070032 parser = argparse.ArgumentParser()
33 parser.add_argument('filenames', nargs='*')
Vinay Karanam64b9f3d2018-06-26 23:34:04 +053034 parser.add_argument('--assume-in-merge', action='store_true')
Guy Kisel779a4292015-03-13 16:30:14 -070035 args = parser.parse_args(argv)
36
Vinay Karanam64b9f3d2018-06-26 23:34:04 +053037 if not is_in_merge() and not args.assume_in_merge:
Anthony Sottile635fa7d2015-03-20 16:15:09 -070038 return 0
39
Guy Kisel779a4292015-03-13 16:30:14 -070040 retcode = 0
41 for filename in args.filenames:
Anthony Sottile4a01f642016-05-26 11:20:32 -070042 with open(filename, 'rb') as inputfile:
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040043 for i, line in enumerate(inputfile, start=1):
Guy Kisel779a4292015-03-13 16:30:14 -070044 for pattern in CONFLICT_PATTERNS:
45 if line.startswith(pattern):
Anthony Sottilefea76b92020-02-03 08:41:48 -080046 print(
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040047 f'{filename}:{i}: Merge conflict string '
48 f'{pattern.strip().decode()!r} found',
Anthony Sottilefea76b92020-02-03 08:41:48 -080049 )
Guy Kisel779a4292015-03-13 16:30:14 -070050 retcode = 1
51
52 return retcode
53
Anthony Sottile70e405e2016-11-30 09:56:42 -080054
Guy Kisel779a4292015-03-13 16:30:14 -070055if __name__ == '__main__':
Anthony Sottile39ab2ed2021-10-23 13:23:50 -040056 raise SystemExit(main())