Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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