From 78c1e11bef8eacf8beca3fb8c18bdf5cc5465dc5 Mon Sep 17 00:00:00 2001 From: Null <56081759+NullWanderer@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:05:32 +0000 Subject: [PATCH] Add CLRF workflow --- .github/workflows/check-crlf.yml | 15 +++++++++++++ Tools/check_crlf.py | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 .github/workflows/check-crlf.yml create mode 100755 Tools/check_crlf.py diff --git a/.github/workflows/check-crlf.yml b/.github/workflows/check-crlf.yml new file mode 100644 index 00000000000..0afcab734ff --- /dev/null +++ b/.github/workflows/check-crlf.yml @@ -0,0 +1,15 @@ +name: CRLF Check + +on: + pull_request: + types: [ opened, reopened, synchronize, ready_for_review ] + +jobs: + build: + name: CRLF Check + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3.6.0 + - name: Check for CRLF + run: Tools/check_crlf.py diff --git a/Tools/check_crlf.py b/Tools/check_crlf.py new file mode 100755 index 00000000000..d691e15c703 --- /dev/null +++ b/Tools/check_crlf.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import subprocess +from typing import Iterable + +def main() -> int: + any_failed = False + for file_name in get_text_files(): + if is_file_crlf(file_name): + 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.") + any_failed = True + + return 1 if any_failed else 0 + + +def get_text_files() -> Iterable[str]: + # https://stackoverflow.com/a/24350112/4678631 + process = subprocess.run( + ["git", "grep", "--cached", "-Il", ""], + check=True, + encoding="utf-8", + stdout=subprocess.PIPE) + + for x in process.stdout.splitlines(): + yield x.strip() + +def is_file_crlf(path: str) -> bool: + # https://stackoverflow.com/a/29697732/4678631 + with open(path, "rb") as f: + for line in f: + if line.endswith(b"\r\n"): + return True + + return False + +exit(main())