@@ -7,6 +7,7 @@ cd "$(git rev-parse --show-toplevel)" >/dev/null
77eval $(
88 zz_args " Create HotFix branch" $0 " $@ " << -help
99 r - rebase force rebase of current commits onto hotfix branch
10+ s - stash stash current staged changes before creating hotfix branch and reapply them after
1011help
1112)
1213
2425# or if rebase is forced via command line option
2526if [ -n " $rebase " ]; then
2627 zz_log i " Rebase forced via command line option, will rebase commits onto hotfix branch"
27- elif git log --all --ancestry-path --pretty=format:%s " $main " ..HEAD | grep -vE " ^( fix(\(.+\))?:|Merge) " > /dev/null; then
28+ elif git log " $main " ..HEAD --pretty=%B | grep -E ' ^[a-zA-Z] ' | grep -vE " $main |^ fix(\(.+\))?:" > /dev/null; then
2829 zz_log w " There are commits since $main that are not of type 'fix:', creating hotfix branch only"
2930 unset rebase
3031elif [ -z " $stash " ]; then
31- zz_log i " All commits since $main are of type 'fix:', creating hotfix branch and rebasing current history onto it"
32+ zz_log i " All commits since $main are of type 'fix:', creating hotfix branch and rebasing current history + stash on top of it"
3233 rebase=true
3334fi
3435
4243
4344# Ensure working directory is clean
4445if [ -n " $( git status --porcelain) " ]; then
45- zz_log e " Working directory is not clean. Please commit or stash changes."
46- exit 1
46+
47+ if [ -n " $stash " ]; then
48+ zz_log w " Working directory is not clean. Stashing staged changes before creating hotfix branch..."
49+ git stash save -k -m " Hotfix stash: $( date +%Y-%m-%d-%H-%M-%S) "
50+ else
51+ zz_log e " Working directory is not clean. Please commit or stash changes. Use -s option to automatically stash and reapply changes."
52+ exit 1
53+ fi
54+ else
55+ zz_log i " Working directory is clean, no need to stash changes"
56+ unset stash
4757fi
4858
49- # ### PREVENT GIT EDITOR PROMPT
59+ # Set GIT_EDITOR to no-op to avoid opening editor during rebase or cherry-pick
5060GIT_EDITOR=:
5161
5262hotfix=$( echo " $main " | sed -E ' s/([0-9]+)\.([0-9]+)\.([0-9]+)/\1.\2.X/' )
5363
54- # ### START HOTFIX
55- git flow hotfix start $hotfix
64+ # Create hotfix branch
65+ git flow hotfix start $current
5666
57- # ### RESTORE STATUS AND HANDLE REBASE
67+ # If stash was used, pop it back
68+ if [ -n " $stash " ]; then
69+ zz_log i " Applying stashed changes..."
70+ git stash pop --index
71+ fi
72+
73+ # If rebase needed,
74+ # pick all "fix" commits from develop branch and rebase them onto hotfix branch,
75+ # then reset develop branch to the main tag
5876if [ -n " $rebase " ]; then
5977
60- zz_log i " Rebasing develop commits onto hotfix branch..."
61- git fix base -p hotfix/$hotfix
78+ zz_log i " Rebasing: inverting develop and hotfix branches..."
79+
80+ git fix base -p develop hotfix/" $current "
81+
82+ # Return to hotfix branch
83+ git checkout " hotfix/$current "
84+
85+ zz_log s " Successfully inverted develop and hotfix branches"
6286fi
6387
0 commit comments