-
Notifications
You must be signed in to change notification settings - Fork 20
/
fix_newlines.py
executable file
·35 lines (30 loc) · 1.19 KB
/
fix_newlines.py
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
#!/usr/bin/env python3
from pathlib import Path
from typing import Collection
import argparse
import io
import subprocess
import sys
def main(exclude_patterns: Collection[str]) -> int:
has_fixed_a_file = False
for filename in map(Path, subprocess.check_output(["git", "ls-files"]).decode().splitlines()):
should_skip_file = False
for exclude_pattern in exclude_patterns:
if filename.match(exclude_pattern):
should_skip_file = True
if should_skip_file:
continue
with filename.open("r+b") as file:
file.seek(0, io.SEEK_END)
if file.tell() > 0:
file.seek(-1, io.SEEK_END)
if file.read() != b"\n":
print(f"Fixed missing newline at end of {filename!s}")
file.write(b"\n")
has_fixed_a_file = True
return 1 if has_fixed_a_file else 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Enforce trailing newlines")
parser.add_argument("--exclude", dest="exclude", help="file pattern to exclude", action="append")
args = parser.parse_args()
sys.exit(main(exclude_patterns=args.exclude))