diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2022-09-18 20:10:57 +0200 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2022-09-18 18:45:25 -0700 |
commit | de581ca5fdeb7006f0dd23a2f63310a65159caa5 (patch) | |
tree | 23a7cd4a53817f7188c274928b0f2861c01a5d07 /Meta/check-newlines-at-eof.py | |
parent | 4caaa78bafbe104cc249a0bb8a6cf3a88f498555 (diff) | |
download | serenity-de581ca5fdeb7006f0dd23a2f63310a65159caa5.zip |
Meta: Only check changed files in check-newlines-at-eof
This speeds up the script from about 120ms down to about 20ms for
reasonably common changesets.
100ms may not feel like much, but it adds up quickly, especially since
we run a dozen scripts during pre-commit.
Diffstat (limited to 'Meta/check-newlines-at-eof.py')
-rwxr-xr-x | Meta/check-newlines-at-eof.py | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/Meta/check-newlines-at-eof.py b/Meta/check-newlines-at-eof.py index c65620bbed..07445fc4ab 100755 --- a/Meta/check-newlines-at-eof.py +++ b/Meta/check-newlines-at-eof.py @@ -1,40 +1,47 @@ #!/usr/bin/env python3 import os +import re import subprocess import sys +RE_RELEVANT_FILE_EXTENSION = re.compile('\\.(cpp|h|gml|html|js|css|sh|py|json|txt)$') + + +def should_check_file(filename): + if not RE_RELEVANT_FILE_EXTENSION.search(filename): + return False + if filename.startswith('Userland/Libraries/LibCodeComprehension/Cpp/Tests/'): + return False + if filename.startswith('Userland/Libraries/LibCpp/Tests/parser/'): + return False + if filename.startswith('Userland/Libraries/LibCpp/Tests/preprocessor/'): + return False + if filename == 'Kernel/FileSystem/ext2_fs.h': + return False + if filename.endswith('.txt'): + return 'CMake' in filename + 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(): """Check files checked in to git for trailing newlines at end of file.""" - files = subprocess.run( - [ - "git", "ls-files", "--", - "*.cpp", - "*.h", - "*.gml", - "*.html", - "*.js", - "*.css", - "*.sh", - "*.py", - "*.json", - "CMake*.txt", - "**/CMake*.txt", - ":!:Kernel/FileSystem/ext2_fs.h", - ':!:Userland/Libraries/LibCodeComprehension/Cpp/Tests/*', - ':!:Userland/Libraries/LibCpp/Tests/parser/*', - ':!:Userland/Libraries/LibCpp/Tests/preprocessor/*' - ], - check=True, - capture_output=True - ).stdout.decode().strip('\n').split('\n') - no_newline_at_eof_errors = [] blank_lines_at_eof_errors = [] did_fail = False - for filename in files: + for filename in find_files_here_or_argv(): with open(filename, "r") as f: f.seek(0, os.SEEK_END) |