From 307d578d739eb254ef3000fdde94271af9b8923e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 30 Jan 2022 12:02:58 +0100 Subject: Adding upstream version 4.1.0. Signed-off-by: Daniel Baumann --- pre_commit_hooks/check_merge_conflict.py | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 pre_commit_hooks/check_merge_conflict.py (limited to 'pre_commit_hooks/check_merge_conflict.py') diff --git a/pre_commit_hooks/check_merge_conflict.py b/pre_commit_hooks/check_merge_conflict.py new file mode 100644 index 0000000..aed1e9c --- /dev/null +++ b/pre_commit_hooks/check_merge_conflict.py @@ -0,0 +1,54 @@ +import argparse +import os.path +from typing import Optional +from typing import Sequence + +from pre_commit_hooks.util import cmd_output + + +CONFLICT_PATTERNS = [ + b'<<<<<<< ', + b'======= ', + b'=======\n', + b'>>>>>>> ', +] + + +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: Optional[Sequence[str]] = 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: + for i, line in enumerate(inputfile): + for pattern in CONFLICT_PATTERNS: + if line.startswith(pattern): + print( + f'Merge conflict string "{pattern.decode()}" ' + f'found in {filename}:{i + 1}', + ) + retcode = 1 + + return retcode + + +if __name__ == '__main__': + raise SystemExit(main()) -- cgit v1.2.3