From fe3cd4b0cb40474a632b1513b577cd09fe5fef1d Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 22 May 2024 14:26:50 +0200 Subject: [PATCH] Add check for missing or duplicate newlines at the end of files --- .github/workflows/checks.yml | 1 + Makefile | 8 ++++++-- Tools/astyle/check_newlines.sh | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 Tools/astyle/check_newlines.sh diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index d977aee7c1..a2d7aa5f97 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -16,6 +16,7 @@ jobs: matrix: check: [ "check_format", + "check_newlines", "tests", "tests_coverage", "px4_fmu-v2_default stack_check", diff --git a/Makefile b/Makefile index 2d4116d73b..fe8580402d 100644 --- a/Makefile +++ b/Makefile @@ -379,9 +379,9 @@ doxygen: @$(PX4_MAKE) -C "$(SRC_DIR)"/build/doxygen @touch "$(SRC_DIR)"/build/doxygen/Documentation/.nojekyll -# Astyle +# Style # -------------------------------------------------------------------- -.PHONY: check_format format +.PHONY: check_format format check_newlines check_format: $(call colorecho,'Checking formatting with astyle') @@ -392,6 +392,10 @@ format: $(call colorecho,'Formatting with astyle') @"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix +check_newlines: + $(call colorecho,'Checking for missing or duplicate newlines at the end of files') + @"$(SRC_DIR)"/Tools/astyle/check_newlines.sh + # Testing # -------------------------------------------------------------------- .PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance diff --git a/Tools/astyle/check_newlines.sh b/Tools/astyle/check_newlines.sh new file mode 100755 index 0000000000..5160476935 --- /dev/null +++ b/Tools/astyle/check_newlines.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +return_value=0 + +# Check if there are files checked in that don't end in a newline (POSIX requirement) +git grep --cached -Il '' | xargs -L1 bash -c 'if test "$(tail -c 1 "$0")"; then echo "No new line at end of $0"; exit 1; fi' + +if [ $? -ne 0 ]; then + return_value=1 +fi + +# Check if there are files checked in that have duplicate newlines at the end (fail trailing whitespace checks) +git grep --cached -Il '' | xargs -L1 bash -c 'if tail -c 2 "$0" | ( read x && read y && [ x"$x" = x ] && [ x"$y" = x ]); then echo "Multiple newlines at the end of $0"; exit 1; fi' + +if [ $? -ne 0 ]; then + return_value=1 +fi + +exit $return_value