Skip to content

Instantly share code, notes, and snippets.

@jimhester
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save jimhester/9926be89c877b8c3f2aa to your computer and use it in GitHub Desktop.

Select an option

Save jimhester/9926be89c877b8c3f2aa to your computer and use it in GitHub Desktop.
Travis pre-commit hook for R projects
#!/bin/sh
# To use, place this script in your project .git/hooks directory.
# The script will automatically run travis-tool.sh run_tests before committing, so you won't break your project!.
# Modified from http://stackoverflow.com/questions/20479794/how-do-i-properly-git-stash-pop-in-pre-commit-hooks-to-get-a-clean-working-tree
# script to run tests on what is to be committed
# First, stash index and work dir, keeping only the
# to-be-committed changes in the working directory.
old_stash=$(git rev-parse -q --verify refs/stash)
git stash save -q --keep-index
new_stash=$(git rev-parse -q --verify refs/stash)
# If there were no changes (e.g., `--amend` or `--allow-empty`)
# then nothing was stashed, and we should skip everything,
# including the tests themselves. (Presumably the tests passed
# on the previous commit, so there is no need to re-run them.)
if [ "$old_stash" = "$new_stash" ]; then
echo "pre-commit script: no changes to test"
sleep 1 # XXX hack, editor may erase message
exit 0
fi
# Download travis if it doesn't exist
if [ -z travis-tool.sh ]; then
curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh
chmod 755 ./travis-tool.sh
fi
# Run Tests
travis-tool.sh run_tests
# Keep status of travis run
status=$?
# Cleanup build file and check directory
FILE=$(ls -1t *.tar.gz | head -n 1)
rm "$FILE"
DIR=$(ls -1t -d */ | head -n 1)
rm -r "$DIR"
# Restore changes
git reset --hard -q && git stash apply --index -q && git stash drop -q
# Exit with status from test-run: nonzero prevents commit
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment