set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
ROOT="$(realpath "$(dirname "$0")"/../)"
GREEN="\033[32m"
RED="\033[31m"
NORMAL="\033[0m"
info()
{
printf "%b %b\n" "${GREEN}info" "${NORMAL}${*}"
}
error()
{
printf "%b %b\n" "${RED}error" "${NORMAL}${*}"
}
info "Formatting nix files"
if ! command -v nixpkgs-fmt >/dev/null 2>&1; then
error "nixpkgs-fmt not found. Are you running in a nix shell? See CONTRIBUTING.md."
exit 1
fi
nixpkgs-fmt "$ROOT"
info "Formatting shell scripts"
if ! command -v shfmt >/dev/null 2>&1; then
error "shfmt not found. Are you running in a nix shell? See CONTRIBUTING.md."
exit 1
fi
SHELL_SCRIPTS=$(git grep -l '' :/ | xargs printf "'%s' " | xargs -L 1 shfmt -f)
echo $SHELL_SCRIPTS | xargs -L 1 shfmt -s -w -l -i 2 -ci -fn
info "Formatting python scripts with ruff"
if ! command -v ruff >/dev/null 2>&1; then
error "ruff not found. Are you running in a nix shell? See CONTRIBUTING.md."
exit 1
fi
ruff format "$ROOT"
info "Formatting c files"
if ! command -v clang-format >/dev/null 2>&1; then
error "clang-format not found. Are you running in a nix shell? See CONTRIBUTING.md."
exit 1
fi
nproc=$(getconf _NPROCESSORS_ONLN || echo 1)
git ls-files -- ":/*.c" ":/*.h" | xargs -P "$nproc" -I {} sh -c '
# Ignore symlinks
if [[ ! -L {} ]]; then
clang-format -i {}
fi'
info "Expanding tabs"
expand-tabs()
{
git ls-files -- ":/" ":/!:Makefile" ":/!:**/Makefile" ":/!:**/Makefile.*" ":/!:Makefile.*" ":/!:*.mk" ":/!:*.patch" ":/!:nix/valgrind/*.txt" | xargs -P "$nproc" -I {} sh -c '
if [ ! -L {} ] && grep -Pq '"'"'\t'"'"' "{}"; then
tmp=$(mktemp)
expand -t 4 "{}" > "$tmp" && mv "$tmp" "{}"
echo "{}"
fi'
}
expand-tabs
info "Checking for eol"
check-eol()
{
git ls-files -- ":/" ":/!:*.png" | xargs -P "$nproc" -I {} sh -c '
# Ignore symlinks
if [[ ! -L {} && $(tail -c1 "{}" | wc -l) == 0 ]]; then
echo "" >>"{}"
echo "{}"
fi'
}
check-eol