-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-lava
More file actions
executable file
·358 lines (279 loc) · 9.01 KB
/
git-lava
File metadata and controls
executable file
·358 lines (279 loc) · 9.01 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# Painless git flow hotfixes
main() {
if [ "$1" == "-Xed" ]
then
if [ $(basename "$3") == "TAG_EDITMSG" ]
then
sed -e "1s/.*/$2/" -i "" "$3"
fi
# cat "$3"
exit
elif [ "$1" == "-h" -o "$1" == "--help" ]
then
echo "Usage: git lava [<commit message>]"
echo
echo "See https://github.com/WebEngage/git-lava for more details"
exit
elif [ "$1" != "" ]
then
COMMIT_MESSAGE=$1
hotflow
else
COMMIT_MESSAGE=""
hotflow
fi
}
hotflow() {
if ! git status > /dev/null 2>&1
then
echo "Something wrong with git"
exit 1
fi
GIT_ROOT=$(git rev-parse --show-toplevel)
cd "$GIT_ROOT"
if ! which git-flow > /dev/null 2>&1
then
echo "git-flow is not installed. Refer https://github.com/nvie/gitflow/wiki/Installation"
exit 1
fi
HOTFIX_PREFIX=$(git config --get gitflow.prefix.hotfix)
RELEASE_PREFIX=$(git config --get gitflow.prefix.release)
MASTER_BRANCH=$(git config --get gitflow.branch.master)
DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
if [ "$HOTFIX_PREFIX" == "" -o "$RELEASE_PREFIX" == "" -o "$MASTER_BRANCH" == "" -o "$DEVELOP_BRANCH" == "" ]
then
echo "Run 'git flow init' first"
exit 1
fi
HOTFIX_PREFIX=${HOTFIX_PREFIX/\//}
RELEASE_PREFIX=${RELEASE_PREFIX/\//}
PREFIX=$(git config --get gitlava.tagprefix)
if [ "$PREFIX" == "" ]
then
err "Tag prefix is not set!"
echo "Run"
pretty "git config gitlava.tagprefix <prefix>"
echo
echo "For prefix 'shifu' and hotfix version 3.1.4"
echo " - change commit is tagged as 'shifu-3.1.4'"
echo " - hotfix branch is created as '$HOTFIX_PREFIX/shifu-3.1.4'"
echo
echo "If you don't want a prefix set it as '-'"
exit 1
elif [ "$PREFIX" == "-" ]
then
PREFIX=""
else
PREFIX="$PREFIX-"
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ $CURRENT_BRANCH != "$MASTER_BRANCH" ]
then
echo "Not on master branch '$MASTER_BRANCH'"
exit 1
fi
# add unstaged changes except submodules"
git diff --name-only | while read CHANGE
do
if ! git config --file=.gitmodules "submodule.$CHANGE.url" > /dev/null
then
pretty "git add --all \"$CHANGE\""
git add --all "$CHANGE"
fi
done
# are there any staged changes?
if git diff --cached --no-ext-diff --quiet --exit-code
then
echo "No changes"
exit 1
fi
# status after staging changes
git status --short --untracked-files=no
STASH_NAME="gitlava@$(date +%s)000"
pretty "git stash save \"$STASH_NAME\"" "stashing staged changes and modifications"
git stash save "$STASH_NAME"
# was a stash saved?
if git stash list | head -1 | grep -q "$STASH_NAME"
then
STASH_SAVED=yes
fi
pretty "git pull" "in master branch"
git pull
CURRENT_RELEASE_VERSION=$(git log --merges --grep="Merge branch '$RELEASE_PREFIX/$PREFIX[0-9]*\.[0-9]*\(\.0\)\?'" --format="%s" -n 1 | sed "s/Merge branch '$RELEASE_PREFIX\/$PREFIX\([0-9]*\.[0-9]*\)\(\.0\)\{0,1\}'.*/\1/")
pretty "git checkout $DEVELOP_BRANCH"
git checkout "$DEVELOP_BRANCH"
pretty "git pull" "in develop branch"
git pull
LAST_VERSION=$(git log --merges --grep="Merge branch '$HOTFIX_PREFIX/$PREFIX[0-9]*\.[0-9]*\.[0-9]*' into $DEVELOP_BRANCH" --format="%s" -n 1 | sed "s/Merge branch '$HOTFIX_PREFIX\/$PREFIX\([0-9]*\.[0-9]*\.[0-9]*\)' into $DEVELOP_BRANCH/\1/")
if [ "$CURRENT_RELEASE_VERSION" == "" ]
then
# rollback; can't proceed
pretty "git checkout $MASTER_BRANCH"
git checkout "$MASTER_BRANCH"
if [ "$STASH_SAVED" == "yes" ]
then
pretty "git stash pop" "unstashing"
git stash pop
fi
echo
err "A current release version was not detected!"
echo "git-lava searches the log for commit messages like \"Merge branch '$RELEASE_PREFIX/${PREFIX}x.y'\""
echo
echo "If no release has ever been created in the project, create one before running 'git lava'"
exit 1
fi
MAJOR_VERSION=$(echo $LAST_VERSION | sed "s/\([0-9]*\.[0-9]*\)\.[0-9]*/\1/")
MINOR_VERSION=$(echo $LAST_VERSION | sed "s/[0-9]*\.[0-9]*\.\([0-9]*\)/\1/")
if [ "$LAST_VERSION" != "" ] && greater $MAJOR_VERSION $CURRENT_RELEASE_VERSION
then
echo
err "Latest hotfix ($LAST_VERSION) has a major version greater than current release version ($CURRENT_RELEASE_VERSION)"
err "Rickrolling back"
# rollback
pretty "git checkout $MASTER_BRANCH"
git checkout "$MASTER_BRANCH"
if [ "$STASH_SAVED" == "yes" ]
then
pretty "git stash pop" "unstashing"
git stash pop
fi
exit 1
elif [ "$MAJOR_VERSION" != "$CURRENT_RELEASE_VERSION" ]
then
MAJOR_VERSION="$CURRENT_RELEASE_VERSION"
MINOR_VERSION=0
fi
let NEXT_MINOR_VERSION=$MINOR_VERSION+1
NEXT_VERSION="$MAJOR_VERSION".$NEXT_MINOR_VERSION
while git rev-parse "$PREFIX$NEXT_VERSION" > /dev/null 2>&1
do
let NEXT_MINOR_VERSION=$NEXT_MINOR_VERSION+1
NEXT_VERSION="$MAJOR_VERSION".$NEXT_MINOR_VERSION
done
TAG_NAME="$PREFIX$NEXT_VERSION"
BRANCH_NAME="$HOTFIX_PREFIX/$TAG_NAME"
# setting up a trap to rollback on ctrl+C
trap rollback SIGINT
pretty "git flow hotfix start $TAG_NAME"
git flow hotfix start $TAG_NAME
if [ $? -ne 0 ]
then
rollback
fi
if [ "$STASH_SAVED" == "yes" ]
then
pretty "git stash apply --index"
git stash apply --index
fi
if [ $? -ne 0 ]
then
rollback
fi
pretty "git --no-pager diff --cached" "showing all modifications"
git --no-pager diff --cached
if [ "$COMMIT_MESSAGE" == "" ]
then
pretty "git commit"
git commit
else
pretty "git commit -m \"$COMMIT_MESSAGE\""
git commit -m "$COMMIT_MESSAGE"
fi
# get the location of this script
SCRIPT_DIRECTORY=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
# BIG hack - sets itself as editor for merge commit and tag messages
# so that 'git flow hotfix finish' goes on non-interactively
export GIT_EDITOR="$SCRIPT_DIRECTORY/$SCRIPT_NAME -Xed $TAG_NAME"
pretty "git flow hotfix finish $TAG_NAME"
git flow hotfix finish $TAG_NAME
if [ $? -ne 0 ]
then
rollback
fi
pretty "git push" "push develop branch to origin"
git push
if [ $? -ne 0 ]
then
rollback
fi
pretty "git checkout $MASTER_BRANCH"
git checkout "$MASTER_BRANCH"
pretty "git push --follow-tags" "push master branch along with tags to origin"
git push --follow-tags
if [ $? -ne 0 ]
then
echo
err "Couldn't push to origin/$MASTER_BRANCH... also cannot rollback ☹"
echo "Changes are preserved in 'stash@{0}'"
exit
fi
if [ "$STASH_SAVED" == "yes" ]
then
pretty "git stash drop"
git stash drop
fi
yo "Created hotfix ($TAG_NAME)"
# rollback
}
rollback() {
trap - SIGINT # reset trap
echo
err "Rolling back ☹"
# in case we are still in the hotfix branch and there were merge commits on stash apply
pretty "git reset --hard" "cleaning up index and working tree changes"
git reset --hard
pretty "git checkout $DEVELOP_BRANCH"
git checkout "$DEVELOP_BRANCH"
pretty "git reset --hard origin/$DEVELOP_BRANCH"
git reset --hard "origin/$DEVELOP_BRANCH"
pretty "git checkout $MASTER_BRANCH"
git checkout "$MASTER_BRANCH"
pretty "git reset --hard origin/$MASTER_BRANCH"
git reset --hard "origin/$MASTER_BRANCH"
pretty "git branch -D $BRANCH_NAME"
git branch -D "$BRANCH_NAME"
pretty "git tag -d $TAG_NAME"
git tag -d "$TAG_NAME"
if [ "$STASH_SAVED" == "yes" ]
then
pretty "git stash pop"
git stash pop
fi
echo
err "Couldn't create hotfix ($TAG_NAME)"
exit 1
}
greater() {
VERSION_A=$1
VERSION_B=$2
VERSION_A_MAJOR=$(echo $VERSION_A | sed "s/\([0-9]*\)\.[0-9]*/\1/")
VERSION_A_MINOR=$(echo $VERSION_A | sed "s/[0-9]*\.\([0-9]*\)/\1/")
VERSION_B_MAJOR=$(echo $VERSION_B | sed "s/\([0-9]*\)\.[0-9]*/\1/")
VERSION_B_MINOR=$(echo $VERSION_B | sed "s/[0-9]*\.\([0-9]*\)/\1/")
if [ $VERSION_A_MAJOR -gt $VERSION_B_MAJOR ]
then
return 0
fi
if [ $VERSION_A_MAJOR -eq $VERSION_B_MAJOR -a $VERSION_A_MINOR -gt $VERSION_B_MINOR ]
then
return 0
fi
return 1
}
pretty() {
if [ "$2" == "" ]
then
printf "\n\e[1m\e[103m\e[31m→ \e[30m$1\e[0m\n"
else
printf "\n\e[1m\e[103m\e[31m→ \e[30m$1 \e[2m\e[21m# $2\e[0m\n"
fi
}
err() {
printf "\e[1m\e[31m✗✗✗ \e[0m$1\n"
}
yo() {
printf "\n\e[1m\e[32m✓✓✓ \e[0m$1\n"
}
main "$1" "$2" "$3"