|
| 1 | +#!/bin/bash |
| 2 | +####################################################################################################### |
| 3 | +# GIT PRECOMMIT HOOK # |
| 4 | +# This script checks if the formatting of .cpp and .hpp files is correct and # |
| 5 | +# proposes to fix it automatically, let the user do it or ignore the formatting (don't do it!). # |
| 6 | +# # |
| 7 | +# This file has been entirely copied from Switcher's sources: https://github.com/sat-metalab/switcher # |
| 8 | +# # |
| 9 | +####################################################################################################### |
| 10 | + |
| 11 | +################################## |
| 12 | +# Global variables |
| 13 | +CLANG_FORMAT="clang-format" |
| 14 | +CLANG_FORMAT_DIFF="clang-format-diff" |
| 15 | +files_unformatted="" |
| 16 | +cpp_files=() |
| 17 | +################################### |
| 18 | + |
| 19 | +################################## |
| 20 | +# Helpers |
| 21 | + |
| 22 | +reformat_file() { |
| 23 | + while true |
| 24 | + do |
| 25 | + read -p "Do you want to do the reformatting (M)anually or let it be done (A)utomatically? [m/A]" reply < /dev/tty |
| 26 | + case $reply in |
| 27 | + [Mm]) echo -e "Commit will be aborted, awaiting reformatting of \e[0;32m${file}\e[0m."; files_unformatted+="\t\e[0;32m$1\e[0m\n"; return;; |
| 28 | + "") ;& # Fallthrough default behaviour, the reformatting is automatic. |
| 29 | + [Aa]) echo -e "Automatic reformatting of \e[0;32m${file}\e[0m will be done now."; git diff --cached -U0 $1 | ${CLANG_FORMAT_DIFF} -i -p1; git add $1; return;; |
| 30 | + *) echo "Unrecognized option." ;; |
| 31 | + esac |
| 32 | + done |
| 33 | +} |
| 34 | + |
| 35 | +final_check() { |
| 36 | + echo -e "\n\nThe following files are not correctly formatted:" |
| 37 | + echo -e $files_unformatted |
| 38 | + |
| 39 | + while true |
| 40 | + do |
| 41 | + read -p "Do you want to (I)gnore the formatting and commit anyway (not recommended) or do the reformatting (Y)ourself ? [i/Y]" reply < /dev/tty |
| 42 | + case $reply in |
| 43 | + [Ii]) echo "Ignoring the formatting and committing files as-is."; exit 0;; |
| 44 | + "") ;& # Fallthrough default behaviour, the commit is aborted. |
| 45 | + [Yy]) echo "Commit aborted, awaiting reformatting of the previously listed files."; exit 1;; |
| 46 | + *) echo "Unrecognized option." ;; |
| 47 | + esac |
| 48 | + done |
| 49 | +} |
| 50 | + |
| 51 | +check_command() { |
| 52 | + if ! type $1 > /dev/null |
| 53 | + then |
| 54 | + echo "Error: $1 executable not found." |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +} |
| 58 | +################################## |
| 59 | + |
| 60 | +################################## |
| 61 | +# Main |
| 62 | + |
| 63 | +check_command $CLANG_FORMAT |
| 64 | +check_command $CLANG_FORMAT_DIFF |
| 65 | + |
| 66 | +cpp_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cpp$\|\.hpp$') |
| 67 | + |
| 68 | +# Nothing to test if no file was added, changed or modified. |
| 69 | +[ -z "$cpp_files" ] && exit 0 |
| 70 | + |
| 71 | +for file in $cpp_files |
| 72 | +do |
| 73 | + output=$(${CLANG_FORMAT} ${file} -style=file -output-replacements-xml | grep "<replacement " > /dev/null) |
| 74 | + if [ $? -ne 1 ] |
| 75 | + then |
| 76 | + echo -e "\e[0;31m${file} does not conform to coding standards, here are the changes needed:\n\n\e[0m$(git diff --cached -U0 ${file} | ${CLANG_FORMAT_DIFF} -p1)" |
| 77 | + reformat_file $file |
| 78 | + fi |
| 79 | +done |
| 80 | + |
| 81 | +## All .[c|h]pp files are properly formatted. |
| 82 | +[ -z "$files_unformatted" ] && exit 0 |
| 83 | + |
| 84 | +final_check |
| 85 | + |
| 86 | +################################## |
0 commit comments