-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgucl.in
87 lines (74 loc) · 2.26 KB
/
gucl.in
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
#!/usr/bin/env bash
m4_include(gu_tools.inc.sh)
usage() {
printf '%s\n' \
'gucl @VERSION@' \
'' \
'gucl deletes and recreate a branch based from ' \
'a specified branch or from default development branch.' \
'' \
'Usage: gucl [-h] [[-R] recreate] [-r remote] [-b branch]' \
'' \
' -h Show this usage.' \
' recreate: Name of the branch to delete and recreate.' \
' remote: Name of the remote to base/create from.' \
' branch: Name of the branch to base/create from.'
}
# defaults
if [ -z "${REMOTE-}" ]; then
REMOTE=origin
fi
if [ -z "${BRANCH-}" ]; then
BRANCH=development
fi
# process flags
pointer=1
while [[ $pointer -le $# ]]; do
if [[ ${!pointer} != "-"* ]]; then ((pointer++)) # not a parameter flag so advance pointer
else
param=${!pointer}
((pointer_plus = pointer + 1))
slice_len=1
case $param in
# paramter-flags with arguments
-R|--recreate) RECREATE=${!pointer_plus}; ((slice_len++));;
-r|--remote) REMOTE=${!pointer_plus}; ((slice_len++));;
-b|--branch) BRANCH=${!pointer_plus}; ((slice_len++));;
# binary flags
-h|--help) HELP=true;;
esac
# splice out pointer frame from positional list
[[ $pointer -gt 1 ]] \
&& set -- ${@:1:((pointer - 1))} ${@:((pointer + $slice_len)):$#} \
|| set -- ${@:((pointer + $slice_len)):$#};
fi
done
if [ ! -z "${HELP-}" ]; then
usage
exit 0
fi
if [ -z "${RECREATE-}" ]; then
RECREATE=$1
fi
set -e
current_branch=$(git branch | grep "*" | sed 's/* //')
if [ "$current_branch" = "${RECREATE}" ]; then
echo Switching to a different branch
if branch_avail_on_remote ${REMOTE} ${BRANCH}; then
git checkout development
else
git checkout master
fi
fi
if remote_avail ${REMOTE}; then
read -r -p "You are about delete \"${RECREATE}\" branch and create a new branch based from \"${REMOTE}/${BRANCH}\" branch... Press CTRL-c to cancel or Enter to continue.." -n 1 dummy
if asksure; then
git_cleaning && git checkout ${BRANCH} && git pull
git branch -D ${RECREATE}
git checkout ${BRANCH} -b ${RECREATE}
fi
else
echo The ${REMOTE} is not available.
fi
git checkout $current_branch
# vim: set et ts=2 sw=2: