From d534651ac40ddf67710031f10eaa8a3420dbc0c2 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 7 Sep 2018 10:38:58 -0600 Subject: [PATCH] check keywords in diff --- Scripts/precommit.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/Scripts/precommit.py b/Scripts/precommit.py index 4b8a2df16..08a21261a 100755 --- a/Scripts/precommit.py +++ b/Scripts/precommit.py @@ -313,7 +313,7 @@ def process(filepath): filename = os.path.basename(filepath) if filename.startswith('.'): - return + raise "shouldn't call process with dotfile" file_ext = os.path.splitext(filename)[1] if file_ext in ('.swift'): env_copy = os.environ.copy() @@ -376,7 +376,7 @@ def should_ignore_path(path): return False - + def process_if_appropriate(filepath): filename = os.path.basename(filepath) if filename.startswith('.'): @@ -388,7 +388,33 @@ def process_if_appropriate(filepath): return process(filepath) - + +def check_diff_for_keywords(): + keywords = ["OWSAssert\(", "OWSFail\(", "ows_add_overflow\(", "ows_sub_overflow\("] + matching_expression = "|".join(keywords) + command_line = 'git diff --staged | grep --color=always -C 3 -E "%s"' % matching_expression + print(command_line) + try: + output = subprocess.check_output(command_line, shell=True) + except subprocess.CalledProcessError, e: + # > man grep + # EXIT STATUS + # The grep utility exits with one of the following values: + # 0 One or more lines were selected. + # 1 No lines were selected. + # >1 An error occurred. + if e.returncode == 1: + # no keywords in diff output + return + else: + # some other error - bad grep expression? + raise e + + if len(output) > 0: + print("⚠️ keywords detected in diff:") + print(output) + + if __name__ == "__main__": parser = argparse.ArgumentParser(description='Precommit script.') @@ -426,3 +452,5 @@ if __name__ == "__main__": print 'git clang-format...' print commands.getoutput('git clang-format') + + check_diff_for_keywords()