blob: bdc05c3bbb07c1d42384163a300c9371a6f85e7d [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#!/bin/sh
2
3## repo default configuration
4##
Shawn O. Pearce24d8dfb2008-12-18 07:21:32 -08005REPO_URL='git://android.git.kernel.org/tools/repo.git'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07006REPO_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--'
Shawn O. Pearce7542d662008-10-21 07:11:36 -070023"""exec" python -E "$0" "$@" """#$magic"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024if __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
Shawn O. Pearce88443382010-10-08 10:02:09 +020031VERSION = (1, 9)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
33# increment this if the MAINTAINER_KEYS block is modified
34KEYRING_VERSION = (1,0)
35MAINTAINER_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
44xI1WfzLqdJ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h18VtJJh+nwWeez/RueN4ig59gRTtkcc0PR
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-----
78"""
79
80GIT = 'git' # our git command
81MIN_GIT_VERSION = (1, 5, 4) # minimum supported git version
82repodir = '.repo' # name of repo's private directory
83S_repo = 'repo' # special repo reposiory
84S_manifests = 'manifests' # special manifest repository
85REPO_MAIN = S_repo + '/main.py' # main script
86
87
88import optparse
89import os
90import re
91import readline
92import subprocess
93import sys
94
95home_dot_repo = os.path.expanduser('~/.repoconfig')
96gpg_dir = os.path.join(home_dot_repo, 'gnupg')
97
98extra_args = []
99init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
100
101# Logging
102group = init_optparse.add_option_group('Logging options')
103group.add_option('-q', '--quiet',
104 dest="quiet", action="store_true", default=False,
105 help="be quiet")
106
107# Manifest
108group = init_optparse.add_option_group('Manifest options')
109group.add_option('-u', '--manifest-url',
110 dest='manifest_url',
111 help='manifest repository location', metavar='URL')
112group.add_option('-b', '--manifest-branch',
113 dest='manifest_branch',
114 help='manifest branch or revision', metavar='REVISION')
115group.add_option('-m', '--manifest-name',
116 dest='manifest_name',
117 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800118group.add_option('--mirror',
119 dest='mirror', action='store_true',
120 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200121group.add_option('--reference',
122 dest='reference',
123 help='location of mirror directory', metavar='DIR')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124
125# Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700126group = init_optparse.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127group.add_option('--repo-url',
128 dest='repo_url',
129 help='repo repository location', metavar='URL')
130group.add_option('--repo-branch',
131 dest='repo_branch',
132 help='repo branch or revision', metavar='REVISION')
133group.add_option('--no-repo-verify',
134 dest='no_repo_verify', action='store_true',
135 help='do not verify repo source code')
136
137
138class CloneFailure(Exception):
139 """Indicate the remote clone of repo itself failed.
140 """
141
142
143def _Init(args):
144 """Installs repo by cloning it over the network.
145 """
146 opt, args = init_optparse.parse_args(args)
147 if args or not opt.manifest_url:
148 init_optparse.print_usage()
149 sys.exit(1)
150
151 url = opt.repo_url
152 if not url:
153 url = REPO_URL
154 extra_args.append('--repo-url=%s' % url)
155
156 branch = opt.repo_branch
157 if not branch:
158 branch = REPO_REV
159 extra_args.append('--repo-branch=%s' % branch)
160
161 if branch.startswith('refs/heads/'):
162 branch = branch[len('refs/heads/'):]
163 if branch.startswith('refs/'):
164 print >>sys.stderr, "fatal: invalid branch name '%s'" % branch
165 raise CloneFailure()
166
167 if not os.path.isdir(repodir):
168 try:
169 os.mkdir(repodir)
170 except OSError, e:
171 print >>sys.stderr, \
172 'fatal: cannot make %s directory: %s' % (
173 repodir, e.strerror)
174 # Don't faise CloneFailure; that would delete the
175 # name. Instead exit immediately.
176 #
177 sys.exit(1)
178
179 _CheckGitVersion()
180 try:
181 if _NeedSetupGnuPG():
182 can_verify = _SetupGnuPG(opt.quiet)
183 else:
184 can_verify = True
185
186 if not opt.quiet:
187 print >>sys.stderr, 'Getting repo ...'
188 print >>sys.stderr, ' from %s' % url
189
190 dst = os.path.abspath(os.path.join(repodir, S_repo))
191 _Clone(url, dst, opt.quiet)
192
193 if can_verify and not opt.no_repo_verify:
194 rev = _Verify(dst, branch, opt.quiet)
195 else:
196 rev = 'refs/remotes/origin/%s^0' % branch
197
198 _Checkout(dst, branch, rev, opt.quiet)
199 except CloneFailure:
200 if opt.quiet:
201 print >>sys.stderr, \
202 'fatal: repo init failed; run without --quiet to see why'
203 raise
204
205
206def _CheckGitVersion():
207 cmd = [GIT, '--version']
208 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
209 ver_str = proc.stdout.read().strip()
210 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700211 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700212
213 if not ver_str.startswith('git version '):
214 print >>sys.stderr, 'error: "%s" unsupported' % ver_str
215 raise CloneFailure()
216
217 ver_str = ver_str[len('git version '):].strip()
218 ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3]))
219 if ver_act < MIN_GIT_VERSION:
220 need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION))
221 print >>sys.stderr, 'fatal: git %s or later required' % need
222 raise CloneFailure()
223
224
225def _NeedSetupGnuPG():
226 if not os.path.isdir(home_dot_repo):
227 return True
228
229 kv = os.path.join(home_dot_repo, 'keyring-version')
230 if not os.path.exists(kv):
231 return True
232
233 kv = open(kv).read()
234 if not kv:
235 return True
236
237 kv = tuple(map(lambda x: int(x), kv.split('.')))
238 if kv < KEYRING_VERSION:
239 return True
240 return False
241
242
243def _SetupGnuPG(quiet):
244 if not os.path.isdir(home_dot_repo):
245 try:
246 os.mkdir(home_dot_repo)
247 except OSError, e:
248 print >>sys.stderr, \
249 'fatal: cannot make %s directory: %s' % (
250 home_dot_repo, e.strerror)
251 sys.exit(1)
252
253 if not os.path.isdir(gpg_dir):
254 try:
255 os.mkdir(gpg_dir, 0700)
256 except OSError, e:
257 print >>sys.stderr, \
258 'fatal: cannot make %s directory: %s' % (
259 gpg_dir, e.strerror)
260 sys.exit(1)
261
262 env = dict(os.environ)
263 env['GNUPGHOME'] = gpg_dir
264
265 cmd = ['gpg', '--import']
266 try:
267 proc = subprocess.Popen(cmd,
268 env = env,
269 stdin = subprocess.PIPE)
270 except OSError, e:
271 if not quiet:
272 print >>sys.stderr, 'warning: gpg (GnuPG) is not available.'
273 print >>sys.stderr, 'warning: Installing it is strongly encouraged.'
274 print >>sys.stderr
275 return False
276
277 proc.stdin.write(MAINTAINER_KEYS)
278 proc.stdin.close()
279
280 if proc.wait() != 0:
281 print >>sys.stderr, 'fatal: registering repo maintainer keys failed'
282 sys.exit(1)
283 print
284
285 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
286 fd.write('.'.join(map(lambda x: str(x), KEYRING_VERSION)) + '\n')
287 fd.close()
288 return True
289
290
291def _SetConfig(local, name, value):
292 """Set a git configuration option to the specified value.
293 """
294 cmd = [GIT, 'config', name, value]
295 if subprocess.Popen(cmd, cwd = local).wait() != 0:
296 raise CloneFailure()
297
298
299def _Fetch(local, quiet, *args):
300 cmd = [GIT, 'fetch']
301 if quiet:
302 cmd.append('--quiet')
303 err = subprocess.PIPE
304 else:
305 err = None
306 cmd.extend(args)
307 cmd.append('origin')
308
309 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
310 if err:
311 proc.stderr.read()
312 proc.stderr.close()
313 if proc.wait() != 0:
314 raise CloneFailure()
315
316
317def _Clone(url, local, quiet):
318 """Clones a git repository to a new subdirectory of repodir
319 """
320 try:
321 os.mkdir(local)
322 except OSError, e:
323 print >>sys.stderr, \
324 'fatal: cannot make %s directory: %s' \
325 % (local, e.strerror)
326 raise CloneFailure()
327
328 cmd = [GIT, 'init', '--quiet']
329 try:
330 proc = subprocess.Popen(cmd, cwd = local)
331 except OSError, e:
332 print >>sys.stderr
333 print >>sys.stderr, "fatal: '%s' is not available" % GIT
334 print >>sys.stderr, 'fatal: %s' % e
335 print >>sys.stderr
336 print >>sys.stderr, 'Please make sure %s is installed'\
337 ' and in your path.' % GIT
338 raise CloneFailure()
339 if proc.wait() != 0:
340 print >>sys.stderr, 'fatal: could not create %s' % local
341 raise CloneFailure()
342
343 _SetConfig(local, 'remote.origin.url', url)
344 _SetConfig(local, 'remote.origin.fetch',
345 '+refs/heads/*:refs/remotes/origin/*')
346 _Fetch(local, quiet)
347 _Fetch(local, quiet, '--tags')
348
349
350def _Verify(cwd, branch, quiet):
351 """Verify the branch has been signed by a tag.
352 """
353 cmd = [GIT, 'describe', 'origin/%s' % branch]
354 proc = subprocess.Popen(cmd,
355 stdout=subprocess.PIPE,
356 stderr=subprocess.PIPE,
357 cwd = cwd)
358 cur = proc.stdout.read().strip()
359 proc.stdout.close()
360
361 proc.stderr.read()
362 proc.stderr.close()
363
364 if proc.wait() != 0 or not cur:
365 print >>sys.stderr
366 print >>sys.stderr,\
367 "fatal: branch '%s' has not been signed" \
368 % branch
369 raise CloneFailure()
370
371 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
372 if m:
373 cur = m.group(1)
374 if not quiet:
375 print >>sys.stderr
376 print >>sys.stderr, \
377 "info: Ignoring branch '%s'; using tagged release '%s'" \
378 % (branch, cur)
379 print >>sys.stderr
380
381 env = dict(os.environ)
382 env['GNUPGHOME'] = gpg_dir
383
384 cmd = [GIT, 'tag', '-v', cur]
385 proc = subprocess.Popen(cmd,
386 stdout = subprocess.PIPE,
387 stderr = subprocess.PIPE,
388 cwd = cwd,
389 env = env)
390 out = proc.stdout.read()
391 proc.stdout.close()
392
393 err = proc.stderr.read()
394 proc.stderr.close()
395
396 if proc.wait() != 0:
397 print >>sys.stderr
398 print >>sys.stderr, out
399 print >>sys.stderr, err
400 print >>sys.stderr
401 raise CloneFailure()
402 return '%s^0' % cur
403
404
405def _Checkout(cwd, branch, rev, quiet):
406 """Checkout an upstream branch into the repository and track it.
407 """
408 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
409 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
410 raise CloneFailure()
411
412 _SetConfig(cwd, 'branch.default.remote', 'origin')
413 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
414
415 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
416 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
417 raise CloneFailure()
418
419 cmd = [GIT, 'read-tree', '--reset', '-u']
420 if not quiet:
421 cmd.append('-v')
422 cmd.append('HEAD')
423 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
424 raise CloneFailure()
425
426
427def _FindRepo():
428 """Look for a repo installation, starting at the current directory.
429 """
430 dir = os.getcwd()
431 repo = None
432
433 while dir != '/' and not repo:
434 repo = os.path.join(dir, repodir, REPO_MAIN)
435 if not os.path.isfile(repo):
436 repo = None
437 dir = os.path.dirname(dir)
438 return (repo, os.path.join(dir, repodir))
439
440
441class _Options:
442 help = False
443
444
445def _ParseArguments(args):
446 cmd = None
447 opt = _Options()
448 arg = []
449
450 for i in xrange(0, len(args)):
451 a = args[i]
452 if a == '-h' or a == '--help':
453 opt.help = True
454
455 elif not a.startswith('-'):
456 cmd = a
457 arg = args[i + 1:]
458 break
459 return cmd, opt, arg
460
461
462def _Usage():
463 print >>sys.stderr,\
464"""usage: repo COMMAND [ARGS]
465
466repo is not yet installed. Use "repo init" to install it here.
467
468The most commonly used repo commands are:
469
470 init Install repo in the current working directory
471 help Display detailed help on a command
472
473For access to the full online help, install repo ("repo init").
474"""
475 sys.exit(1)
476
477
478def _Help(args):
479 if args:
480 if args[0] == 'init':
481 init_optparse.print_help()
482 else:
483 print >>sys.stderr,\
484 "error: '%s' is not a bootstrap command.\n"\
485 ' For access to online help, install repo ("repo init").'\
486 % args[0]
487 else:
488 _Usage()
489 sys.exit(1)
490
491
492def _NotInstalled():
493 print >>sys.stderr,\
494'error: repo is not installed. Use "repo init" to install it here.'
495 sys.exit(1)
496
497
498def _NoCommands(cmd):
499 print >>sys.stderr,\
500"""error: command '%s' requires repo to be installed first.
501 Use "repo init" to install it here.""" % cmd
502 sys.exit(1)
503
504
505def _RunSelf(wrapper_path):
506 my_dir = os.path.dirname(wrapper_path)
507 my_main = os.path.join(my_dir, 'main.py')
508 my_git = os.path.join(my_dir, '.git')
509
510 if os.path.isfile(my_main) and os.path.isdir(my_git):
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700511 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700512 'project.py',
513 'subcmds']:
514 if not os.path.exists(os.path.join(my_dir, name)):
515 return None, None
516 return my_main, my_git
517 return None, None
518
519
520def _SetDefaultsTo(gitdir):
521 global REPO_URL
522 global REPO_REV
523
524 REPO_URL = gitdir
525 proc = subprocess.Popen([GIT,
526 '--git-dir=%s' % gitdir,
527 'symbolic-ref',
528 'HEAD'],
529 stdout = subprocess.PIPE,
530 stderr = subprocess.PIPE)
531 REPO_REV = proc.stdout.read().strip()
532 proc.stdout.close()
533
534 proc.stderr.read()
535 proc.stderr.close()
536
537 if proc.wait() != 0:
538 print >>sys.stderr, 'fatal: %s has no current branch' % gitdir
539 sys.exit(1)
540
541
542def main(orig_args):
543 main, dir = _FindRepo()
544 cmd, opt, args = _ParseArguments(orig_args)
545
546 wrapper_path = os.path.abspath(__file__)
547 my_main, my_git = _RunSelf(wrapper_path)
548
549 if not main:
550 if opt.help:
551 _Usage()
552 if cmd == 'help':
553 _Help(args)
554 if not cmd:
555 _NotInstalled()
556 if cmd == 'init':
557 if my_git:
558 _SetDefaultsTo(my_git)
559 try:
560 _Init(args)
561 except CloneFailure:
562 for root, dirs, files in os.walk(repodir, topdown=False):
563 for name in files:
564 os.remove(os.path.join(root, name))
565 for name in dirs:
566 os.rmdir(os.path.join(root, name))
567 os.rmdir(repodir)
568 sys.exit(1)
569 main, dir = _FindRepo()
570 else:
571 _NoCommands(cmd)
572
573 if my_main:
574 main = my_main
575
576 ver_str = '.'.join(map(lambda x: str(x), VERSION))
577 me = [main,
578 '--repo-dir=%s' % dir,
579 '--wrapper-version=%s' % ver_str,
580 '--wrapper-path=%s' % wrapper_path,
581 '--']
582 me.extend(orig_args)
583 me.extend(extra_args)
584 try:
585 os.execv(main, me)
586 except OSError, e:
587 print >>sys.stderr, "fatal: unable to start %s" % main
588 print >>sys.stderr, "fatal: %s" % e
589 sys.exit(148)
590
591
592if __name__ == '__main__':
593 main(sys.argv[1:])
594