Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Last active November 5, 2021 11:50
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samsonjs/3c24c0c7b333f209bc5fcab0d8390c01 to your computer and use it in GitHub Desktop.
Save samsonjs/3c24c0c7b333f209bc5fcab0d8390c01 to your computer and use it in GitHub Desktop.
[iOS] git pre-commit hook to catch misplaced views in xibs and storyboards, and focused or disabled tests and specs
#!/usr/bin/env bash
#
# Based on http://merowing.info/2016/08/setting-up-pre-commit-hook-for-ios/
set -eu
diff-index() {
git diff-index -p -M --cached HEAD -- "$@"
}
failed=0
test_pattern='\b(fdescribe|fit|fcontext|xdescribe|xit|xcontext)\b'
test_glob='*Tests.swift *Specs.swift'
if diff-index $test_glob | grep '^+' | egrep "$test_pattern" >/dev/null 2>&1
then
echo "COMMIT REJECTED for fdescribe/fit/fcontext/xdescribe/xit/xcontext." >&2
echo "Remove focused and disabled tests before committing." >&2
git grep -E "$test_pattern" $test_glob || true >&2
echo '----' >&2
failed=1
fi
misplaced_pattern='misplaced="YES"'
misplaced_glob='*.xib *.storyboard'
if diff-index $misplaced_glob | grep '^+' | egrep "$misplaced_pattern" >/dev/null 2>&1
then
echo "COMMIT REJECTED for misplaced views. Correct them before committing." >&2
git grep -E "$misplaced_pattern" $misplaced_glob || true >&2
echo '----' >&2
failed=1
fi
exit $failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment