Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Last active December 30, 2016 20:12
Show Gist options
  • Save phrohdoh/639068899478088f81c3dc3f4d34bb09 to your computer and use it in GitHub Desktop.
Save phrohdoh/639068899478088f81c3dc3f4d34bb09 to your computer and use it in GitHub Desktop.
git: Prevent WIP commits and use <root>/.last-test file with Makefile `test` target to make sure tests are passing
#!/usr/bin/env bash
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"/../../ && pwd -P)"
# echo "basedir: $BASEDIR"
# REV_PARSE_HEAD="$(git rev-parse --short head)"
# remote="$1"
# url="$2"
# echo "remote: $1"
# echo "url: $2"
exit_code=0
# prevent WIP commits
z40=0000000000000000000000000000000000000000
IFS=' '
while read -r local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ] ; then
: # Handle delete
else
if [ "$remote_sha" = $z40 ] ; then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=$(git rev-list -n 1 --grep '^WIP' "$range")
if [ -n "$commit" ] ; then
echo "Found WIP commit in $local_ref, not pushing"
((exit_code++))
fi
fi
done
LAST_TEST_PATH="$BASEDIR/.last-test"
LAST_TEST_PATH_DISPLAY="${BASEDIR%/}"
LAST_TEST_PATH_DISPLAY="${LAST_TEST_PATH_DISPLAY##*/}/.last-test" # example: tsqlust/.last-test (given tsqlust is a git repo: tsqlust/.git/)
if [ -f "$LAST_TEST_PATH" ]; then
# echo "Found $LAST_TEST_PATH_DISPLAY"
LAST_TEST_CONTENTS="$(cat "$LAST_TEST_PATH")"
LAST_TEST_CONTENTS_ARR=($LAST_TEST_CONTENTS)
if [ -z "$LAST_TEST_CONTENTS" ]; then
echo "$LAST_TEST_PATH_DISPLAY is empty, please run \`make test\`"
((exit_code++))
elif [ "${LAST_TEST_CONTENTS[0]}" == "fail" ]; then
FAILED_TEST_COMMIT=${LAST_TEST_CONTENTS_ARR[1]}
echo "Tests failed for $FAILED_TEST_COMMIT, please fix this, run \`make test\`, then retry."
((exit_code++))
#elif [ "${LAST_TEST_CONTENTS[0]}" != "$REV_PARSE_HEAD" ]; then
# LAST_TESTED_COMMIT=${LAST_TEST_CONTENTS_ARR[1]}
# if [ "$LAST_TESTED_COMMIT" != "$REV_PARSE_HEAD" ]; then
# echo "$LAST_TEST_PATH_DISPLAY is outdated, please run \`make test\`"
# ((exit_code++))
# else
# echo "Ehhhh... unexpected state:"
# echo "LAST_TESTED_COMMIT: $LAST_TESTED_COMMIT"
# echo "REV_PARSE_HEAD: $REV_PARSE_HEAD"
# echo "LAST_TEST_CONTENTS: $LAST_TEST_CONTENTS"
# ((exit_code++))
# fi
fi
elif [ -d "$LAST_TEST_PATH" ]; then
echo "$LAST_TEST_PATH_DISPLAY is a directory, expected a file"
else
: # Ok, last-test isn't used in this project
fi
exit $exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment