Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Template pre-commit script for running tests on Rust projects especially, but appropriate for any project.
#!/usr/bin/env sh
# pre-commit.sh
STASH_NAME="pre-commit-$(date +%s)"
ROOT_DIR="$(git rev-parse --show-toplevel)"
BUILD_DIR="${ROOT_DIR}/target"
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
BOLD='\033[1m'
# Check if commit is on a rebase, if not proceed as usual
if [ $BRANCH_NAME != '(no branch)' ]
then
stash=0
# Check to make sure commit isn't emtpy, exit with status 0 to let git handle it
# prevents blocking when ammending an empty commit
if git diff-index --cached --quiet HEAD --; then
echo "${RED}You've tried to commit an empty commit${NC}"
echo "\tMake sure to add your changes with 'git add'"
exit 0
else
# Stash all changes in the working directory so we test only commit files
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${NC}\n\n"
# use && to combine test commands so if any one fails it's accurately represented
# in the exit code
cargo check &&
cargo test
# Capture exit code from tests
status=$?
# Revert stash if changes were stashed to restore working directory files
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
# Inform user of build failure
if [ "$status" -ne "0" ]
then
echo "${RED}Build failed:${NC} if you still want to commit use ${BOLD}'--no-verify'${NC}"
fi
# Exit with exit code from tests, so if they fail, prevent commit
exit $status
else
# Tests were skipped for rebase, inform user and exit zero
echo "${YELLOW}Skipping tests on branchless commit${NC}"
exit 0
fi

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

This comment has been minimized.

Copy link
Owner Author

glfmn commented May 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.