1
0

check_crlf.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. import subprocess
  3. from typing import Iterable
  4. def main() -> int:
  5. any_failed = False
  6. for file_name in get_text_files():
  7. if is_file_crlf(file_name):
  8. print(f"::error file={file_name},title=File contains CRLF line endings::The file '{file_name}' was committed with CRLF new lines. Please make sure your git client is configured correctly and you are not uploading files directly to GitHub via the web interface.")
  9. any_failed = True
  10. return 1 if any_failed else 0
  11. def get_text_files() -> Iterable[str]:
  12. # https://stackoverflow.com/a/24350112/4678631
  13. process = subprocess.run(
  14. ["git", "grep", "--cached", "-Il", ""],
  15. check=True,
  16. encoding="utf-8",
  17. stdout=subprocess.PIPE)
  18. for x in process.stdout.splitlines():
  19. yield x.strip()
  20. def is_file_crlf(path: str) -> bool:
  21. # https://stackoverflow.com/a/29697732/4678631
  22. with open(path, "rb") as f:
  23. for line in f:
  24. if line.endswith(b"\r\n"):
  25. return True
  26. return False
  27. exit(main())