From 5070cb0f522478cada0683fb9a86cf2ef6340765 Mon Sep 17 00:00:00 2001 From: William Throwe Date: Fri, 10 Jan 2025 19:47:22 -0500 Subject: [PATCH] Use git-clang-format corresponding to clang-format --- cmake/FindClangFormat.cmake | 12 ++++++++++++ tools/Hooks/pre-commit.sh | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmake/FindClangFormat.cmake b/cmake/FindClangFormat.cmake index 424da9fa30bf..eb97a483f814 100644 --- a/cmake/FindClangFormat.cmake +++ b/cmake/FindClangFormat.cmake @@ -38,6 +38,18 @@ if (CLANG_FORMAT_BIN) string(REGEX REPLACE "clang-format version " "" CLANG_FORMAT_VERSION ${CLANG_FORMAT_VERSION}) endif() + + # Look for a git-clang-format program in the same place as + # clang-format. Versions of git-clang-format are not always + # compatible with other clang-format executables. + get_filename_component(CLANG_FORMAT_DIR ${CLANG_FORMAT_BIN} DIRECTORY) + get_filename_component(CLANG_FORMAT_NAME ${CLANG_FORMAT_BIN} NAME) + find_program( + GIT_CLANG_FORMAT_BIN + NAMES git-${CLANG_FORMAT_NAME} + HINTS ${CLANG_FORMAT_DIR} + NO_DEFAULT_PATH + ) endif() diff --git a/tools/Hooks/pre-commit.sh b/tools/Hooks/pre-commit.sh index 210b9494c860..b2be66e3610e 100755 --- a/tools/Hooks/pre-commit.sh +++ b/tools/Hooks/pre-commit.sh @@ -33,10 +33,10 @@ printf '%s\0' "${commit_files[@]}" | run_checks "${standard_checks[@]}" ############################################################################### # Use git-clang-format to check for any suspicious formatting of code. -@CLANG_FORMAT_BIN@ --version > /dev/null +@GIT_CLANG_FORMAT_BIN@ --help > /dev/null if [ $? -eq 0 ]; then - clang_format_diff=$(@GIT_EXECUTABLE@ --no-pager \ - clang-format --binary @CLANG_FORMAT_BIN@ --diff --quiet) + clang_format_diff=$( + @GIT_CLANG_FORMAT_BIN@ --binary @CLANG_FORMAT_BIN@ --diff --quiet) # Clang-format didn't always return the right exit code before version 15, # so we check the diff output instead (see issue: # https://github.com/llvm/llvm-project/issues/54758)