check-newlines-at-eof.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. import subprocess
  5. import sys
  6. RE_RELEVANT_FILE_EXTENSION = re.compile('\\.(cpp|h|mm|swift|gml|html|js|css|sh|py|json|txt|cmake|gn|gni)$')
  7. def should_check_file(filename):
  8. if not RE_RELEVANT_FILE_EXTENSION.search(filename):
  9. return False
  10. if filename.startswith('Tests/LibWeb/Layout/'):
  11. return False
  12. if filename.endswith('.txt'):
  13. return 'CMake' in filename
  14. return True
  15. def find_files_here_or_argv():
  16. if len(sys.argv) > 1:
  17. raw_list = sys.argv[1:]
  18. else:
  19. process = subprocess.run(["git", "ls-files"], check=True, capture_output=True)
  20. raw_list = process.stdout.decode().strip('\n').split('\n')
  21. return filter(should_check_file, raw_list)
  22. def run():
  23. """Check files checked in to git for trailing newlines at end of file."""
  24. no_newline_at_eof_errors = []
  25. blank_lines_at_eof_errors = []
  26. did_fail = False
  27. for filename in find_files_here_or_argv():
  28. with open(filename, "r") as f:
  29. f.seek(0, os.SEEK_END)
  30. f.seek(f.tell() - 1, os.SEEK_SET)
  31. if f.read(1) != '\n':
  32. did_fail = True
  33. no_newline_at_eof_errors.append(filename)
  34. continue
  35. while True:
  36. f.seek(f.tell() - 2, os.SEEK_SET)
  37. char = f.read(1)
  38. if not char.isspace():
  39. break
  40. if char == '\n':
  41. did_fail = True
  42. blank_lines_at_eof_errors.append(filename)
  43. break
  44. if no_newline_at_eof_errors:
  45. print("Files with no newline at the end:", " ".join(no_newline_at_eof_errors))
  46. if blank_lines_at_eof_errors:
  47. print("Files that have blank lines at the end:", " ".join(blank_lines_at_eof_errors))
  48. if did_fail:
  49. sys.exit(1)
  50. if __name__ == '__main__':
  51. os.chdir(os.path.dirname(__file__) + "/..")
  52. run()