Commit 203a793f authored by Vincent Driessen's avatar Vincent Driessen

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.
parent 31e637d8
...@@ -186,10 +186,10 @@ gitflow_is_initialized() { ...@@ -186,10 +186,10 @@ gitflow_is_initialized() {
# loading settings that can be overridden using git config # loading settings that can be overridden using git config
gitflow_load_settings() { gitflow_load_settings() {
export DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) 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 MASTER_BRANCH=$(git config --get gitflow.branch.master)
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop) export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
export ORIGIN=$(git config --get gitflow.origin || echo origin) export ORIGIN=$(git config --get gitflow.origin || echo origin)
export GITFLOW_DIR_HOOKS="$DOT_GIT_DIR"/hooks
} }
# #
...@@ -319,40 +319,43 @@ require_branches_equal() { ...@@ -319,40 +319,43 @@ require_branches_equal() {
} }
# #
# do_hook # run_pre_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.
# #
# The hook called should be executable. # Looks for a Git hook script called
# The hooks should return an exit code 0 on success. Any other return code will result in aborting the git flow process. #
# pre-flow-<subcmd>-<subaction>
# #
# Naming convention of a hook: # If such a hook script exists and is executable, it is called with the given
# <prefix>-flow-<SUB COMMAND>-<SUB ACTION> # positional arguments. If its return code non-zero, the git-flow action is
# Example for a hook called before the command git flow feature start test is executed: # aborted.
# pre-flow-feature-start
# #
do_hook() { run_pre_hook() {
local prefix="$1" local scriptfile="${HOOKS_DIR}/pre-flow-${SUBCOMMAND}-${SUBACTION}"
local return_code=0 local exitcode=0
if [ -z $prefix ]; then if [ -x $scriptfile ]; then
die "Hook implementation error - No prefix given" $scriptfile "$@"
fi exitcode=$?
if [ $prefix != "pre" ] && [ $prefix != "post" ]; then
die "Hook implementation error - Bad Prefix"
fi
if [ -x ${GITFLOW_DIR_HOOKS}${prefix}-flow-${SUBCOMMAND}-${SUBACTION} ]; then if [ $exitcode -gt 0 ]; then
${GITFLOW_DIR_HOOKS}${prefix}-flow-${SUBCOMMAND}-${SUBACTION} die "fatal: Hook command $scriptfile ended with exit code $exitcode."
return_code=$?
if [ $return_code -gt 0 ]; then
die "Hook command ${prefix}-flow-${SUBCOMMAND}-${SUBACTION} failed. Exit code $return_code"
fi fi
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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment