Jelajahi Sumber

Merge pull request #45141 from Calinou/clang-format-hook-check-version

Check for clang-format version in the pre-commit hook
Rémi Verschelde 4 tahun lalu
induk
melakukan
6525662f27
1 mengubah file dengan 18 tambahan dan 4 penghapusan
  1. 18 4
      misc/hooks/pre-commit-clang-format

+ 18 - 4
misc/hooks/pre-commit-clang-format

@@ -74,25 +74,39 @@ else
     against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
     against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 fi
 fi
 
 
+# To get consistent formatting, we recommend contributors to use the same
+# clang-format version as CI.
+RECOMMENDED_CLANG_FORMAT_MAJOR="11"
+
 if [ ! -x "$CLANG_FORMAT" ] ; then
 if [ ! -x "$CLANG_FORMAT" ] ; then
+	message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR.x.x."
+
     if [ ! -t 1 ] ; then
     if [ ! -t 1 ] ; then
         if [ -x "$ZENITY" ] ; then
         if [ -x "$ZENITY" ] ; then
-            $ZENITY --error --title="Error" --text="Error: clang-format executable not found."
+            $ZENITY --error --title="Error" --text="$message"
             exit 1
             exit 1
         elif [ -x "$XMSG" ] ; then
         elif [ -x "$XMSG" ] ; then
-            $XMSG -center -title "Error" "Error: clang-format executable not found."
+            $XMSG -center -title "Error" "$message"
             exit 1
             exit 1
         elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
         elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
             winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
             winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
-            $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "Error: clang-format executable not found."
+            $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "$message"
             exit 1
             exit 1
         fi
         fi
     fi
     fi
-    printf "Error: clang-format executable not found.\n"
+    printf "$message\n"
     printf "Set the correct path in $(canonicalize_filename "$0").\n"
     printf "Set the correct path in $(canonicalize_filename "$0").\n"
     exit 1
     exit 1
 fi
 fi
 
 
+CLANG_FORMAT_VERSION="$(clang-format --version | cut -d' ' -f3)"
+CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d'.' -f1)"
+
+if [ "$CLANG_FORMAT_MAJOR" != "$RECOMMENDED_CLANG_FORMAT_MAJOR" ]; then
+    echo "Warning: Your clang-format binary is the wrong version ($CLANG_FORMAT_VERSION, expected $CLANG_FORMAT_MAJOR.x.x)."
+    echo "         Consider upgrading or downgrading clang-format as formatting may not be applied correctly."
+fi
+
 # create a random filename to store our generated patch
 # create a random filename to store our generated patch
 prefix="pre-commit-clang-format"
 prefix="pre-commit-clang-format"
 suffix="$(date +%s)"
 suffix="$(date +%s)"