-
Notifications
You must be signed in to change notification settings - Fork 5
/
push.sh
executable file
·156 lines (138 loc) · 3.68 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
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
#!/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.
# --tags ALSO pushes tags to remote
# options@
#
# @examples
# 1) push
# # pushes current branch
# 2) push some-other-branch
# # pushes some-other-branch...
# 3) push --tags
# # pushes current branch -AND- pushes any tags
# 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}
force=""
# parse params
numArgs=$#
if (( numArgs > 0 && numArgs < 6 )); then
until [ -z "$1" ]; do
[ "$1" = "--admin" ] && [ "$ADMIN" = "true" ] && isAdmin=true
{ [ "$1" = "-a" ] || [ "$1" = "--all" ]; } && pushToAll=true
{ [ "$1" = "-q" ] || [ "$1" = "--quiet" ]; } && isQuiet=true
{ [ "$1" = "-f" ] || [ "$1" = "--force" ]; } && force="-f"
[ "$1" = "--tags" ] && pushTags=true
! echo "$1" | egrep -q "^-" && branch="$1"
shift 1
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
if [ $pushToAll ]; then
# if --all flag was passed, just push to all remotes. Otherwise, see if they want to choose a remote to push it to.
# run loop. reads from temp file.
# send branch data to temp file
tmp="${gitscripts_temp_path}remotes"
git remote > $tmp
# run loop. reads from temp file.
declare -a remoteNames
while read remote; do
pieces=( $remote )
remoteNames[${#remoteNames[@]}]="${pieces[0]}"
done <"$tmp"
# temp file no longer necessary
rm "$tmp"
# let's get this party started...
for (( i = 0; i < ${#remoteNames[@]}; i++ )); do
echo "$ git push ${remoteNames[$i]} ${branch}"
git push "${remoteNames[$i]}" "${branch}"
done
else
# 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 $force 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 $force ${A}pushing${X} to:${X} ${COL_GREEN} ${_remote} ${X}"
echo ${O}${H2HL}
echo "$ git push $force ${_remote} ${branch}"
git push $force "${_remote}" "${branch}"
echo ${O}${H2HL}${X}
echo
else
echo ${E}" No remote could be found. Push aborted. "${X}
exit 1
fi
fi
if [ $pushTags ]; then
echo
echo "Now ${A}pushing${X} tags to: ${COL_GREEN} ${_remote} ${X}"
echo "$ git push --tags ${_remote}"
git push --tags ${_remote}
echo ${O}${H2HL}${X}
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-to $_remote/$branch $branch
fi
fi
fi
exit