Dactyloidae/security/nss/automation/clang-format/run_clang_format.sh

68 lines
1.7 KiB
Bash
Raw Normal View History

2018-02-06 11:46:26 +01:00
#!/usr/bin/env bash
if [[ $(id -u) -eq 0 ]]; then
# Drop privileges by re-running this script.
# Note: this mangles arguments, better to avoid running scripts as root.
exec su worker -c "$0 $*"
fi
2018-02-23 11:04:39 +01:00
set -e
2018-02-06 11:46:26 +01:00
# Apply clang-format on the provided folder and verify that this doesn't change any file.
# If any file differs after formatting, the script eventually exits with 1.
# Any differences between formatted and unformatted files is printed to stdout to give a hint what's wrong.
# Includes a default set of directories NOT to clang-format on.
2026-06-29 21:29:25 +01:00
blocklist=(
2018-02-06 11:46:26 +01:00
"./automation" \
"./coreconf" \
"./doc" \
"./pkg" \
"./tests" \
"./lib/libpkix" \
"./lib/zlib" \
"./lib/sqlite" \
"./gtests/google_test" \
"./out" \
)
2018-02-23 11:04:39 +01:00
top=$(cd "$(dirname $0)/../.."; pwd -P)
2018-02-06 11:46:26 +01:00
if [ $# -gt 0 ]; then
dirs=("$@")
else
2018-02-23 11:04:39 +01:00
cd "$top"
dirs=($(find . -maxdepth 2 -mindepth 1 -type d ! -path '*/.*' -print))
2018-02-06 11:46:26 +01:00
fi
format_folder()
{
2026-06-29 21:29:25 +01:00
for block in "${blocklist[@]}"; do
if [[ "$1" == "$block"* ]]; then
2018-02-06 11:46:26 +01:00
echo "skip $1"
return 1
fi
done
return 0
}
for dir in "${dirs[@]}"; do
2018-02-23 11:04:39 +01:00
if format_folder "$dir"; then
2018-02-06 11:46:26 +01:00
c="${dir//[^\/]}"
echo "formatting $dir ..."
2018-02-23 11:04:39 +01:00
depth=()
2018-02-06 11:46:26 +01:00
if [ "${#c}" == "1" ]; then
2018-02-23 11:04:39 +01:00
depth+=(-maxdepth 1)
2018-02-06 11:46:26 +01:00
fi
2026-06-29 21:29:25 +01:00
find "$dir" "${depth[@]}" -type f \( -name '*.[ch]' -o -name '*.cc' \) -exec clang-format -sort-includes=false -i {} \+
2018-02-06 11:46:26 +01:00
fi
done
TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXX)
2018-02-23 11:04:39 +01:00
trap 'rm -f $TMPFILE' exit
if [[ -d "$top/.hg" ]]; then
2018-02-06 11:46:26 +01:00
hg diff --git "$top" | tee $TMPFILE
else
git -C "$top" diff | tee $TMPFILE
fi
[[ ! -s $TMPFILE ]]