blob: 80eff8ff3aff33463f609c74a08fa869017ebf98 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#!/bin/sh
2
3## repo default configuration
4##
Shawn O. Pearce3f619502012-06-05 07:57:24 -07005REPO_URL='https://gerrit.googlesource.com/git-repo'
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
Conley Owensc5aa4d32012-10-26 11:34:11 -070031VERSION = (1, 19)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
33# increment this if the MAINTAINER_KEYS block is modified
Conley Owens55e4d462012-10-01 16:12:28 -070034KEYRING_VERSION = (1,1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035MAINTAINER_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+ZkPY5qO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LIC KEY BLOCK-----
Conley Owens55e4d462012-10-01 16:12:28 -070078
79 Conley Owens <cco3@android.com>
80-----BEGIN PGP PUBLIC KEY BLOCK-----
81Version: GnuPG v1.4.11 (GNU/Linux)
82
83mQENBFBiLPwBCACvISTASOgFXwADw2GYRH2I2z9RvYkYoZ6ThTTNlMXbbYYKO2Wo
84a9LQDNW0TbCEekg5UKk0FD13XOdWaqUt4Gtuvq9c43GRSjMO6NXH+0BjcQ8vUtY2
85/W4CYUevwdo4nQ1+1zsOCu1XYe/CReXq0fdugv3hgmRmh3sz1soo37Q44W2frxxg
86U7Rz3Da4FjgAL0RQ8qndD+LwRHXTY7H7wYM8V/3cYFZV7pSodd75q3MAXYQLf0ZV
87QR1XATu5l1QnXrxgHvz7MmDwb1D+jX3YPKnZveaukigQ6hDHdiVcePBiGXmk8LZC
882jQkdXeF7Su1ZYpr2nnEHLJ6vOLcCpPGb8gDABEBAAG0H0NvbmxleSBPd2VucyA8
89Y2NvM0BhbmRyb2lkLmNvbT6JATgEEwECACIFAlBiLPwCGwMGCwkIBwMCBhUIAgkK
90CwQWAgMBAh4BAheAAAoJEBkmlFUziHGkHVkH/2Hks2Cif5i2xPtv2IFZcjL42joU
91T7lO5XFqUYS9ZNHpGa/V0eiPt7rHoO16glR83NZtwlrq2cSN89i9HfOhMYV/qLu8
92fLCHcV2muw+yCB5s5bxnI5UkToiNZyBNqFkcOt/Kbj9Hpy68A1kmc6myVEaUYebq
932Chx/f3xuEthan099t746v1K+/6SvQGDNctHuaMr9cWdxZtHjdRf31SQRc99Phe5
94w+ZGR/ebxNDKRK9mKgZT8wVFHlXerJsRqWIqtx1fsW1UgLgbpcpe2MChm6B5wTu0
95s1ltzox3l4q71FyRRPUJxXyvGkDLZWpK7EpiHSCOYq/KP3HkKeXU3xqHpcG5AQ0E
96UGIs/AEIAKzO/7lO9cB6dshmZYo8Vy/b7aGicThE+ChcDSfhvyOXVdEM2GKAjsR+
97rlBWbTFX3It301p2HwZPFEi9nEvJxVlqqBiW0bPmNMkDRR55l2vbWg35wwkg6RyE
98Bc5/TQjhXI2w8IvlimoGoUff4t3JmMOnWrnKSvL+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-----
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110"""
111
112GIT = 'git' # our git command
Conley Owensc5aa4d32012-10-26 11:34:11 -0700113MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114repodir = '.repo' # name of repo's private directory
Conley Owensc9129d92012-10-01 16:12:28 -0700115S_repo = 'repo' # special repo repository
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116S_manifests = 'manifests' # special manifest repository
117REPO_MAIN = S_repo + '/main.py' # main script
118
119
120import optparse
121import os
122import re
Sarah Owens60798a32012-10-25 17:53:09 -0700123import stat
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124import subprocess
125import sys
Sarah Owens1f7627f2012-10-31 09:21:55 -0700126try:
127 import urllib2
128except ImportError:
129 # For python3
130 import urllib.request
131 import urllib.error
132else:
133 # For python2
134 import imp
135 urllib = imp.new_module('urllib')
136 urllib.request = urllib2
137 urllib.error = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138
139home_dot_repo = os.path.expanduser('~/.repoconfig')
140gpg_dir = os.path.join(home_dot_repo, 'gnupg')
141
142extra_args = []
143init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
144
145# Logging
146group = init_optparse.add_option_group('Logging options')
147group.add_option('-q', '--quiet',
148 dest="quiet", action="store_true", default=False,
149 help="be quiet")
150
151# Manifest
152group = init_optparse.add_option_group('Manifest options')
153group.add_option('-u', '--manifest-url',
154 dest='manifest_url',
155 help='manifest repository location', metavar='URL')
156group.add_option('-b', '--manifest-branch',
157 dest='manifest_branch',
158 help='manifest branch or revision', metavar='REVISION')
159group.add_option('-m', '--manifest-name',
160 dest='manifest_name',
161 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800162group.add_option('--mirror',
163 dest='mirror', action='store_true',
164 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200165group.add_option('--reference',
166 dest='reference',
167 help='location of mirror directory', metavar='DIR')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700168group.add_option('--depth', type='int', default=None,
169 dest='depth',
170 help='create a shallow clone with given depth; see git clone')
Colin Cross5acde752012-03-28 20:15:45 -0700171group.add_option('-g', '--groups',
Conley Owens971de8e2012-04-16 10:36:08 -0700172 dest='groups', default='default',
Colin Cross5acde752012-03-28 20:15:45 -0700173 help='restrict manifest projects to ones with a specified group',
174 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700175group.add_option('-p', '--platform',
176 dest='platform', default="auto",
Conley Owensc9129d92012-10-01 16:12:28 -0700177 help='restrict manifest projects to ones with a specified '
Conley Owensd21720d2012-04-16 11:02:21 -0700178 'platform group [auto|all|none|linux|darwin|...]',
179 metavar='PLATFORM')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700180
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700181
182# Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700183group = init_optparse.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700184group.add_option('--repo-url',
185 dest='repo_url',
186 help='repo repository location', metavar='URL')
187group.add_option('--repo-branch',
188 dest='repo_branch',
189 help='repo branch or revision', metavar='REVISION')
190group.add_option('--no-repo-verify',
191 dest='no_repo_verify', action='store_true',
192 help='do not verify repo source code')
193
Victor Boivie841be342011-04-05 11:31:10 +0200194# Other
195group = init_optparse.add_option_group('Other options')
196group.add_option('--config-name',
197 dest='config_name', action="store_true", default=False,
198 help='Always prompt for name/e-mail')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700199
200class CloneFailure(Exception):
201 """Indicate the remote clone of repo itself failed.
202 """
203
204
205def _Init(args):
206 """Installs repo by cloning it over the network.
207 """
208 opt, args = init_optparse.parse_args(args)
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800209 if args:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 init_optparse.print_usage()
211 sys.exit(1)
212
213 url = opt.repo_url
214 if not url:
215 url = REPO_URL
216 extra_args.append('--repo-url=%s' % url)
217
218 branch = opt.repo_branch
219 if not branch:
220 branch = REPO_REV
221 extra_args.append('--repo-branch=%s' % branch)
222
223 if branch.startswith('refs/heads/'):
224 branch = branch[len('refs/heads/'):]
225 if branch.startswith('refs/'):
226 print >>sys.stderr, "fatal: invalid branch name '%s'" % branch
227 raise CloneFailure()
228
229 if not os.path.isdir(repodir):
230 try:
231 os.mkdir(repodir)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700232 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 print >>sys.stderr, \
234 'fatal: cannot make %s directory: %s' % (
235 repodir, e.strerror)
236 # Don't faise CloneFailure; that would delete the
237 # name. Instead exit immediately.
238 #
239 sys.exit(1)
240
241 _CheckGitVersion()
242 try:
Conley Owensc9129d92012-10-01 16:12:28 -0700243 if NeedSetupGnuPG():
244 can_verify = SetupGnuPG(opt.quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700245 else:
246 can_verify = True
247
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248 dst = os.path.abspath(os.path.join(repodir, S_repo))
249 _Clone(url, dst, opt.quiet)
250
251 if can_verify and not opt.no_repo_verify:
252 rev = _Verify(dst, branch, opt.quiet)
253 else:
254 rev = 'refs/remotes/origin/%s^0' % branch
255
256 _Checkout(dst, branch, rev, opt.quiet)
257 except CloneFailure:
258 if opt.quiet:
259 print >>sys.stderr, \
260 'fatal: repo init failed; run without --quiet to see why'
261 raise
262
263
264def _CheckGitVersion():
265 cmd = [GIT, '--version']
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700266 try:
267 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700268 except OSError as e:
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700269 print >>sys.stderr
270 print >>sys.stderr, "fatal: '%s' is not available" % GIT
271 print >>sys.stderr, 'fatal: %s' % e
272 print >>sys.stderr
273 print >>sys.stderr, 'Please make sure %s is installed'\
274 ' and in your path.' % GIT
275 raise CloneFailure()
276
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277 ver_str = proc.stdout.read().strip()
278 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700279 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700280
281 if not ver_str.startswith('git version '):
282 print >>sys.stderr, 'error: "%s" unsupported' % ver_str
283 raise CloneFailure()
284
285 ver_str = ver_str[len('git version '):].strip()
286 ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3]))
287 if ver_act < MIN_GIT_VERSION:
288 need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION))
289 print >>sys.stderr, 'fatal: git %s or later required' % need
290 raise CloneFailure()
291
292
Conley Owensc9129d92012-10-01 16:12:28 -0700293def NeedSetupGnuPG():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700294 if not os.path.isdir(home_dot_repo):
295 return True
296
297 kv = os.path.join(home_dot_repo, 'keyring-version')
298 if not os.path.exists(kv):
299 return True
300
301 kv = open(kv).read()
302 if not kv:
303 return True
304
305 kv = tuple(map(lambda x: int(x), kv.split('.')))
306 if kv < KEYRING_VERSION:
307 return True
308 return False
309
310
Conley Owensc9129d92012-10-01 16:12:28 -0700311def SetupGnuPG(quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700312 if not os.path.isdir(home_dot_repo):
313 try:
314 os.mkdir(home_dot_repo)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700315 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700316 print >>sys.stderr, \
317 'fatal: cannot make %s directory: %s' % (
318 home_dot_repo, e.strerror)
319 sys.exit(1)
320
321 if not os.path.isdir(gpg_dir):
322 try:
Sarah Owens60798a32012-10-25 17:53:09 -0700323 os.mkdir(gpg_dir, stat.S_IRWXU)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700324 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 print >>sys.stderr, \
326 'fatal: cannot make %s directory: %s' % (
327 gpg_dir, e.strerror)
328 sys.exit(1)
329
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800330 env = os.environ.copy()
331 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332
333 cmd = ['gpg', '--import']
334 try:
335 proc = subprocess.Popen(cmd,
336 env = env,
337 stdin = subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700338 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700339 if not quiet:
340 print >>sys.stderr, 'warning: gpg (GnuPG) is not available.'
341 print >>sys.stderr, 'warning: Installing it is strongly encouraged.'
342 print >>sys.stderr
343 return False
344
345 proc.stdin.write(MAINTAINER_KEYS)
346 proc.stdin.close()
347
348 if proc.wait() != 0:
349 print >>sys.stderr, 'fatal: registering repo maintainer keys failed'
350 sys.exit(1)
351 print
352
353 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
354 fd.write('.'.join(map(lambda x: str(x), KEYRING_VERSION)) + '\n')
355 fd.close()
356 return True
357
358
359def _SetConfig(local, name, value):
360 """Set a git configuration option to the specified value.
361 """
362 cmd = [GIT, 'config', name, value]
363 if subprocess.Popen(cmd, cwd = local).wait() != 0:
364 raise CloneFailure()
365
366
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700367def _InitHttp():
368 handlers = []
369
Sarah Owens1f7627f2012-10-31 09:21:55 -0700370 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700371 try:
372 import netrc
373 n = netrc.netrc()
374 for host in n.hosts:
375 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800376 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
377 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700378 except:
379 pass
Sarah Owens1f7627f2012-10-31 09:21:55 -0700380 handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
381 handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700382
383 if 'http_proxy' in os.environ:
384 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700385 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700386 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700387 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
388 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
389 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700390
391def _Fetch(url, local, src, quiet):
392 if not quiet:
393 print >>sys.stderr, 'Get %s' % url
394
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700395 cmd = [GIT, 'fetch']
396 if quiet:
397 cmd.append('--quiet')
398 err = subprocess.PIPE
399 else:
400 err = None
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700401 cmd.append(src)
402 cmd.append('+refs/heads/*:refs/remotes/origin/*')
403 cmd.append('refs/tags/*:refs/tags/*')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404
405 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
406 if err:
407 proc.stderr.read()
408 proc.stderr.close()
409 if proc.wait() != 0:
410 raise CloneFailure()
411
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700412def _DownloadBundle(url, local, quiet):
413 if not url.endswith('/'):
414 url += '/'
415 url += 'clone.bundle'
416
417 proc = subprocess.Popen(
418 [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
419 cwd = local,
420 stdout = subprocess.PIPE)
421 for line in proc.stdout:
422 m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
423 if m:
424 new_url = m.group(1)
425 old_url = m.group(2)
426 if url.startswith(old_url):
427 url = new_url + url[len(old_url):]
428 break
429 proc.stdout.close()
430 proc.wait()
431
432 if not url.startswith('http:') and not url.startswith('https:'):
433 return False
434
435 dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
436 try:
437 try:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700438 r = urllib.request.urlopen(url)
439 except urllib.error.HTTPError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700440 if e.code == 404:
441 return False
442 print >>sys.stderr, 'fatal: Cannot get %s' % url
443 print >>sys.stderr, 'fatal: HTTP error %s' % e.code
444 raise CloneFailure()
Sarah Owens1f7627f2012-10-31 09:21:55 -0700445 except urllib.error.URLError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700446 print >>sys.stderr, 'fatal: Cannot get %s' % url
447 print >>sys.stderr, 'fatal: error %s' % e.reason
448 raise CloneFailure()
449 try:
450 if not quiet:
451 print >>sys.stderr, 'Get %s' % url
452 while True:
453 buf = r.read(8192)
454 if buf == '':
455 return True
456 dest.write(buf)
457 finally:
458 r.close()
459 finally:
460 dest.close()
461
462def _ImportBundle(local):
463 path = os.path.join(local, '.git', 'clone.bundle')
464 try:
465 _Fetch(local, local, path, True)
466 finally:
467 os.remove(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700468
469def _Clone(url, local, quiet):
470 """Clones a git repository to a new subdirectory of repodir
471 """
472 try:
473 os.mkdir(local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700474 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 print >>sys.stderr, \
476 'fatal: cannot make %s directory: %s' \
477 % (local, e.strerror)
478 raise CloneFailure()
479
480 cmd = [GIT, 'init', '--quiet']
481 try:
482 proc = subprocess.Popen(cmd, cwd = local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700483 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700484 print >>sys.stderr
485 print >>sys.stderr, "fatal: '%s' is not available" % GIT
486 print >>sys.stderr, 'fatal: %s' % e
487 print >>sys.stderr
488 print >>sys.stderr, 'Please make sure %s is installed'\
489 ' and in your path.' % GIT
490 raise CloneFailure()
491 if proc.wait() != 0:
492 print >>sys.stderr, 'fatal: could not create %s' % local
493 raise CloneFailure()
494
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700495 _InitHttp()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700496 _SetConfig(local, 'remote.origin.url', url)
497 _SetConfig(local, 'remote.origin.fetch',
498 '+refs/heads/*:refs/remotes/origin/*')
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700499 if _DownloadBundle(url, local, quiet):
500 _ImportBundle(local)
501 else:
502 _Fetch(url, local, 'origin', quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700503
504
505def _Verify(cwd, branch, quiet):
506 """Verify the branch has been signed by a tag.
507 """
508 cmd = [GIT, 'describe', 'origin/%s' % branch]
509 proc = subprocess.Popen(cmd,
510 stdout=subprocess.PIPE,
511 stderr=subprocess.PIPE,
512 cwd = cwd)
513 cur = proc.stdout.read().strip()
514 proc.stdout.close()
515
516 proc.stderr.read()
517 proc.stderr.close()
518
519 if proc.wait() != 0 or not cur:
520 print >>sys.stderr
521 print >>sys.stderr,\
522 "fatal: branch '%s' has not been signed" \
523 % branch
524 raise CloneFailure()
525
526 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
527 if m:
528 cur = m.group(1)
529 if not quiet:
530 print >>sys.stderr
531 print >>sys.stderr, \
532 "info: Ignoring branch '%s'; using tagged release '%s'" \
533 % (branch, cur)
534 print >>sys.stderr
535
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800536 env = os.environ.copy()
537 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700538
539 cmd = [GIT, 'tag', '-v', cur]
540 proc = subprocess.Popen(cmd,
541 stdout = subprocess.PIPE,
542 stderr = subprocess.PIPE,
543 cwd = cwd,
544 env = env)
545 out = proc.stdout.read()
546 proc.stdout.close()
547
548 err = proc.stderr.read()
549 proc.stderr.close()
550
551 if proc.wait() != 0:
552 print >>sys.stderr
553 print >>sys.stderr, out
554 print >>sys.stderr, err
555 print >>sys.stderr
556 raise CloneFailure()
557 return '%s^0' % cur
558
559
560def _Checkout(cwd, branch, rev, quiet):
561 """Checkout an upstream branch into the repository and track it.
562 """
563 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
564 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
565 raise CloneFailure()
566
567 _SetConfig(cwd, 'branch.default.remote', 'origin')
568 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
569
570 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
571 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
572 raise CloneFailure()
573
574 cmd = [GIT, 'read-tree', '--reset', '-u']
575 if not quiet:
576 cmd.append('-v')
577 cmd.append('HEAD')
578 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
579 raise CloneFailure()
580
581
582def _FindRepo():
583 """Look for a repo installation, starting at the current directory.
584 """
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200585 curdir = os.getcwd()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700586 repo = None
587
Anthony Newnamdf14a702011-01-09 17:31:57 -0800588 olddir = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200589 while curdir != '/' \
590 and curdir != olddir \
Anthony Newnamdf14a702011-01-09 17:31:57 -0800591 and not repo:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200592 repo = os.path.join(curdir, repodir, REPO_MAIN)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700593 if not os.path.isfile(repo):
594 repo = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200595 olddir = curdir
596 curdir = os.path.dirname(curdir)
597 return (repo, os.path.join(curdir, repodir))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598
599
600class _Options:
601 help = False
602
603
604def _ParseArguments(args):
605 cmd = None
606 opt = _Options()
607 arg = []
608
Sarah Owensa6053d52012-11-01 13:36:50 -0700609 for i in range(len(args)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700610 a = args[i]
611 if a == '-h' or a == '--help':
612 opt.help = True
613
614 elif not a.startswith('-'):
615 cmd = a
616 arg = args[i + 1:]
617 break
618 return cmd, opt, arg
619
620
621def _Usage():
622 print >>sys.stderr,\
623"""usage: repo COMMAND [ARGS]
624
625repo is not yet installed. Use "repo init" to install it here.
626
627The most commonly used repo commands are:
628
629 init Install repo in the current working directory
630 help Display detailed help on a command
631
632For access to the full online help, install repo ("repo init").
633"""
634 sys.exit(1)
635
636
637def _Help(args):
638 if args:
639 if args[0] == 'init':
640 init_optparse.print_help()
Trond Norbyed3fd5372011-01-03 11:35:15 +0100641 sys.exit(0)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700642 else:
643 print >>sys.stderr,\
644 "error: '%s' is not a bootstrap command.\n"\
645 ' For access to online help, install repo ("repo init").'\
646 % args[0]
647 else:
648 _Usage()
649 sys.exit(1)
650
651
652def _NotInstalled():
653 print >>sys.stderr,\
654'error: repo is not installed. Use "repo init" to install it here.'
655 sys.exit(1)
656
657
658def _NoCommands(cmd):
659 print >>sys.stderr,\
660"""error: command '%s' requires repo to be installed first.
661 Use "repo init" to install it here.""" % cmd
662 sys.exit(1)
663
664
665def _RunSelf(wrapper_path):
666 my_dir = os.path.dirname(wrapper_path)
667 my_main = os.path.join(my_dir, 'main.py')
668 my_git = os.path.join(my_dir, '.git')
669
670 if os.path.isfile(my_main) and os.path.isdir(my_git):
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700671 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700672 'project.py',
673 'subcmds']:
674 if not os.path.exists(os.path.join(my_dir, name)):
675 return None, None
676 return my_main, my_git
677 return None, None
678
679
680def _SetDefaultsTo(gitdir):
681 global REPO_URL
682 global REPO_REV
683
684 REPO_URL = gitdir
685 proc = subprocess.Popen([GIT,
686 '--git-dir=%s' % gitdir,
687 'symbolic-ref',
688 'HEAD'],
689 stdout = subprocess.PIPE,
690 stderr = subprocess.PIPE)
691 REPO_REV = proc.stdout.read().strip()
692 proc.stdout.close()
693
694 proc.stderr.read()
695 proc.stderr.close()
696
697 if proc.wait() != 0:
698 print >>sys.stderr, 'fatal: %s has no current branch' % gitdir
699 sys.exit(1)
700
701
702def main(orig_args):
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200703 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700704 cmd, opt, args = _ParseArguments(orig_args)
705
706 wrapper_path = os.path.abspath(__file__)
707 my_main, my_git = _RunSelf(wrapper_path)
708
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200709 if not repo_main:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700710 if opt.help:
711 _Usage()
712 if cmd == 'help':
713 _Help(args)
714 if not cmd:
715 _NotInstalled()
716 if cmd == 'init':
717 if my_git:
718 _SetDefaultsTo(my_git)
719 try:
720 _Init(args)
721 except CloneFailure:
722 for root, dirs, files in os.walk(repodir, topdown=False):
723 for name in files:
724 os.remove(os.path.join(root, name))
725 for name in dirs:
726 os.rmdir(os.path.join(root, name))
727 os.rmdir(repodir)
728 sys.exit(1)
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200729 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700730 else:
731 _NoCommands(cmd)
732
733 if my_main:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200734 repo_main = my_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700735
736 ver_str = '.'.join(map(lambda x: str(x), VERSION))
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200737 me = [repo_main,
738 '--repo-dir=%s' % rel_repo_dir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700739 '--wrapper-version=%s' % ver_str,
740 '--wrapper-path=%s' % wrapper_path,
741 '--']
742 me.extend(orig_args)
743 me.extend(extra_args)
744 try:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200745 os.execv(repo_main, me)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700746 except OSError as e:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200747 print >>sys.stderr, "fatal: unable to start %s" % repo_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700748 print >>sys.stderr, "fatal: %s" % e
749 sys.exit(148)
750
751
752if __name__ == '__main__':
753 main(sys.argv[1:])