#!/bin/sh
# pre-push -- quietly run the test suite before pushing.
#
# Installed by `make prepare` / `npm run hooks`, which points
# core.hooksPath at .githooks/.
#
#   git push --no-verify     skip once
#   SKIP_PREPUSH=1 ...       skip (for scripted pushes)
#
# Tests run only when a branch is being pushed; deleting a remote branch
# or pushing just tags skips them.

set -u

zero=0000000000000000000000000000000000000000

needs_tests=0
while read -r local_ref local_sha remote_ref remote_sha; do
    [ "$local_sha" = "$zero" ] && continue      # deleting a ref
    case "$local_ref" in
        refs/tags/*) continue ;;                # tags don't change the code
    esac
    needs_tests=1
done

[ "$needs_tests" = 1 ] || exit 0
[ "${SKIP_PREPUSH:-}" = "" ] || exit 0

hook_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) || exit 1
. "$hook_dir/_quiet"

cd "$(git rev-parse --show-toplevel)" || exit 1

hook_action="push"
hook_skip_hint="'git push --no-verify' or SKIP_PREPUSH=1"
quiet_run "npm test" npm test
