forked from cmcculloh/GitScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
push.sh
executable file
·121 lines (108 loc) · 2.7 KB
/
push.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
#!/bin/bash
## /*
# @usage push [branch-name]
#
# @description
# This is a quick script that pushes a specified branch or the current branch to
# the remote, if it exists. To push a specific branch, simply include it as the
# first parameter when using this script.
# description@
#
# @options
# -q, --quiet Suppress the "Pushing not allowed" warning message and silently exit.
# options@
#
# @examples
# 1) push
# # pushes current branch
# 2) push some-other-branch
# # pushes some-other-branch...
# examples@
#
# @dependencies
# functions/5000.branch_exists_local.sh
# functions/5000.parse_git_branch.sh
# functions/5000.set_remote.sh
# dependencies@
#
# @file push.sh
## */
$loadfuncs
# reset styles
echo ${X}
# parse params
numArgs=$#
if (( numArgs > 0 && numArgs < 3 )); then
until [ -z "$1" ]; do
[ "$1" = "--admin" ] && [ "$ADMIN" = "true" ] && isAdmin=true
{ [ "$1" = "-q" ] || [ "$1" = "--quiet" ]; } && isQuiet=true
! echo "$1" | egrep -q "^-" && branch="$1"
shift
done
fi
# setup FAILURE conditions
# set pushing branch if specified, otherwise...
if [ -n "$branch" ]; then
if ! __branch_exists_local "$branch"; then
echo ${E}" The branch \`${branch}\` does not exist locally! Aborting... "${X}
exit 1
fi
# ...grab current branch and validate
else
cb=$(__parse_git_branch)
[ $cb ] && { branch="$cb"; } || {
echo ${E}" Could not determine current branch! "${X}
exit 1
}
fi
# check for protected branches
if __is_branch_protected --push "$branch" && [ ! $isAdmin ]; then
[ ! $isQuiet ] && echo " ${W}WARNING:${X} Pushing to ${B}\`${branch}\`${X} is not allowed. Aborting..."
exit 1
fi
# a remote is required to push to
if ! __set_remote; then
echo ${E}" Aborting... "${X}
exit 1
fi
# setup default answers
if [ "$pushanswer" == "y" ] || [ "$pushanswer" == "Y" ]; then
defO=" (y) n"
defA="y"
else
defO=" y (n)"
defA="n"
fi
# --quiet will use default answer
if [ ! $isQuiet ]; then
echo ${Q}"Would you like to push ${B}\`${branch}\`${Q} to ${COL_GREEN}${_remote}${Q}?${defO}"${X}
read yn
fi
if [ -z "$yn" ]; then
yn=$defA
fi
echo
if [ "$yn" == "y" ] || [ "$yn" == "Y" ]; then
if [ -n "$_remote" ]; then
echo
echo "Now ${A}pushing${X} to:${X} ${COL_GREEN} ${_remote} ${X}"
echo ${O}${H2HL}
echo "$ git push ${_remote} ${branch}"
git push "${_remote}" "${branch}"
echo ${O}${H2HL}${X}
echo
else
echo ${E}" No remote could be found. Push aborted. "${X}
exit 1
fi
fi
hasRemote=$(git config branch.$branch.remote 2> /dev/null)
if [ -z "$hasRemote" ]; then
echo
echo ${Q}"Setup remote tracking of ${COL_GREEN}${_remote}${Q} for ${B}\`${branch}\`${Q}? (y) n"
read yn
if [ -z "$yn" ] || [ "$yn" = "y" ]; then
git branch --set-upstream $_remote/$branch $branch
fi
fi
exit