Add check for missing or duplicate newlines at the end of files

This commit is contained in:
Matthias Grob
2024-05-22 14:26:50 +02:00
parent 1b9f1b78e5
commit fe3cd4b0cb
3 changed files with 26 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ jobs:
matrix:
check: [
"check_format",
"check_newlines",
"tests",
"tests_coverage",
"px4_fmu-v2_default stack_check",

View File

@@ -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

19
Tools/astyle/check_newlines.sh Executable file
View File

@@ -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