BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
STASH_NAME="pre-commit-$BRANCH_NAME-$(date +%s)"
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
BOLD='\033[1m'
if [ $BRANCH_NAME != '(no branch)' ]
then
stash=0
if git diff-index --quiet HEAD --; then
echo "${RED}You've tried to commit an empty commit${NC}"
echo "\tMake sure to add your changes with 'git add'"
else
if git stash save -u -k -q $STASH_NAME; then
echo "${YELLOW}Stashed changes as:${NC} ${STASH_NAME}\n\n"
stash=1
fi
fi
echo "${GREEN} Testing commit\n\n${NC}"
cargo doc --no-deps &&
cargo check &&
cargo check --benches &&
cargo test --all &&
status=$?
for crate in $(find $(pwd) -name Cargo.toml); do
(
echo >&2 "\tRunning tests for ${GREEN}${crate}${NC}:"
export RUST_BACKTRACE=1
cargo test --manifest-path=${crate}
) || status=1
done
if [ "$stash" -eq 1 ]
then
if git stash pop -q; then
echo "\n\n${GREEN}Reverted stash command${NC}"
else
echo "\n\n${RED}Unable to revert stash command${NC}"
fi
fi
if [ "$status" -ne "0" ]
then
echo "${RED}Build failed:${NC} if you still want to commit use ${BOLD}'--no-verify'${NC}"
fi
exit $status
else
echo "${YELLOW}Skipping tests on branchless commit${NC}"
exit 0
fi