Skip to content

Commit 166fcde

Browse files
committed
fix(gitutils): šŸ› add stash option to handle uncommitted changes before hotfix
1 parent e112b91 commit 166fcde

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

ā€Žsrc/gitutils/_git-fix-base.shā€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
eval $(
55
zz_args "Fix git base - rebase commits from one branch to another" $0 "$@" <<-help
66
p - push force push changes to remote
7-
d - dryrun show what would be done without making changes
7+
n - dry-run show what would be done without making changes
8+
- source source source branch to take commits from (default: current branch)
89
- target target target branch to rebase commits onto
910
- source source source branch to take commits from (default: current branch)
1011
help
@@ -176,8 +177,8 @@ else
176177
zz_log i "Staying on '$target' branch"
177178
fi
178179

179-
# Push changes if force flag is set
180-
if [ -n "$force" ]; then
180+
# Push changes if push flag is set
181+
if [ -n "$push" ]; then
181182
zz_log i "Pushing changes to remote..."
182183

183184
# Push source branch (reset)

ā€Žsrc/gitutils/_git-release-hotfix.shā€Ž

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cd "$(git rev-parse --show-toplevel)" >/dev/null
77
eval $(
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
1011
help
1112
)
1213

@@ -24,11 +25,11 @@ fi
2425
# or if rebase is forced via command line option
2526
if [ -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
3031
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"
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
3334
fi
3435

@@ -42,22 +43,45 @@ fi
4243

4344
# Ensure working directory is clean
4445
if [ -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
4757
fi
4858

49-
#### PREVENT GIT EDITOR PROMPT
59+
# Set GIT_EDITOR to no-op to avoid opening editor during rebase or cherry-pick
5060
GIT_EDITOR=:
5161

5262
hotfix=$(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
5876
if [ -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"
6286
fi
6387

0 commit comments

Comments
Ā (0)