Add directory setup to the git flow init process

git flow will fail when the directories are not setup in the git config.

Signed-off-by: Peter van der Does <peter@avirtualhome.com>
Signed-off-by: Vincent Driessen <vincent@3rdcloud.com>
diff --git a/git-flow-init b/git-flow-init
index 4afa1c2..b64fa62 100644
--- a/git-flow-init
+++ b/git-flow-init
@@ -222,7 +222,7 @@
 		git checkout -q "$develop_branch"
 	fi
 
-	# finally, ask the user for naming conventions (branch and tag prefixes)
+	# Ask the user for naming conventions (branch and tag prefixes )
 	if flag force || \
 	   ! git config --get gitflow.prefix.feature >/dev/null 2>&1 || 
 	   ! git config --get gitflow.prefix.release >/dev/null 2>&1 || 
@@ -303,6 +303,47 @@
 		git config gitflow.prefix.versiontag "$prefix"
 	fi
 
+	# Ask the user for gitflow configuration directory (main and hooks )
+	if flag force || \
+	   ! git config --get gitflow.dir.main >/dev/null 2>&1 ||
+	   ! git config --get gitflow.dir.hooks >/dev/null 2>&1; then
+		echo
+		echo "Where is the gitflow directory located?"
+	fi
+
+	local dir
+	# Main directory
+	if ! git config --get gitflow.dir.main >/dev/null 2>&1 || flag force; then
+		default_suggestion=$(git config --get gitflow.dir.main || echo .gitflow/)
+		printf "gitflow configuration directory? [$default_suggestion] "
+		if noflag defaults; then
+			read answer
+		else
+			printf "\n"
+		fi
+		[ "$answer" = "-" ] && dir= || dir=${answer:-$default_suggestion}
+		if [ ! -d "$dir" ] ; then
+			mkdir "$dir" >/dev/null 2>&1 || die "Can not create directory ${dir}"
+		fi
+		git config gitflow.dir.main "$dir"
+	fi
+
+	local subdir
+	# Hooks directory
+	if ! git config --get gitflow.dir.hooks >/dev/null 2>&1 || flag force; then
+		default_suggestion=$(git config --get gitflow.dir.hooks || echo hooks/)
+		printf "gitflow hooks directory? [$default_suggestion] "
+		if noflag defaults; then
+			read answer
+		else
+			printf "\n"
+		fi
+		[ "$answer" = "-" ] && subdir= || subdir=${answer:-$default_suggestion}
+		if [ ! -d "$dir""$subdir" ] ; then
+			mkdir -p "$dir""$subdir"  >/dev/null 2>&1 || die "Can not create directory ${dir}${subdir}"
+		fi
+		git config gitflow.dir.hooks "$subdir"
+	fi
 
 	# TODO: what to do with origin?
 }
diff --git a/gitflow-common b/gitflow-common
index fb515de..1b13739 100644
--- a/gitflow-common
+++ b/gitflow-common
@@ -175,12 +175,18 @@
 	git config --get gitflow.prefix.versiontag >/dev/null 2>&1
 }
 
+gitflow_has_dirs_configured() {
+	git config --get gitflow.dir.main >/dev/null 2>&1      && \
+	git config --get gitflow.dir.hooks >/dev/null 2>&1
+}
+
 gitflow_is_initialized() {
 	gitflow_has_master_configured                    && \
 	gitflow_has_develop_configured                   && \
 	[ "$(git config --get gitflow.branch.master)" !=    \
 	  "$(git config --get gitflow.branch.develop)" ] && \
-	gitflow_has_prefixes_configured
+	gitflow_has_prefixes_configured                  && \
+	gitflow_has_dirs_configured
 }
 
 # loading settings that can be overridden using git config
@@ -189,6 +195,7 @@
 	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=$(git config --get gitflow.dir.main)$(git config --get gitflow.dir.hooks)
 }
 
 #