Give all subcommands an optional setup() function that will be called by git-flow in order to let the subcommand initialize its environment.
Give all the branch-type subcommands a default explicit "list" action, too.
Order the functions inside each of the subcommands in a specific order, for consistency:
- usage()
- setup()
- cmd_default()
- cmd_list()
- cmd_help()
- parse_args()
- other commands
diff --git a/git-flow b/git-flow
index de8d9a1..96c2e52 100755
--- a/git-flow
+++ b/git-flow
@@ -90,6 +90,10 @@
fi
# run the specified action
+ # if the subcommand declares a setup() function, call that first
+ if typeset -f setup >/dev/null; then
+ setup
+ fi
cmd_$SUBACTION "$@"
}
diff --git a/git-flow-feature b/git-flow-feature
index 6f3a0bb..8060d47 100644
--- a/git-flow-feature
+++ b/git-flow-feature
@@ -13,7 +13,7 @@
#
usage() {
- echo "usage: git flow feature"
+ echo "usage: git flow feature [list]"
echo " git flow feature start <name> [<base>]"
echo " git flow feature finish <name> [<base>]"
echo " git flow feature publish <name>"
@@ -34,21 +34,16 @@
#echo "--push Push to the origin repo when finished"
}
-parse_args() {
- NAME="$1"
- BASE="${2:-$DEVELOP_BRANCH}"
- if [ "$NAME" = "" ]; then
- echo "Missing argument <name>."
- usage
- exit 1
- fi
+# setup will always be called before the actual cmd_* functions
+setup() {
PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
- BRANCH=$PREFIX$NAME
}
cmd_default() {
- # TODO: Refactor getting this prefix into a general function
- PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
+ cmd_list "$@"
+}
+
+cmd_list() {
FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
if [ -z "$FEATURE_BRANCHES" ]; then
warn "No feature branches exist."
@@ -62,6 +57,18 @@
exit 0
}
+parse_args() {
+ NAME="$1"
+ BASE="${2:-$DEVELOP_BRANCH}"
+ if [ "$NAME" = "" ]; then
+ echo "Missing argument <name>."
+ usage
+ exit 1
+ fi
+ PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
+ BRANCH=$PREFIX$NAME
+}
+
cmd_start() {
parse_args "$@"
diff --git a/git-flow-hotfix b/git-flow-hotfix
index d921762..153c5ea 100644
--- a/git-flow-hotfix
+++ b/git-flow-hotfix
@@ -13,7 +13,7 @@
#
usage() {
- echo "usage: git flow hotfix"
+ echo "usage: git flow hotfix [list]"
echo " git flow hotfix start <version> [<base>]"
echo " git flow hotfix finish <version> [<base>]"
# TODO
@@ -28,21 +28,16 @@
#echo "--push Push to the origin repo when finished"
}
-parse_args() {
- VERSION="$1"
- BASE="${2:-$MASTER_BRANCH}"
- if [ "$VERSION" = "" ]; then
- echo "Missing argument <version>."
- usage
- exit 1
- fi
+# setup will always be called before the actual cmd_* functions
+setup() {
PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
- BRANCH=$PREFIX$VERSION
}
cmd_default() {
- # TODO: Refactor getting this prefix into a general function
- PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
+ cmd_list "$@"
+}
+
+cmd_list() {
HOTFIX_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
if [ -z "$HOTFIX_BRANCHES" ]; then
warn "No hotfix branches exist."
@@ -56,6 +51,18 @@
exit 0
}
+parse_args() {
+ VERSION="$1"
+ BASE="${2:-$MASTER_BRANCH}"
+ if [ "$VERSION" = "" ]; then
+ echo "Missing argument <version>."
+ usage
+ exit 1
+ fi
+ PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/)
+ BRANCH=$PREFIX$VERSION
+}
+
cmd_start() {
parse_args "$@"
diff --git a/git-flow-init b/git-flow-init
index 84ba142..7b1b4b6 100644
--- a/git-flow-init
+++ b/git-flow-init
@@ -16,11 +16,6 @@
echo "usage: git flow init"
}
-cmd_help() {
- usage
- exit 0
-}
-
# Default entry when no SUBACTION is given
cmd_default() {
echo
@@ -76,3 +71,7 @@
fi
}
+cmd_help() {
+ usage
+ exit 0
+}
diff --git a/git-flow-release b/git-flow-release
index 1fec600..b5f5959 100644
--- a/git-flow-release
+++ b/git-flow-release
@@ -13,7 +13,7 @@
#
usage() {
- echo "usage: git flow release"
+ echo "usage: git flow release [list]"
echo " git flow release start <version>"
echo " git flow release finish <version>"
# TODO
@@ -29,21 +29,17 @@
#echo "--push Push to the origin repo when finished"
}
-parse_args() {
- VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
- VERSION="$1"
- if [ "$VERSION" = "" ]; then
- echo "Missing argument <version>."
- usage
- exit 1
- fi
+# setup will always be called before the actual cmd_* functions
+setup() {
PREFIX=$(git config --get gitflow.prefix.release || echo release/)
- BRANCH=$PREFIX$VERSION
+ VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
}
cmd_default() {
- # TODO: Refactor getting this prefix into a general function
- PREFIX=$(git config --get gitflow.prefix.release || echo release/)
+ cmd_list "$@"
+}
+
+cmd_list() {
RELEASE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
if [ -z "$RELEASE_BRANCHES" ]; then
warn "No release branches exist."
@@ -57,6 +53,16 @@
exit 0
}
+parse_args() {
+ VERSION="$1"
+ if [ "$VERSION" = "" ]; then
+ echo "Missing argument <version>."
+ usage
+ exit 1
+ fi
+ BRANCH=$PREFIX$VERSION
+}
+
cmd_start() {
parse_args "$@"
diff --git a/git-flow-support b/git-flow-support
index adb336d..fe7ce17 100644
--- a/git-flow-support
+++ b/git-flow-support
@@ -13,10 +13,32 @@
#
usage() {
- echo "usage: git flow support"
+ echo "usage: git flow support [list]"
echo " git flow support start <version> [<base>]"
}
+setup() {
+ PREFIX=$(git config --get gitflow.prefix.support || echo support/)
+}
+
+cmd_default() {
+ cmd_list "$@"
+}
+
+cmd_list() {
+ SUPPORT_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
+ if [ -z "$SUPPORT_BRANCHES" ]; then
+ warn "No support branches exist."
+ exit 0
+ fi
+ echo "$SUPPORT_BRANCHES" | sed "s?^$PREFIX??g"
+}
+
+cmd_help() {
+ usage
+ exit 0
+}
+
parse_args() {
VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
VERSION="$1"
@@ -30,22 +52,6 @@
BRANCH=$PREFIX$VERSION
}
-cmd_default() {
- # TODO: Refactor getting this prefix into a general function
- PREFIX=$(git config --get gitflow.prefix.support || echo support/)
- SUPPORT_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
- if [ -z "$SUPPORT_BRANCHES" ]; then
- warn "No support branches exist."
- exit 0
- fi
- echo "$SUPPORT_BRANCHES" | sed "s?^$PREFIX??g"
-}
-
-cmd_help() {
- usage
- exit 0
-}
-
cmd_start() {
parse_args "$@"
diff --git a/git-flow-version b/git-flow-version
index cbb66b6..e394de9 100644
--- a/git-flow-version
+++ b/git-flow-version
@@ -17,11 +17,11 @@
echo "usage: git flow version"
}
+cmd_default() {
+ echo "$GITFLOW_VERSION"
+}
+
cmd_help() {
usage
exit 0
}
-
-cmd_default() {
- echo "$GITFLOW_VERSION"
-}