Skip to content

Instantly share code, notes, and snippets.

@rizalgowandy
Last active August 11, 2021 21:09
Show Gist options
  • Save rizalgowandy/ce6bdd67300ce57a564bc2f1f1078585 to your computer and use it in GitHub Desktop.
Save rizalgowandy/ce6bdd67300ce57a564bc2f1f1078585 to your computer and use it in GitHub Desktop.
Git Pre-push Hook for Golang Developer
WARNING THIS IS ON DEVELOPMENT, DO NOT USE THIS
WARNING THIS IS ON DEVELOPMENT, DO NOT USE THIS
WARNING THIS IS ON DEVELOPMENT, DO NOT USE THIS
#!/bin/bash
# git test pre-push hook
#
# To use, store as .git/hooks/pre-push inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.
# Pre-push configuration
remote=$1
url=$2
echo >&2 "Try pushing $2 to $1"
# Run test and stop on first failed
CMD="go test ./... -v --cover -failfast"
# Prevent pushing directly to master
protected_branch='master'
# Check if we actually have commits to push
commits=`git log @{u}..`
if [ -z "$commits" ]; then
exit 0
fi
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
# Return if current branch is pushed directly to master
[[ $current_branch == $protected_branch ]] && exit 1
# Run test and return if failed
printf "Running gotest..."
$CMD
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo >&2 "$RESULT"
echo >&2 "FAILED $CMD"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment