Skip to content

Commit 7195876

Browse files
committed
fix(gitutils): 🐛 update hotfix branch creation logic to use ancestry path
1 parent c1744ba commit 7195876

1 file changed

Lines changed: 7 additions & 53 deletions

File tree

src/gitutils/_git-release-hotfix.sh

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ fi
2424
# or if rebase is forced via command line option
2525
if [ -n "$rebase" ]; then
2626
zz_log i "Rebase forced via command line option, will rebase commits onto hotfix branch"
27-
elif git log "$main"..HEAD --pretty=%B | grep -v -E '^fix(\(.+\))?: ' | grep -E '^[a-zA-Z]' >/dev/null; then
28-
zz_log w "There are commits since $main that are not of type 'fix:', creating hotfix branch and reapplying stash"
29-
rebase=false
30-
else
31-
zz_log i "All commits since $main are of type 'fix:', creating hotfix branch and rebasing current history + stash on top of it"
27+
elif git log --all --ancestry-path --pretty=format:%s "$main"..HEAD | grep -vE "^(fix(\(.+\))?:|Merge)" >/dev/null; then
28+
zz_log w "There are commits since $main that are not of type 'fix:', creating hotfix branch only"
29+
unset rebase
30+
elif [ -z "$stash" ]; then
31+
zz_log i "All commits since $main are of type 'fix:', creating hotfix branch and rebasing current history onto it"
3232
rebase=true
3333
fi
3434

3535
# If rebase needed, check that develop branch has not been pushed since last tag
3636
if [ -n "$rebase" ]; then
37-
if [ "$(git rev-parse develop)" != "$(git rev-parse origin/develop)" ]; then
37+
if [ "$(git rev-parse develop)" = "$(git rev-parse origin/develop)" ]; then
3838
zz_log e "Develop branch has been pushed since last tag, cannot rebase safely, aborting"
3939
exit 1
4040
fi
@@ -58,53 +58,7 @@ git flow hotfix start $current
5858
if [ -n "$rebase" ]; then
5959

6060
zz_log i "Rebasing: inverting develop and hotfix branches..."
61-
62-
# Store current branch heads
63-
develop_head=$(git rev-parse develop)
64-
hotfix_head=$(git rev-parse "hotfix/$current")
65-
66-
# Pick all "fix" commits from develop and rebase them onto hotfix branch
67-
zz_log i "Cherry-picking fix commits from develop branch..."
68-
69-
# Get all fix commits from develop since the main tag
70-
fix_commits=$(git log --reverse --pretty=format:"%H" "$main"..develop --grep="^fix")
71-
72-
if [ -n "$fix_commits" ]; then
73-
# Cherry-pick each fix commit onto the hotfix branch
74-
echo "$fix_commits" | while read commit; do
75-
76-
zz_log i "Cherry-picking commit: $(git log --oneline -1 $commit)"
77-
if ! git cherry-pick "$commit"; then
78-
zz_log e "Failed to cherry-pick commit $commit"
79-
zz_log i "Please resolve conflicts and run 'git cherry-pick --continue'"
80-
exit 1
81-
fi
82-
done
83-
zz_log s "Successfully cherry-picked all fix commits"
84-
85-
# Now, remove the fix commits on develop branch that are now on hotfix
86-
echo "$fix_commits" | while read commit; do
87-
88-
if ! git checkout develop && git revert --no-commit "$commit"; then
89-
zz_log e "Failed to revert commit $commit on develop"
90-
exit 1
91-
fi
92-
93-
done
9461

95-
96-
else
97-
zz_log i "No fix commits found to cherry-pick"
98-
fi
99-
100-
# Reset develop branch to the main tag (removing the fix commits that are now on hotfix)
101-
git checkout develop
102-
git reset --hard "$main"
103-
zz_log i "Reset develop branch to $main"
104-
105-
# Return to hotfix branch
106-
git checkout "hotfix/$current"
107-
108-
zz_log s "Successfully inverted develop and hotfix branches"
62+
git fix base hotfix/$current develop
10963
fi
11064

0 commit comments

Comments
 (0)