-
Notifications
You must be signed in to change notification settings - Fork 5
/
new.sh
executable file
·294 lines (251 loc) · 7.53 KB
/
new.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
#!/bin/bash
## /*
# @usage new <branch_name> [from <existing_branch>]
#
# @description
# This script provides several ways to create a new branch. You will be able to create a branch
# based off of:
# 1. Specified <existing_branch>
# 2. The current branch you are on
# 3. The master branch
#
# In addition, the branches will automatically be tracked and pushed up to any remotes you have
# configured. Don't worry, you will get the option to choose which remote you want the branch
# pushed to if you have configured more than one.
# description@
#
# @notes
# - You will end on the new branch. That is, it will be the currently checked out branch.
# - The stash and reset options in the initial menu currently only work when your
# starting branch is the master branch.
# notes@
#
# @examples
# 1) new some-work
# 2) new some-work from old-work
# examples@
#
# @dependencies
# functions/0100.bad_usage.sh
# functions/5000.branch_exists.sh
# functions/5000.parse_git_status.sh
# functions/5000.set_remote.sh
# dependencies@
#
# @file new.sh
## */
$loadfuncs
echo ${X}
# default branch to start from, current branch, and any remotes
startingBranch="master"
currentBranch=$(__parse_git_branch)
# first parameter required.
if [ -z "$1" ]; then
echo
__bad_usage new "New requires the new branch name as the first parameter."
exit 1
fi
echo
echo
echo "Configuring remote(s), if any..."
__set_remote
echo "Remote set to ${_remote}"
#no reason to continue if user is trying to create a branch that already exists
if __branch_exists_local "$1"; then
echo
echo ${E}" Branch \`$1\` already exists! "${X}
read checkoutdecision
if [ -z "$checkoutdecision" ] || [ "$checkoutdecision" = "y" ]; then
${gitscripts_path}checkout.sh "$1"
fi
#don't create new branch since we checked the local copy out, just exit...
exit 1
elif __branch_exists_remote "${_remote}/$1"; then
echo "branch_exists_remote: ${1}";
__branch_exists_remote "$1";
echo "branch_exists_remote: master";
__branch_exists_remote master;
if [ "$4" != "--no-questions" ]; then
echo
echo ${E}" Branch \`$1\` already exists on the remote! "${X}
echo ${Q}" Check \`$1\` out from remote? (y) n "${Q}
read checkoutremotedecision
else
checkoutremotedecision="y"
fi
if [ -z "$checkoutremotedecision" ] || [ "$checkoutremotedecision" = "y" ]; then
startingBranch="${_remote}/${1}"
checkoutremote=true
else
exit 1
fi
fi
#user may specify a different base branch
if [ -n "$2" ] && [ "$2" == "from" ]; then
if [ -n "$3" ] && __branch_exists "$3"; then
startingBranch="$3"
else
echo
echo ${E}" The specified base branch \`$3\` does not exist. Aborting... "${X}
exit 1
fi
fi
echo ${H1}${H1HL}
echo "Creating new branch: ${H1B}\`${1}\`${H1} "
echo ${H1HL}${X}
echo
echo
echo "This tells your local git about all changes on the remote. Then we'll check your git status."
echo ${O}${H2HL}
echo "$ git fetch --all --prune"
git fetch --all --prune
echo ${O}
echo
echo "$ git status"
git status
echo ${O}${H2HL}${X}
echo
if ! __parse_git_status clean; then
declare -a choices
choices[0]="${A}Stash${STYLE_MENU_OPTION} changes and and contiue "${X}
choices[1]="${A}Reset${STYLE_MENU_OPTION} (revert) all changes to ONLY tracked files and continue"${X}
choices[2]="${A}Commit${STYLE_MENU_OPTION} ALL changes and continue "${X}
choices[3]="Just ${A}continue${STYLE_MENU_OPTION}, I want to move all my changes to the new branch..."${X}
if __menu "${choices[@]}"; then
echo ${X}
# handle decision cases
case $_menu_sel_index in
# stash changes and continue
1)
echo "This ${A}stashes${X} any local changes you might have made and forgot to commit."
echo "To apply these changes later, use: ${A}git stash apply"${X}
echo ${O}${H2HL}
echo "$ git stash"
git stash
echo ${O}
echo
echo "$ git status"
git status
echo ${O}${H2HL}${X};;
# revert changes to tracked files and continue
2)
echo "This attempts to ${A}reset${X} your current branch to the last stable commit."
echo "If you have made any changes to untracked files, they will NOT be affected."
echo ${O}${H2HL}
echo "$ git reset --hard"
git reset --hard
echo ${O}
echo
echo "$ git status"
git status
echo ${O}${H2HL}${X};;
#commit changes and continue
3)
echo "Please enter a commit message"
read commitmsg
${gitscripts_path}commit.sh "$commitmsg" "-A";;
#just keep going
4)
echo "Ignoring changes and continuing";;
#abort process
*)
echo "Aborting creation of ${B}\`$1\`${X}..."
exit 0;;
esac
else
echo ${E}" Unable to determine a course of action. Aborting... "${X}
exit 1
fi
choices=()
fi
#if the branch is not on the remote, allow them to create it
if [ ! $checkoutremote ]; then
declare -a choices
choices[0]="Create branch ${STYLE_NEWBRANCH}\`${1}\`${STYLE_MENU_OPTION} from ${STYLE_OLDBRANCH_H1}\`${startingBranch}\`"${X}
choices[1]="Create branch ${STYLE_NEWBRANCH}\`${1}\`${STYLE_MENU_OPTION} from the current branch ${STYLE_OLDBRANCH_H1}\`${currentBranch}\`"${X}
if [ "$startingBranch" != "master" ]; then
choices[2]="Create branch ${STYLE_NEWBRANCH}\`${1}\`${STYLE_MENU_OPTION} from ${STYLE_OLDBRANCH_H1}\`master\`"${X}
fi
if __menu "${choices[@]}"; then
echo ${X}
# handle decision cases
case $_menu_sel_index in
# create new branch from specified branch
1)
#nothing to do here since startingBranch is already at the correct value
;;
# create new branch from whatever branch user is currently on
2)
startingBranch="$currentBranch"
;;
#create the branch from master
3)
startingBranch="master"
;;
# abort process
*)
echo "Aborting creation of ${B}\`$1\`${X}..."
exit 0;;
esac
else
echo ${E}" Unable to determine a course of action. Aborting... "${X}
exit 1
fi
choices=()
fi
echo "Base new branch off of ${B}\`${startingBranch}\`${X}"
if [ "$startingBranch" = "master" ]; then
echo
echo
__set_remote
if [ -n "$_remote" ]; then
echo "This branches ${B}\`master\`${X} to create a new branch named ${B}\`$1\`${X}"
echo "and then checks out the ${B}\`$1\`${X} branch. We will make sure"
echo "to get all updates (if available) to ${B}\`master\`${X} as well."
echo ${O}${H2HL}
echo "$ git checkout master"
git checkout master
echo "$ git pull $_remote master"
git pull "$_remote" master
fi
echo "$ git checkout -b $1"
git checkout -b "$1"
echo ${O}${H2HL}${X}
elif [ $checkoutremote ]; then
echo
echo
echo "This checks out the remote ${B}\`${startingBranch}\`${X} to create a new local branch named ${B}\`$1\`${X}"
echo ${O}${H2HL}
echo "$ git checkout -b ${1} ${startingBranch}"
git checkout -b $1 $startingBranch
echo ${O}${H2HL}${X}
else
echo
echo
echo "Create ${B}\`$1\`${X} from ${B}\`${startingBranch}\`${X}? (y) n"
read branchNonMaster
if [ -z "$branchNonMaster" ] || [ "$branchNonMaster" = "y" ] || [ "$branchNonMaster" = "Y" ]; then
echo
echo
echo "This branches ${B}\`${startingBranch}\`${X} to create a new branch named ${B}\`$1\`${X}"
echo ${O}${H2HL}
echo "$ git checkout -b ${1} ${startingBranch}"
git checkout -b $1 $startingBranch
echo ${O}${H2HL}${X}
else
echo
echo 'Aborting'
exit 0
fi
fi
#set up tracking for when the branch later gets pushed
git config branch.$1.remote $_remote
git config branch.$1.merge refs/heads/$1
# do this later in the push.sh script. Leaving here in case
# we want a flag that you can set in your config that auto-pushes
# new branches...
# if a remote exists, push to it.
if [ -n "$_remote" ] && [ "$autopushnewbranch" = "true" ]; then
"${gitscripts_path}"push.sh --quiet
fi
exit