pre-commit-clang-format 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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="/usr/bin/clang-format"
  16. CLANG_FORMAT=`which clang-format`
  17. # Remove any older patches from previous commits. Set to true or false.
  18. # DELETE_OLD_PATCHES=false
  19. DELETE_OLD_PATCHES=false
  20. # Only parse files with the extensions in FILE_EXTS. Set to true or false.
  21. # If false every changed file in the commit will be parsed with clang-format.
  22. # If true only files matching one of the extensions are parsed with clang-format.
  23. # PARSE_EXTS=true
  24. PARSE_EXTS=true
  25. # File types to parse. Only effective when PARSE_EXTS is true.
  26. # FILE_EXTS=".c .h .cpp .hpp"
  27. FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m .mm .inc .java .glsl"
  28. # Use pygmentize instead of cat to parse diff with highlighting.
  29. # Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac)
  30. # READER="pygmentize -l diff"
  31. READER=cat
  32. ##################################################################
  33. # There should be no need to change anything below this line.
  34. . "$(dirname -- "$0")/canonicalize_filename.sh"
  35. # exit on error
  36. set -e
  37. # check whether the given file matches any of the set extensions
  38. matches_extension() {
  39. local filename=$(basename "$1")
  40. local extension=".${filename##*.}"
  41. local ext
  42. for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
  43. return 1
  44. }
  45. # necessary check for initial commit
  46. if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
  47. against=HEAD
  48. else
  49. # Initial commit: diff against an empty tree object
  50. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  51. fi
  52. if [ ! -x "$CLANG_FORMAT" ] ; then
  53. printf "Error: clang-format executable not found.\n"
  54. printf "Set the correct path in $(canonicalize_filename "$0").\n"
  55. exit 1
  56. fi
  57. # create a random filename to store our generated patch
  58. prefix="pre-commit-clang-format"
  59. suffix="$(date +%s)"
  60. patch="/tmp/$prefix-$suffix.patch"
  61. # clean up any older clang-format patches
  62. $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
  63. # create one patch containing all changes to the files
  64. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  65. do
  66. # ignore thirdparty files
  67. if grep -q "thirdparty" <<< $file; then
  68. continue;
  69. fi
  70. # ignore file if we do check for file extensions and the file
  71. # does not match any of the extensions specified in $FILE_EXTS
  72. if $PARSE_EXTS && ! matches_extension "$file"; then
  73. continue;
  74. fi
  75. # clang-format our sourcefile, create a patch with diff and append it to our $patch
  76. # The sed call is necessary to transform the patch from
  77. # --- $file timestamp
  78. # +++ - timestamp
  79. # to both lines working on the same file and having a/ and b/ prefix.
  80. # Else it can not be applied with 'git apply'.
  81. "$CLANG_FORMAT" -style=file "$file" | \
  82. diff -u "$file" - | \
  83. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  84. done
  85. # if no patch has been generated all is ok, clean up the file stub and exit
  86. if [ ! -s "$patch" ] ; then
  87. printf "Files in this commit comply with the clang-format rules.\n"
  88. rm -f "$patch"
  89. exit 0
  90. fi
  91. # a patch has been created, notify the user and exit
  92. printf "\nThe following differences were found between the code to commit "
  93. printf "and the clang-format rules:\n\n"
  94. $READER "$patch"
  95. printf "\n"
  96. # Allows us to read user input below, assigns stdin to keyboard
  97. exec < /dev/tty
  98. while true; do
  99. read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
  100. case $yn in
  101. [Yy] ) git apply $patch;
  102. printf "The patch was applied. You can now stage the changes and commit again.\n\n";
  103. break
  104. ;;
  105. [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n";
  106. printf "(may need to be called from the root directory of your repository)\n";
  107. printf "Aborting commit. Apply changes and commit again or skip checking with";
  108. printf " --no-verify (not recommended).\n\n";
  109. break
  110. ;;
  111. [Ss] ) git apply $patch;
  112. git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
  113. do git add $file;
  114. done
  115. printf "The patch was applied and the changed files staged. You can now commit.\n\n";
  116. break
  117. ;;
  118. * ) echo "Please answer yes or no."
  119. ;;
  120. esac
  121. done
  122. exit 1 # we don't commit in any case