-
Notifications
You must be signed in to change notification settings - Fork 177
/
format-objc-hook
executable file
·42 lines (35 loc) · 1.33 KB
/
format-objc-hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
# ~/.git_template.local/hooks/pre-commit
# format-objc-hook
# pre-commit hook to check if any unformatted Objective-C files would be committed. Fails the check if so, and provides instructions.
#
# Copyright 2015 Square, Inc
IFS=$'\n'
export CDPATH=""
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source "$DIR"/lib/common-lib.sh
# Don't do anything unless a .clang-format file exists
[ -e ".clang-format" ] || exit 0
objc_files=$(objc_files_to_format "$1")
[ -z "$objc_files" ] && exit 0
function format_objc() {
success=0
for file in $objc_files; do
difference=$("$DIR"/format-objc-file-dry-run.sh "$file" | diff -q "$file" - | wc -l)
if [ $difference -gt 0 ]; then
if [ $success -eq 0 ]; then
echo -e "🚸 Format and stage individual files:"
fi
# This is what the dev can run to fixup an individual file
echo "\"$DIR\"/format-objc-file.sh '$file' && git add '$file';"
success=1
fi
done
if [ $success -gt 0 ]; then
echo -e "\n🚀 Format and stage all affected files:\n\t \"$DIR\"/format-objc-files.sh -s"
fi
return $success
}
success=0
format_objc || (echo -e "\n🔴 There were formatting issues with this commit, run the👆 above👆 command to fix.\n💔 Commit anyway and skip this check by running git commit --no-verify" && success=1)
exit $success