Refactor the code a bit.
It now has explicit pre and post functions to communicate the difference
in exit code handling. Also, allow for passing in positional arguments
that make sense on a per-action basis.
diff --git a/gitflow-common b/gitflow-common
index f85d575..dbb09e3 100644
--- a/gitflow-common
+++ b/gitflow-common
@@ -186,10 +186,10 @@
# loading settings that can be overridden using git config
gitflow_load_settings() {
export DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
+ export HOOKS_DIR="$DOT_GIT_DIR"/hooks
export MASTER_BRANCH=$(git config --get gitflow.branch.master)
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
export ORIGIN=$(git config --get gitflow.origin || echo origin)
- export GITFLOW_DIR_HOOKS="$DOT_GIT_DIR"/hooks
}
#
@@ -319,40 +319,43 @@
}
#
-# do_hook
-# Inputs:
-# $1 = prefix of the hook
-# Currently the following prefixes are supported
-# pre - Executed before the command is executed
-# post - Executed after the command is successfully executed.
+# run_pre_hook
#
-# The hook called should be executable.
-# The hooks should return an exit code 0 on success. Any other return code will result in aborting the git flow process.
+# Looks for a Git hook script called
+#
+# pre-flow-<subcmd>-<subaction>
#
-# Naming convention of a hook:
-# <prefix>-flow-<SUB COMMAND>-<SUB ACTION>
-# Example for a hook called before the command git flow feature start test is executed:
-# pre-flow-feature-start
+# If such a hook script exists and is executable, it is called with the given
+# positional arguments. If its return code non-zero, the git-flow action is
+# aborted.
#
-do_hook() {
- local prefix="$1"
- local return_code=0
+run_pre_hook() {
+ local scriptfile="${HOOKS_DIR}/pre-flow-${SUBCOMMAND}-${SUBACTION}"
+ local exitcode=0
- if [ -z $prefix ]; then
- die "Hook implementation error - No prefix given"
- fi
- if [ $prefix != "pre" ] && [ $prefix != "post" ]; then
- die "Hook implementation error - Bad Prefix"
- fi
+ if [ -x $scriptfile ]; then
+ $scriptfile "$@"
+ exitcode=$?
- if [ -x ${GITFLOW_DIR_HOOKS}${prefix}-flow-${SUBCOMMAND}-${SUBACTION} ]; then
- ${GITFLOW_DIR_HOOKS}${prefix}-flow-${SUBCOMMAND}-${SUBACTION}
-
- return_code=$?
-
- if [ $return_code -gt 0 ]; then
- die "Hook command ${prefix}-flow-${SUBCOMMAND}-${SUBACTION} failed. Exit code $return_code"
+ if [ $exitcode -gt 0 ]; then
+ die "fatal: Hook command $scriptfile ended with exit code $exitcode."
fi
fi
+}
-}
\ No newline at end of file
+#
+# run_post_hook
+#
+# Looks for a Git hook script called
+#
+# post-flow-<subcmd>-<subaction>
+#
+# If such a hook script exists and is executable, it is called with the given
+# positional arguments. Its return code is ignored.
+#
+run_post_hook() {
+ local scriptfile="${HOOKS_DIR}/post-flow-${SUBCOMMAND}-${SUBACTION}"
+ if [ -x $scriptfile ]; then
+ $scriptfile "$@"
+ fi
+}