forked from cms-sw/cms-git-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cms-init
executable file
·372 lines (339 loc) · 15.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
#!/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 "-q, --quiet, -z \tdo not print out progress"
$ECHO "-y, --yes \tassume yes to all questions"
exit $1
}
# configuration
CHECK=
CUSTOM_REMOTE=
PROTOCOL=mixed
VERBOSE=1
VERBOSE_STREAM=/dev/stderr
DEBUG_STREAM=/dev/null
OAUTH_TOKEN=869cfb0477e0cae72fb233be5e1f02bd97905bad
PROXY=`git config https.proxy` && PROXY="-x $PROXY"
# colors and formatting
RED='\033[31m'
NORMAL='\033[0m'
verbose () {
if [ "X$VERBOSE" = X1 ]; 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_STREAM=/dev/stderr ;;
-q | --quiet | -z )
shift; set +x; VERBOSE=0; VERBOSE_STREAM=/dev/null ;;
-y | --yes )
shift; ASSUME_YES=1 ;;
--https )
shift; PROTOCOL=https ;;
--ssh )
shift; PROTOCOL=ssh ;;
-*)
$ECHO Unknown option $1 ; usage 1 ;;
*)
if [ "X$CMSSW_TAG" = X ]; then
CMSSW_TAG=$1
else
$ECHO Unexpected argument $1
usage 1
fi
shift 1
;;
esac
done
# 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
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 symbolic-ref --short HEAD`
CURRENT_TAG=`echo $CURRENT_BRANCH | sed -e's/^from-*//'`
fi
if [ "X$CMSSW_TAG" = X ]; then
if [ "$CURRENT_BRANCH" ]; then
verbose No release tags specified, using current branch $CURRENT_BRANCH.
CMSSW_TAG=$CURRENT_TAG
elif [ "$CMSSW_GIT_HASH" ]; then
verbose No release tags specified, using default $CMSSW_GIT_HASH.
CMSSW_TAG=$CMSSW_GIT_HASH
else
verbose 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
$ECHO "Attention: git is unable to access GitHub over https, aborting." > $VERBOSE_STREAM
$ECHO "You may want to retry with the --ssh option." > $VERBOSE_STREAM
exit 1
else
# unable to contact GitHub over https, switching to ssh
$ECHO "Attention: git is unable to access GitHub over https, switching to ssh." > $VERBOSE_STREAM
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 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`
$ECHO "Attention: using the 'my-cmssw' remote from your git configuration: $RED$USER_CMSSW_REPO$NORMAL" > $VERBOSE_STREAM
fi
# check if a shared reference repository is available, otherwise set up a personal one
if [ "$CMSSW_GIT_REFERENCE" = "" ]; then
case `hostname -f` in
*.cern.ch)
if [ -e /afs/cern.ch/cms/git-cmssw-mirror/cmssw.git ]; then
CMSSW_GIT_REFERENCE=/afs/cern.ch/cms/git-cmssw-mirror/cmssw.git
else
CMSSW_GIT_REFERENCE=~/.cmsgit-cache
fi
;;
*) CMSSW_GIT_REFERENCE=~/.cmsgit-cache ;;
esac
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 / ? ] "
QUESTION_DONE=1
else
REPLY=y
QUESTION_DONE=1
fi
case $REPLY in
y|Y|yes|YES)
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 2>$VERBOSE_STREAM >$VERBOSE_STREAM)
fi
# check if the user repository is accessible
if [ "$CUSTOM_REMOTE" ] || [ "$PROTOCOL" = "ssh" ]; then
if [ "$CHECK" ] && ! git ls-remote $USER_CMSSW_REPO >& $DEBUG_STREAM; then
$ECHO "Attention: git is unable to access your 'my-cmssw' remote repository ($RED$USER_CMSSW_REPO$NORMAL). " > $VERBOSE_STREAM
$ECHO "You can work locally, but you will not be able to push your changes to it." > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
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 $DEBUG_STREAM | grep -q -i 'Not Found' ; then
$ECHO "You don't seem to have a GitHub accout, or your GitHub username ($RED$GITHUB_USERNAME$NORMAL) is not correct." > $VERBOSE_STREAM
$ECHO "($RED$GITHUB_USERNAME$NORMAL) is not correct." > $VERBOSE_STREAM
$ECHO "You can work locally, but you will not be able to push your changes to GitHub for inclusion " > $VERBOSE_STREAM
$ECHO "in the official CMSSW distribution." > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "You can correct your GitHub user name via:" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO " git config --global user.github <your github username>" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "To create a personal repository:" > $VERBOSE_STREAM
$ECHO " visit to https://github.com/ and register a new account" > $VERBOSE_STREAM
$ECHO " visit to https://github.com/cms-sw/cmssw and click on the Fork button" > $VERBOSE_STREAM
$ECHO " select the option to fork the repository under your username ($RED$GITHUB_USERNAME$NORMAL)" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
USER_CMSSW_REPO=""
elif ! curl -L -s $PROXY -H "Authorization: token $OAUTH_TOKEN" "https://api.github.com/users/$GITHUB_USERNAME/repos" | tee $DEBUG_STREAM | grep -q '"name": *"cmssw"'; then
$ECHO "You don't seem to have a personal repository, or your GitHub username ($RED$GITHUB_USERNAME$NORMAL) is not correct." > $VERBOSE_STREAM
$ECHO "You can work locally, but you will not be able to push your changes to GitHub for inclusion " > $VERBOSE_STREAM
$ECHO "in the official CMSSW distribution." > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "You can correct your GitHub user name via:" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO " git config --global user.github <your github username>" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
$ECHO "To create a personal repository:" > $VERBOSE_STREAM
$ECHO " - go to https://github.com/ and log in" > $VERBOSE_STREAM
$ECHO " - go to https://github.com/cms-sw/cmssw and click on the Fork button" > $VERBOSE_STREAM
$ECHO " - select the option to fork the repository under your username ($RED$GITHUB_USERNAME$NORMAL)" > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
USER_CMSSW_REPO=""
elif [ "$CHECK" ] && ! git ls-remote $USER_CMSSW_REPO >& $DEBUG_STREAM; then
$ECHO "Attention: your GitHub account ($RED$GITHUB_USERNAME$NORMAL) and personal repository ($RED$USER_CMSSW_REPO$NORMAL) " > $VERBOSE_STREAM
$ECHO "are properly configured, but git is unable to access it." > $VERBOSE_STREAM
$ECHO "You can work locally, but you will not be able to push your changes to GitHub for inclusion " > $VERBOSE_STREAM
$ECHO "in the official CMSSW distribution." > $VERBOSE_STREAM
$ECHO "" > $VERBOSE_STREAM
USER_CMSSW_REPO=""
fi
fi
mkdir -p $CMSSW_BASE/src
cd $CMSSW_BASE/src
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_STREAM
else
git clone --branch $CMSSW_BRANCH --no-checkout --origin official-cmssw $OFFICIAL_CMSSW_REPO $CMSSW_BASE/src >& $VERBOSE_STREAM
fi
git fetch official-cmssw --tags 2> $VERBOSE_STREAM
# create a new branch, pointing to the commit corresponding to $CMSSW_TAG, and switch to it
git branch from-$CMSSW_TAG $CMSSW_TAG
git symbolic-ref HEAD refs/heads/from-$CMSSW_TAG
# check if the user's remote repository exists
if [ "$USER_CMSSW_REPO" ]; then
if ! [ "$CUSTOM_REMOTE" ]; 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
fi
git fetch my-cmssw 2> $VERBOSE_STREAM
fi
# setup sparse checkout
git config core.sparsecheckout true
echo "${LEADING_SLASH}.gitignore" > $CMSSW_BASE/src/.git/info/sparse-checkout
git checkout $CMSSW_BRANCH -- .gitignore
git read-tree -mu HEAD
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
verbose You are on branch `git symbolic-ref --short HEAD`