From de9158ba6ab2bacfae26fb13f91b4673c3507752 Mon Sep 17 00:00:00 2001 From: Paulo Silva Date: Wed, 3 Aug 2016 16:51:07 +0100 Subject: [PATCH 1/2] Automate Pre-Commit hook install if it exists on git-flow hooks directory If there's a local pre-commit hook, a backup will be created. Then a symbolic link is created .git/hooks/pre-commit -> $hooks_dir/pre-commit --- git-flow-init | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/git-flow-init b/git-flow-init index f577ab08..63639abc 100644 --- a/git-flow-init +++ b/git-flow-init @@ -374,6 +374,17 @@ file= use given config file git_do config $gitflow_config_option gitflow.path.hooks "$hooks_dir" fi + # Automate Pre-Commit hook install if it exists on git-flow hooks directory + hooks_dir=$(git config --get gitflow.path.hooks) + if [ -f "$hooks_dir/pre-commit" ]; then + # Check whether there's a previous local pre-commit hook + if [ -f "./.git/hooks/pre-commit" ]; then + echo "A backup of your local pre-commit hook will be created." + fi + ln -s "$hooks_dir/pre-commit" -t "./.git/hooks" --backup + fi + + # TODO: what to do with origin? } From 0edb59fc20048325962cdde042c883faa2c94bd0 Mon Sep 17 00:00:00 2001 From: PauloASilva Date: Thu, 11 Aug 2016 01:09:33 +0100 Subject: [PATCH 2/2] allow user to decide whether to install the pre-commit hook default option is "Y" (install the hook). where --force is used the local pre-commit hook, if some, will be saved. --- git-flow-init | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/git-flow-init b/git-flow-init index 63639abc..ee209134 100644 --- a/git-flow-init +++ b/git-flow-init @@ -374,17 +374,28 @@ file= use given config file git_do config $gitflow_config_option gitflow.path.hooks "$hooks_dir" fi - # Automate Pre-Commit hook install if it exists on git-flow hooks directory + # Automate pre-commit hook install if it exists on git-flow hooks directory hooks_dir=$(git config --get gitflow.path.hooks) + hooks_dir=${hooks_dir%%/} if [ -f "$hooks_dir/pre-commit" ]; then - # Check whether there's a previous local pre-commit hook - if [ -f "./.git/hooks/pre-commit" ]; then - echo "A backup of your local pre-commit hook will be created." + DOT_GIT_DIR=$(git rev-parse --git-dir) + LOCAL_GIT_HOOKS_DIR="$DOT_GIT_DIR/hooks" + # Check whether there's a previous local pre-commit hook to backup + if [ -f "$LOCAL_GIT_HOOKS_DIR/pre-commit" ]; then + backup="(local hook will be saved)" + fi + default_suggestion="Y" + printf "Install pre-commit hook%s? [$default_suggestion] " " $backup" + if noflag defaults; then + read answer + else + printf "\n" + fi + if [ "${answer:-$default_suggestion}" = "Y" ]; then + ln -s "$hooks_dir/pre-commit" -t "$LOCAL_GIT_HOOKS_DIR" --backup fi - ln -s "$hooks_dir/pre-commit" -t "./.git/hooks" --backup fi - # TODO: what to do with origin? }