pre-commit-clang-format 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env bash
  2. # git pre-commit hook that runs a clang-format stylecheck.
  3. # Features:
  4. # - abort commit when commit does not comply with the style guidelines
  5. # - create a patch of the proposed style changes
  6. # Modifications for clang-format by rene.milk@wwu.de
  7. # This file is part of a set of unofficial pre-commit hooks available
  8. # at github.
  9. # Link: https://github.com/githubbrowser/Pre-commit-hooks
  10. # Contact: David Martin, david.martin.mailbox@googlemail.com
  11. # Some quality of life modifications made for Godot Engine.
  12. ##################################################################
  13. # SETTINGS
  14. # Set path to clang-format binary.
  15. CLANG_FORMAT=`which clang-format 2>/dev/null`
  16. # Remove any older patches from previous commits. Set to true or false.
  17. DELETE_OLD_PATCHES=false
  18. # Only parse files with the extensions in FILE_EXTS. Set to true or false.
  19. # If false every changed file in the commit will be parsed with clang-format.
  20. # If true only files matching one of the extensions are parsed with clang-format.
  21. PARSE_EXTS=true
  22. # File types to parse. Only effective when PARSE_EXTS is true.
  23. FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m .mm .inc .java .glsl"
  24. # Use pygmentize instead of cat to parse diff with highlighting.
  25. # Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac)
  26. PYGMENTIZE=`which pygmentize 2>/dev/null`
  27. if [ ! -z "$PYGMENTIZE" ]; then
  28. READER="pygmentize -l diff"
  29. else
  30. READER=cat
  31. fi
  32. # Path to zenity
  33. ZENITY=`which zenity 2>/dev/null`
  34. # Path to xmessage
  35. XMSG=`which xmessage 2>/dev/null`
  36. # Path to powershell (Windows only)
  37. PWSH=`which powershell 2>/dev/null`
  38. # Path to osascript (macOS only)
  39. OSA=`which osascript 2>/dev/null`
  40. ##################################################################
  41. # There should be no need to change anything below this line.
  42. . "$(dirname -- "$0")/canonicalize_filename.sh"
  43. # exit on error
  44. set -e
  45. # check whether the given file matches any of the set extensions
  46. matches_extension() {
  47. local filename=$(basename "$1")
  48. local extension=".${filename##*.}"
  49. local ext
  50. for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
  51. return 1
  52. }
  53. # necessary check for initial commit
  54. if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
  55. against=HEAD
  56. else
  57. # Initial commit: diff against an empty tree object
  58. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  59. fi
  60. # To get consistent formatting, we recommend contributors to use the same
  61. # clang-format version as CI.
  62. RECOMMENDED_CLANG_FORMAT_MAJOR_MIN="13"
  63. RECOMMENDED_CLANG_FORMAT_MAJOR_MAX="16"
  64. if [ ! -x "$CLANG_FORMAT" ] ; then
  65. message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX."
  66. if [ ! -t 1 ] ; then
  67. if [ -x "$ZENITY" ] ; then
  68. $ZENITY --error --title="Error" --text="$message"
  69. exit 1
  70. elif [ -x "$XMSG" ] ; then
  71. $XMSG -center -title "Error" "$message"
  72. exit 1
  73. elif [ -x "$OSA" ] ; then
  74. asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")"
  75. $OSA "$asmessage" -center -title "Error" --text "$message"
  76. exit 1
  77. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  78. winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")"
  79. $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "$message"
  80. exit 1
  81. fi
  82. fi
  83. printf "$message\n"
  84. printf "Set the correct path in $(canonicalize_filename "$0").\n"
  85. exit 1
  86. fi
  87. # The returned string can be inconsistent depending on where clang-format comes from.
  88. # Example output strings reported by `clang-format --version`:
  89. # - Ubuntu: "Ubuntu clang-format version 11.0.0-2"
  90. # - Fedora: "clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)"
  91. CLANG_FORMAT_VERSION="$(clang-format --version | sed "s/[^0-9\.]*\([0-9\.]*\).*/\1/")"
  92. CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d. -f1)"
  93. if [[ "$CLANG_FORMAT_MAJOR" -lt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MIN" || "$CLANG_FORMAT_MAJOR" -gt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MAX" ]]; then
  94. echo "Warning: Your clang-format binary is the wrong version ($CLANG_FORMAT_VERSION, expected between $RECOMMENDED_CLANG_FORMAT_MAJOR_MIN and $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX)."
  95. echo " Consider upgrading or downgrading clang-format as formatting may not be applied correctly."
  96. fi
  97. # create a random filename to store our generated patch
  98. prefix="pre-commit-clang-format"
  99. suffix="$(date +%s)"
  100. patch="/tmp/$prefix-$suffix.patch"
  101. # clean up any older clang-format patches
  102. $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
  103. # create one patch containing all changes to the files
  104. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  105. do
  106. # ignore thirdparty files
  107. if grep -q "thirdparty" <<< $file; then
  108. continue;
  109. fi
  110. if grep -q "platform/android/java/lib/src/com" <<< $file; then
  111. continue;
  112. fi
  113. if grep -q "\-so_wrap." <<< $file; then
  114. continue;
  115. fi
  116. # ignore file if we do check for file extensions and the file
  117. # does not match any of the extensions specified in $FILE_EXTS
  118. if $PARSE_EXTS && ! matches_extension "$file"; then
  119. continue;
  120. fi
  121. # clang-format our sourcefile, create a patch with diff and append it to our $patch
  122. # The sed call is necessary to transform the patch from
  123. # --- $file timestamp
  124. # +++ - timestamp
  125. # to both lines working on the same file and having a/ and b/ prefix.
  126. # Else it can not be applied with 'git apply'.
  127. "$CLANG_FORMAT" -style=file "$file" --Wno-error=unknown | \
  128. diff -u "$file" - | \
  129. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  130. done
  131. # if no patch has been generated all is ok, clean up the file stub and exit
  132. if [ ! -s "$patch" ] ; then
  133. printf "Files in this commit comply with the clang-format rules.\n"
  134. rm -f "$patch"
  135. exit 0
  136. fi
  137. # a patch has been created, notify the user and exit
  138. printf "\nThe following differences were found between the code to commit "
  139. printf "and the clang-format rules:\n\n"
  140. if [ -t 1 ] ; then
  141. $READER "$patch"
  142. printf "\n"
  143. # Allows us to read user input below, assigns stdin to keyboard
  144. exec < /dev/tty
  145. terminal="1"
  146. else
  147. cat "$patch"
  148. printf "\n"
  149. # Allows non zero zenity/powershell output
  150. set +e
  151. terminal="0"
  152. fi
  153. while true; do
  154. if [ $terminal = "0" ] ; then
  155. if [ -x "$ZENITY" ] ; then
  156. choice=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage")
  157. if [ "$?" = "0" ] ; then
  158. yn="Y"
  159. else
  160. if [ "$choice" = "Apply and stage" ] ; then
  161. yn="S"
  162. else
  163. yn="N"
  164. fi
  165. fi
  166. elif [ -x "$XMSG" ] ; then
  167. $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"
  168. choice=$?
  169. if [ "$choice" = "100" ] ; then
  170. yn="Y"
  171. elif [ "$choice" = "200" ] ; then
  172. yn="S"
  173. else
  174. yn="N"
  175. fi
  176. elif [ -x "$OSA" ] ; then
  177. asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")"
  178. choice=`$OSA "$asmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"`
  179. if [ "$choice" = "100" ] ; then
  180. yn="Y"
  181. elif [ "$choice" = "200" ] ; then
  182. yn="S"
  183. else
  184. yn="N"
  185. fi
  186. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  187. winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")"
  188. $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"
  189. choice=$?
  190. if [ "$choice" = "100" ] ; then
  191. yn="Y"
  192. elif [ "$choice" = "200" ] ; then
  193. yn="S"
  194. else
  195. yn="N"
  196. fi
  197. else
  198. printf "Error: zenity, xmessage, osascript, or powershell executable not found.\n"
  199. exit 1
  200. fi
  201. else
  202. read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
  203. fi
  204. case $yn in
  205. [Yy] ) git apply $patch;
  206. printf "The patch was applied. You can now stage the changes and commit again.\n\n";
  207. break
  208. ;;
  209. [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n";
  210. printf "(may need to be called from the root directory of your repository)\n";
  211. printf "Aborting commit. Apply changes and commit again or skip checking with";
  212. printf " --no-verify (not recommended).\n\n";
  213. break
  214. ;;
  215. [Ss] ) git apply $patch;
  216. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  217. do git add $file;
  218. done
  219. printf "The patch was applied and the changed files staged. You can now commit.\n\n";
  220. break
  221. ;;
  222. * ) echo "Please answer yes or no."
  223. ;;
  224. esac
  225. done
  226. exit 1 # we don't commit in any case