Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

implement hook logic to rename branch in bugfix and feature (fix #352) #353

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions git-flow-bugfix
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ F,[no]fetch Fetch from origin before performing local operation
git_fetch_branch "$ORIGIN" "$base"
fi

# hook for branch renaming
run_prepare_name_branch_hook "$NAME"

# Sanity checks
require_branch_absent "$BRANCH"

Expand Down
29 changes: 19 additions & 10 deletions git-flow-feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
initialize() {
require_git_repo
require_gitflow_initialized
git config --get gitflow.prefix.feature >/dev/null 2>&1 || die "Feature prefix not set. Please run 'git flow init'."
git config --get gitflow.prefix.feature >/dev/null 2>&1 || die "Feature prefix not set. Please run 'git flow
init'."
gitflow_load_settings
PREFIX=$(git config --get gitflow.prefix.feature)
}
Expand Down Expand Up @@ -205,6 +206,9 @@ F,[no]fetch Fetch from origin before performing local operation
git_fetch_branch "$ORIGIN" "$base"
fi

# hook for branch renaming
run_prepare_name_branch_hook "$NAME"

# Sanity checks
require_branch_absent "$BRANCH"

Expand All @@ -214,8 +218,6 @@ F,[no]fetch Fetch from origin before performing local operation
require_branches_equal "$base" "$ORIGIN/$base"
fi

run_pre_hook "$NAME" "$ORIGIN" "$BRANCH" "$base"

gitflow_config_set_base_branch $base $BRANCH

# create branch
Expand Down Expand Up @@ -299,7 +301,8 @@ no-ff! Never fast-forward during the merge

BASE_BRANCH=$(gitflow_config_get_base_branch $BRANCH)
BASE_BRANCH=${BASE_BRANCH:-$DEVELOP_BRANCH}
git_local_branch_exists "$BASE_BRANCH" || die "The base '$BASE_BRANCH' doesn't exists locally or is not a branch. Can't finish the feature branch '$BRANCH'."
git_local_branch_exists "$BASE_BRANCH" || die "The base '$BASE_BRANCH' doesn't exists locally or is not a
branch. Can't finish the feature branch '$BRANCH'."

# Detect if we're restoring from a merge conflict
if [ -f "$DOT_GIT_DIR/.gitflow/MERGE_BASE" ]; then
Expand Down Expand Up @@ -400,7 +403,8 @@ no-ff! Never fast-forward during the merge
fi
else
git_do merge --squash "$BRANCH"
flag squash_info && gitflow_create_squash_message "Merged feature branch '$BRANCH'" "$BASE_BRANCH" "$BRANCH" > "$DOT_GIT_DIR/SQUASH_MSG"
flag squash_info && gitflow_create_squash_message "Merged feature branch '$BRANCH'" "$BASE_BRANCH"
"$BRANCH" > "$DOT_GIT_DIR/SQUASH_MSG"
git_do commit
fi

Expand Down Expand Up @@ -470,7 +474,8 @@ helper_finish_cleanup() {
fi

if flag push; then
git_do push "$ORIGIN" "$BASE_BRANCH" || die "Could not push branch '$BASE_BRANCH' to remote '$ORIGIN'."
git_do push "$ORIGIN" "$BASE_BRANCH" || die "Could not push branch '$BASE_BRANCH' to remote
'$ORIGIN'."
fi

echo
Expand Down Expand Up @@ -665,7 +670,8 @@ p,[no]preserve-merges Preserve merges

require_branch "$BRANCH"

git_local_branch_exists "$BASE_BRANCH" || die "The base '$BASE_BRANCH' doesn't exists locally or is not a branch. Can't rebase the feature branch '$BRANCH'."
git_local_branch_exists "$BASE_BRANCH" || die "The base '$BASE_BRANCH' doesn't exists locally or is not a
branch. Can't rebase the feature branch '$BRANCH'."

git_do checkout -q "$BRANCH" || die "Could not check out branch '$BRANCH'."
if flag interactive; then
Expand Down Expand Up @@ -703,7 +709,8 @@ showcommands! Show git commands while executing them
# Define flags
DEFINE_boolean 'rebase' false "pull with rebase" r

warn "The command 'git flow feature pull' will be deprecated per version 2.0.0. Use 'git flow feature track' instead."
warn "The command 'git flow feature pull' will be deprecated per version 2.0.0. Use 'git flow feature track'
instead."

# Parse arguments
parse_remote_name "$@"
Expand Down Expand Up @@ -739,7 +746,8 @@ showcommands! Show git commands while executing them
# remote changes in
if flag rebase; then
if ! git_do pull --rebase -q "$REMOTE" "$BRANCH"; then
warn "Pull was aborted. There might be conflicts during rebase or '$REMOTE' might be inaccessible."
warn "Pull was aborted. There might be conflicts during rebase or '$REMOTE' might be
inaccessible."
exit 1
fi
else
Expand All @@ -749,7 +757,8 @@ showcommands! Show git commands while executing them
echo "Pulled $REMOTE's changes into $BRANCH."
else
# Setup the local branch clone for the first time
git_do fetch -q "$REMOTE" "$BRANCH" || die "Could not fetch branch '$BRANCH' from remote '$REMOTE'." # Stores in FETCH_HEAD
git_do fetch -q "$REMOTE" "$BRANCH" || die "Could not fetch branch '$BRANCH' from remote '$REMOTE'."
# Stores in FETCH_HEAD
git_do branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed."
git_do checkout -q "$BRANCH" || die "Could not check out branch '$BRANCH'."
echo "Created local branch $BRANCH based on $REMOTE's $BRANCH."
Expand Down
50 changes: 44 additions & 6 deletions gitflow-common
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ gitflow_rename_branch() {
NAME=$2
fi
BRANCH=${PREFIX}${NAME}
NEW_BRANCH=${PREFIX}${NEW_NAME}
NEW_NAME=${PREFIX}${NEW_NAME}

if [ -z "$NEW_NAME" ]; then
die "No new name given."
Expand All @@ -633,16 +633,16 @@ gitflow_rename_branch() {

# Sanity checks
require_branch "$BRANCH"
require_branch_absent "$NEW_BRANCH"
require_branch_absent "$NEW_NAME"

run_pre_hook "$NAME" "$ORIGIN" "$BRANCH"
git_do branch -m "$BRANCH" "$NEW_BRANCH" || die "Error renaming branch '$BRANCH' to '$NEW_BRANCH'"
gitflow_config_rename_sections "$BRANCH" "$NEW_BRANCH"
git_do branch -m "$BRANCH" "$NEW_NAME" || die "Error renaming branch '$BRANCH' to '$NEW_NAME'"
gitflow_config_rename_sections "$BRANCH" "$NEW_NAME"
run_post_hook "$NAME" "$ORIGIN" "$BRANCH"

echo
echo "Summary of actions:"
echo "- Branch '$BRANCH' has been renamed to '$NEW_BRANCH'."
echo "- Branch '$BRANCH' has been renamed to '$NEW_NAME'."
echo "- You are now on branch '$(git_current_branch)'"
echo
}
Expand All @@ -656,7 +656,8 @@ require_git_repo() {

require_gitflow_initialized() {
gitflow_is_initialized || die "Not a gitflow-enabled repo yet. Please run 'git flow init' first."
$(git config --get gitflow.prefix.versiontag >/dev/null 2>&1) || die "Version tag not set. Please run 'git flow init'."
$(git config --get gitflow.prefix.versiontag >/dev/null 2>&1) || die "Version tag not set. Please run 'git
flow init'."
}

require_clean_working_tree() {
Expand Down Expand Up @@ -760,6 +761,43 @@ run_filter_hook() {
fi
}

#
# run_prepare_name_branch_hook
#
# Looks for a Git hook script called
#
# pre-flow-<subcmd>-prepare-name-branch
#
# 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.
#
run_prepare_name_branch_hook() {
local scriptfile exitcode
echo "in run_prepare_name_branch_hook"

scriptfile="${HOOKS_DIR}/pre-flow-${SUBCOMMAND}-prepare-name-branch"
exitcode=0
if [ -x "$scriptfile" ]; then
OUTPUT=$("$scriptfile" "$@")
exitcode=$?
echo "exitcode=" $exitcode
if [ $exitcode -gt 0 ]; then
die "Hook command $scriptfile ended with exit code $exitcode."
fi
read NEW_NAME REST <<< $OUTPUT
if [ ! -z "$NEW_NAME" ]; then
if [ ! -z "$REST" ]; then
die "Hook command $scriptfile returned more than 1 argument instead of 1 (BRANCH)"
else
echo "Renaming $NAME to $NEW_NAME"
NAME=$NEW_NAME
BRANCH=$PREFIX$NEW_NAME
fi
fi
fi
}

#
# run_pre_hook
#
Expand Down
18 changes: 18 additions & 0 deletions hooks/pre-flow-bugfix-prepare-name-branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
#
# Runs before git flow bugfix start
#
# Positional arguments:
# $1 The friendly name of the branch
#
# The following variables are available as they are exported by git-flow:
#
# MASTER_BRANCH - The branch defined as Master
# DEVELOP_BRANCH - The branch defined as Develop
#
NAME=$1

# Implement your script here that outputs to stdout the new name of the branch

# To terminate the git-flow action, return a non-zero exit code.
exit 0
18 changes: 18 additions & 0 deletions hooks/pre-flow-feature-prepare-name-branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
#
# Runs before git flow feature start
#
# Positional arguments:
# $1 The friendly name of the branch
#
# The following variables are available as they are exported by git-flow:
#
# MASTER_BRANCH - The branch defined as Master
# DEVELOP_BRANCH - The branch defined as Develop
#
NAME=$1

# Implement your script here that outputs to stdout the new name of the branch

# To terminate the git-flow action, return a non-zero exit code.
exit 0