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.
This commit is contained in:
Ben Wiederhake 2022-09-18 20:10:57 +02:00 committed by Brian Gianforcaro
parent 4caaa78baf
commit de581ca5fd
Notes: sideshowbarker 2024-07-17 06:51:07 +09:00

View File

@ -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)