Skip to content

Instantly share code, notes, and snippets.

@htinlinn
Created September 18, 2015 19:11
Show Gist options
  • Save htinlinn/eb816d2a229a28c68433 to your computer and use it in GitHub Desktop.
Save htinlinn/eb816d2a229a28c68433 to your computer and use it in GitHub Desktop.
Custom pre-commit hook for space commander
#!/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 "$file" - | wc -l)
if [ $difference -gt 0 ]; then
success=1
return $success
fi
done
return $success
}
success=0
format_objc || (echo -e "🔴 There were formatting issues with this commit, fix by running \`git add --all && make clean-code-staged\`. Commit anyway and skip this check by running git commit --no-verify" && success=1)
exit $success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment