Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 3 | import argparse |
Anthony Sottile | 635fa7d | 2015-03-20 16:15:09 -0700 | [diff] [blame] | 4 | import os.path |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 5 | from typing import Sequence |
| 6 | |
Ersin Yildirim | 07af540 | 2021-10-04 21:50:38 +0200 | [diff] [blame] | 7 | from pre_commit_hooks.util import cmd_output |
| 8 | |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 9 | |
| 10 | CONFLICT_PATTERNS = [ |
Anthony Sottile | 4a01f64 | 2016-05-26 11:20:32 -0700 | [diff] [blame] | 11 | b'<<<<<<< ', |
| 12 | b'======= ', |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 13 | b'=======\r\n', |
Anthony Sottile | 4a01f64 | 2016-05-26 11:20:32 -0700 | [diff] [blame] | 14 | b'=======\n', |
Anthony Sottile | 2a902e0 | 2017-07-12 18:35:24 -0700 | [diff] [blame] | 15 | b'>>>>>>> ', |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 16 | ] |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 17 | |
| 18 | |
Ersin Yildirim | 07af540 | 2021-10-04 21:50:38 +0200 | [diff] [blame] | 19 | def is_in_merge() -> bool: |
| 20 | git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip() |
Anthony Sottile | 635fa7d | 2015-03-20 16:15:09 -0700 | [diff] [blame] | 21 | return ( |
Ersin Yildirim | 07af540 | 2021-10-04 21:50:38 +0200 | [diff] [blame] | 22 | os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and |
Anthony Sottile | 6076fd1 | 2017-06-12 10:39:07 -0700 | [diff] [blame] | 23 | ( |
Ersin Yildirim | 07af540 | 2021-10-04 21:50:38 +0200 | [diff] [blame] | 24 | 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 Sottile | 6076fd1 | 2017-06-12 10:39:07 -0700 | [diff] [blame] | 27 | ) |
Anthony Sottile | 635fa7d | 2015-03-20 16:15:09 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 31 | def main(argv: Sequence[str] | None = None) -> int: |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 32 | parser = argparse.ArgumentParser() |
| 33 | parser.add_argument('filenames', nargs='*') |
Vinay Karanam | 64b9f3d | 2018-06-26 23:34:04 +0530 | [diff] [blame] | 34 | parser.add_argument('--assume-in-merge', action='store_true') |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 35 | args = parser.parse_args(argv) |
| 36 | |
Vinay Karanam | 64b9f3d | 2018-06-26 23:34:04 +0530 | [diff] [blame] | 37 | if not is_in_merge() and not args.assume_in_merge: |
Anthony Sottile | 635fa7d | 2015-03-20 16:15:09 -0700 | [diff] [blame] | 38 | return 0 |
| 39 | |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 40 | retcode = 0 |
| 41 | for filename in args.filenames: |
Anthony Sottile | 4a01f64 | 2016-05-26 11:20:32 -0700 | [diff] [blame] | 42 | with open(filename, 'rb') as inputfile: |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 43 | for i, line in enumerate(inputfile, start=1): |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 44 | for pattern in CONFLICT_PATTERNS: |
| 45 | if line.startswith(pattern): |
Anthony Sottile | fea76b9 | 2020-02-03 08:41:48 -0800 | [diff] [blame] | 46 | print( |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 47 | f'{filename}:{i}: Merge conflict string ' |
| 48 | f'{pattern.strip().decode()!r} found', |
Anthony Sottile | fea76b9 | 2020-02-03 08:41:48 -0800 | [diff] [blame] | 49 | ) |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 50 | retcode = 1 |
| 51 | |
| 52 | return retcode |
| 53 | |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 54 | |
Guy Kisel | 779a429 | 2015-03-13 16:30:14 -0700 | [diff] [blame] | 55 | if __name__ == '__main__': |
Anthony Sottile | 39ab2ed | 2021-10-23 13:23:50 -0400 | [diff] [blame] | 56 | raise SystemExit(main()) |