summaryrefslogtreecommitdiff
path: root/buildscripts/install-hooks
diff options
context:
space:
mode:
authorRobert Guo <robert.guo@10gen.com>2018-02-13 19:11:06 -0500
committerRobert Guo <robert.guo@10gen.com>2018-02-15 17:22:05 -0500
commitbf5fd394cf5bfba4cd3bf6a34cc769b814e6f51f (patch)
treef1695cf7a22429d99c27aa9006b04cc6e90871a6 /buildscripts/install-hooks
parent8dc995ebef6d9ae671a82b1cdc521e0960d32ec8 (diff)
downloadmongo-bf5fd394cf5bfba4cd3bf6a34cc769b814e6f51f.tar.gz
SERVER-33335 support all git hook types in buildscripts/install-hooks
Diffstat (limited to 'buildscripts/install-hooks')
-rwxr-xr-xbuildscripts/install-hooks136
1 files changed, 77 insertions, 59 deletions
diff --git a/buildscripts/install-hooks b/buildscripts/install-hooks
index 5389bc615e1..1148b485ddc 100755
--- a/buildscripts/install-hooks
+++ b/buildscripts/install-hooks
@@ -1,28 +1,45 @@
#!/bin/bash
-# Create a pre-push hook driver in this repo. The hook will run all
-# excutable scripts in ~/.githooks/<repo name>/pre-push
+# Create a git hook driver in this repo. The hook will run all
+# excutable scripts in the directory ~/.githooks/<repo name>/[HOOK_TRIGGER]/
+# where HOOK_TRIGGER can be any one of the following:
+# https://git-scm.com/docs/githooks#_hooks
+# If you add a new type of HOOK_TRIGGER, this script needs to be run again.
# Find out the repo name and where the .git directory is for this repo
-origin="`git config remote.origin.url`"
-repo="`basename -s .git $origin`"
-tld="`git rev-parse --show-toplevel`"
+origin="$(git config remote.origin.url)"
+repo="$(basename -s .git $origin)"
+tld="$(git rev-parse --show-toplevel)"
# Location for the hooks.
# WARNING: if you change this you'll need to change the value of the
-# "hooksdir" variable in the heredoc below as well
-hooksdir="$HOME/.githooks/$repo/pre-push"
+# "hooks_dir" variable in the heredoc below as well
+hooks_dir="$HOME/.githooks/$repo"
+
+tmp_hook=$(mktemp)
usage() {
- echo "Usage: `basename $0` [-f|-h]"
+ echo "Usage: $(basename $0) [-f|-h]"
echo
- echo "Install a pre-push hook in $tld/.git/hooks"
+ echo " git hooks in $tld/.git/hooks that run scripts in $hooks_dir"
echo " -f force overwriting existing hooks"
echo " -h print this help"
echo
- exit 0
+ return
}
+if [ ! -d "$hooks_dir" ]; then
+ # Bail out if the githooks directory doesn't exist.
+ echo "Place your scripts in the directory $hooks_dir/[HOOK_TRIGGER]/ where [HOOK_TRIGGER] is the"
+ echo "name of the git hook that you want your script to run under, like 'pre-push' or 'pre-commit'"
+ echo
+ echo "See the full list of git hooks here: https://git-scm.com/docs/githooks#_hooks"
+ echo
+ echo "Once you have placed your scripts, re-run this file."
+ echo
+ return
+fi
+
# --- Command line options ---
force=0
while getopts fh opt_arg; do
@@ -31,77 +48,78 @@ while getopts fh opt_arg; do
*) usage ;;
esac
done
-shift `expr $OPTIND - 1`
+shift $(expr $OPTIND - 1)
# ----------------------------
set -e
-# If there's already a pre-push hook installed bail out, we don't want to
-# overwrite it (unless -f is passed in the command line)
-pre_push_hook="$tld/.git/hooks/pre-push"
-
-if [ -e "$pre_push_hook" -a $force -eq 0 ]; then
- echo "ERROR: found an existing pre-push hook: $pre_push_hook"
- exit 1
-fi
-
-echo -n "Installing pre-push hook in $pre_push_hook: "
-
-cat > $pre_push_hook <<'EOF'
+cat > $tmp_hook <<'EOF'
#!/bin/bash
# set GITHOOKS_QUIET to anything to anything to make this script quiet
quiet=${GITHOOKS_QUIET:-""}
+[ -z "$quiet" ] && echo -e "[INFO] Starting up hook runner ..."
-origin="`git config remote.origin.url`"
-repo="`basename -s .git $origin`"
+origin="$(git config remote.origin.url)"
+repo="$(basename -s .git $origin)"
+this_script=$(basename "$0")
-hooksdir="$HOME/.githooks/$repo/pre-push"
-if [ ! -d "$hooksdir" ]; then
- echo
- echo "WARNING:"
- echo " hooks directory doesn't exist: $hooksdir"
- echo " pushing anyway"
- echo
- sleep 1
+hooks_dir=$HOME/.githooks/$repo/$this_script
+if [ ! -d "$hooks_dir" ]; then
+ echo "[WARNING] Hooks directory doesn't exist: $hooks_dir. Running $this_script anyway"
+ sleep 2
exit 0
fi
-hooksrun=0
-allhooks="`/bin/ls -1 $hooksdir 2>/dev/null`"
-
-for hook in `echo $allhooks` ; do
- if [ -x "$hooksdir/$hook" ]; then
- [ -z "$quiet" ] && echo -e "\n=== running hook: $hooksdir/$hook"
- $hooksdir/$hook "$@"
- rc=$?
- hooksrun=$((hooksrun + 1))
- if [ $rc -ne 0 ]; then
- echo
- echo "ERROR: hook $hooksdir/$hook returned non-zero status: $rc"
- echo
- exit $rc
+num_hooks_run=0
+all_hooks="$(/bin/ls -1 $hooks_dir 2>/dev/null)"
+
+for hook_name in $(echo $all_hooks) ; do
+ hook_path=$hooks_dir/$hook_name
+
+ if [ -e "$hook_path" ]; then
+ [ -z "$quiet" ] && echo -e "[INFO] Running hook: $hook_path"
+ $hook_path "$@"
+
+ hook_status=$?
+ num_hooks_run=$((num_hooks_run + 1))
+
+ if [ $hook_status -ne 0 ]; then
+ echo "[ERROR] Hook $hook_path returned non-zero status: $hook_status"
+ exit $hook_status
else
- [ -z "$quiet" ] && echo "=== done."
+ [ -z "$quiet" ] && echo "[INFO] Done."
fi
fi
done
-if [ $hooksrun -eq 0 ]; then
- echo
- echo "WARNING:"
- echo " couldn't find any pre-push hooks to run in $hooksdir"
- echo " pushing anyway"
- sleep 1
+if [ $num_hooks_run -eq 0 ]; then
+ echo "[WARNING] Couldn't find any hooks to run in $hooks_dir. Running $this_script anyway"
+ sleep 2
fi
-echo
exit 0
EOF
-chmod +x $pre_push_hook
+hook_types=($(ls -d $hooks_dir/*))
+
+for cur_hook_full_path in "${hook_types[@]}"
+do
+ cur_hook_type=$(basename $cur_hook_full_path)
+ cur_hook_path="$tld/.git/hooks/$cur_hook_type"
-echo "done."
+ echo "Installing $cur_hook_type hook in $cur_hook_path ..."
+
+ # If there's already a hook installed, bail out. We don't want to overwrite it.
+ # (unless -f is passed in the command line)
+ if [ -e "$cur_hook_path" -a $force -eq 0 ]; then
+ echo "[ERROR] Found an existing $cur_hook_type hook: $cur_hook_path"
+ exit 1
+ fi
+
+ cp $tmp_hook $cur_hook_path
+ chmod +x $cur_hook_path
+done
-mkdir -p "$hooksdir"
-echo "Now add your favorite pre-push scripts to: $hooksdir"
+rm $tmp_hook
+echo "Done."