-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipgit-hotfix
280 lines (234 loc) · 9.2 KB
/
shipgit-hotfix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
usage() {
echo "usage: shipgit hotfix [action] [-options]"
echo " shipgit hotfix start <branch_name>"
echo " shipgit hotfix finish (patch|minor|major) -m(optional)"
echo " shipgit hotfix sync"
echo " shipgit hotfix end"
}
cmd_help() {
usage
exit 0
}
cmd_default() {
usage
exit 0
}
handle_main_merge_conflict() {
MAIN_BRANCH=$(git config --get shipgit.branch.main)
HOTFIX_BRANCH=$1
echo "+++++++++++++++++++++++++++ MERGE CONFLICT +++++++++++++++++++++++++++"
echo ""
echo "There are merge conflicts between $MAIN_BRANCH and $HOTFIX_BRANCH"
echo ""
echo "Please fix the conflicts and when you are done run:"
echo " $ git commit"
echo ""
echo "After that you can checkout back to your hotfix branch and run:"
echo " $ shipgit hotfix end"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
handle_no_tag() {
echo "++++++++++ ATTENTION ++++++++++"
echo ""
echo "No tag to end the hotfix with"
echo ""
echo "+++++++++++++++++++++++++++++++"
exit 1
}
handle_hotfix_finish_conflict() {
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
HOTFIX_BRANCH=$1
BUMP_TYPE=$2
echo "++++++++++++++++++++++++++ REBASE CONFLICT +++++++++++++++++++++++++++"
echo ""
echo "There are conflicts between $HOTFIX_BRANCH and $PRODUCTION_BRANCH"
echo ""
echo "Please fix the conflicts and when you are done, run:"
echo " $ git rebase --continue"
echo ""
echo "After that run:"
echo " $ shipgit hotfix finish $BUMP_TYPE"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
handle_hotfix_sync_conflict() {
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
HOTFIX_BRANCH=$1
echo "++++++++++++++++++++++++++ REBASE CONFLICT +++++++++++++++++++++++++++"
echo ""
echo "There are conflicts while between $HOTFIX_BRANCH with $PRODUCTION_BRANCH"
echo ""
echo "Please fix the conflicts and run:"
echo " git rebase --continue"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
handle_not_in_hotfix_branch() {
ACTION=$1
echo "+++++++++++++++++++++++ ATTENTION +++++++++++++++++++++++"
echo ""
echo "You are not in a hotfix branch."
echo "shipgit hotfix $ACTION must be called in a hotfix branch."
echo ""
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
}
cmd_start() {
PREFIX=$(git config --get shipgit.prefix.hotfix)
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
NAME=$3
HOTFIX_BRANCH=$PREFIX$NAME
echo "======================= Starting hotfix branch ======================="
echo ""
echo "Steps:"
git checkout -q "$PRODUCTION_BRANCH" &> /dev/null
git pull origin "$PRODUCTION_BRANCH" &> /dev/null
git fetch --tags &> /dev/null
echo " 1. Pulled latest changes from remote $PRODUCTION_BRANCH."
git branch --no-track "$HOTFIX_BRANCH" "$PRODUCTION_BRANCH" &> /dev/null
echo " 2. Created new $HOTFIX_BRANCH branch based on $PRODUCTION_BRANCH."
git checkout -q "$HOTFIX_BRANCH" &> /dev/null
git push -u origin "$HOTFIX_BRANCH" &> /dev/null
echo " 3. Pushed $HOTFIX_BRANCH to remote and added upstream reference."
echo ""
echo "======================================================================"
}
cmd_finish() {
PREFIX=$(git config --get shipgit.prefix.hotfix)
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
MAIN_BRANCH=$(git config --get shipgit.branch.main)
BUMP_TYPE=$3
AUTO_COMMIT=$4
SEMANTIC_VERSION_PREFIX=$(git config --get shipgit.prefix.semantic)
HOTFIX_BRANCH=$(git branch --show-current)
if ! [[ "$HOTFIX_BRANCH" =~ ^$PREFIX.* ]]; then
handle_not_in_hotfix_branch "finish"
fi
if ! [[ "$BUMP_TYPE" =~ (patch|minor|major) ]]; then
echo "++++++++++++++++++ ATTENTION ++++++++++++++++++"
echo ""
echo "Please specify the semantic version bump type."
echo "Usage:"
echo " shipgit hotfix finish (patch|minor|major)"
echo ""
echo "+++++++++++++++++++++++++++++++++++++++++++++++"
exit 1
fi
echo "=========================== Finishing hotfix branch ==========================="
echo ""
echo "Steps:"
git checkout -q "$PRODUCTION_BRANCH" &> /dev/null
git pull origin "$PRODUCTION_BRANCH" &> /dev/null
git fetch --tags &> /dev/null
echo " 1. Pulled latest changes from $PRODUCTION_BRANCH."
git checkout -q "$HOTFIX_BRANCH" &> /dev/null
git rebase "$PRODUCTION_BRANCH" &> /dev/null || handle_hotfix_finish_conflict "$HOTFIX_BRANCH" "$BUMP_TYPE"
echo " 2. Rebased $HOTFIX_BRANCH with $PRODUCTION_BRANCH."
# Fallback to prefix0.0.0 if no latest tag found
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "${SEMANTIC_VERSION_PREFIX}0.0.0")
if [ "$SEMANTIC_VERSION_PREFIX" != "" ]; then
LATEST_TAG_WITHOUT_PREFIX=$(echo $LATEST_TAG | sed "s/${SEMANTIC_VERSION_PREFIX}//")
else
LATEST_TAG_WITHOUT_PREFIX=$LATEST_TAG
fi
if [ "$BUMP_TYPE" == "patch" ]; then
NEW_TAG=$(echo $LATEST_TAG_WITHOUT_PREFIX | awk -F. '{OFS="."; $3+=1; print $0}')
elif [ "$BUMP_TYPE" == "minor" ]; then
NEW_TAG=$(echo $LATEST_TAG_WITHOUT_PREFIX | awk -F. '{OFS="."; $2+=1; $3=0; print $0}')
elif [ "$BUMP_TYPE" == "major" ]; then
NEW_TAG=$(echo $LATEST_TAG_WITHOUT_PREFIX | awk -F. '{OFS="."; $1+=1; $2=0; $3=0; print $0}')
fi
NEW_TAG_WITH_PREFIX=$(echo "${SEMANTIC_VERSION_PREFIX}${NEW_TAG}")
if [ "$NEW_TAG_WITH_PREFIX" != "$LATEST_TAG" ]; then
if [ "$AUTO_COMMIT" == "-m" ]; then
git tag -a "$NEW_TAG_WITH_PREFIX" -m "HOTFIX: $NEW_TAG_WITH_PREFIX" &> /dev/null
else
git tag -a "$NEW_TAG_WITH_PREFIX"
fi
fi
echo " 3. Added annotated tag $NEW_TAG_WITH_PREFIX."
git checkout -q "$MAIN_BRANCH" &> /dev/null
git pull origin $MAIN_BRANCH &> /dev/null
# For hotfix since we branch out from the production branch.
# Therefore, we can't use --ff-only to merge because main branch is usually
# much ahead of the production branch.
git merge --log --no-ff "$HOTFIX_BRANCH" &> /dev/null || handle_main_merge_conflict "$HOTFIX_BRANCH"
echo " 4. Merged $HOTFIX_BRANCH into $MAIN_BRANCH."
git push origin "$NEW_TAG_WITH_PREFIX" &> /dev/null
git push origin "$MAIN_BRANCH" &> /dev/null
echo " 5. Pushed tag $NEW_TAG_WITH_PREFIX and $MAIN_BRANCH to remote."
git checkout -q "$PRODUCTION_BRANCH" &> /dev/null
git pull origin $PRODUCTION_BRANCH &> /dev/null
git merge --ff-only "$NEW_TAG_WITH_PREFIX" &> /dev/null
echo " 6. Merged annotated tag $NEW_TAG_WITH_PREFIX into $PRODUCTION_BRANCH."
git push origin "$PRODUCTION_BRANCH" &> /dev/null
echo " 7. Pushed $PRODUCTION_BRANCH to remote."
git branch -D "$HOTFIX_BRANCH" &> /dev/null
git push -d origin "$HOTFIX_BRANCH" &> /dev/null
echo " 8. Deleted $HOTFIX_BRANCH locally and remotely."
git checkout -q "$MAIN_BRANCH" &> /dev/null
echo ""
echo "Summary:"
echo " 1. Version has been bumped from $LATEST_TAG to $NEW_TAG_WITH_PREFIX"
echo ""
echo "==============================================================================="
}
cmd_sync() {
PREFIX=$(git config --get shipgit.prefix.hotfix)
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
HOTFIX_BRANCH=$(git branch --show-current)
if ! [[ "$HOTFIX_BRANCH" =~ ^$PREFIX.* ]]; then
handle_not_in_hotfix_branch "sync"
fi
echo "======================= Syncing hotfix branch ======================="
echo ""
echo "Steps:"
git checkout -q "$PRODUCTION_BRANCH" &> /dev/null
git pull origin $PRODUCTION_BRANCH &> /dev/null
git fetch --tags &> /dev/null
echo " 1. Pulled latest changes from $PRODUCTION_BRANCH."
git checkout -q "$HOTFIX_BRANCH" &> /dev/null
git rebase "$PRODUCTION_BRANCH" &> /dev/null || handle_hotfix_sync_conflict "$HOTFIX_BRANCH"
echo " 2. Rebased $HOTFIX_BRANCH with $PRODUCTION_BRANCH."
echo ""
echo "====================================================================="
}
cmd_end() {
PREFIX=$(git config --get shipgit.prefix.hotfix)
MAIN_BRANCH=$(git config --get shipgit.branch.main)
PRODUCTION_BRANCH=$(git config --get shipgit.branch.production)
SEMANTIC_VERSION_PREFIX=$(git config --get shipgit.prefix.semantic)
HOTFIX_BRANCH=$(git branch --show-current)
if ! [[ "$HOTFIX_BRANCH" =~ ^$PREFIX.* ]]; then
handle_not_in_hotfix_branch "end"
fi
# If it hits here a tag should already be created
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || handle_no_tag)
echo "=========================== Ending hotfix branch ==========================="
echo ""
echo "Steps:"
git checkout -q "$MAIN_BRANCH" &> /dev/null
git push origin "$LATEST_TAG" &> /dev/null
git push origin "$MAIN_BRANCH" &> /dev/null
echo " 1. Pushed $LATEST_TAG and $MAIN_BRANCH to remote."
git checkout -q "$PRODUCTION_BRANCH" &> /dev/null
git pull origin $PRODUCTION_BRANCH &> /dev/null
git merge --ff-only "$LATEST_TAG" &> /dev/null
echo " 2. Merged annotated tag $LATEST_TAG into $PRODUCTION_BRANCH."
git push origin "$PRODUCTION_BRANCH" &> /dev/null
echo " 3. Pushed $PRODUCTION_BRANCH to remote."
git checkout -q "$MAIN_BRANCH" &> /dev/null
git branch -D "$HOTFIX_BRANCH" &> /dev/null
git push -d origin "$HOTFIX_BRANCH" &> /dev/null
echo " 4. Deleted $HOTFIX_BRANCH locally and remotely."
echo ""
echo "Summary:"
echo " 1. New version is now at $LATEST_TAG."
echo ""
echo "============================================================================"
}