2021-01-09 13:38:10 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-01-03 18:31:44 +00:00
|
|
|
|
|
|
|
import os
|
2022-09-18 18:10:57 +00:00
|
|
|
import re
|
2021-01-03 18:31:44 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2021-04-02 09:26:37 +00:00
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
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
|
2022-10-24 07:02:37 +00:00
|
|
|
if filename == 'Kernel/FileSystem/Ext2FS/Definitions.h':
|
2022-09-18 18:10:57 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-04-02 09:26:37 +00:00
|
|
|
def run():
|
|
|
|
"""Check files checked in to git for trailing newlines at end of file."""
|
|
|
|
no_newline_at_eof_errors = []
|
|
|
|
blank_lines_at_eof_errors = []
|
|
|
|
|
|
|
|
did_fail = False
|
2022-09-18 18:10:57 +00:00
|
|
|
for filename in find_files_here_or_argv():
|
2021-04-02 09:26:37 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
f.seek(0, os.SEEK_END)
|
|
|
|
|
|
|
|
f.seek(f.tell() - 1, os.SEEK_SET)
|
|
|
|
if f.read(1) != '\n':
|
2021-01-03 18:31:44 +00:00
|
|
|
did_fail = True
|
2021-04-02 09:26:37 +00:00
|
|
|
no_newline_at_eof_errors.append(filename)
|
|
|
|
continue
|
|
|
|
|
|
|
|
while True:
|
|
|
|
f.seek(f.tell() - 2, os.SEEK_SET)
|
|
|
|
char = f.read(1)
|
|
|
|
if not char.isspace():
|
|
|
|
break
|
|
|
|
if char == '\n':
|
|
|
|
did_fail = True
|
|
|
|
blank_lines_at_eof_errors.append(filename)
|
|
|
|
break
|
|
|
|
|
|
|
|
if no_newline_at_eof_errors:
|
|
|
|
print("Files with no newline at the end:", " ".join(no_newline_at_eof_errors))
|
|
|
|
if blank_lines_at_eof_errors:
|
|
|
|
print("Files that have blank lines at the end:", " ".join(blank_lines_at_eof_errors))
|
|
|
|
|
|
|
|
if did_fail:
|
|
|
|
sys.exit(1)
|
2021-01-03 18:31:44 +00:00
|
|
|
|
|
|
|
|
2021-04-02 09:26:37 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
os.chdir(os.path.dirname(__file__) + "/..")
|
|
|
|
run()
|