Skip to content

Instantly share code, notes, and snippets.

@ruslo
Created February 6, 2024 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslo/8b0ff93c05a17eabcbdb6854d5d2f0a1 to your computer and use it in GitHub Desktop.
Save ruslo/8b0ff93c05a17eabcbdb6854d5d2f0a1 to your computer and use it in GitHub Desktop.
Script to run ClangFormat
#!/bin/bash
# Template: https://gist.github.com/ruslo/09b3e10a21522d43c354d5eb6299f22d
set -e
if [ ! $# -eq 0 ];
then
echo "Unexpected arguments: '$*'"
exit 1
fi
THIS_SCRIPT_PATH="${BASH_SOURCE:-$0}"
THIS_SCRIPT_DIR="`dirname "${THIS_SCRIPT_PATH}"`"
EXPECTED_FILE="${THIS_SCRIPT_DIR}/check_format.sh"
if [ ! -r "${EXPECTED_FILE}" ];
then
echo "File not found: ${EXPECTED_FILE}"
exit 1
fi
EXPECTED_CFG_FILE="${THIS_SCRIPT_DIR}/.clang-format"
if [ ! -r "${EXPECTED_CFG_FILE}" ];
then
echo "File not found: ${EXPECTED_CFG_FILE}"
exit 1
fi
cd "${THIS_SCRIPT_DIR}"
echo "Working directory: `pwd`"
which clang-format
clang-format --version
TEMP_FILE=/tmp/__clang_format_check.txt
echo "Using temp file: ${TEMP_FILE}"
for to_check in `find . \
-type f \( -name '*.cpp' -or -name '*.h' \) \
! -path './_builds/*' \
! -path './third_party/*' \
! -path './_deps/*' \
! -path '*/_venv/*' \
! -path "${HOME}/opt"`;
do
echo "Checking ${to_check} ..."
clang-format -style=file ${to_check} > ${TEMP_FILE}
# https://stackoverflow.com/a/55157772
if ! output=$(diff ${to_check} ${TEMP_FILE});
then
echo "${output}"
echo "---"
echo "Files differ: ${to_check} ${TEMP_FILE}"
exit 1
fi
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment