|
| 1 | +#!/usr/bin/env zsh |
| 2 | +set -euo pipefail |
| 3 | +cd `git rev-parse --show-toplevel` |
| 4 | + |
| 5 | +# Use 'new line' as the only separator in for-loops. |
| 6 | +IFS=$'\n' |
| 7 | + |
| 8 | +# Exit code to signal if all checks passed. |
| 9 | +exit_code=0 |
| 10 | + |
| 11 | +# Sync ctags file on commit. |
| 12 | +ctags **/*.(c|cpp|h|hpp|mm|m|swift|rs) |
| 13 | + |
| 14 | +# Install missing tools. |
| 15 | +[[ ! `command -v clang-format` ]] && brew install clang-format |
| 16 | +[[ ! `command -v gsed` ]] && brew install gnu-sed |
| 17 | + |
| 18 | +# Check for new and renamed files if there is a space in the file name. |
| 19 | +function check_for_spaces_in_file_names() { |
| 20 | + for file in `git diff --name-only --diff-filter=AR --staged | grep '\s'`; do |
| 21 | + echo "error: '$file' contains a space in the file name." |
| 22 | + exit_code=1 |
| 23 | + done |
| 24 | +} |
| 25 | + |
| 26 | +# Check if the staged content contains trailing white space. |
| 27 | +function check_for_trailing_white_space() { |
| 28 | + for file in `git diff --name-only --diff-filter=AM --staged`; do |
| 29 | + if [[ ! -z `file $file | grep text` ]]; then |
| 30 | + if [[ $file == mono.xcodeproj/* ]]; then |
| 31 | + continue |
| 32 | + fi |
| 33 | + |
| 34 | + gsed -i 's/[ \t]*$//' $file |
| 35 | + git add $file |
| 36 | + fi |
| 37 | + done |
| 38 | +} |
| 39 | + |
| 40 | +# Check if the staged content contains tabs. |
| 41 | +function check_for_tabs() { |
| 42 | + for file in `git diff --name-only --diff-filter=AM --staged`; do |
| 43 | + if [[ $file == *.sh ]]; then |
| 44 | + gsed -i 's/\t/ /g' $file |
| 45 | + git add $file |
| 46 | + fi |
| 47 | + done |
| 48 | +} |
| 49 | + |
| 50 | +# Replace #include with #import. |
| 51 | +function check_include_pragmas() { |
| 52 | + for file in `git diff --name-only --diff-filter=AM --staged`; do |
| 53 | + if [[ $file == Third/* ]]; then |
| 54 | + continue |
| 55 | + fi |
| 56 | + |
| 57 | + if [[ $file == *.(c|cpp|mm|h|hpp) ]]; then |
| 58 | + gsed -i 's/#include/#import/' $file |
| 59 | + git add $file |
| 60 | + fi |
| 61 | + done |
| 62 | +} |
| 63 | + |
| 64 | +# Check if the staged files which were added or modified are formatted completely. |
| 65 | +function check_for_formatting() { |
| 66 | + for file in `git diff --name-only --diff-filter=AM --staged`; do |
| 67 | + if [[ $file == *.(c|cpp|mm|m|h|hpp) ]]; then |
| 68 | + clang-format -i --style=file $file |
| 69 | + git add $file |
| 70 | + fi |
| 71 | + |
| 72 | + if [[ $file == *.(rs) ]]; then |
| 73 | + rustfmt $file |
| 74 | + git add $file |
| 75 | + fi |
| 76 | + done |
| 77 | +} |
| 78 | + |
| 79 | +check_for_formatting |
| 80 | +check_for_spaces_in_file_names |
| 81 | +check_for_trailing_white_space |
| 82 | +check_for_tabs |
| 83 | +check_include_pragmas |
| 84 | + |
| 85 | +exit $exit_code |
0 commit comments