Loading scripts/check_format.sh 0 → 100644 +103 −0 Original line number Diff line number Diff line #!/bin/bash PROJECT_SRC="$(pwd)/src" PROJECT_INCLUDE="$(pwd)/include" RUN_FORMAT=false usage_short() { echo " usage: check_format.sh [-h] [-s <PROJECT_SRC>] [-i <PROJECT_INCLUDE>] " } help_msg() { usage_short echo " This script checks if the code is formatted correctly by the rules set in .clang-format optional arguments: -h, --help shows this help message and exits -s <PROJECT_SRC>, --src <PROJECT_SRC> path to the src/ directory of the project (default: 'pwd/src') -i <PROJECT_INCLUDE>, --include <PROJECT_INCLUDE> path to the include/ directory of the project (default: 'pwd/include') -r, --run_format run clang-formatter before formatting check DISCLAIMER: FILES ARE MODIFIED IN PLACE! " } POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" case ${key} in -s | --src) PROJECT_SRC="$(readlink -mn "${2}")" shift # past argument shift # past value ;; -i | --include) PROJECT_INCLUDE="$(readlink -mn "${2}")" shift # past argument shift # past value ;; -r | --run_format) RUN_FORMAT=true shift # past argument ;; -h | --help) help_msg exit ;; *) # unknown option POSITIONAL+=("$1") # save it in an array for later shift # past argument ;; esac done set -- "${POSITIONAL[@]}" # restore positional parameters FAIL=false echo "* Source directory: $PROJECT_SRC" if [[ ! -d "$PROJECT_SRC" ]]; then echo "*** ERR: $PROJECT_SRC does not exist." exit 1 fi echo "* Include directory: $PROJECT_INCLUDE" if [[ ! -d "$PROJECT_INCLUDE" ]]; then echo "*** ERR: $PROJECT_INCLUDE does not exist." exit 1 fi echo "* Checking for unformatted files ..." if [[ "$RUN_FORMAT" == true ]]; then echo "*** Running clang-format on all files and formatting them in place." echo "* The following files and number of lines were formatted:" else echo "* The following files and number of lines need to be formatted:" fi echo "------------------------------------------" while IFS= read -r -d '' FILE; do UNFORMATTED_LINES=$(diff -u <(cat "$FILE") <(clang-format -style=file "$FILE") | wc -l) if ((UNFORMATTED_LINES > 0)); then if [[ "$RUN_FORMAT" == true ]]; then echo "*** Reformatting $UNFORMATTED_LINES lines in '$FILE'" clang-format -i -style=file "$FILE" else echo -n "$FILE " echo "$UNFORMATTED_LINES" FAIL=true fi fi done < <(find "$PROJECT_SRC" "$PROJECT_INCLUDE" -type f \( -name "*.cpp" -o -name "*.hpp" \) -print0) echo "------------------------------------------" if [[ "$FAIL" == true ]]; then echo "* Format check failed! Format your code with 'clang-format -style=file' or pass '-r' to reformat files in place." exit 1 fi echo "* Format check successful." Loading
scripts/check_format.sh 0 → 100644 +103 −0 Original line number Diff line number Diff line #!/bin/bash PROJECT_SRC="$(pwd)/src" PROJECT_INCLUDE="$(pwd)/include" RUN_FORMAT=false usage_short() { echo " usage: check_format.sh [-h] [-s <PROJECT_SRC>] [-i <PROJECT_INCLUDE>] " } help_msg() { usage_short echo " This script checks if the code is formatted correctly by the rules set in .clang-format optional arguments: -h, --help shows this help message and exits -s <PROJECT_SRC>, --src <PROJECT_SRC> path to the src/ directory of the project (default: 'pwd/src') -i <PROJECT_INCLUDE>, --include <PROJECT_INCLUDE> path to the include/ directory of the project (default: 'pwd/include') -r, --run_format run clang-formatter before formatting check DISCLAIMER: FILES ARE MODIFIED IN PLACE! " } POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" case ${key} in -s | --src) PROJECT_SRC="$(readlink -mn "${2}")" shift # past argument shift # past value ;; -i | --include) PROJECT_INCLUDE="$(readlink -mn "${2}")" shift # past argument shift # past value ;; -r | --run_format) RUN_FORMAT=true shift # past argument ;; -h | --help) help_msg exit ;; *) # unknown option POSITIONAL+=("$1") # save it in an array for later shift # past argument ;; esac done set -- "${POSITIONAL[@]}" # restore positional parameters FAIL=false echo "* Source directory: $PROJECT_SRC" if [[ ! -d "$PROJECT_SRC" ]]; then echo "*** ERR: $PROJECT_SRC does not exist." exit 1 fi echo "* Include directory: $PROJECT_INCLUDE" if [[ ! -d "$PROJECT_INCLUDE" ]]; then echo "*** ERR: $PROJECT_INCLUDE does not exist." exit 1 fi echo "* Checking for unformatted files ..." if [[ "$RUN_FORMAT" == true ]]; then echo "*** Running clang-format on all files and formatting them in place." echo "* The following files and number of lines were formatted:" else echo "* The following files and number of lines need to be formatted:" fi echo "------------------------------------------" while IFS= read -r -d '' FILE; do UNFORMATTED_LINES=$(diff -u <(cat "$FILE") <(clang-format -style=file "$FILE") | wc -l) if ((UNFORMATTED_LINES > 0)); then if [[ "$RUN_FORMAT" == true ]]; then echo "*** Reformatting $UNFORMATTED_LINES lines in '$FILE'" clang-format -i -style=file "$FILE" else echo -n "$FILE " echo "$UNFORMATTED_LINES" FAIL=true fi fi done < <(find "$PROJECT_SRC" "$PROJECT_INCLUDE" -type f \( -name "*.cpp" -o -name "*.hpp" \) -print0) echo "------------------------------------------" if [[ "$FAIL" == true ]]; then echo "* Format check failed! Format your code with 'clang-format -style=file' or pass '-r' to reformat files in place." exit 1 fi echo "* Format check successful."