From 67109647b99727f643378c71177a528e239f94e7 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 15 Jul 2026 03:17:59 +0200 Subject: [PATCH] chore(format): use clang-format 20 --- .github/workflows/clang-format.yml | 14 +++++++++---- scripts/clang-format-common.sh | 32 ++++++++++++++++++++++++++++++ scripts/format-check.sh | 5 ++++- scripts/format.sh | 7 +++++-- 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 scripts/clang-format-common.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index ea07d8b..dc71faa 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -8,15 +8,21 @@ on: jobs: format-check: runs-on: ubuntu-latest + env: + CLANG_FORMAT_VERSION: "20" + CLANG_FORMAT_PYPI_VERSION: "20.1.8" + CLANG_FORMAT_INSTALL_DIR: "clang-format-venv" steps: - name: Checkout uses: actions/checkout@v4 - - name: Install clang-format 17 + - name: Install clang-format 20 run: | - sudo apt-get update - sudo apt-get install -y clang-format-17 - sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100 + python3 -m venv "${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}" + "${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/python" -m pip install --upgrade pip + "${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/python" -m pip install "clang-format==${CLANG_FORMAT_PYPI_VERSION}" + echo "CLANG_FORMAT=${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/clang-format" >> "${GITHUB_ENV}" + "${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/clang-format" --version - name: Run format-check run: ./scripts/format-check.sh diff --git a/scripts/clang-format-common.sh b/scripts/clang-format-common.sh new file mode 100644 index 0000000..a27f056 --- /dev/null +++ b/scripts/clang-format-common.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +CLANG_FORMAT_VERSION="${CLANG_FORMAT_VERSION:-20}" +CLANG_FORMAT="${CLANG_FORMAT:-clang-format-${CLANG_FORMAT_VERSION}}" + +resolve_clang_format() { + if [[ ! "${CLANG_FORMAT_VERSION}" =~ ^[0-9]+$ ]]; then + echo "error: CLANG_FORMAT_VERSION must be a numeric major version." >&2 + return 1 + fi + + local candidate="" + if command -v "${CLANG_FORMAT}" >/dev/null 2>&1; then + candidate="${CLANG_FORMAT}" + elif [ "${CLANG_FORMAT}" = "clang-format-${CLANG_FORMAT_VERSION}" ] && command -v clang-format >/dev/null 2>&1; then + candidate="clang-format" + else + echo "error: clang-format ${CLANG_FORMAT_VERSION}.x was not found." >&2 + echo "Set CLANG_FORMAT to the clang-format binary to use." >&2 + return 1 + fi + + local version_output + version_output="$("${candidate}" --version)" + if [[ ! "${version_output}" =~ (^|[[:space:]])version[[:space:]]+${CLANG_FORMAT_VERSION}\. ]]; then + echo "error: expected clang-format ${CLANG_FORMAT_VERSION}.x, got: ${version_output}" >&2 + echo "Set CLANG_FORMAT_VERSION to override the required major version." >&2 + return 1 + fi + + printf '%s\n' "${candidate}" +} diff --git a/scripts/format-check.sh b/scripts/format-check.sh index 35ac7f0..5335a7d 100755 --- a/scripts/format-check.sh +++ b/scripts/format-check.sh @@ -3,6 +3,9 @@ set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" +source "${SCRIPT_DIR}/clang-format-common.sh" + +CLANG_FORMAT_BIN="$(resolve_clang_format)" files=() while IFS= read -r -d '' file; do @@ -19,7 +22,7 @@ fi echo "Checking formatting on ${#files[@]} files..." failed=0 for file in "${files[@]}"; do - if ! clang-format --dry-run --Werror "${file}"; then + if ! "${CLANG_FORMAT_BIN}" --dry-run --Werror "${file}"; then failed=1 fi done diff --git a/scripts/format.sh b/scripts/format.sh index 1f595b4..7e73ad4 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -3,6 +3,9 @@ set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" +source "${SCRIPT_DIR}/clang-format-common.sh" + +CLANG_FORMAT_BIN="$(resolve_clang_format)" files=() while IFS= read -r -d '' file; do @@ -16,5 +19,5 @@ if [ "${#files[@]}" -eq 0 ]; then exit 0 fi -echo "Formatting ${#files[@]} files with clang-format (style from ${REPO_ROOT}/.clang-format)..." -clang-format -i "${files[@]}" +echo "Formatting ${#files[@]} files with ${CLANG_FORMAT_BIN} (style from ${REPO_ROOT}/.clang-format)..." +"${CLANG_FORMAT_BIN}" -i "${files[@]}"