Skip to content

Instantly share code, notes, and snippets.

@glfmn
Last active January 23, 2024 06:46
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save glfmn/0c5e9e2b41b48007ed3497d11e3dbbfa to your computer and use it in GitHub Desktop.
Save glfmn/0c5e9e2b41b48007ed3497d11e3dbbfa to your computer and use it in GitHub Desktop.
pre-commit Test Automation Script
#!/usr/bin/env sh
# pre-commit.sh
# Colors
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
BOLD='\033[1m'
# Git Metadata
ROOT_DIR="$(git rev-parse --show-toplevel)"
BUILD_DIR="${ROOT_DIR}/target"
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
STASH_NAME="pre-commit-$(date +%s) on ${BRANCH_NAME}"
echo "* ${BOLD}Checking for unstashed changes:${NC}"
stash=0
# Check to make sure commit isn't empty
if git diff-index --cached --quiet HEAD --; then
# It was empty, exit with status 0 to let git handle it
exit 0
else
# Stash changes that aren't added to the staging index so we test
# only the changes to be committed
old_stash=$(git rev-parse -q --verify refs/stash)
git stash push -q --keep-index -m "$STASH_NAME"
new_stash=$(git rev-parse -q --verify refs/stash)
echo " - Stashed changes as: ${BOLD}${STASH_NAME}${NC}"
if [ "$old_stash" = "$new_stash" ]; then
echo " - no changes, ${YELLOW}skipping tests${NC}"
exit 0
else
stash=1
fi
fi
echo "* ${BOLD}Testing:${NC}"
git diff --cached --stat
echo ""
# use && to combine test commands so if any one fails it's accurately represented
# in the exit code
cargo check --all-targets --examples --benches &&
cargo test &&
cargo +nightly fmt --all -- --check
# Capture exit code from tests
status=$?
# Inform user of build failure
echo "* ${BOLD}Build status:${NC}"
if [ "$status" -ne "0" ]
then
echo " - ${RED}failed:${NC} if you still want to commit use ${BOLD}'--no-verify'${NC}"
else
echo " - ${GREEN}passed${NC}"
fi
# Revert stash if changes were stashed to restore working directory files
if [ "$stash" -eq 1 ]
then
echo "* ${BOLD}Resotring working tree${NC}"
if git reset --hard -q &&
git stash apply --index -q &&
git stash drop -q
then
echo " - ${GREEN}restored${NC} ${STASH_NAME}"
else
echo " - ${RED}unable to revert stash command${NC}"
fi
fi
# Exit with exit code from tests, so if they fail, prevent commit
exit $status

Installation

Copy this file under the name pre-commit into your repository's .git/hooksdirectory. You may need to use chmod +x to enable execution.

Skipping Tests

Script exscution can be skipped with git commit --no-verify which can skip lengthy test execution on simple changes to documentation.

Edge Cases

If you have used git add --patch to add an edited hunk, stashing and then unstashing changes will inevitably result in a merge conflict blocking your commit and ruining your day. In this case, use git commit --no-verify.

Copyright © 2018 Gwen Lofman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@glfmn
Copy link
Author

glfmn commented May 22, 2018

@starthal
Copy link

starthal commented Jun 4, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment