File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ #
3+ # A git hook called by "git push" after it has checked the remote status, but before anything has been pushed.
4+ #
5+ # If this script exits with a non-zero status, nothing will be pushed.
6+ #
7+ # This hook is called with the following parameters:
8+ #
9+ # $1 - name of the remote to which the push is being done
10+ # $2 - URL to which the push is being done
11+ #
12+ # If pushing without using a named remote, these arguments will be equal.
13+ #
14+ # Information about the commits which are being pushed is supplied as lines to stdin in the form:
15+ #
16+ # <local ref> <local sha1> <remote ref> <remote sha1>
17+ #
18+
19+ # VARIABLES #
20+
21+ remote=" $1 "
22+ url=" $2 "
23+
24+ # Define the command to run tests:
25+ export GIT_HOOK_TEST=' make test'
26+
27+
28+ # FUNCTIONS #
29+
30+ # Defines an error handler.
31+ #
32+ # $1 - error status
33+ on_error () {
34+ cleanup
35+ exit " $1 "
36+ }
37+
38+ # Runs clean-up tasks.
39+ cleanup () {
40+ echo ' ' >&2
41+ }
42+
43+ # Checks if commits exist to push, as `git push` will execute regardless of whether commits exist to push or not.
44+ has_commits () {
45+ local commits=` git log @{u}..`
46+ if [[ -z " $commits " ]]; then
47+ return 1
48+ fi
49+ return 0
50+ }
51+
52+ # Runs tests.
53+ run_tests () {
54+ echo ' Running tests...'
55+ $GIT_HOOK_TEST
56+ if [[ " $? " -ne 0 ]]; then
57+ echo " $GIT_HOOK_TEST failed." >&2
58+ return 1
59+ fi
60+ echo ' Tests passed.' >&2
61+ return 0
62+ }
63+
64+ # Main execution sequence.
65+ main () {
66+ has_commits
67+ if [[ " $? " -ne 0 ]]; then
68+ on_error 1
69+ fi
70+ run_tests
71+ if [[ " $? " -ne 0 ]]; then
72+ on_error 1
73+ fi
74+ cleanup
75+ exit 0
76+ }
77+
78+ # Run main:
79+ main
You can’t perform that action at this time.
0 commit comments