Skip to content

Instantly share code, notes, and snippets.

@chris-piekarski
Created February 27, 2019 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-piekarski/c7ed74e13799381042bd0dd608218281 to your computer and use it in GitHub Desktop.
Save chris-piekarski/c7ed74e13799381042bd0dd608218281 to your computer and use it in GitHub Desktop.
Git pre-commit hook to check for wildcard imports
#!/bin/sh
#
# cp to .git/hooks
# chomd +x .git/hooks/pre_commit
#
# This hook checks for wildcard imports (other than kotlinx) when added to staging.
# TODO: add ktlint check
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
echo "** pre-commit check **"
commit_files=$(git diff --cached --name-only)
for i in $commit_files
do
echo "checking $i"
c=$(grep "import *.*\*$" $i | grep -v "import kotlinx\.*")
echo -e "${RED}$c${NC}"
done
echo " "
if [ -z "$c" ]
then
echo -e "import check ${GREEN}passed${NC}"
else
echo -e "${RED}Error:${NC} Attempt to add a file that uses wildcard imports."
echo "This can cause problems if you want to get a PR review approved"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment