-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-github
executable file
·345 lines (294 loc) · 8.85 KB
/
git-github
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
#!/bin/bash
#
# Script that syncs a github fork
#
TAGS=""
ALL=""
BRANCH=""
COMMAND="unset"
FORK_REMOTENAME="gtws_github_fork"
UPSTREAM_REMOTENAME="gtws_github_upstream"
# Set usage output
DESCRIPTION="Sync a github fork with upstream"
USAGE="[-h |--help] (info | init | push | sync)"
LONGUSAGE="Common options:
\t-h, --help\n\t\tPrint this help message
\t Commands:
\tinfo\n\t\tPrints information about the sync repo and state
\tinit\n\t\tInitialize syncing repo. Must be done once.
\tsync\n\t\tSync local fork repo with upstream repo.
\tpush\n\t\tPush from local fork repo to remote fork repo.
\tFor more info about each command, pass --help to that command
"
# Standard functions
GTWS_LOC=$(readlink -f $(dirname "$0"))
source ${GTWS_LOC}/gtws.sh
# Script name
ME=$(basename $0)
# Parse global arguments
ARGS=`getopt -o +h --long help -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
info_opts() {
local ME="${ME} ${COMMAND}"
local USAGE="[-h |--help]"
local LONGUSAGE="
\t-h, --help\n\t\tPrint this help message
"
local SUBARGS=""
SUBARGS=`getopt -o h --long help -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$SUBARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
}
init_opts() {
local ME="${ME} ${COMMAND}"
local USAGE="[-h |--help] <fork-repo-uri> <upstream-repo-uri>"
local LONGUSAGE="
Initialize syncing repo. Must be done once.
\t-h, --help\n\t\tPrint this help message
\t<fork-repo-uri>\n\t\tURI of repository containing fork
\t<upstream-repo-uri>\n\t\tURI of upsstream repo fork was made from
"
local SUBARGS=""
SUBARGS=`getopt -o h --long help -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$SUBARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
if [ -z "$1" ]; then
usage "init requires two parameters - no fork repo given"
fi
FORK_REPO=$1; shift
if [ -z "$1" ]; then
usage "init requires two parameters - no upstream repo given"
fi
UPSTREAM_REPO=$1; shift
}
sync_opts() {
local ME="${ME} ${COMMAND}"
local USAGE="[-h |--help] [-a | --all] [-t | --tags] [-f | --force] [-s | --stash] [<branch>]"
local LONGUSAGE="
Sync the local forked repo with it's upstream. Either --all, or <branch> must be given.
\t-h, --help\n\t\tPrint this help message
\t-a, --all\n\t\tOperate on all branches
\t-t, --tags\n\t\tSync tags in addition to commits
\t-f, --force\n\t\tForce operation, even if branches differ
\t-S, --stash\n\t\tStash before operation
\t<branch>\n\t\tBranch to operate on.
"
local SUBARGS=""
SUBARGS=`getopt -o hafst --long help,all,force,stash,tags -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$SUBARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-a|--all) ALL="--all"; shift ;;
-f|--force) FORCE="--force"; shift ;;
-S|--stash) STASH="yes"; shift ;;
-t|--tags) TAGS="--tags"; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
if [ -n "$1" ]; then
if [ -n "${ALL}" ]; then
usage "Cannot give both <branch> and --all"
fi
BRANCH=$1; shift
fi
if [[ -z "${BRANCH}" && -z "${ALL}" ]]; then
usage "Must give one of <branch> or --all"
fi
}
push_opts() {
local ME="${ME} ${COMMAND}"
local USAGE="[-h |--help] [-a | --all] [-t | --tags] [-f | --force] [<branch>]"
local LONGUSAGE="
Push the local forked repo to it's remote. Either --all, or <branch> must be given.
\t-h, --help\n\t\tPrint this help message
\t-a, --all\n\t\tOperate on all branches
\t-t, --tags\n\t\tSync tags in addition to commits
\t-f, --force\n\t\tForce operation, even if branches differ
\t<branch>\n\t\tBranch to operate on.
"
local SUBARGS=""
SUBARGS=`getopt -o haft --long help,all,force,tags -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$SUBARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-a|--all) ALL="--all"; shift ;;
-f|--force) FORCE="--force"; shift ;;
-t|--tags) TAGS="--tags"; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
if [ -n "$1" ]; then
if [ -n "${ALL}" ]; then
usage "Cannot give both <branch> and --all"
fi
BRANCH=$1; shift
fi
if [[ -z "${BRANCH}" && -z "${ALL}" ]]; then
usage "Must give one of <branch> or --all"
fi
}
# Now get the subcommand. It will be in $1.
COMMAND=$1; shift
case "${COMMAND}" in
"info") info_opts "$@" ;;
"init") init_opts "$@" ;;
"push") push_opts "$@" ;;
"sync") sync_opts "$@" ;;
"") usage "Must give a command" ;;
* ) usage "Unknown command ${COMMAND}";;
esac
# Remaining arguments are in $1, $2, etc. as normal
# Source the git environment. Checks to see if we're in a git repo
SUBDIRECTORY_OK=Yes
source "$(git --exec-path)/git-sh-setup"
if git-issvn > /dev/null 2>&1; then
die "Cannot currently sync a git-svn repo with Github"
fi
SAVEDIR=${PWD}
CURBRANCH=$(git branch | grep "\*" | sed 's/\* //')
cd_to_toplevel
if [ -n "${STASH}" ]; then
git stash || die "Couldn't stash"
fi
# Sync a branch with a remote. Remote should have been fetched already
function sync_branch {
local remote=$1
local branch=$2
if [[ -z "${remote}" || -z "${branch}" ]]; then
die "${FUNCNAME} - must give remote and branch" || return 1
fi
# Check out branch
git branch --no-track "${branch}" > /dev/null 2>&1
git checkout "${branch}" \
|| die "${FUNCNAME} - Failed to checkout ${branch}" || return 1
git reset --hard "${remote}/${branch}" \
|| die "${FUNCNAME} - Failed to reset ${branch} to ${remote}" || return 1
git clean -f -d || die "${FUNCNAME} - Failed to clean ${branch}" || return 1
git submodule update || die "${FUNCNAME} - Failed to update submodules for ${branch}" || return 1
}
# Sync all branches from a remote to this repo
function sync_all {
local remote=$1
if [ -z "${remote}" ]; then
die "${FUNCNAME} - must give remote" || return 1
fi
local branch=""
local branches=$(git branch -r | grep "${remote}" | grep -v master | \
grep -v HEAD | sed -e 's/.*\///g')
for branch in ${branches}; do
sync_branch "${remote}" "${branch}" || return 1
done
}
# Checck to see if this repo has been set up for syncing
function is_inited {
local remotes=$(git remote show)
echo "${remotes}" | grep "${FORK_REMOTENAME}" > /dev/null 2>&1 || return 1
echo "${remotes}" | grep "${UPSTREAM_REMOTENAME}" > /dev/null 2>&1 || return 1
return 0
}
function cmd_info {
if ! is_inited; then
die "${COMMAND} - Repo is not initialized" && return 1
fi
local remotes=$(git remote -v show)
mapfile -t <<< "${remotes}"
for entry in "${MAPFILE[@]}"; do
local rem=($entry)
if [ "${rem[2]}" != "(fetch)" ]; then
continue
elif [ "${rem[0]}" == "${FORK_REMOTENAME}" ]; then
echo -e "Fork repo:\t${rem[1]}"
elif [ "${rem[0]}" == "${UPSTREAM_REMOTENAME}" ]; then
echo -e "Upstream repo:\t${rem[1]}"
fi
done
}
function cmd_init {
is_inited && die "init - Repo is already initialized" && return 1
git remote add -f --tags "${FORK_REMOTENAME}" "${FORK_REPO}" > /dev/null 2>&1 \
|| die "init - failed to add fork remote" || return 1
git remote add -f --tags "${UPSTREAM_REMOTENAME}" "${UPSTREAM_REPO}" > /dev/null 2>&1 \
|| die "init - failed to add upstream remote" || return 1
echo "Initialized to sync ${FORK_REPO} with ${UPSTREAM_REPO}"
}
function cmd_sync {
is_inited || die "sync - Repo has not been initialized" || return 1
git fetch ${TAGS} "${FORK_REMOTENAME}" > /dev/null 2>&1 \
|| die "sync - failed to fetch fork remote" || return 1
git fetch ${TAGS} "${UPSTREAM_REMOTENAME}" > /dev/null 2>&1 \
|| die "sync - failed to fetch upstream remote" || return 1
if [ -n "${ALL}" ]; then
#sync_all "${FORK_REMOTENAME}" || return 1
sync_all "${UPSTREAM_REMOTENAME}" || return 1
return 0
fi
# Sync a specific branch
sync_branch "${UPSTREAM_REMOTENAME}" "${BRANCH}" || return 1
}
function cmd_push {
is_inited || die "push - Repo has not been initialized" || return 1
if [ -n "${TAGS}" ]; then
git push ${FORCE} ${TAGS} "${FORK_REMOTENAME}" \
|| die "push - failed to push tags to fork remote" || return 1
fi
if [ -n "${ALL}" ]; then
git push ${FORCE} --all "${FORK_REMOTENAME}" \
|| die "push - failed to push all branches to fork remote" \
|| return 1
elif [ -n "${BRANCH}" ]; then
git push ${FORCE} "${FORK_REMOTENAME}" "${BRANCH}" \
|| die "push - failed to push ${BRANCH} to fork remote" \
|| return 1
fi
}
# Run command
case "${COMMAND}" in
"info") cmd_info ;;
"init") cmd_init ;;
"push") cmd_push ;;
"sync") cmd_sync ;;
* ) usage "Unknown command ${COMMAND}";;
esac
git checkout "${CURBRANCH}" > /dev/null 2>&1
if [ -n "${STASH}" ]; then
git stash pop
fi
cd "${SAVEDIR}"