-
Notifications
You must be signed in to change notification settings - Fork 26
/
git-cms-init
executable file
·479 lines (441 loc) · 16.7 KB
/
git-cms-init
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#! /bin/bash -e
# Initialises a local git repository for use with the official cmssw github remote and the user's github remote repositories
case `uname` in
Darwin)
ECHO="echo" ;;
*)
ECHO="echo -e" ;;
esac
usage () {
$ECHO "git cms-init [options] [release]"
$ECHO
$ECHO "Options:"
$ECHO " -h \tthis help message"
$ECHO
$ECHO " -d, --debug \tenable debug output"
$ECHO " --check \trun additional checks on the status of the user repository"
$ECHO " --https \tuse https, rather than ssh to access your personal repository"
$ECHO " --ssh \tuse ssh, rather than https to access the official repository"
$ECHO " --upstream-only \tconfigure only the official upstream repository, not the user repository"
$ECHO " -w, --enable-push \tenable pushing to the official upstream repository"
$ECHO " -x, --extra NAME \tconfigure an additional repository at https://github.com/NAME/cmssw with pushing disabled"
$ECHO " -X, --extra-push NAME\tconfigure an additional repository at https://github.com/NAME/cmssw with pushing enabled"
$ECHO " -q, --quiet, -z \tdo not print out progress"
$ECHO " -y, --yes \tassume yes to all questions"
exit $1
}
# get default protocol from git config, or use "mixed" by default
PROTOCOL=$(git config --get cms.protocol || echo mixed)
if [ "$PROTOCOL" != "https" ] && [ "$PROTOCOL" != "ssh" ] && [ "$PROTOCOL" != "mixed" ]; then
$ECHO "Unsupported value $PROTOCOL in cms.protocol (choose https, ssh, mixed)"
exit 1
fi
# configuration
CHECK=
CUSTOM_REMOTE=false
DEBUG=false
VERBOSE=true
UPSTREAM_ONLY=false
UPSTREAM_PUSH=false
EXTRAS=
EXTRAS_PUSH=
OAUTH_TOKEN="$(echo YmRjNTIxODVjOTVlOTA2Y2UxMzhlY2E5NTczNGU2ZTFlNjY5YTM0Ngo= | base64 --decode)"
PROXY=`git config http.proxy` && PROXY="-x $PROXY"
# colors and formatting
RED='\033[31m'
NORMAL='\033[0m'
debug() {
if $DEBUG; then
$ECHO "$@"
fi
}
verbose() {
if $VERBOSE; then
$ECHO "$@"
fi
}
CMSSW_TAG=
while [ "$#" != 0 ]; do
case "$1" in
-h | --help )
usage 0;;
--check )
shift; CHECK=true ;;
-d | --debug )
shift; set -x; DEBUG=true ;;
-q | --quiet | -z )
shift; set +x; DEBUG=false; VERBOSE=false ;;
-y | --yes )
shift; ASSUME_YES=1 ;;
--https )
shift; PROTOCOL=https ;;
--ssh )
shift; PROTOCOL=ssh ;;
-w | --enable-push )
shift; UPSTREAM_PUSH=true ;;
--upstream-only )
shift; UPSTREAM_ONLY=true ;;
-x | --extra )
[ "$2" ] || { $ECHO "git cms-init: missing argument to option $1"; $ECHO; usage 1; }
EXTRAS="$EXTRAS $2"
shift 2 ;;
-X | --extra-push )
[ "$2" ] || { $ECHO "git cms-init: missing argument to option $1"; $ECHO; usage 1; }
EXTRAS_PUSH="$EXTRAS_PUSH $2"
shift 2 ;;
-*)
$ECHO "git cms-init: unknown option $1"; $ECHO; usage 1 ;;
*)
if ! [ "$CMSSW_TAG" ]; then
CMSSW_TAG=$1
else
$ECHO "git cms-init: unexpected argument $1"
$ECHO
usage 1
fi
shift 1
;;
esac
done
BASH_FULL_VERSION=$((${BASH_VERSINFO[0]} * 10000 + ${BASH_VERSINFO[1]} * 100 + ${BASH_VERSINFO[2]}))
if (( BASH_FULL_VERSION >= 40100 )); then
# bash 4.1 or newer
if $VERBOSE; then
# send verbose messages to stderr
exec {verbose}>&2
else
# send verbose messages to /dev/null
exec {verbose}> /dev/null
fi
if $DEBUG; then
# send debug messages to stderr
exec {debug}>&2
else
# send debug messages to /dev/null
exec {debug}> /dev/null
fi
else
# bash 4.0 or older
verbose=11
if $VERBOSE; then
# send verbose messages to stderr
exec 11>&2
else
# send verbose messages to /dev/null
exec 11> /dev/null
fi
debug=12
if $DEBUG; then
# send debug messages to stderr
exec 12>&2
else
# send debug messages to /dev/null
exec 12> /dev/null
fi
fi
if ! $UPSTREAM_ONLY; then
# check the user details in the git configuration
USER_FULLNAME="`git config --global --get user.name || true`"
USER_EMAIL="`git config --global --get user.email || true`"
GITHUB_USERNAME="`git config --global --get user.github || true`"
if [ -z "$GITHUB_USERNAME" ] || [ -z "$USER_FULLNAME" ] || [ -z "$USER_EMAIL" ]; then
$ECHO "Cannot find your details in the git configuration."
if [ "X$USER_FULLNAME" = X ]; then
$ECHO
$ECHO "Please set up your full name via:"
$ECHO
$ECHO " git config --global user.name '<your name> <your last name>'"
$ECHO
fi
if [ "X$USER_EMAIL" = X ]; then
$ECHO
$ECHO "Please set up your email via:"
$ECHO
$ECHO " git config --global user.email '<your e-mail>'"
$ECHO
fi
if [ "X$GITHUB_USERNAME" = X ]; then
$ECHO
$ECHO "Please set up your GitHub user name via:"
$ECHO
$ECHO " git config --global user.github <your github username>"
$ECHO
fi
exit 1
fi
fi
if [ "X$CMSSW_BASE" = X ]; then
if [ "X$CMSSW_TAG" = X ]; then
verbose "CMSSW environment not setup. Do cmsenv in some workarea or specify tag to checkout."
exit 1
else
verbose "Warning CMSSW environment not found. Checking out packages in $PWD/src."
CMSSW_BASE=$PWD
fi
fi
if [ -d $CMSSW_BASE/src/.git ]; then
CURRENT_BRANCH=`git --git-dir=$CMSSW_BASE/src/.git symbolic-ref --short HEAD`
CURRENT_TAG=`echo $CURRENT_BRANCH | sed -e's/^from-*//'`
fi
if [ "X$CMSSW_TAG" = X ]; then
if [ "$CURRENT_BRANCH" ]; then
debug "No release tags specified, using current branch $CURRENT_BRANCH."
CMSSW_TAG=$CURRENT_TAG
elif [ "$CMSSW_GIT_HASH" ]; then
debug "No release tags specified, using default $CMSSW_GIT_HASH."
CMSSW_TAG=$CMSSW_GIT_HASH
else
debug "No release tags specified, using default $CMSSW_VERSION."
CMSSW_TAG=$CMSSW_VERSION
fi
fi
if [ "$CURRENT_BRANCH" ] && [ "$CURRENT_TAG" != "$CMSSW_TAG" ]; then
$ECHO "Attention: you have requested a release tag $CMSSW_TAG, but your local git repository has been set up for $CURRENT_TAG."
$ECHO "Aborting."
exit 1
fi
CMSSW_BASE_BRANCH=`echo $CMSSW_TAG | sed -e 's/\(CMSSW_[0-9][0-9]*_[0-9][0-9]*\).*/\1/'`
# SLHC releases
case $CMSSW_TAG in
*_SLHC*)
CMSSW_BRANCH=${CMSSW_BASE_BRANCH}_X_SLHC ;;
*)
CMSSW_BRANCH=${CMSSW_BASE_BRANCH}_X ;;
esac
# This is not the case at FNAL. Disabling it for now.
#IN_RELEASE=`echo $PWD | grep -q -e "^$CMSSW_BASE" 2> /dev/null && echo 1 || echo 0`
#if [ "X$IN_RELEASE" = X0 ]; then
# OLD_CMSSW_BASE=$CMSSW_BASE
# eval `scram run -sh` >/dev/null
# IN_RELEASE=`echo $PWD | grep -q -e "^$CMSSW_BASE" 2> /dev/null && echo 1 || echo 0`
# if [ "X$IN_RELEASE" = X0 ]; then
# $ECHO git cms-addpkg must be run from inside a CMSSW area.
# exit 1
# else
# $ECHO Error: You have currently set up the environment of $OLD_CMSSW_BASE, however you are running inside $CMSSW_BASE.
# $ECHO Please go inside $OLD_CMSSW_BASE, or use cmsenv to switch to $CMSSW_BASE.
# exit 1
# fi
#fi
case `git --version` in
git\ version\ 1.7*)
# make the reference checkout work on git 1.7.x (see commit d5ed1e3aa81f22f993c19073321ee65d2c46c0c5 to git-cms-addpkg)
PROTOCOL=ssh
# git 1.7.x does not support a leading slash in .gitignore and .git/info/sparse-checkout
LEADING_SLASH=
;;
*)
LEADING_SLASH=/
;;
esac
# check if we can access GitHub over https
if [ "$PROTOCOL" != "ssh" ] && ! curl -L -s $PROXY -H "Authorization: token $OAUTH_TOKEN" "https://api.github.com/meta" > /dev/null; then
if [ "$PROTOCOL" = "https" ]; then
# unable to contact GitHub over https, aborting
verbose "Attention: git is unable to access GitHub over https, aborting."
verbose "You may want to retry with the --ssh option."
exit 1
else
# unable to contact GitHub over https, switching to ssh
verbose "Attention: git is unable to access GitHub over https, switching to ssh."
PROTOCOL=ssh
OFFICIAL_CMSSW_REPO="[email protected]:cms-sw/cmssw.git"
fi
fi
if [ "$PROTOCOL" = "ssh" ]; then
[email protected]:cms-sw/cmssw.git
[email protected]:$GITHUB_USERNAME/cmssw.git
elif [ "$PROTOCOL" = "https" ]; then
OFFICIAL_CMSSW_REPO=https://github.com/cms-sw/cmssw.git
USER_CMSSW_REPO=https://github.com/$GITHUB_USERNAME/cmssw.git
elif [ "$PROTOCOL" = "mixed" ]; then
OFFICIAL_CMSSW_REPO=https://github.com/cms-sw/cmssw.git
USER_CMSSW_REPO=https://github.com/$GITHUB_USERNAME/cmssw.git
[email protected]:$GITHUB_USERNAME/cmssw.git
fi
# check if a shared reference repository is available, otherwise set up a personal one
if [ "$CMSSW_GIT_REFERENCE" = "" ]; then
if [ -e /cvmfs/cms-ib.cern.ch/git/cms-sw/cmssw.git ] ; then
CMSSW_GIT_REFERENCE=/cvmfs/cms-ib.cern.ch/git/cms-sw/cmssw.git
elif [ -e /cvmfs/cms.cern.ch/cmssw.git.daily ] ; then
CMSSW_GIT_REFERENCE=/cvmfs/cms.cern.ch/cmssw.git.daily
else
CMSSW_GIT_REFERENCE=~/.cmsgit-cache
fi
fi
if [ ! -e $CMSSW_GIT_REFERENCE ]; then
while [ X$QUESTION_DONE = X ]; do
if [ X$ASSUME_YES = X ]; then
read -n 1 -p "Your reference git repository does not seem to exist, would you like to create it? [ y / N / ? ] "
echo
QUESTION_DONE=1
else
REPLY=y
QUESTION_DONE=1
fi
case $REPLY in
y|Y)
git clone --bare $OFFICIAL_CMSSW_REPO $CMSSW_GIT_REFERENCE
touch $CMSSW_GIT_REFERENCE/create-`whoami`
;;
[?])
$ECHO
$ECHO Answering yes will create a new cache directory in \$CMSSW_GIT_REFERENCE:
$ECHO
$ECHO i.e. $CMSSW_GIT_REFERENCE
$ECHO
$ECHO and put there a copy of the official CMSSW repository. git cms-addpkg will then
$ECHO reuse this information to keep your CMSSW workareas to a minimal size.
$ECHO The initial checkout could take a few minutes, but all the others should then
$ECHO take only a few seconds.
$ECHO
$ECHO Just say yes.
$ECHO
QUESTION_DONE=
;;
*)
CMSSW_GIT_REFERENCE=
;;
esac
done
fi
# if using a personal reference repository, update it from the official one
if [ -e $CMSSW_GIT_REFERENCE/create-`whoami` ]; then
(cd $CMSSW_GIT_REFERENCE ; git remote update origin >&${verbose} 2>&1)
fi
[ -e $CMSSW_BASE/src ] || mkdir $CMSSW_BASE/src
cd $CMSSW_BASE/src
# setup the upstream "official" repository
if [ ! -d "$CMSSW_BASE/src/.git" ]; then
# clone the official repository from $OFFICIAL_CMSSW_REPO, using $CMSSW_GIT_REFERENCE as a local reference, inside $CMSSW_BASE/src
# name the remote repository "official-cmssw"
# start on the branch $CMSSW_BRANCH
# do not checkout any files
# import all remote tags
if [ "$CMSSW_GIT_REFERENCE" ]; then
git clone --branch $CMSSW_BRANCH --no-checkout --reference $CMSSW_GIT_REFERENCE --origin official-cmssw $OFFICIAL_CMSSW_REPO $CMSSW_BASE/src >&${verbose} 2>&1
else
git clone --branch $CMSSW_BRANCH --no-checkout --origin official-cmssw $OFFICIAL_CMSSW_REPO $CMSSW_BASE/src >&${verbose} 2>&1
fi
if ! $UPSTREAM_PUSH; then
# disable pushing to the official upstream repository
git remote set-url --push official-cmssw disabled
fi
# update all branches and tags from the upstream repository
git fetch official-cmssw --tags 2>&${verbose}
# setup sparse checkout
git config core.sparsecheckout true
{
echo "${LEADING_SLASH}.gitignore"
echo "${LEADING_SLASH}.clang-tidy"
echo "${LEADING_SLASH}.clang-format"
} > $CMSSW_BASE/src/.git/info/sparse-checkout
git read-tree -mu HEAD
# set up extra read-only repositories
for EXTRA in $EXTRAS; do
if [ "$PROTOCOL" = "ssh" ]; then
git remote add $EXTRA [email protected]:$EXTRA/cmssw.git
elif [ "$PROTOCOL" = "https" ]; then
git remote add $EXTRA https://github.com/$EXTRA/cmssw.git
elif [ "$PROTOCOL" = "mixed" ]; then
git remote add $EXTRA https://github.com/$EXTRA/cmssw.git
fi
git remote set-url --push $EXTRA disabled
git fetch $EXTRA
done
# set up extra read-write repositories
for EXTRA in $EXTRAS_PUSH; do
if [ "$PROTOCOL" = "ssh" ]; then
git remote add $EXTRA [email protected]:$EXTRA/cmssw.git
elif [ "$PROTOCOL" = "https" ]; then
git remote add $EXTRA https://github.com/$EXTRA/cmssw.git
elif [ "$PROTOCOL" = "mixed" ]; then
git remote add $EXTRA https://github.com/$EXTRA/cmssw.git
git remote set-url --push $EXTRA [email protected]:$EXTRA/cmssw.git
fi
git fetch $EXTRA
done
# create a new branch, pointing to the commit corresponding to $CMSSW_TAG, and switch to it
git checkout $CMSSW_TAG -b from-$CMSSW_TAG 2>&${verbose}
else
# update all branches and tags from the upstream repository
git fetch official-cmssw --tags 2>&${verbose}
fi
# setup the user's repository, if it doesn't exist already
if ! $UPSTREAM_ONLY && ! git remote | grep my-cmssw -q; then
# check if the user has explicitly configured a remote repository
if git config remote.my-cmssw.url >& /dev/null; then
CUSTOM_REMOTE=true
USER_CMSSW_REPO=`git config remote.my-cmssw.url`
USER_CMSSW_REPO_PUSH=`git config remote.my-cmssw.pushurl`
verbose "Attention: using the 'my-cmssw' remote from your git configuration: $RED$USER_CMSSW_REPO$NORMAL"
verbose ""
fi
# check if the user repository is accessible
if $CUSTOM_REMOTE || [ "$PROTOCOL" = "ssh" ]; then
if [ "$CHECK" ] && ! git ls-remote $USER_CMSSW_REPO >&${debug} 2>&1; then
verbose "Attention: git is unable to access your 'my-cmssw' remote repository ($RED$USER_CMSSW_REPO$NORMAL). "
verbose "You can work locally, but you will not be able to push your changes to it."
verbose ""
USER_CMSSW_REPO=""
fi
else
# check the user setup on GitHub
if curl -L -s $PROXY -H "Authorization: token $OAUTH_TOKEN" "https://api.github.com/users/$GITHUB_USERNAME" | tee >(cat >&${debug}) | grep -q -i 'Not Found' ; then
verbose "You don't seem to have a GitHub accout, or your GitHub username ($RED$GITHUB_USERNAME$NORMAL) is not correct."
verbose "($RED$GITHUB_USERNAME$NORMAL) is not correct."
verbose "You can work locally, but you will not be able to push your changes to GitHub for inclusion "
verbose "in the official CMSSW distribution."
verbose ""
verbose "You can correct your GitHub user name via:"
verbose ""
verbose " git config --global user.github <your github username>"
verbose ""
verbose ""
verbose "To create a personal repository:"
verbose " visit to https://github.com/ and register a new account"
verbose " visit to https://github.com/cms-sw/cmssw and click on the Fork button"
verbose " select the option to fork the repository under your username ($RED$GITHUB_USERNAME$NORMAL)"
verbose ""
USER_CMSSW_REPO=""
elif ! curl -L -s $PROXY -H "Authorization: token $OAUTH_TOKEN" "https://api.github.com/users/$GITHUB_USERNAME/repos" | tee >(cat >&${debug}) | grep -q '"name": *"cmssw"'; then
verbose "You don't seem to have a personal repository, or your GitHub username ($RED$GITHUB_USERNAME$NORMAL) is not correct."
verbose "You can work locally, but you will not be able to push your changes to GitHub for inclusion "
verbose "in the official CMSSW distribution."
verbose ""
verbose "You can correct your GitHub user name via:"
verbose ""
verbose " git config --global user.github <your github username>"
verbose ""
verbose ""
verbose "To create a personal repository:"
verbose " - go to https://github.com/ and log in"
verbose " - go to https://github.com/cms-sw/cmssw and click on the Fork button"
verbose " - select the option to fork the repository under your username ($RED$GITHUB_USERNAME$NORMAL)"
verbose ""
USER_CMSSW_REPO=""
elif [ "$CHECK" ] && ! git ls-remote $USER_CMSSW_REPO >&${debug} 2>&1; then
verbose "Attention: your GitHub account ($RED$GITHUB_USERNAME$NORMAL) and personal repository ($RED$USER_CMSSW_REPO$NORMAL) "
verbose "are properly configured, but git is unable to access it."
verbose "You can work locally, but you will not be able to push your changes to GitHub for inclusion "
verbose "in the official CMSSW distribution."
verbose ""
USER_CMSSW_REPO=""
fi
fi
if [ "$USER_CMSSW_REPO" ]; then
# add the user's remote repository
git remote add my-cmssw $USER_CMSSW_REPO
[ "$USER_CMSSW_REPO_PUSH" ] && git remote set-url --push my-cmssw $USER_CMSSW_REPO_PUSH
git fetch my-cmssw 2>&${verbose}
fi
fi
# avoid asking for password for 24 hours.
GIT_CREDENTIAL_CACHE="`git config --get credential.cache || true`"
if [ "X$GIT_CREDENTIAL_CACHE" = X ]; then
case `uname` in
Darwin*) git config --add credential.cache osxkeychain ;;
Linux*) git config --add credential.cache "cache --timeout=86400 --socket $TMPDIR/.git-credential-cache-socket" ;;
esac
fi
debug "You are on branch `git symbolic-ref --short HEAD`"