summaryrefslogtreecommitdiff
path: root/Meta/check-newlines-at-eof.py
blob: 07445fc4ab7c6f1ba9226efe505e9fa374c206c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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."""
    no_newline_at_eof_errors = []
    blank_lines_at_eof_errors = []

    did_fail = False
    for filename in find_files_here_or_argv():
        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':
                did_fail = True
                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)


if __name__ == '__main__':
    os.chdir(os.path.dirname(__file__) + "/..")
    run()