pre-commit-clang-format 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 [email protected]
  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, [email protected]
  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. if [ ! -x "$CLANG_FORMAT" ] ; then
  59. if [ ! -t 1 ] ; then
  60. if [ -x "$ZENITY" ] ; then
  61. $ZENITY --error --title="Error" --text="Error: clang-format executable not found."
  62. exit 1
  63. elif [ -x "$XMSG" ] ; then
  64. $XMSG -center -title "Error" "Error: clang-format executable not found."
  65. exit 1
  66. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  67. winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
  68. $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "Error: clang-format executable not found."
  69. exit 1
  70. fi
  71. fi
  72. printf "Error: clang-format executable not found.\n"
  73. printf "Set the correct path in $(canonicalize_filename "$0").\n"
  74. exit 1
  75. fi
  76. # create a random filename to store our generated patch
  77. prefix="pre-commit-clang-format"
  78. suffix="$(date +%s)"
  79. patch="/tmp/$prefix-$suffix.patch"
  80. # clean up any older clang-format patches
  81. $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
  82. # create one patch containing all changes to the files
  83. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  84. do
  85. # ignore thirdparty files
  86. if grep -q "thirdparty" <<< $file; then
  87. continue;
  88. fi
  89. if grep -q "platform/android/java/lib/src/com" <<< $file; then
  90. continue;
  91. fi
  92. # ignore file if we do check for file extensions and the file
  93. # does not match any of the extensions specified in $FILE_EXTS
  94. if $PARSE_EXTS && ! matches_extension "$file"; then
  95. continue;
  96. fi
  97. # clang-format our sourcefile, create a patch with diff and append it to our $patch
  98. # The sed call is necessary to transform the patch from
  99. # --- $file timestamp
  100. # +++ - timestamp
  101. # to both lines working on the same file and having a/ and b/ prefix.
  102. # Else it can not be applied with 'git apply'.
  103. "$CLANG_FORMAT" -style=file "$file" | \
  104. diff -u "$file" - | \
  105. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  106. done
  107. # if no patch has been generated all is ok, clean up the file stub and exit
  108. if [ ! -s "$patch" ] ; then
  109. printf "Files in this commit comply with the clang-format rules.\n"
  110. rm -f "$patch"
  111. exit 0
  112. fi
  113. # a patch has been created, notify the user and exit
  114. printf "\nThe following differences were found between the code to commit "
  115. printf "and the clang-format rules:\n\n"
  116. if [ -t 1 ] ; then
  117. $READER "$patch"
  118. printf "\n"
  119. # Allows us to read user input below, assigns stdin to keyboard
  120. exec < /dev/tty
  121. terminal="1"
  122. else
  123. cat "$patch"
  124. printf "\n"
  125. # Allows non zero zenity/powershell output
  126. set +e
  127. terminal="0"
  128. fi
  129. while true; do
  130. if [ $terminal = "0" ] ; then
  131. if [ -x "$ZENITY" ] ; then
  132. 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")
  133. if [ "$?" = "0" ] ; then
  134. yn="Y"
  135. else
  136. if [ "$ans" = "Apply and stage" ] ; then
  137. yn="S"
  138. else
  139. yn="N"
  140. fi
  141. fi
  142. elif [ -x "$XMSG" ] ; then
  143. $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?"
  144. ans=$?
  145. if [ "$ans" = "100" ] ; then
  146. yn="Y"
  147. elif [ "$ans" = "200" ] ; then
  148. yn="S"
  149. else
  150. yn="N"
  151. fi
  152. elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
  153. winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
  154. $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?"
  155. ans=$?
  156. if [ "$ans" = "100" ] ; then
  157. yn="Y"
  158. elif [ "$ans" = "200" ] ; then
  159. yn="S"
  160. else
  161. yn="N"
  162. fi
  163. else
  164. printf "Error: zenity, xmessage, or powershell executable not found.\n"
  165. exit 1
  166. fi
  167. else
  168. read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
  169. fi
  170. case $yn in
  171. [Yy] ) git apply $patch;
  172. printf "The patch was applied. You can now stage the changes and commit again.\n\n";
  173. break
  174. ;;
  175. [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n";
  176. printf "(may need to be called from the root directory of your repository)\n";
  177. printf "Aborting commit. Apply changes and commit again or skip checking with";
  178. printf " --no-verify (not recommended).\n\n";
  179. break
  180. ;;
  181. [Ss] ) git apply $patch;
  182. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  183. do git add $file;
  184. done
  185. printf "The patch was applied and the changed files staged. You can now commit.\n\n";
  186. break
  187. ;;
  188. * ) echo "Please answer yes or no."
  189. ;;
  190. esac
  191. done
  192. exit 1 # we don't commit in any case