This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbgit
executable file
·424 lines (367 loc) · 12.3 KB
/
bgit
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
#!/bin/bash
# Copyright 2015 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Scripts for helping with Blockly++ development.
# Run source ./tools/bgit setup [-a --append] to set the path and environment
# variables for this script, then use the commands with bgit <command>
#
# Assumes the following directory structure (NOTE: bgit fclone will create
# the web-<github username> directory for you):
# blockly/
# tools/bgit
# web-<github username>/appengine/...
COLOR_NONE="\e[0m"
GREEN="\033[0;32m"
BLUE="\033[0;34m"
ORANGE="\033[0;33m"
RED="\033[0;31m"
reenter_instructions() {
echo -e "${ORANGE}$*${COLOR_NONE}" >&2
}
success() {
echo -e "${GREEN}[SUCCESS]:${COLOR_NONE} $*" >&2
}
inf() {
echo -e "${BLUE}[INFO]:${COLOR_NONE} $*" >&2
}
warn() {
echo -e "${ORANGE}[WARN]:${COLOR_NONE} $*" >&2
}
err() {
echo -e "${RED}[ERROR]:${COLOR_NONE} $*" >&2
}
#######################################
# Sets up global variables and appends directory of tool to path for the curent
# shell and all proccesses started from the current shell.
# Globals:
# BLOCKLY_ROOT
# BLOCKLY_USER
# BLOCKLY_INIT
# Arguments:
# Whether to set up environment variables permanently for all future bash
# sessions by appending to .bashrc file.
#######################################
source_setup() {
if [[ ! -d "tools" ]]; then
err "Setup must be run in root directory (containing tools directory). Aborting."
return 1
fi
if [[ "$0" == "$BASH_SOURCE" ]]; then
err "Setup must be run sourced (i.e. with source ./tools/bgit setup)"
return 1
fi
local append_bashrc=false
while [[ "$1" != "" ]]; do
case $1 in
-a | --append ) append_bashrc=true;;
* ) warn "unknown arg $1";;
esac
shift
done
if [[ "${append_bashrc}" == true ]] && grep -q "export BLOCKLY_INIT=" ~/.bashrc ; then
# Do not append environment variables again.
err "BLOCKLY_INIT already set in .bashrc. You must manually edit .bashrc."
return 1
fi
chmod u+x tools/bgit
export BLOCKLY_ROOT="$(pwd)"
if [ "$BLOCKLY_USER" = "" ]; then
while true; do
read -p "GitHub username:" user
if [ "$user" == "" ]; then
reenter_instructions "Username may not be empty"
else
success "Set your username to $user."
export BLOCKLY_USER=$user
break
fi
done
else
inf "Username is ${BLOCKLY_USER}. To update, use 'export BLOCKLY_USER=<new user>'."
fi
# Appends Blockly root directory to path if not already.
[[ ":$PATH:" != *":${BLOCKLY_ROOT}/tools:"* ]] && export PATH="${PATH}:${BLOCKLY_ROOT}/tools"
export BLOCKLY_INIT=true
# Enables rerere recording of merge resolutions
git config --global rerere.enabled true
if [[ "${append_bashrc}" == true ]] ; then
echo "" >> ~/.bashrc
echo "# Blockly tools setup" >> ~/.bashrc
echo "export BLOCKLY_ROOT=\"${BLOCKLY_ROOT}\"" >> ~/.bashrc
echo "export BLOCKLY_USER=\"${BLOCKLY_USER}\"" >> ~/.bashrc
echo "export BLOCKLY_INIT=true" >> ~/.bashrc
echo "# Appends Blockly root directory to path if not already." >> ~/.bashrc
echo '[[ ":$PATH:" != *":${BLOCKLY_ROOT}/tools:"* ]] && export PATH="${PATH}:${BLOCKLY_ROOT}/tools"' >> ~/.bashrc
success "Updated .bashrc file"
fi
}
#######################################
# Closes remote and local branch, changing current branch to develop.
#
# Arguments:
# None
#######################################
close() {
local cur_branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ "${cur_branch}" == "master" ]]; then
err "Cannot close the master branch"
exit 1
fi
if [[ "${cur_branch}" == "develop" ]]; then
err "Cannot close the develop branch"
exit 1
fi
if [[ "${cur_branch}" == "HEAD" ]]; then
err "Not on a branch"
exit 1
fi
while true; do
read -p "This will delete the remote and local branches for ${cur_branch}. Continue? (y/n): " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit 0;;
* ) reenter_instructions "Please type y or n";;
esac
done
local return_branch='develop'
if [ $(git branch | grep " develop$" | wc -l) = "0" ]; then
return_branch='master'
fi
git push origin :${cur_branch}
git checkout ${return_branch}
git branch -D ${cur_branch}
}
#######################################
# Clones fork.
# Globals:
# BLOCKLY_INIT
# BLOCKLY_USER
# BLOCKLY_ROOT
# Arguments:
# Optional: Project to clone. Either 'web', 'samples" or 'devtools' ('web' by default).
#######################################
clone_fork() {
if [[ "${BLOCKLY_INIT}" != true ]]; then
err "Must run \"bgit setup\" before updating your web fork."
exit 1
fi
local fork_repo_base="https://github.com/${BLOCKLY_USER}/blockly"
local fork_project="$1"
if [[ "$fork_project" != "" ]]; then
if [ "$fork_project" = "samples" ]; then
fork_repo_ext="-samples"
elif [ "$fork_project" = "devtools" ]; then
fork_repo_ext="-devtools"
elif [ "$fork_project" != "web" ]; then
# 'web' is a valid value, but we don't do anything for it
# which is why we just check it at the end.
err "Must specify one of 'web', 'samples', or 'devtools' to clone a fork of."
exit 1;
fi
fi
local fork_repo="$fork_repo_base$fork_repo_ext"
local fork_local_dir="${BLOCKLY_ROOT}/${fork_project}-${BLOCKLY_USER}"
if [ -d "${fork_local_dir}" ]; then
err "webfork directory already exists at ${fork_local_dir}. Aborting."
exit 1
fi
git clone "${fork_repo}" "${fork_local_dir}"
if [ ! -d "${fork_local_dir}" ]; then
err "Failed to clone repo. Ensure you have a fork of blockly on your GitHub account."
exit 1
fi
local track_branch_flags="-t master"
if [ $(git ls-remote --heads "https://github.com/google/blockly${fork_repo_ext}" develop | wc -l) = "1" ]; then
track_branch_flags="${track_branch_flags} -t develop"
fi
cd "${fork_local_dir}"
git remote add ${track_branch_flags} upstream "https://github.com/google/blockly${fork_repo_ext}"
}
#######################################
# Verifies that you are currently in a forked git project with
# no uncommitted changes
# Globals:
# BLOCKLY_INIT
# BLOCKLY_USER
# Arguments:
# None
#######################################
function verify_fork() {
if [[ "${BLOCKLY_INIT}" != true ]]; then
err "Must run \"bgit setup\" before updating your web fork."
exit 1
fi
# Check that we're in a forked repo
if [ $(git remote -v | grep -i origin.*$BLOCKLY_USER | wc -l) = "0" ]; then
err "bgit fupdate or fbranch must be run from a git project with your fork as origin."
exit 1
fi
# Check that google is the upstream repo
if [ $(git remote -v | grep -i upstream.*google | wc -l) = "0" ]; then
err "google's project must be configured as upstream. Did you use 'bgit fclone' to clone the repo?"
exit 1
fi
# Check that there's nothing to commit that will block checkouts
if [ $(git status | grep -i "working tree clean" | wc -l) = "0" ]; then
err "You have uncommited changes. Please commit or revert your changes before updating."
exit 1
fi
# Ensure there's a master and develop branch locally
if [ $(git branch | grep " master$" | wc -l) = "0" ]; then
git branch -f master remotes/origin/master
fi
if [ $(git branch | grep " develop$" | wc -l) = "0" ]; then
# Add develop if it exists on origin.
if [ $(git ls-remote --heads origin develop | wc -l) = "1" ]; then
git branch -f develop remotes/origin/develop
fi
fi
success "Verified fork"
}
#######################################
# Updates fork.
# Globals:
# BLOCKLY_INIT
# BLOCKLY_USER
# Arguments:
# Optional: name of additional branch to update.
#######################################
update_fork() {
verify_fork
git fetch upstream
git checkout master
git pull upstream master
git push origin HEAD:master
inf "Updated master"
# Update develop if it exists
if [ $(git branch | grep " develop$" | wc -l) = "1" ]; then
echo 'sdfsd'
git checkout develop
git pull upstream develop
git push origin HEAD:develop
inf "Updated develop"
fi
local branch=$1
if [[ ! -z "${branch}" ]]; then
git checkout ${branch}
git pull upstream ${branch}
git push origin HEAD:${branch}
inf "Updated ${branch}"
fi
}
#######################################
# Creates a new branch off of specified branch or develop (or master if develop does not exists).
# Globals:
# BLOCKLY_INIT
# BLOCKLY_USER
# Arguments:
# The name of the new branch.
# Optional: name of base branch
#######################################
branch_fork() {
local new_branch=$1
if [[ -z "${new_branch}" ]]; then
err "Missing branch name. Syntax is \"bgit fbranch <branch_name>\""
exit 1
fi
if [[ "${new_branch}" == "master" ]]; then
err "Cannot create branch named master"
exit 1
fi
if [[ "${new_branch}" == "develop" ]]; then
err "Cannot create branch named develop"
exit 1
fi
local base_branch=$2
if [[ -z "${base_branch}" ]]; then
if [ $(git branch | grep " develop$" | wc -l) = "1" ]; then
base_branch='develop'
else
base_branch='master'
fi
fi
verify_fork
git branch -f "${new_branch}" remotes/origin/${base_branch}
git checkout "${new_branch}"
inf "Created ${new_branch} off of remotes/origin/${base_branch}"
}
#######################################
# Pushes local changes to remote branch.
# Arguments:
# Whether to force push.
#######################################
push() {
if [[ $(git config --get remote.google.url) == *"google/blockly" ]]; then
err "You are on the public Blockly project. Do not push here."
exit 1
fi
local args=""
while [[ "$1" != "" ]]; do
case $1 in
-f | --force ) args="$args $1";;
* ) warn "unknown arg $1";;
esac
shift
done
local branch="$(git rev-parse --abbrev-ref HEAD)"
git push$args origin "${branch}"
success "Pushed change to ${branch}. Add a reviewer on the website."
}
#######################################
# Resets the local branch to upstream state.
# Arguments:
# The branch to reset to upstream state.
#######################################
reset_fork_branch(){
local branch=$1
if [[ -z "${branch}" ]]; then
err "Missing branch name. Syntax is \"bgit freset <branch_name>\""
exit 1
fi
if ! git checkout "${branch}"; then
err "Unable to checkout ${branch}. Are you sure there isn't a typo?"
exit 1
fi
git pull upstream "${branch}"
git reset --hard "upstream/${branch}"
success "Reset to upstream state. To reset origin to upstream state run \"bgit push -f\""
}
#######################################
# Prints usage information for bgit command.
#######################################
function help {
echo "Available commands are:"
echo "source ./tools/bgit setup [-a --append] # Setup environment variables for bgit tool. (Must be run in root directory)."
echo "bgit close # Delete the current branch on GitHub and locally"
echo "bgit push [-f --force] # Push the current branch to GitHub. Creates a remote branch if needed."
echo "bgit fclone [project=web] # Clones an existing fork of Blockly on your account. Project must be one of 'web', 'samples', or 'devtools'"
echo "bgit fbranch <new_branch_name> [base_branch_name='develop|master'] # Create a new branch in your forked repo based off specified base branch."
echo "bgit fupdate # Updates the master and pond-multi branches for the current project. Must be in a forked git repo."
echo "bgit freset <branch_name> # Force resets local branch to match upstream."
}
if [ "$1" = "" ]; then
help
else
command=$1
shift
case $command in
setup ) source_setup $@;;
close ) close $@;;
push ) push $@;;
fclone) clone_fork $@;;
fupdate) update_fork $@;;
fbranch) branch_fork $@;;
freset) reset_fork_branch $@;;
*) help;;
esac
fi