Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: sync grass_clang_format script to pre-commit config #4186

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions utils/grass_clang_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ set -eu
# It is also possible to format the content in a (one) given directory:
# ./utils/grass_clang_format.sh ./lib/raster
#
# COPYRIGHT: (C) 2023 by the GRASS Development Team
# COPYRIGHT: (C) 2023-2024 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
###############################################################################

# Check if variable is integer (POSIX compliant)
# based on https://unix.stackexchange.com/a/598047
is_integer ()
{
case "${1#[+-]}" in
(*[!0123456789]*) echo 1 ;;
('') echo 1 ;;
(*) echo 0 ;;
esac
}

# Required clang-format version
req_cf_v="15"
req_cf_v="18"

# No need to continue if the .clang-format file isn't found
if [ ! -f .clang-format ]; then
Expand All @@ -52,6 +63,17 @@ if ! (${fmt} --version >/dev/null); then
exit 1
fi

# Try extract ClangFormat version from pre-commit config file
pre_commit_config_file=".pre-commit-config.yaml"
pre_commit_version=$(grep -A 1 --regex "repo:.*clang-format" \
"${pre_commit_config_file}" | sed -En 's/.*rev: v([0-9]+)\..*/\1/p')
if [ "$(is_integer "$pre_commit_version")" -eq "1" ]; then
echo "Warning: failed to retrieve ClangFormat version number from"
echo " ${pre_commit_config_file}. Falling back to version {$req_cf_v}."
else
req_cf_v="${pre_commit_version}"
fi

clang_version_full=$(${fmt} --version)
clang_version=$(echo "${clang_version_full}" | \
sed -En 's/.*version ([0-9]+)\.[0-9]+\.[0-9]+.*/\1/p')
Expand Down
Loading