pre-commit-clang-format 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. ##################################################################
  39. # There should be no need to change anything below this line.
  40. . "$(dirname -- "$0")/canonicalize_filename.sh"
  41. # exit on error
  42. set -e
  43. # check whether the given file matches any of the set extensions
  44. matches_extension() {
  45. local filename=$(basename "$1")
  46. local extension=".${filename##*.}"
  47. local ext
  48. for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
  49. return 1
  50. }
  51. # necessary check for initial commit
  52. if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
  53. against=HEAD
  54. else
  55. # Initial commit: diff against an empty tree object
  56. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  57. fi
  58. # To get consistent formatting, we recommend contributors to use the same
  59. # clang-format version as CI.
  60. RECOMMENDED_CLANG_FORMAT_MAJOR_MIN="12"
  61. RECOMMENDED_CLANG_FORMAT_MAJOR_MAX="14"
  62. if [ ! -x "$CLANG_FORMAT" ] ; then
  63. message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX."
  64. if [ ! -t 1 ] ; then
  65. if [ -x "$ZENITY" ] ; then
  66. $ZENITY --error --title="Error" --text="$message"
  67. exit 1
  68. elif [ -x "$XMSG" ] ; then
  69. $XMSG -center -title "Error" "$message"
  70. exit 1
  71. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  72. winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
  73. $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "$message"
  74. exit 1
  75. fi
  76. fi
  77. printf "$message\n"
  78. printf "Set the correct path in $(canonicalize_filename "$0").\n"
  79. exit 1
  80. fi
  81. # The returned string can be inconsistent depending on where clang-format comes from.
  82. # Example output strings reported by `clang-format --version`:
  83. # - Ubuntu: "Ubuntu clang-format version 11.0.0-2"
  84. # - Fedora: "clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)"
  85. CLANG_FORMAT_VERSION="$(clang-format --version | sed "s/[^0-9\.]*\([0-9\.]*\).*/\1/")"
  86. CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d. -f1)"
  87. if [[ "$CLANG_FORMAT_MAJOR" -lt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MIN" || "$CLANG_FORMAT_MAJOR" -gt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MAX" ]]; then
  88. 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)."
  89. echo " Consider upgrading or downgrading clang-format as formatting may not be applied correctly."
  90. fi
  91. # create a random filename to store our generated patch
  92. prefix="pre-commit-clang-format"
  93. suffix="$(date +%s)"
  94. patch="/tmp/$prefix-$suffix.patch"
  95. # clean up any older clang-format patches
  96. $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
  97. # create one patch containing all changes to the files
  98. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  99. do
  100. # ignore thirdparty files
  101. if grep -q "thirdparty" <<< $file; then
  102. continue;
  103. fi
  104. if grep -q "platform/android/java/lib/src/com" <<< $file; then
  105. continue;
  106. fi
  107. if grep -q "\-so_wrap." <<< $file; then
  108. continue;
  109. fi
  110. # ignore file if we do check for file extensions and the file
  111. # does not match any of the extensions specified in $FILE_EXTS
  112. if $PARSE_EXTS && ! matches_extension "$file"; then
  113. continue;
  114. fi
  115. # clang-format our sourcefile, create a patch with diff and append it to our $patch
  116. # The sed call is necessary to transform the patch from
  117. # --- $file timestamp
  118. # +++ - timestamp
  119. # to both lines working on the same file and having a/ and b/ prefix.
  120. # Else it can not be applied with 'git apply'.
  121. "$CLANG_FORMAT" -style=file "$file" --Wno-error=unknown | \
  122. diff -u "$file" - | \
  123. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  124. done
  125. # if no patch has been generated all is ok, clean up the file stub and exit
  126. if [ ! -s "$patch" ] ; then
  127. printf "Files in this commit comply with the clang-format rules.\n"
  128. rm -f "$patch"
  129. exit 0
  130. fi
  131. # a patch has been created, notify the user and exit
  132. printf "\nThe following differences were found between the code to commit "
  133. printf "and the clang-format rules:\n\n"
  134. if [ -t 1 ] ; then
  135. $READER "$patch"
  136. printf "\n"
  137. # Allows us to read user input below, assigns stdin to keyboard
  138. exec < /dev/tty
  139. terminal="1"
  140. else
  141. cat "$patch"
  142. printf "\n"
  143. # Allows non zero zenity/powershell output
  144. set +e
  145. terminal="0"
  146. fi
  147. while true; do
  148. if [ $terminal = "0" ] ; then
  149. if [ -x "$ZENITY" ] ; then
  150. ans=$($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")
  151. if [ "$?" = "0" ] ; then
  152. yn="Y"
  153. else
  154. if [ "$ans" = "Apply and stage" ] ; then
  155. yn="S"
  156. else
  157. yn="N"
  158. fi
  159. fi
  160. elif [ -x "$XMSG" ] ; then
  161. $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?"
  162. ans=$?
  163. if [ "$ans" = "100" ] ; then
  164. yn="Y"
  165. elif [ "$ans" = "200" ] ; then
  166. yn="S"
  167. else
  168. yn="N"
  169. fi
  170. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  171. winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
  172. $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?"
  173. ans=$?
  174. if [ "$ans" = "100" ] ; then
  175. yn="Y"
  176. elif [ "$ans" = "200" ] ; then
  177. yn="S"
  178. else
  179. yn="N"
  180. fi
  181. else
  182. printf "Error: zenity, xmessage, or powershell executable not found.\n"
  183. exit 1
  184. fi
  185. else
  186. read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
  187. fi
  188. case $yn in
  189. [Yy] ) git apply $patch;
  190. printf "The patch was applied. You can now stage the changes and commit again.\n\n";
  191. break
  192. ;;
  193. [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n";
  194. printf "(may need to be called from the root directory of your repository)\n";
  195. printf "Aborting commit. Apply changes and commit again or skip checking with";
  196. printf " --no-verify (not recommended).\n\n";
  197. break
  198. ;;
  199. [Ss] ) git apply $patch;
  200. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  201. do git add $file;
  202. done
  203. printf "The patch was applied and the changed files staged. You can now commit.\n\n";
  204. break
  205. ;;
  206. * ) echo "Please answer yes or no."
  207. ;;
  208. esac
  209. done
  210. exit 1 # we don't commit in any case