blob: f56d678189965f307f4c73c411c845a1c5b19e0e [file] [log] [blame]
msb@chromium.org38b04f82010-07-13 23:03:34 +00001#!/bin/sh
2
3## repo default configuration
4##
szager@chromium.org538283f2013-08-13 21:37:50 +00005REPO_URL='https://chromium.googlesource.com/external/repo'
msb@chromium.org38b04f82010-07-13 23:03:34 +00006REPO_REV='stable'
7
8# Copyright (C) 2008 Google Inc.
9#
10# Licensed under the Apache License, Version 2.0 (the "License");
11# you may not use this file except in compliance with the License.
12# You may obtain a copy of the License at
13#
14# http://www.apache.org/licenses/LICENSE-2.0
15#
16# Unless required by applicable law or agreed to in writing, software
17# distributed under the License is distributed on an "AS IS" BASIS,
18# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19# See the License for the specific language governing permissions and
20# limitations under the License.
21
22magic='--calling-python-from-/bin/sh--'
23"""exec" python -E "$0" "$@" """#$magic"
24if __name__ == '__main__':
25 import sys
26 if sys.argv[-1] == '#%s' % magic:
27 del sys.argv[-1]
28del magic
29
30# increment this whenever we make important changes to this script
ferringb@google.com8db6c882012-12-25 17:02:37 +000031VERSION = (1, 18)
msb@chromium.org38b04f82010-07-13 23:03:34 +000032
33# increment this if the MAINTAINER_KEYS block is modified
szager@chromium.org538283f2013-08-13 21:37:50 +000034KEYRING_VERSION = (1,2)
msb@chromium.org38b04f82010-07-13 23:03:34 +000035MAINTAINER_KEYS = """
36
37 Repo Maintainer <repo@android.kernel.org>
38-----BEGIN PGP PUBLIC KEY BLOCK-----
39Version: GnuPG v1.4.2.2 (GNU/Linux)
40
41mQGiBEj3ugERBACrLJh/ZPyVSKeClMuznFIrsQ+hpNnmJGw1a9GXKYKk8qHPhAZf
42WKtrBqAVMNRLhL85oSlekRz98u41H5si5zcuv+IXJDF5MJYcB8f22wAy15lUqPWi
43VCkk1l8qqLiuW0fo+ZkPY5qOgrvc0HW1SmdH649uNwqCbcKb6CxaTxzhOwCgj3AP
44xI1WfzLqdJjsm1Nq98L0cLcD/iNsILCuw44P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tkcc0PR
6135tX2DR8+xCCFVW/NcJ4PSePYzCuuLvp1vEDHnj41R52Fz51hgddT4rBsp0nL+5I
62socSOIIezw8T9vVzMY4ArCKFAVu2IVyBcahTfBS8q5EM63mONU6UVJEozfGljiMw
63xuQ7JwKcw0AUEKTKG7aBgBaTAgT8TOevpvlw91cAAwUP/jRkyVi/0WAb0qlEaq/S
64ouWxX1faR+vU3b+Y2/DGjtXQMzG0qpetaTHC/AxxHpgt/dCkWI6ljYDnxgPLwG0a
65Oasm94BjZc6vZwf1opFZUKsjOAAxRxNZyjUJKe4UZVuMTk6zo27Nt3LMnc0FO47v
66FcOjRyquvgNOS818irVHUf12waDx8gszKxQTTtFxU5/ePB2jZmhP6oXSe4K/LG5T
67+WBRPDrHiGPhCzJRzm9BP0lTnGCAj3o9W90STZa65RK7IaYpC8TB35JTBEbrrNCp
68w6lzd74LnNEp5eMlKDnXzUAgAH0yzCQeMl7t33QCdYx2hRs2wtTQSjGfAiNmj/WW
69Vl5Jn+2jCDnRLenKHwVRFsBX2e0BiRWt/i9Y8fjorLCXVj4z+7yW6DawdLkJorEo
70p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/
710V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz
725xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ
73HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W
74zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp
75TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2
76=CMiZ
77-----END PGP PUBLIC KEY BLOCK-----
ferringb@google.com8db6c882012-12-25 17:02:37 +000078
79 Conley Owens <cco3@android.com>
80-----BEGIN PGP PU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vL+5iuRj12p9WmanCHzw3Ee7ztf
99/aU/q+FTpr3DLerb6S8xbv86ySgnJT6o5CyL2DCWRtnYQyGVi0ZmLzEouAYiO0hs
100z0AAu28Mj+12g2WwePRz6gfM9rHtI37ylYW3oT/9M9mO9ei/Bc/1D7Dz6qNV+0vg
101uSVJxM2Bl6GalHPZLhHntFEdIA6EdoUAEQEAAYkBHwQYAQIACQUCUGIs/AIbDAAK
102CRAZJpRVM4hxpNfkB/0W/hP5WK/NETXBlWXXW7JPaWO2c5kGwD0lnj5RRmridyo1
103vbm5PdM91jOsDQYqRu6YOoYBnDnEhB2wL2bPh34HWwwrA+LwB8hlcAV2z1bdwyfl
1043R823fReKN3QcvLHzmvZPrF4Rk97M9UIyKS0RtnfTWykRgDWHIsrtQPoNwsXrWoT
1059LrM2v+1+9mp3vuXnE473/NHxmiWEQH9Ez+O/mOxQ7rSOlqGRiKq/IBZCfioJOtV
106fTQeIu/yASZnsLBqr6SJEGwYBoWcyjG++k4fyw8ocOAo4uGDYbxgN7yYfNQ0OH7o
107V6pfUgqKLWa/aK7/N1ZHnPdFLD8Xt0Dmy4BPwrKC
108=O7am
109-----END PGP PUBLIC KEY BLOCK-----
szager@chromium.org538283f2013-08-13 21:37:50 +0000110
111 Stefan Zager <szager@chromium.org>
112-----BEGIN PGP PUBLIC KEY BLOCK-----
113Version: GnuPG v1.4.11 (GNU/Linux)
114
115mQINBFIJOcgBEADwZIq4GRGoO1RJFKlrtVK501cwT5H+Acbizc9N5RxTkFmqxDjb
1169ApUaPW6S1b8+nrzE9P1Ri5erfzipuStfaZ/Wl3mP1JjKulibddmgnPOEbAJ673k
117Vj85RUO4rt2oZAHnZN3D3gFJzVY8JVlZ47Enj9fTqzcW78FVsPCpIT9P2LpTLWeE
118jX9Cjxeimy6VvyJstIcDLYhlpUN5UWen79L4LFAkHf3luLuU4W3p9NriqUsy5UG2
1198vO6QdhKrCr5wsjDFFeVnpMtjlSeZJAWH+XhFFibMX1xP5R9BTuJfzw3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RAhzAhCRaUq02cMWuELKH6Vuh9nzgEefFWty6vPbKEyZLu19D
137B80aqP1cTN88FjtKQ/eTF29TUB6AefUeBS17e2e3WUMy4nc8tduuOFYfiHP40ScP
138wOoatwfzpiTIPGbocUEPL+pS0O/Xy8SINxFMCud3zA==
139=Vd2S
140-----END PGP PUBLIC KEY BLOCK-----
msb@chromium.org38b04f82010-07-13 23:03:34 +0000141"""
142
143GIT = 'git' # our git command
144MIN_GIT_VERSION = (1, 5, 4) # minimum supported git version
145repodir = '.repo' # name of repo's private directory
ferringb@google.com8db6c882012-12-25 17:02:37 +0000146S_repo = 'repo' # special repo repository
msb@chromium.org38b04f82010-07-13 23:03:34 +0000147S_manifests = 'manifests' # special manifest repository
148REPO_MAIN = S_repo + '/main.py' # main script
149
150
151import optparse
152import os
153import re
msb@chromium.org38b04f82010-07-13 23:03:34 +0000154import subprocess
155import sys
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000156import urllib2
msb@chromium.org38b04f82010-07-13 23:03:34 +0000157
158home_dot_repo = os.path.expanduser('~/.repoconfig')
159gpg_dir = os.path.join(home_dot_repo, 'gnupg')
160
161extra_args = []
162init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
163
164# Logging
165group = init_optparse.add_option_group('Logging options')
166group.add_option('-q', '--quiet',
167 dest="quiet", action="store_true", default=False,
168 help="be quiet")
169
170# Manifest
171group = init_optparse.add_option_group('Manifest options')
172group.add_option('-u', '--manifest-url',
173 dest='manifest_url',
174 help='manifest repository location', metavar='URL')
msb@chromium.org38b04f82010-07-13 23:03:34 +0000175group.add_option('-b', '--manifest-branch',
176 dest='manifest_branch',
177 help='manifest branch or revision', metavar='REVISION')
178group.add_option('-m', '--manifest-name',
179 dest='manifest_name',
maruel@chromium.orgb25b69b2011-06-16 17:41:05 +0000180 help='initial manifest file', metavar='NAME.xml')
msb@chromium.org38b04f82010-07-13 23:03:34 +0000181group.add_option('--mirror',
182 dest='mirror', action='store_true',
183 help='mirror the forrest')
msb@chromium.org2c64b712011-01-11 22:57:47 +0000184group.add_option('--reference',
185 dest='reference',
186 help='location of mirror directory', metavar='DIR')
maruel@chromium.orgb25b69b2011-06-16 17:41:05 +0000187group.add_option('--depth', type='int', default=None,
188 dest='depth',
189 help='create a shallow clone with given depth; see git clone')
ferringb@google.com79245bf2012-06-14 21:22:01 +0000190group.add_option('-g', '--groups',
191 dest='groups', default='default',
192 help='restrict manifest projects to ones with a specified group',
193 metavar='GROUP')
194group.add_option('-p', '--platform',
195 dest='platform', default="auto",
ferringb@google.com8db6c882012-12-25 17:02:37 +0000196 help='restrict manifest projects to ones with a specified '
ferringb@google.com79245bf2012-06-14 21:22:01 +0000197 'platform group [auto|all|none|linux|darwin|...]',
198 metavar='PLATFORM')
maruel@chromium.orgb25b69b2011-06-16 17:41:05 +0000199
msb@chromium.org38b04f82010-07-13 23:03:34 +0000200
201# Tool
202group = init_optparse.add_option_group('repo Version options')
203group.add_option('--repo-url',
204 dest='repo_url',
205 help='repo repository location', metavar='URL')
206group.add_option('--repo-branch',
207 dest='repo_branch',
208 help='repo branch or revision', metavar='REVISION')
209group.add_option('--no-repo-verify',
210 dest='no_repo_verify', action='store_true',
211 help='do not verify repo source code')
212
ferringb@google.com79245bf2012-06-14 21:22:01 +0000213# Other
214group = init_optparse.add_option_group('Other options')
215group.add_option('--config-name',
216 dest='config_name', action="store_true", default=False,
217 help='Always prompt for name/e-mail')
msb@chromium.org38b04f82010-07-13 23:03:34 +0000218
219class CloneFailure(Exception):
220 """Indicate the remote clone of repo itself failed.
221 """
222
223
224def _Init(args):
225 """Installs repo by cloning it over the network.
226 """
227 opt, args = init_optparse.parse_args(args)
ferringb@google.com79245bf2012-06-14 21:22:01 +0000228 if args:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000229 init_optparse.print_usage()
230 sys.exit(1)
231
232 url = opt.repo_url
233 if not url:
234 url = REPO_URL
235 extra_args.append('--repo-url=%s' % url)
236
237 branch = opt.repo_branch
238 if not branch:
239 branch = REPO_REV
240 extra_args.append('--repo-branch=%s' % branch)
241
242 if branch.startswith('refs/heads/'):
243 branch = branch[len('refs/heads/'):]
244 if branch.startswith('refs/'):
245 print >>sys.stderr, "fatal: invalid branch name '%s'" % branch
246 raise CloneFailure()
247
248 if not os.path.isdir(repodir):
249 try:
250 os.mkdir(repodir)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000251 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000252 print >>sys.stderr, \
253 'fatal: cannot make %s directory: %s' % (
254 repodir, e.strerror)
255 # Don't faise CloneFailure; that would delete the
256 # name. Instead exit immediately.
257 #
258 sys.exit(1)
259
260 _CheckGitVersion()
261 try:
ferringb@google.com8db6c882012-12-25 17:02:37 +0000262 if NeedSetupGnuPG():
263 can_verify = SetupGnuPG(opt.quiet)
msb@chromium.org38b04f82010-07-13 23:03:34 +0000264 else:
265 can_verify = True
266
msb@chromium.org38b04f82010-07-13 23:03:34 +0000267 dst = os.path.abspath(os.path.join(repodir, S_repo))
268 _Clone(url, dst, opt.quiet)
269
270 if can_verify and not opt.no_repo_verify:
271 rev = _Verify(dst, branch, opt.quiet)
272 else:
273 rev = 'refs/remotes/origin/%s^0' % branch
274
275 _Checkout(dst, branch, rev, opt.quiet)
276 except CloneFailure:
277 if opt.quiet:
278 print >>sys.stderr, \
279 'fatal: repo init failed; run without --quiet to see why'
280 raise
281
282
283def _CheckGitVersion():
284 cmd = [GIT, '--version']
ferringb@google.com79245bf2012-06-14 21:22:01 +0000285 try:
286 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000287 except OSError as e:
ferringb@google.com79245bf2012-06-14 21:22:01 +0000288 print >>sys.stderr
289 print >>sys.stderr, "fatal: '%s' is not available" % GIT
290 print >>sys.stderr, 'fatal: %s' % e
291 print >>sys.stderr
292 print >>sys.stderr, 'Please make sure %s is installed'\
293 ' and in your path.' % GIT
294 raise CloneFailure()
295
msb@chromium.org38b04f82010-07-13 23:03:34 +0000296 ver_str = proc.stdout.read().strip()
297 proc.stdout.close()
298 proc.wait()
299
300 if not ver_str.startswith('git version '):
301 print >>sys.stderr, 'error: "%s" unsupported' % ver_str
302 raise CloneFailure()
303
304 ver_str = ver_str[len('git version '):].strip()
305 ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3]))
306 if ver_act < MIN_GIT_VERSION:
307 need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION))
308 print >>sys.stderr, 'fatal: git %s or later required' % need
309 raise CloneFailure()
310
311
ferringb@google.com8db6c882012-12-25 17:02:37 +0000312def NeedSetupGnuPG():
msb@chromium.org38b04f82010-07-13 23:03:34 +0000313 if not os.path.isdir(home_dot_repo):
314 return True
315
316 kv = os.path.join(home_dot_repo, 'keyring-version')
317 if not os.path.exists(kv):
318 return True
319
320 kv = open(kv).read()
321 if not kv:
322 return True
323
324 kv = tuple(map(lambda x: int(x), kv.split('.')))
325 if kv < KEYRING_VERSION:
326 return True
327 return False
328
329
ferringb@google.com8db6c882012-12-25 17:02:37 +0000330def SetupGnuPG(quiet):
msb@chromium.org38b04f82010-07-13 23:03:34 +0000331 if not os.path.isdir(home_dot_repo):
332 try:
333 os.mkdir(home_dot_repo)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000334 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000335 print >>sys.stderr, \
336 'fatal: cannot make %s directory: %s' % (
337 home_dot_repo, e.strerror)
338 sys.exit(1)
339
340 if not os.path.isdir(gpg_dir):
341 try:
342 os.mkdir(gpg_dir, 0700)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000343 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000344 print >>sys.stderr, \
345 'fatal: cannot make %s directory: %s' % (
346 gpg_dir, e.strerror)
347 sys.exit(1)
348
msb@chromium.org2c64b712011-01-11 22:57:47 +0000349 env = os.environ.copy()
350 env['GNUPGHOME'] = gpg_dir.encode()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000351
352 cmd = ['gpg', '--import']
353 try:
354 proc = subprocess.Popen(cmd,
355 env = env,
356 stdin = subprocess.PIPE)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000357 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000358 if not quiet:
359 print >>sys.stderr, 'warning: gpg (GnuPG) is not available.'
360 print >>sys.stderr, 'warning: Installing it is strongly encouraged.'
361 print >>sys.stderr
362 return False
363
364 proc.stdin.write(MAINTAINER_KEYS)
365 proc.stdin.close()
366
367 if proc.wait() != 0:
368 print >>sys.stderr, 'fatal: registering repo maintainer keys failed'
369 sys.exit(1)
370 print
371
372 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
373 fd.write('.'.join(map(lambda x: str(x), KEYRING_VERSION)) + '\n')
374 fd.close()
375 return True
376
377
378def _SetConfig(local, name, value):
379 """Set a git configuration option to the specified value.
380 """
381 cmd = [GIT, 'config', name, value]
382 if subprocess.Popen(cmd, cwd = local).wait() != 0:
383 raise CloneFailure()
384
385
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000386def _InitHttp():
387 handlers = []
388
389 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
390 try:
391 import netrc
392 n = netrc.netrc()
393 for host in n.hosts:
394 p = n.hosts[host]
ferringb@google.com79245bf2012-06-14 21:22:01 +0000395 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
396 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000397 except:
398 pass
399 handlers.append(urllib2.HTTPBasicAuthHandler(mgr))
ferringb@google.com79245bf2012-06-14 21:22:01 +0000400 handlers.append(urllib2.HTTPDigestAuthHandler(mgr))
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000401
402 if 'http_proxy' in os.environ:
403 url = os.environ['http_proxy']
404 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
405 if 'REPO_CURL_VERBOSE' in os.environ:
406 handlers.append(urllib2.HTTPHandler(debuglevel=1))
407 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
408 urllib2.install_opener(urllib2.build_opener(*handlers))
409
410def _Fetch(url, local, src, quiet):
411 if not quiet:
412 print >>sys.stderr, 'Get %s' % url
413
msb@chromium.org38b04f82010-07-13 23:03:34 +0000414 cmd = [GIT, 'fetch']
415 if quiet:
416 cmd.append('--quiet')
417 err = subprocess.PIPE
418 else:
419 err = None
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000420 cmd.append(src)
421 cmd.append('+refs/heads/*:refs/remotes/origin/*')
422 cmd.append('refs/tags/*:refs/tags/*')
msb@chromium.org38b04f82010-07-13 23:03:34 +0000423
424 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
425 if err:
426 proc.stderr.read()
427 proc.stderr.close()
428 if proc.wait() != 0:
429 raise CloneFailure()
430
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000431def _DownloadBundle(url, local, quiet):
432 if not url.endswith('/'):
433 url += '/'
434 url += 'clone.bundle'
435
436 proc = subprocess.Popen(
437 [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
438 cwd = local,
439 stdout = subprocess.PIPE)
440 for line in proc.stdout:
441 m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
442 if m:
443 new_url = m.group(1)
444 old_url = m.group(2)
445 if url.startswith(old_url):
446 url = new_url + url[len(old_url):]
447 break
448 proc.stdout.close()
449 proc.wait()
450
451 if not url.startswith('http:') and not url.startswith('https:'):
452 return False
453
454 dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
455 try:
456 try:
457 r = urllib2.urlopen(url)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000458 except urllib2.HTTPError as e:
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000459 if e.code == 404:
460 return False
461 print >>sys.stderr, 'fatal: Cannot get %s' % url
462 print >>sys.stderr, 'fatal: HTTP error %s' % e.code
463 raise CloneFailure()
ferringb@google.com8db6c882012-12-25 17:02:37 +0000464 except urllib2.URLError as e:
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000465 print >>sys.stderr, 'fatal: Cannot get %s' % url
466 print >>sys.stderr, 'fatal: error %s' % e.reason
467 raise CloneFailure()
468 try:
469 if not quiet:
470 print >>sys.stderr, 'Get %s' % url
471 while True:
472 buf = r.read(8192)
473 if buf == '':
474 return True
475 dest.write(buf)
476 finally:
477 r.close()
478 finally:
479 dest.close()
480
481def _ImportBundle(local):
482 path = os.path.join(local, '.git', 'clone.bundle')
483 try:
484 _Fetch(local, local, path, True)
485 finally:
486 os.remove(path)
msb@chromium.org38b04f82010-07-13 23:03:34 +0000487
488def _Clone(url, local, quiet):
489 """Clones a git repository to a new subdirectory of repodir
490 """
491 try:
492 os.mkdir(local)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000493 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000494 print >>sys.stderr, \
495 'fatal: cannot make %s directory: %s' \
496 % (local, e.strerror)
497 raise CloneFailure()
498
499 cmd = [GIT, 'init', '--quiet']
500 try:
501 proc = subprocess.Popen(cmd, cwd = local)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000502 except OSError as e:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000503 print >>sys.stderr
504 print >>sys.stderr, "fatal: '%s' is not available" % GIT
505 print >>sys.stderr, 'fatal: %s' % e
506 print >>sys.stderr
507 print >>sys.stderr, 'Please make sure %s is installed'\
508 ' and in your path.' % GIT
509 raise CloneFailure()
510 if proc.wait() != 0:
511 print >>sys.stderr, 'fatal: could not create %s' % local
512 raise CloneFailure()
513
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000514 _InitHttp()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000515 _SetConfig(local, 'remote.origin.url', url)
516 _SetConfig(local, 'remote.origin.fetch',
517 '+refs/heads/*:refs/remotes/origin/*')
jeffbailey@chromium.orgad390562011-11-12 00:37:07 +0000518 if _DownloadBundle(url, local, quiet):
519 _ImportBundle(local)
520 else:
521 _Fetch(url, local, 'origin', quiet)
msb@chromium.org38b04f82010-07-13 23:03:34 +0000522
523
524def _Verify(cwd, branch, quiet):
525 """Verify the branch has been signed by a tag.
526 """
527 cmd = [GIT, 'describe', 'origin/%s' % branch]
528 proc = subprocess.Popen(cmd,
529 stdout=subprocess.PIPE,
530 stderr=subprocess.PIPE,
531 cwd = cwd)
532 cur = proc.stdout.read().strip()
533 proc.stdout.close()
534
535 proc.stderr.read()
536 proc.stderr.close()
537
538 if proc.wait() != 0 or not cur:
539 print >>sys.stderr
540 print >>sys.stderr,\
541 "fatal: branch '%s' has not been signed" \
542 % branch
543 raise CloneFailure()
544
545 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
546 if m:
547 cur = m.group(1)
548 if not quiet:
549 print >>sys.stderr
550 print >>sys.stderr, \
551 "info: Ignoring branch '%s'; using tagged release '%s'" \
552 % (branch, cur)
553 print >>sys.stderr
554
msb@chromium.org2c64b712011-01-11 22:57:47 +0000555 env = os.environ.copy()
556 env['GNUPGHOME'] = gpg_dir.encode()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000557
558 cmd = [GIT, 'tag', '-v', cur]
559 proc = subprocess.Popen(cmd,
560 stdout = subprocess.PIPE,
561 stderr = subprocess.PIPE,
562 cwd = cwd,
563 env = env)
564 out = proc.stdout.read()
565 proc.stdout.close()
566
567 err = proc.stderr.read()
568 proc.stderr.close()
569
570 if proc.wait() != 0:
571 print >>sys.stderr
572 print >>sys.stderr, out
573 print >>sys.stderr, err
574 print >>sys.stderr
575 raise CloneFailure()
576 return '%s^0' % cur
577
578
579def _Checkout(cwd, branch, rev, quiet):
580 """Checkout an upstream branch into the repository and track it.
581 """
582 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
583 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
584 raise CloneFailure()
585
586 _SetConfig(cwd, 'branch.default.remote', 'origin')
587 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
588
589 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
590 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
591 raise CloneFailure()
592
593 cmd = [GIT, 'read-tree', '--reset', '-u']
594 if not quiet:
595 cmd.append('-v')
596 cmd.append('HEAD')
597 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
598 raise CloneFailure()
599
600
601def _FindRepo():
602 """Look for a repo installation, starting at the current directory.
603 """
ferringb@google.com8db6c882012-12-25 17:02:37 +0000604 curdir = os.getcwd()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000605 repo = None
606
msb@chromium.org2c64b712011-01-11 22:57:47 +0000607 olddir = None
ferringb@google.com8db6c882012-12-25 17:02:37 +0000608 while curdir != '/' \
609 and curdir != olddir \
msb@chromium.org2c64b712011-01-11 22:57:47 +0000610 and not repo:
ferringb@google.com8db6c882012-12-25 17:02:37 +0000611 repo = os.path.join(curdir, repodir, REPO_MAIN)
msb@chromium.org38b04f82010-07-13 23:03:34 +0000612 if not os.path.isfile(repo):
613 repo = None
ferringb@google.com8db6c882012-12-25 17:02:37 +0000614 olddir = curdir
615 curdir = os.path.dirname(curdir)
616 return (repo, os.path.join(curdir, repodir))
msb@chromium.org38b04f82010-07-13 23:03:34 +0000617
618
619class _Options:
620 help = False
621
622
623def _ParseArguments(args):
624 cmd = None
625 opt = _Options()
626 arg = []
627
628 for i in xrange(0, len(args)):
629 a = args[i]
630 if a == '-h' or a == '--help':
631 opt.help = True
632
633 elif not a.startswith('-'):
634 cmd = a
635 arg = args[i + 1:]
636 break
637 return cmd, opt, arg
638
639
640def _Usage():
641 print >>sys.stderr,\
642"""usage: repo COMMAND [ARGS]
643
644repo is not yet installed. Use "repo init" to install it here.
645
646The most commonly used repo commands are:
647
648 init Install repo in the current working directory
649 help Display detailed help on a command
650
651For access to the full online help, install repo ("repo init").
652"""
653 sys.exit(1)
654
655
656def _Help(args):
657 if args:
658 if args[0] == 'init':
659 init_optparse.print_help()
msb@chromium.org2c64b712011-01-11 22:57:47 +0000660 sys.exit(0)
msb@chromium.org38b04f82010-07-13 23:03:34 +0000661 else:
662 print >>sys.stderr,\
663 "error: '%s' is not a bootstrap command.\n"\
664 ' For access to online help, install repo ("repo init").'\
665 % args[0]
666 else:
667 _Usage()
668 sys.exit(1)
669
670
671def _NotInstalled():
672 print >>sys.stderr,\
673'error: repo is not installed. Use "repo init" to install it here.'
674 sys.exit(1)
675
676
677def _NoCommands(cmd):
678 print >>sys.stderr,\
679"""error: command '%s' requires repo to be installed first.
680 Use "repo init" to install it here.""" % cmd
681 sys.exit(1)
682
683
684def _RunSelf(wrapper_path):
685 my_dir = os.path.dirname(wrapper_path)
686 my_main = os.path.join(my_dir, 'main.py')
687 my_git = os.path.join(my_dir, '.git')
688
689 if os.path.isfile(my_main) and os.path.isdir(my_git):
690 for name in ['git_config.py',
691 'project.py',
692 'subcmds']:
693 if not os.path.exists(os.path.join(my_dir, name)):
694 return None, None
695 return my_main, my_git
696 return None, None
697
698
699def _SetDefaultsTo(gitdir):
700 global REPO_URL
701 global REPO_REV
702
703 REPO_URL = gitdir
704 proc = subprocess.Popen([GIT,
705 '--git-dir=%s' % gitdir,
706 'symbolic-ref',
707 'HEAD'],
708 stdout = subprocess.PIPE,
709 stderr = subprocess.PIPE)
710 REPO_REV = proc.stdout.read().strip()
711 proc.stdout.close()
712
713 proc.stderr.read()
714 proc.stderr.close()
715
716 if proc.wait() != 0:
717 print >>sys.stderr, 'fatal: %s has no current branch' % gitdir
718 sys.exit(1)
719
720
721def main(orig_args):
ferringb@google.com8db6c882012-12-25 17:02:37 +0000722 repo_main, rel_repo_dir = _FindRepo()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000723 cmd, opt, args = _ParseArguments(orig_args)
724
725 wrapper_path = os.path.abspath(__file__)
726 my_main, my_git = _RunSelf(wrapper_path)
727
ferringb@google.com8db6c882012-12-25 17:02:37 +0000728 if not repo_main:
msb@chromium.org38b04f82010-07-13 23:03:34 +0000729 if opt.help:
730 _Usage()
731 if cmd == 'help':
732 _Help(args)
733 if not cmd:
734 _NotInstalled()
735 if cmd == 'init':
736 if my_git:
737 _SetDefaultsTo(my_git)
738 try:
739 _Init(args)
740 except CloneFailure:
741 for root, dirs, files in os.walk(repodir, topdown=False):
742 for name in files:
743 os.remove(os.path.join(root, name))
744 for name in dirs:
745 os.rmdir(os.path.join(root, name))
746 os.rmdir(repodir)
747 sys.exit(1)
ferringb@google.com8db6c882012-12-25 17:02:37 +0000748 repo_main, rel_repo_dir = _FindRepo()
msb@chromium.org38b04f82010-07-13 23:03:34 +0000749 else:
750 _NoCommands(cmd)
751
szager@chromium.org538283f2013-08-13 21:37:50 +0000752 if cmd == 'sync' and NeedSetupGnuPG():
753 SetupGnuPG(False)
754
msb@chromium.org38b04f82010-07-13 23:03:34 +0000755 if my_main:
ferringb@google.com8db6c882012-12-25 17:02:37 +0000756 repo_main = my_main
msb@chromium.org38b04f82010-07-13 23:03:34 +0000757
758 ver_str = '.'.join(map(lambda x: str(x), VERSION))
ferringb@google.com8db6c882012-12-25 17:02:37 +0000759 me = [repo_main,
760 '--repo-dir=%s' % rel_repo_dir,
msb@chromium.org38b04f82010-07-13 23:03:34 +0000761 '--wrapper-version=%s' % ver_str,
762 '--wrapper-path=%s' % wrapper_path,
763 '--']
764 me.extend(orig_args)
765 me.extend(extra_args)
766 try:
ferringb@google.com8db6c882012-12-25 17:02:37 +0000767 os.execv(repo_main, me)
768 except OSError as e:
769 print >>sys.stderr, "fatal: unable to start %s" % repo_main
msb@chromium.org38b04f82010-07-13 23:03:34 +0000770 print >>sys.stderr, "fatal: %s" % e
771 sys.exit(148)
772
773
774if __name__ == '__main__':
775 main(sys.argv[1:])