Skip to content

Commit ca6828e

Browse files
feat: add cherrypick option to hotfix finish (#73)
Allows for the commits that resolve the hotfix to be applied to the develop branch instead of the entire hotfix branch. This option is not compatible with the nobackmerge or releaseBackmerge flags. The initial pull request provided by [simonweil](https://github.com/simonweil) closes #67 closes #37 closes petervanderdoes#164 closes petervanderdoes#165 Co-authored-by: simonweil <[email protected]>
1 parent 25c387c commit ca6828e

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

git-flow-hotfix

+55-4
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ showcommands! Show git commands while executing them
366366

367367
cmd_finish() {
368368
OPTIONS_SPEC="\
369-
git flow hotfix finish [-h] [-F] [-s] [-u] [-m | -f ] [-p] [-k] [-n] [-b] [-S] <version>
369+
git flow hotfix finish [-h] [-F] [-s] [-u] [-m | -f ] [-p] [-k] [-n] [-b | -c] [-S] <version>
370370
371371
Finish hotfix branch <version>
372372
--
@@ -387,6 +387,7 @@ b,[no]nobackmerge Don't back-merge master, or tag if applicable, in develop
387387
r,releaseBackmerge Back-merge to release branch if exists
388388
S,[no]squash Squash hotfix during merge
389389
T,tagname! Use given tag name
390+
c,cherrypick Cherry Pick to $DEVELOP_BRANCH instead of merge
390391
"
391392
local opts commit keepmsg remotebranchdeleted localbranchdeleted
392393

@@ -407,6 +408,7 @@ T,tagname! Use given tag name
407408
DEFINE_boolean 'squash' false "squash release during merge" S
408409
DEFINE_boolean 'squash-info' false "add branch info during squash"
409410
DEFINE_string 'tagname' "" "use the given tag name" T
411+
DEFINE_boolean 'cherrypick' false "Cherry Pick to $DEVELOP_BRANCH instead of merge" c
410412

411413
# Override defaults with values from config
412414
gitflow_override_flag_boolean "hotfix.finish.fetch" "fetch"
@@ -424,6 +426,7 @@ T,tagname! Use given tag name
424426
gitflow_override_flag_string "hotfix.finish.signingkey" "signingkey"
425427
gitflow_override_flag_string "hotfix.finish.message" "message"
426428
gitflow_override_flag_string "hotfix.finish.messagefile" "messagefile"
429+
gitflow_override_flag_boolean "hotfix.finish.cherrypick" "cherrypick"
427430

428431
# Parse arguments
429432
parse_args "$@"
@@ -453,6 +456,15 @@ T,tagname! Use given tag name
453456
FLAGS_keep=$FLAGS_TRUE
454457
fi
455458

459+
# Check that not both no merge flags were given
460+
if flag cherrypick && flag nobackmerge; then
461+
die "You can't use 'cherrypick' and 'nobackmerge' together."
462+
fi
463+
464+
if flag cherrypick && flag releasebackmerge; then
465+
die "You can't use 'cherrypick' and 'releasebackmerge' together."
466+
fi
467+
456468
# Sanity checks
457469
require_branch "$BRANCH"
458470
require_clean_working_tree
@@ -521,6 +533,42 @@ T,tagname! Use given tag name
521533

522534
run_pre_hook "$VERSION_PREFIX$TAGNAME" "$ORIGIN" "$BRANCH"
523535

536+
if flag cherrypick; then
537+
printf 'this is the cherrypick\n'
538+
read
539+
git_do checkout "$DEVELOP_BRANCH" || die "Could not check out branch '$DEVELOP_BRANCH'."
540+
541+
local old_IFS=$IFS # save the field separator
542+
IFS=$'\n' # new field separator, the end of line
543+
for git_line in $(git log --format="%H %s" --reverse $MASTER_BRANCH..$BRANCH | grep -vE "^[a-z0-9]* Merge branch '[^'].*?'$"); do
544+
local commit_hash=$(echo $git_line | cut -d" " -f1)
545+
if [[ $(git log $DEVELOP_BRANCH --grep "$commit_hash" | wc -l) -eq 0 ]]; then
546+
echo "\n${LIGHTGREEN}Cherry picking: $git_line${NOCOLOR}"
547+
git_do cherry-pick -x -s $commit_hash
548+
if [[ $? -ne 0 ]]; then
549+
echo "
550+
===============================================================
551+
= Cherry pick has conflicts, steps to continue:
552+
= 1. Fix the conflicts
553+
= 2. Stage the fixed file: '${BLUE}git add <file names>${NOCOLOR}'
554+
= 3. Continue the cherry pick: '${BLUE}git cherry-pick --continue${NOCOLOR}'
555+
= *** If fixing the conflict results in an empty commit,
556+
= you will need to run this command: '${BLUE}git commit --allow-empty${NOCOLOR}'
557+
= 4. Switch back to the hotfix branch: '${BLUE}git checkout $BRANCH${NOCOLOR}'
558+
= 5. Rerun the finish command: '${BLUE}git flow hotfix finish${NOCOLOR}'
559+
= OR run '${BLUE}git cherry-pick --abort${NOCOLOR}' to abort the cherry pick
560+
===============================================================\n"
561+
die "Cherry pick failed for commit: $commit_hash"
562+
fi
563+
else
564+
echo "\n${LIGHTGREEN}Commit has already been Cherry Picked: $git_line${NOCOLOR}"
565+
fi
566+
done
567+
IFS=$old_IFS # restore default field separator
568+
569+
git_do checkout $BRANCH || die "Could not check out branch '$BRANCH'."
570+
fi
571+
524572
# Try to merge into BASE.
525573
# In case a previous attempt to finish this release branch has failed,
526574
# but the merge into BASE was successful, we skip it now
@@ -556,7 +604,7 @@ T,tagname! Use given tag name
556604
fi
557605
fi
558606

559-
if [ "$BASE_BRANCH" = "$MASTER_BRANCH" ] && noflag nobackmerge; then
607+
if [ "$BASE_BRANCH" = "$MASTER_BRANCH" ] && noflag nobackmerge && noflag cherrypick; then
560608
# Try to merge into develop unless the user specified the nobackmerge option.
561609
# In case a previous attempt to finish this release branch has failed,
562610
# but the merge into develop was successful, we skip it now
@@ -660,12 +708,15 @@ T,tagname! Use given tag name
660708
if noflag notag; then
661709
echo "- The hotfix was tagged '$VERSION_PREFIX$TAGNAME'"
662710
fi
663-
if [ "$BASE_BRANCH" = "$MASTER_BRANCH" ]; then
711+
if flag cherrypick; then
712+
echo "- All commits from the hotfix branch have been cherry picked into '$DEVELOP_BRANCH'"
713+
elif [ "$BASE_BRANCH" = "$MASTER_BRANCH" ]; then
664714
[ "$commit" = "$BASE_BRANCH" ] && echo "- Master branch '$BASE_BRANCH' has been back-merged into '$DEVELOP_BRANCH'"
665715
[ -n "$release_branch" ] && echo "- Hotfix tag '$VERSION_PREFIX$TAGNAME' has been back-merged into '$release_branch'"
666-
[ "$commit" = "$VERSION_PREFIX$TAGNAME" ] && echo "- Hotfix tag '$VERSION_PREFIX$TAGNAME' has been back-merged into '$DEVELOP_BRANCH'"
716+
[ "$commit" = "$VERSION_PREFIX$VERSION" ] && echo "- Hotfix tag '$VERSION_PREFIX$VERSION' has been back-merged into '$DEVELOP_BRANCH'"
667717
[ "$commit" = "$BRANCH" ] && echo "- Hotfix branch '$BRANCH' has been merged into '$DEVELOP_BRANCH'"
668718
fi
719+
669720
if noflag keep; then
670721
if [ $localbranchdeleted -eq $FLAGS_TRUE ]; then
671722
keepmsg="has been locally deleted"

gitflow-common

+19
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,22 @@ run_post_hook() {
811811
flags_help() {
812812
eval "$( echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "-h" || echo exit $? )"
813813
}
814+
815+
#
816+
# Color constants
817+
#
818+
echo_color_support() {
819+
local echo_path=$(which echo)
820+
local echo_out=$($echo_path -e)
821+
if [[ $echo_out == "" ]]; then
822+
alias echo="$echo_path -e "
823+
BLUE="\e[34m"
824+
LIGHTGREEN="\e[92m"
825+
NOCOLOR="\e[0m"
826+
else
827+
BLUE=""
828+
LIGHTGREEN=""
829+
NOCOLOR=""
830+
fi
831+
}
832+
echo_color_support

0 commit comments

Comments
 (0)