From 1d3943a72baa4f7f43132c6a77b399788f3f51d7 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Fri, 21 Jun 2024 16:39:05 +0800 Subject: [PATCH] CI: Enforce newline at end of files Reference: https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e --- .ci/check-newline.sh | 19 +++++++++++++++++++ .github/workflows/status-check.yaml | 1 + examples/.clang-format | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100755 .ci/check-newline.sh diff --git a/.ci/check-newline.sh b/.ci/check-newline.sh new file mode 100755 index 0000000..6b3b784 --- /dev/null +++ b/.ci/check-newline.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +set -e -u -o pipefail + +ret=0 +show=0 +# Reference: https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e +while IFS= read -rd '' f; do + if file --mime-encoding "$f" | grep -qv binary; then + tail -c1 < "$f" | read -r _ || show=1 + if [ $show -eq 1 ]; then + echo "Warning: No newline at end of file $f" + ret=1 + show=0 + fi + fi +done < <(git ls-files -z examples) + +exit $ret diff --git a/.github/workflows/status-check.yaml b/.github/workflows/status-check.yaml index 8789ba2..c32711a 100644 --- a/.github/workflows/status-check.yaml +++ b/.github/workflows/status-check.yaml @@ -26,6 +26,7 @@ jobs: sudo apt-get install -q -y clang-format-12 sudo apt-get install -q -y cppcheck sudo apt-get install -q -y gcc-11 + .ci/check-newline.sh .ci/check-format.sh .ci/static-analysis.sh .ci/build-n-run.sh diff --git a/examples/.clang-format b/examples/.clang-format index e1e184f..7d37d7c 100644 --- a/examples/.clang-format +++ b/examples/.clang-format @@ -95,4 +95,4 @@ SpacesInParentheses: false SpacesInSquareBrackets: false Standard: Cpp03 TabWidth: 4 -UseTab: Never \ No newline at end of file +UseTab: Never