From f8a42ef0b3bf3665448491e3c8067df2e59b160f Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sun, 18 Sep 2022 20:10:57 +0200 Subject: [PATCH] Meta: Only check changed files in check-style.py during pre-commit This speeds up the script from about 90ms down to about 10ms, for reasonably common changesets. 80ms may not feel like much, but it adds up quickly, especially since we run a dozen scripts during pre-commit. --- Meta/check-style.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Meta/check-style.py b/Meta/check-style.py index 25b9d93165e..5baf25a8578 100755 --- a/Meta/check-style.py +++ b/Meta/check-style.py @@ -37,18 +37,33 @@ PRAGMA_ONCE_CHECK_EXCLUDES = { # We make sure that there's a blank line before and after pragma once GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)') -GIT_LS_FILES = ['git', 'ls-files', '--', '*.cpp', '*.h', ':!:Base', ':!:Kernel/FileSystem/ext2_fs.h'] + +def should_check_file(filename): + if not filename.endswith('.cpp') and not filename.endswith('.h'): + return False + if filename.startswith('Base/'): + return False + if filename == 'Kernel/FileSystem/ext2_fs.h': + return False + return True + + +def find_files_here_or_argv(): + if len(sys.argv) > 1: + raw_list = sys.argv[1:] + else: + process = subprocess.run(["git", "ls-files"], check=True, capture_output=True) + raw_list = process.stdout.decode().strip('\n').split('\n') + + return filter(should_check_file, raw_list) def run(): - files = subprocess.run(GIT_LS_FILES, check=True, capture_output=True).stdout.decode().strip('\n').split('\n') - assert len(files) > 1000 - errors_license = [] errors_pragma_once_bad = [] errors_pragma_once_missing = [] - for filename in files: + for filename in find_files_here_or_argv(): with open(filename, "r") as f: file_content = f.read() if not any(filename.startswith(forbidden_prefix) for forbidden_prefix in LICENSE_HEADER_CHECK_EXCLUDES):