-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgittie.sh
executable file
·468 lines (407 loc) · 11.9 KB
/
gittie.sh
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env bash
# CONFIGURATION ======================================================
# ====================================================================
VERSION=0.3
GITTIE_IS_GIT_REPO=$(git rev-parse --is-inside-work-tree 2>/dev/null)
if [[ $GITTIE_IS_GIT_REPO == "true" ]]; then
GITTIE_GIT_CURRENT_BRANCH=$(git symbolic-ref -q HEAD)
GITTIE_GIT_CURRENT_BRANCH_SHORT=$(git symbolic-ref --short -q HEAD)
fi
# VARIABLES ==========================================================
# ====================================================================
SCRIPT=$0
COMMAND=$1
BIN=/usr/local/bin/gittie
LAST_ANSWER=''
# USAGE ==============================================================
# ====================================================================
usage() {
cat << EOF
Usage: ${0##*/} [-h]
Git for dummies, a.k.a gittie!
Version: $VERSION
Options:
-h Display this help and exit.
Commands:
c Commit all changes. <message>
a Amend changes to last commit.
p Push current branch to [<origin/current> or <branch>].
s Squash <commits> together.
f Fetch <branch>.
pl Pull <branch>.
ch Checkout to <branch>.
m Merge current branch with <branch>
ms Merge & squash current branch with <branch>
fp Force push current branch to [<origin/current> or <branch>].
rb Rebase current branch onto [<origin/current> or <branch>].
rba Abort rebasing.
rbc Continue rebasing.
rbd Preserve commit(s) date.
r Reset mixed current branch <commits> backward.
rh Reset hard current branch <commits> backward.
rs Reset soft current branch <commits> backward.
wf Fetch updates, rebase, preserve dates and force push.
Integration:
install Install to your OS.
purge Remove from your OS.
EOF
}
# COMMANDS ===========================================================
# ====================================================================
# [c] COMMIT CHANGES =================================================
# ====================================================================
git_commit() {
if [ $(git status -s | wc -l) -le 0 ]; then
echo "Nothing to commit."
exit 1
fi
if [ -z "$1" ]; then
_ask_question "Type commit message?"
local message=${LAST_ANSWER}
elif [ -n "$1" ]; then
local message=${*}
fi
git add --all .
git commit --all -m "${message}"
_color_green "All changes commited."
}
# [a] COMMIT AMEND ==================================================
# ====================================================================
git_amend() {
git add --all .
if [ -n "$1" ]; then
git commit -m "$*" --all --amend
_color_green "All changes amended with new message \e[42m\e[39m $* \e[0m\e[0m."
elif [ -z "$1" ]; then
git commit -C HEAD --all --amend
_color_green "All changes amended."
fi
}
# [p] PUSH ===========================================================
# ====================================================================
git_push() {
local branch=${1:-${GITTIE_GIT_CURRENT_BRANCH_SHORT}}
local commits=$(git log --oneline "origin/${branch}"..${GITTIE_GIT_CURRENT_BRANCH_SHORT} | wc -l)
_ask_yn_question "Do you really want to push \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m into \e[41m\e[39m origin/${branch} \e[0m\e[0m?"
local yn=$?
if [ $yn -eq 1 ]; then
git push origin ${branch}
fi
}
# [s] SQUASH =========================================================
# ====================================================================
git_squash() {
local branch=${GITTIE_GIT_CURRENT_BRANCH_SHORT}
local commits=${1:-2}
local lastmessage=$(git log -n 1 --skip $((${commits}-1)) --pretty=%B)
_ask_yn_question "Do you really want to squash \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m on \e[41m\e[39m ${branch} \e[0m\e[0m into 1 commit?"
local yn=$?
if [ $yn -eq 1 ]; then
git reset --soft HEAD~${commits}
_ask_question "Type new commit message?" "${lastmessage}"
git_commit "${LAST_ANSWER}"
fi
}
# [ch] CHECKOUT ======================================================
# ====================================================================
git_checkout() {
if [ -z "$1" ]; then
_ask_question "Checkout to branch?"
local branch=${LAST_ANSWER}
elif [ -n "$1" ]; then
local branch=${1}
fi
git checkout "${branch}"
}
# [m] MERGE ==========================================================
# ====================================================================
git_merge() {
if [ -z "$1" ]; then
_ask_question "Merge branch?"
local branch=${LAST_ANSWER}
elif [ -n "$1" ]; then
local branch=${1}
fi
git merge "${branch}"
}
# [ms] MERGE SQUASH ==================================================
# ====================================================================
git_merge_squash() {
if [ -z "$1" ]; then
_ask_question "Merge & squash branch?"
local branch=${LAST_ANSWER}
elif [ -n "$1" ]; then
local branch=${1}
fi
git merge --squash "${branch}"
}
# [fp] FORCE PUSH ====================================================
# ====================================================================
git_force_push() {
local branch=${1:-${GITTIE_GIT_CURRENT_BRANCH_SHORT}}
local commits=$(git log --oneline "origin/${branch}"..${GITTIE_GIT_CURRENT_BRANCH_SHORT} | wc -l)
_ask_yn_question "Do you really want to force push \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m into \e[41m\e[39m origin/${branch} \e[0m\e[0m?"
local yn=$?
if [ $yn -eq 1 ]; then
git push origin ${branch} -f
elif [ $yn -eq 0 ]; then
echo "Good choice! Force push is eval."
fi
}
# [rb] REBASE ========================================================
# ====================================================================
git_rebase() {
if [ $(git status -s | wc -l) -gt 0 ]; then
echo "Please commit changes, my lord."
git status -s
exit 1
fi
local branch=$(echo ${1:-${GITTIE_GIT_CURRENT_BRANCH_SHORT}} | sed -e "s/origin\///g")
local commits=$(git log --oneline "origin/${branch}"..${GITTIE_GIT_CURRENT_BRANCH_SHORT} | wc -l)
_ask_yn_question "Do you really want to rebase \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m onto \e[41m\e[39m origin/${branch} \e[0m\e[0m?"
local yn=$?
if [ $yn -eq 1 ]; then
git rebase -i "origin/${branch}"
git_rebase_date "${commits}"
fi
}
# [f] FETCH ==========================================================
# ====================================================================
git_fetch() {
local branch=${1:-${GITTIE_GIT_CURRENT_BRANCH_SHORT}}
echo -ne "Fetching updates from \e[41m\e[39m ${branch} \e[0m\e[0m branch.\n"
git fetch origin "${branch}"
}
# [pl] PULL ==========================================================
# ====================================================================
git_pull() {
local branch=${1:-${GITTIE_GIT_CURRENT_BRANCH_SHORT}}
echo -ne "Pulling updates from \e[41m\e[39m ${branch} \e[0m\e[0m branch.\n"
git pull origin "${branch}"
}
# [rba] REBASE - ABORT ===============================================
# ====================================================================
git_rebase_abort() {
git rebase --abort
}
# [rba] REBASE - CONTINUE ============================================
# ====================================================================
git_rebase_continue() {
git rebase --continue
}
# [rbd] REBASE - DATE ================================================
# ====================================================================
git_rebase_date() {
local commits=${1:-1}
_ask_yn_question "Do you want to preserve date for \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m at \e[41m\e[39m ${GITTIE_GIT_CURRENT_BRANCH_SHORT} \e[0m\e[0m?"
local yn=$?
if [ $yn -eq 1 ]; then
git rebase --committer-date-is-author-date HEAD~${commits}
fi
}
# [r] RESET ==========================================================
# ====================================================================
git_reset() {
local branch=${GITTIE_GIT_CURRENT_BRANCH_SHORT}
local commits=${1:-1}
_ask_yn_question "Do you really want to reset \e[41m\e[39m ${branch} \e[0m\e[0m \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m backward?"
local yn=$?
if [ $yn -eq 1 ]; then
git reset HEAD^${commits}
fi
}
# [rh] RESET HARD ====================================================
# ====================================================================
git_reset_hard() {
local branch=${GITTIE_GIT_CURRENT_BRANCH_SHORT}
local commits=${1:-1}
_ask_yn_question "Do you really want to \e[44m\e[39m hard reset \e[0m\e[0m \e[41m\e[39m ${branch} \e[0m\e[0m \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m backward?"
local yn=$?
if [ $yn -eq 1 ]; then
git reset --hard HEAD^${commits}
fi
}
# [rs] RESET SOFT ====================================================
# ====================================================================
git_reset_soft() {
local branch=${GITTIE_GIT_CURRENT_BRANCH_SHORT}
local commits=${1:-1}
_ask_yn_question "Do you really want to \e[44m\e[39m soft reset \e[0m\e[0m \e[41m\e[39m ${branch} \e[0m\e[0m \e[42m\e[39m ${commits} commit(s) \e[0m\e[0m backward?"
local yn=$?
if [ $yn -eq 1 ]; then
git reset --soft HEAD^${commits}
fi
}
# [wf] WORK FLOW =====================================================
# ====================================================================
git_work_flow() {
if [ -z "$1" ]; then
echo "Workflow denied. Please type upstream branch name."
exit 1;
fi
local branch=${1}
git_fetch "${branch}"
git_rebase "origin/${branch}"
git_force_push
}
# INTEGRATION ========================================================
# ====================================================================
integration_install() {
if [ "$(id -u)" != "0" ]; then
_color_red "This script needs root, my lord!"
exit 1
fi
cp ./${SCRIPT} ${BIN}
chmod +x ${BIN}
_color_green "All systems ready, my lord!"
}
integration_purge() {
if [ "$(id -u)" != "0" ]; then
_color_red "This script needs root, my lord!"
exit 1
fi
if [ -e "$BIN" ]; then
rm ${BIN}
_color_green "Everything is removed, my lord!"
else
_color_red "Gittie is not installed, my leader!"
fi
}
# FUNCTIONS ==========================================================
# ====================================================================
_ask_question() {
while true; do
if [ -n "$2" ]; then
echo -ne $(_color_red $(_font_bold $1)" [${2}]")": "
elif [ -n "$1" ]; then
echo -ne $(_color_red $(_font_bold $1))": "
fi
read -r answer
if [ -z "$answer" ] && [ -n "$2" ]; then
LAST_ANSWER=${2}
return 1
elif [ -z "$answer" ]; then
echo "Please answer."
elif [ -n "$answer" ]; then
LAST_ANSWER=${answer}
return 1
fi
done
}
_ask_yn_question() {
echo -ne $(_color_red $(_font_bold $1))" [y/n]: "
while true; do
read yn
case $yn in
[Yy]* ) return 1;;
[Nn]* ) return 0;;
* ) echo "Please answer yes[Yy] or no[Nn].";;
esac
done
}
_color_red() {
echo -e "\e[31m$*\e[0m"
}
_color_green() {
echo -e "\e[32m$*\e[0m"
}
_font_bold() {
echo -e "\e[1m$*\e[0m"
}
# MAIN LOOP ==========================================================
# ====================================================================
# Check environment
if [ -z $BASH ]; then
echo "Please use native calling or bash."
echo "Type: ./${0}"
exit 2
fi
# Shift first argument
if [ "$#" -gt 0 ]; then shift; fi
# Main switch
case ${COMMAND} in
c)
git_commit $*
exit 1
;;
a)
git_amend $*
exit 1
;;
p)
git_push $*
exit 1
;;
s)
git_squash $*
exit 1
;;
f)
git_fetch $*
exit 1
;;
pl)
git_pull $*
exit 1
;;
ch)
git_checkout $*
exit 1
;;
m)
git_merge $*
exit 1
;;
ms)
git_merge_squash $*
exit 1
;;
fp)
git_force_push $*
exit 1
;;
rb)
git_rebase $*
exit 1
;;
rba)
git_rebase_abort $*
exit 1
;;
rbc)
git_rebase_continue $*
exit 1
;;
rbd)
git_rebase_date $*
exit 1
;;
r)
git_reset $*
exit 1
;;
rh)
git_reset_hard $*
exit 1
;;
rs)
git_reset_soft $*
exit 1
;;
wf)
git_work_flow $*
exit 1
;;
install)
integration_install $*
exit 1
;;
purge)
integration_purge $*
exit 1
;;
*)
usage
exit 1
;;
esac