blob: e06a5d5c6f143dc79c70746ee39b2614a67bfa1c [file] [log] [blame]
David Pursehouse8898e2f2012-11-14 07:51:03 +09001#!/usr/bin/env python
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002
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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022# increment this whenever we make important changes to this script
Conley Owensc5aa4d32012-10-26 11:34:11 -070023VERSION = (1, 19)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024
25# increment this if the MAINTAINER_KEYS block is modified
Conley Owens55e4d462012-10-01 16:12:28 -070026KEYRING_VERSION = (1,1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027MAINTAINER_KEYS = """
28
29 Repo Maintainer <repo@android.kernel.org>
30-----BEGIN PGP PUBLIC KEY BLOCK-----
31Version: GnuPG v1.4.2.2 (GNU/Linux)
32
33mQGiBEj3ugERBACrLJh/ZPyVSKeClMuznFIrsQ+hpNnmJGw1a9GXKYKk8qHPhAZf
34WKtrBqAVMNRLhL85oSlekRz98u41H5si5zcuv+IXJDF5MJYcB8f22wAy15lUqPWi
35VCkk1l8qqLiuW0fo+ZkPY5qOgrvc0HW1SmdH649uNwqCbcKb6CxaTxzhOwCgj3AP
36xI1WfzLqdJjsm1Nq98L0cLcD/iNsILCuw44PRds3J75YP0pze7YF/6WFMB6QSFGu
37aU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PSePYzCuuLvp1vEDHnj41R52Fz51hgddT4rBsp0nL+5I
54socSOIIezw8T9vVzMY4ArCKFAVu2IVyBcahTfBS8q5EM63mONU6UVJEozfGljiMw
55xuQ7JwKcw0AUEKTKG7aBgBaTAgT8TOevpvlw91cAAwUP/jRkyVi/0WAb0qlEaq/S
56ouWxX1faR+vU3b+Y2/DGjtXQMzG0qpetaTHC/AxxHpgt/dCkWI6ljYDnxgPLwG0a
57Oasm94BjZc6vZwf1opFZUKsjOAAxRxNZyjUJKe4UZVuMTk6zo27Nt3LMnc0FO47v
58FcOjRyquvgNOS818irVHUf12waDx8gszKxQTTtFxU5/ePB2jZmhP6oXSe4K/LG5T
59+WBRPDrHiGPhCzJRzm9BP0lTnGCAj3o9W90STZa65RK7IaYpC8TB35JTBEbrrNCp
60w6lzd74LnNEp5eMlKDnXzUAgAH0yzCQeMl7t33QCdYx2hRs2wtTQSjGfAiNmj/WW
61Vl5Jn+2jCDnRLenKHwVRFsBX2e0BiRWt/i9Y8fjorLCXVj4z+7yW6DawdLkJorEo
62p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/
630V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz
645xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ
65HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W
66zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp
67TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2
68=CMiZ
69-----END PGP PUBLIC KEY BLOCK-----
Conley Owens55e4d462012-10-01 16:12:28 -070070
71 Conley Owens <cco3@android.com>
72-----BEGIN PGP PUBLIC KEY BLOCK-----
73Version: GnuPG v1.4.11 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/q+FTpr3DLerb6S8xbv86ySgnJT6o5CyL2DCWRtnYQyGVi0ZmLzEouAYiO0hs
92z0AAu28Mj+12g2WwePRz6gfM9rHtI37ylYW3oT/9M9mO9ei/Bc/1D7Dz6qNV+0vg
93uSVJxM2Bl6GalHPZLhHntFEdIA6EdoUAEQEAAYkBHwQYAQIACQUCUGIs/AIbDAAK
94CRAZJpRVM4hxpNfkB/0W/hP5WK/NETXBlWXXW7JPaWO2c5kGwD0lnj5RRmridyo1
95vbm5PdM91jOsDQYqRu6YOoYBnDnEhB2wL2bPh34HWwwrA+LwB8hlcAV2z1bdwyfl
963R823fReKN3QcvLHzmvZPrF4Rk97M9UIyKS0RtnfTWykRgDWHIsrtQPoNwsXrWoT
979LrM2v+1+9mp3vuXnE473/NHxmiWEQH9Ez+O/mOxQ7rSOlqGRiKq/IBZCfioJOtV
98fTQeIu/yASZnsLBqr6SJEGwYBoWcyjG++k4fyw8ocOAo4uGDYbxgN7yYfNQ0OH7o
99V6pfUgqKLWa/aK7/N1ZHnPdFLD8Xt0Dmy4BPwrKC
100=O7am
101-----END PGP PUBLIC KEY BLOCK-----
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102"""
103
104GIT = 'git' # our git command
Conley Owensc5aa4d32012-10-26 11:34:11 -0700105MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106repodir = '.repo' # name of repo's private directory
Conley Owensc9129d92012-10-01 16:12:28 -0700107S_repo = 'repo' # special repo repository
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108S_manifests = 'manifests' # special manifest repository
109REPO_MAIN = S_repo + '/main.py' # main script
110
111
112import optparse
113import os
114import re
Sarah Owens60798a32012-10-25 17:53:09 -0700115import stat
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116import subprocess
117import sys
Sarah Owens1f7627f2012-10-31 09:21:55 -0700118try:
119 import urllib2
120except ImportError:
121 # For python3
122 import urllib.request
123 import urllib.error
124else:
125 # For python2
126 import imp
127 urllib = imp.new_module('urllib')
128 urllib.request = urllib2
129 urllib.error = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130
131home_dot_repo = os.path.expanduser('~/.repoconfig')
132gpg_dir = os.path.join(home_dot_repo, 'gnupg')
133
134extra_args = []
135init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
136
137# Logging
138group = init_optparse.add_option_group('Logging options')
139group.add_option('-q', '--quiet',
140 dest="quiet", action="store_true", default=False,
141 help="be quiet")
142
143# Manifest
144group = init_optparse.add_option_group('Manifest options')
145group.add_option('-u', '--manifest-url',
146 dest='manifest_url',
147 help='manifest repository location', metavar='URL')
148group.add_option('-b', '--manifest-branch',
149 dest='manifest_branch',
150 help='manifest branch or revision', metavar='REVISION')
151group.add_option('-m', '--manifest-name',
152 dest='manifest_name',
153 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800154group.add_option('--mirror',
155 dest='mirror', action='store_true',
156 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200157group.add_option('--reference',
158 dest='reference',
159 help='location of mirror directory', metavar='DIR')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700160group.add_option('--depth', type='int', default=None,
161 dest='depth',
162 help='create a shallow clone with given depth; see git clone')
Colin Cross5acde752012-03-28 20:15:45 -0700163group.add_option('-g', '--groups',
Conley Owens971de8e2012-04-16 10:36:08 -0700164 dest='groups', default='default',
Colin Cross5acde752012-03-28 20:15:45 -0700165 help='restrict manifest projects to ones with a specified group',
166 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700167group.add_option('-p', '--platform',
168 dest='platform', default="auto",
Conley Owensc9129d92012-10-01 16:12:28 -0700169 help='restrict manifest projects to ones with a specified '
Conley Owensd21720d2012-04-16 11:02:21 -0700170 'platform group [auto|all|none|linux|darwin|...]',
171 metavar='PLATFORM')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700172
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700173
174# Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700175group = init_optparse.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176group.add_option('--repo-url',
177 dest='repo_url',
178 help='repo repository location', metavar='URL')
179group.add_option('--repo-branch',
180 dest='repo_branch',
181 help='repo branch or revision', metavar='REVISION')
182group.add_option('--no-repo-verify',
183 dest='no_repo_verify', action='store_true',
184 help='do not verify repo source code')
185
Victor Boivie841be342011-04-05 11:31:10 +0200186# Other
187group = init_optparse.add_option_group('Other options')
188group.add_option('--config-name',
189 dest='config_name', action="store_true", default=False,
190 help='Always prompt for name/e-mail')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191
192class CloneFailure(Exception):
193 """Indicate the remote clone of repo itself failed.
194 """
195
196
197def _Init(args):
198 """Installs repo by cloning it over the network.
199 """
200 opt, args = init_optparse.parse_args(args)
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800201 if args:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700202 init_optparse.print_usage()
203 sys.exit(1)
204
205 url = opt.repo_url
206 if not url:
207 url = REPO_URL
208 extra_args.append('--repo-url=%s' % url)
209
210 branch = opt.repo_branch
211 if not branch:
212 branch = REPO_REV
213 extra_args.append('--repo-branch=%s' % branch)
214
215 if branch.startswith('refs/heads/'):
216 branch = branch[len('refs/heads/'):]
217 if branch.startswith('refs/'):
218 print >>sys.stderr, "fatal: invalid branch name '%s'" % branch
219 raise CloneFailure()
220
221 if not os.path.isdir(repodir):
222 try:
223 os.mkdir(repodir)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700224 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 print >>sys.stderr, \
226 'fatal: cannot make %s directory: %s' % (
227 repodir, e.strerror)
228 # Don't faise CloneFailure; that would delete the
229 # name. Instead exit immediately.
230 #
231 sys.exit(1)
232
233 _CheckGitVersion()
234 try:
Conley Owensc9129d92012-10-01 16:12:28 -0700235 if NeedSetupGnuPG():
236 can_verify = SetupGnuPG(opt.quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 else:
238 can_verify = True
239
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700240 dst = os.path.abspath(os.path.join(repodir, S_repo))
241 _Clone(url, dst, opt.quiet)
242
243 if can_verify and not opt.no_repo_verify:
244 rev = _Verify(dst, branch, opt.quiet)
245 else:
246 rev = 'refs/remotes/origin/%s^0' % branch
247
248 _Checkout(dst, branch, rev, opt.quiet)
249 except CloneFailure:
250 if opt.quiet:
251 print >>sys.stderr, \
252 'fatal: repo init failed; run without --quiet to see why'
253 raise
254
255
256def _CheckGitVersion():
257 cmd = [GIT, '--version']
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700258 try:
259 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700260 except OSError as e:
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700261 print >>sys.stderr
262 print >>sys.stderr, "fatal: '%s' is not available" % GIT
263 print >>sys.stderr, 'fatal: %s' % e
264 print >>sys.stderr
265 print >>sys.stderr, 'Please make sure %s is installed'\
266 ' and in your path.' % GIT
267 raise CloneFailure()
268
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700269 ver_str = proc.stdout.read().strip()
270 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700271 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700272
273 if not ver_str.startswith('git version '):
274 print >>sys.stderr, 'error: "%s" unsupported' % ver_str
275 raise CloneFailure()
276
277 ver_str = ver_str[len('git version '):].strip()
David Pursehouse685f0802012-11-14 08:34:39 +0900278 ver_act = tuple(map(int, ver_str.split('.')[0:3]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700279 if ver_act < MIN_GIT_VERSION:
David Pursehouse685f0802012-11-14 08:34:39 +0900280 need = '.'.join(map(str, MIN_GIT_VERSION))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700281 print >>sys.stderr, 'fatal: git %s or later required' % need
282 raise CloneFailure()
283
284
Conley Owensc9129d92012-10-01 16:12:28 -0700285def NeedSetupGnuPG():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 if not os.path.isdir(home_dot_repo):
287 return True
288
289 kv = os.path.join(home_dot_repo, 'keyring-version')
290 if not os.path.exists(kv):
291 return True
292
293 kv = open(kv).read()
294 if not kv:
295 return True
296
David Pursehouse685f0802012-11-14 08:34:39 +0900297 kv = tuple(map(int, kv.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700298 if kv < KEYRING_VERSION:
299 return True
300 return False
301
302
Conley Owensc9129d92012-10-01 16:12:28 -0700303def SetupGnuPG(quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700304 if not os.path.isdir(home_dot_repo):
305 try:
306 os.mkdir(home_dot_repo)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700307 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700308 print >>sys.stderr, \
309 'fatal: cannot make %s directory: %s' % (
310 home_dot_repo, e.strerror)
311 sys.exit(1)
312
313 if not os.path.isdir(gpg_dir):
314 try:
Sarah Owens60798a32012-10-25 17:53:09 -0700315 os.mkdir(gpg_dir, stat.S_IRWXU)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700316 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700317 print >>sys.stderr, \
318 'fatal: cannot make %s directory: %s' % (
319 gpg_dir, e.strerror)
320 sys.exit(1)
321
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800322 env = os.environ.copy()
323 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324
325 cmd = ['gpg', '--import']
326 try:
327 proc = subprocess.Popen(cmd,
328 env = env,
329 stdin = subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700330 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700331 if not quiet:
332 print >>sys.stderr, 'warning: gpg (GnuPG) is not available.'
333 print >>sys.stderr, 'warning: Installing it is strongly encouraged.'
334 print >>sys.stderr
335 return False
336
337 proc.stdin.write(MAINTAINER_KEYS)
338 proc.stdin.close()
339
340 if proc.wait() != 0:
341 print >>sys.stderr, 'fatal: registering repo maintainer keys failed'
342 sys.exit(1)
343 print
344
345 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
David Pursehouse685f0802012-11-14 08:34:39 +0900346 fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 fd.close()
348 return True
349
350
351def _SetConfig(local, name, value):
352 """Set a git configuration option to the specified value.
353 """
354 cmd = [GIT, 'config', name, value]
355 if subprocess.Popen(cmd, cwd = local).wait() != 0:
356 raise CloneFailure()
357
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700358
359def _InitHttp():
360 handlers = []
361
Sarah Owens1f7627f2012-10-31 09:21:55 -0700362 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700363 try:
364 import netrc
365 n = netrc.netrc()
366 for host in n.hosts:
367 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800368 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
369 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700370 except:
371 pass
Sarah Owens1f7627f2012-10-31 09:21:55 -0700372 handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
373 handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700374
375 if 'http_proxy' in os.environ:
376 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700377 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700378 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700379 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
380 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
381 urllib.request.install_opener(urllib.request.build_opener(*handlers))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700383def _Fetch(url, local, src, quiet):
384 if not quiet:
385 print >>sys.stderr, 'Get %s' % url
386
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700387 cmd = [GIT, 'fetch']
388 if quiet:
389 cmd.append('--quiet')
390 err = subprocess.PIPE
391 else:
392 err = None
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700393 cmd.append(src)
394 cmd.append('+refs/heads/*:refs/remotes/origin/*')
395 cmd.append('refs/tags/*:refs/tags/*')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700396
397 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
398 if err:
399 proc.stderr.read()
400 proc.stderr.close()
401 if proc.wait() != 0:
402 raise CloneFailure()
403
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700404def _DownloadBundle(url, local, quiet):
405 if not url.endswith('/'):
406 url += '/'
407 url += 'clone.bundle'
408
409 proc = subprocess.Popen(
410 [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
411 cwd = local,
412 stdout = subprocess.PIPE)
413 for line in proc.stdout:
414 m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
415 if m:
416 new_url = m.group(1)
417 old_url = m.group(2)
418 if url.startswith(old_url):
419 url = new_url + url[len(old_url):]
420 break
421 proc.stdout.close()
422 proc.wait()
423
424 if not url.startswith('http:') and not url.startswith('https:'):
425 return False
426
427 dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
428 try:
429 try:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700430 r = urllib.request.urlopen(url)
431 except urllib.error.HTTPError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700432 if e.code == 404:
433 return False
434 print >>sys.stderr, 'fatal: Cannot get %s' % url
435 print >>sys.stderr, 'fatal: HTTP error %s' % e.code
436 raise CloneFailure()
Sarah Owens1f7627f2012-10-31 09:21:55 -0700437 except urllib.error.URLError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700438 print >>sys.stderr, 'fatal: Cannot get %s' % url
439 print >>sys.stderr, 'fatal: error %s' % e.reason
440 raise CloneFailure()
441 try:
442 if not quiet:
443 print >>sys.stderr, 'Get %s' % url
444 while True:
445 buf = r.read(8192)
446 if buf == '':
447 return True
448 dest.write(buf)
449 finally:
450 r.close()
451 finally:
452 dest.close()
453
454def _ImportBundle(local):
455 path = os.path.join(local, '.git', 'clone.bundle')
456 try:
457 _Fetch(local, local, path, True)
458 finally:
459 os.remove(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700460
461def _Clone(url, local, quiet):
462 """Clones a git repository to a new subdirectory of repodir
463 """
464 try:
465 os.mkdir(local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700466 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700467 print >>sys.stderr, \
468 'fatal: cannot make %s directory: %s' \
469 % (local, e.strerror)
470 raise CloneFailure()
471
472 cmd = [GIT, 'init', '--quiet']
473 try:
474 proc = subprocess.Popen(cmd, cwd = local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700475 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700476 print >>sys.stderr
477 print >>sys.stderr, "fatal: '%s' is not available" % GIT
478 print >>sys.stderr, 'fatal: %s' % e
479 print >>sys.stderr
480 print >>sys.stderr, 'Please make sure %s is installed'\
481 ' and in your path.' % GIT
482 raise CloneFailure()
483 if proc.wait() != 0:
484 print >>sys.stderr, 'fatal: could not create %s' % local
485 raise CloneFailure()
486
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700487 _InitHttp()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700488 _SetConfig(local, 'remote.origin.url', url)
489 _SetConfig(local, 'remote.origin.fetch',
490 '+refs/heads/*:refs/remotes/origin/*')
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700491 if _DownloadBundle(url, local, quiet):
492 _ImportBundle(local)
493 else:
494 _Fetch(url, local, 'origin', quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700495
496
497def _Verify(cwd, branch, quiet):
498 """Verify the branch has been signed by a tag.
499 """
500 cmd = [GIT, 'describe', 'origin/%s' % branch]
501 proc = subprocess.Popen(cmd,
502 stdout=subprocess.PIPE,
503 stderr=subprocess.PIPE,
504 cwd = cwd)
505 cur = proc.stdout.read().strip()
506 proc.stdout.close()
507
508 proc.stderr.read()
509 proc.stderr.close()
510
511 if proc.wait() != 0 or not cur:
512 print >>sys.stderr
513 print >>sys.stderr,\
514 "fatal: branch '%s' has not been signed" \
515 % branch
516 raise CloneFailure()
517
518 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
519 if m:
520 cur = m.group(1)
521 if not quiet:
522 print >>sys.stderr
523 print >>sys.stderr, \
524 "info: Ignoring branch '%s'; using tagged release '%s'" \
525 % (branch, cur)
526 print >>sys.stderr
527
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800528 env = os.environ.copy()
529 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700530
531 cmd = [GIT, 'tag', '-v', cur]
532 proc = subprocess.Popen(cmd,
533 stdout = subprocess.PIPE,
534 stderr = subprocess.PIPE,
535 cwd = cwd,
536 env = env)
537 out = proc.stdout.read()
538 proc.stdout.close()
539
540 err = proc.stderr.read()
541 proc.stderr.close()
542
543 if proc.wait() != 0:
544 print >>sys.stderr
545 print >>sys.stderr, out
546 print >>sys.stderr, err
547 print >>sys.stderr
548 raise CloneFailure()
549 return '%s^0' % cur
550
551
552def _Checkout(cwd, branch, rev, quiet):
553 """Checkout an upstream branch into the repository and track it.
554 """
555 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
556 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
557 raise CloneFailure()
558
559 _SetConfig(cwd, 'branch.default.remote', 'origin')
560 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
561
562 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
563 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
564 raise CloneFailure()
565
566 cmd = [GIT, 'read-tree', '--reset', '-u']
567 if not quiet:
568 cmd.append('-v')
569 cmd.append('HEAD')
570 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
571 raise CloneFailure()
572
573
574def _FindRepo():
575 """Look for a repo installation, starting at the current directory.
576 """
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200577 curdir = os.getcwd()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578 repo = None
579
Anthony Newnamdf14a702011-01-09 17:31:57 -0800580 olddir = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200581 while curdir != '/' \
582 and curdir != olddir \
Anthony Newnamdf14a702011-01-09 17:31:57 -0800583 and not repo:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200584 repo = os.path.join(curdir, repodir, REPO_MAIN)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700585 if not os.path.isfile(repo):
586 repo = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200587 olddir = curdir
588 curdir = os.path.dirname(curdir)
589 return (repo, os.path.join(curdir, repodir))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700590
591
592class _Options:
593 help = False
594
595
596def _ParseArguments(args):
597 cmd = None
598 opt = _Options()
599 arg = []
600
Sarah Owensa6053d52012-11-01 13:36:50 -0700601 for i in range(len(args)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700602 a = args[i]
603 if a == '-h' or a == '--help':
604 opt.help = True
605
606 elif not a.startswith('-'):
607 cmd = a
608 arg = args[i + 1:]
609 break
610 return cmd, opt, arg
611
612
613def _Usage():
614 print >>sys.stderr,\
615"""usage: repo COMMAND [ARGS]
616
617repo is not yet installed. Use "repo init" to install it here.
618
619The most commonly used repo commands are:
620
621 init Install repo in the current working directory
622 help Display detailed help on a command
623
624For access to the full online help, install repo ("repo init").
625"""
626 sys.exit(1)
627
628
629def _Help(args):
630 if args:
631 if args[0] == 'init':
632 init_optparse.print_help()
Trond Norbyed3fd5372011-01-03 11:35:15 +0100633 sys.exit(0)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634 else:
635 print >>sys.stderr,\
636 "error: '%s' is not a bootstrap command.\n"\
637 ' For access to online help, install repo ("repo init").'\
638 % args[0]
639 else:
640 _Usage()
641 sys.exit(1)
642
643
644def _NotInstalled():
645 print >>sys.stderr,\
646'error: repo is not installed. Use "repo init" to install it here.'
647 sys.exit(1)
648
649
650def _NoCommands(cmd):
651 print >>sys.stderr,\
652"""error: command '%s' requires repo to be installed first.
653 Use "repo init" to install it here.""" % cmd
654 sys.exit(1)
655
656
657def _RunSelf(wrapper_path):
658 my_dir = os.path.dirname(wrapper_path)
659 my_main = os.path.join(my_dir, 'main.py')
660 my_git = os.path.join(my_dir, '.git')
661
662 if os.path.isfile(my_main) and os.path.isdir(my_git):
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700663 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700664 'project.py',
665 'subcmds']:
666 if not os.path.exists(os.path.join(my_dir, name)):
667 return None, None
668 return my_main, my_git
669 return None, None
670
671
672def _SetDefaultsTo(gitdir):
673 global REPO_URL
674 global REPO_REV
675
676 REPO_URL = gitdir
677 proc = subprocess.Popen([GIT,
678 '--git-dir=%s' % gitdir,
679 'symbolic-ref',
680 'HEAD'],
681 stdout = subprocess.PIPE,
682 stderr = subprocess.PIPE)
683 REPO_REV = proc.stdout.read().strip()
684 proc.stdout.close()
685
686 proc.stderr.read()
687 proc.stderr.close()
688
689 if proc.wait() != 0:
690 print >>sys.stderr, 'fatal: %s has no current branch' % gitdir
691 sys.exit(1)
692
693
694def main(orig_args):
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200695 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700696 cmd, opt, args = _ParseArguments(orig_args)
697
698 wrapper_path = os.path.abspath(__file__)
699 my_main, my_git = _RunSelf(wrapper_path)
700
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200701 if not repo_main:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700702 if opt.help:
703 _Usage()
704 if cmd == 'help':
705 _Help(args)
706 if not cmd:
707 _NotInstalled()
708 if cmd == 'init':
709 if my_git:
710 _SetDefaultsTo(my_git)
711 try:
712 _Init(args)
713 except CloneFailure:
714 for root, dirs, files in os.walk(repodir, topdown=False):
715 for name in files:
716 os.remove(os.path.join(root, name))
717 for name in dirs:
718 os.rmdir(os.path.join(root, name))
719 os.rmdir(repodir)
720 sys.exit(1)
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200721 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700722 else:
723 _NoCommands(cmd)
724
725 if my_main:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200726 repo_main = my_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727
David Pursehouse685f0802012-11-14 08:34:39 +0900728 ver_str = '.'.join(map(str, VERSION))
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200729 me = [repo_main,
730 '--repo-dir=%s' % rel_repo_dir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700731 '--wrapper-version=%s' % ver_str,
732 '--wrapper-path=%s' % wrapper_path,
733 '--']
734 me.extend(orig_args)
735 me.extend(extra_args)
736 try:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200737 os.execv(repo_main, me)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700738 except OSError as e:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200739 print >>sys.stderr, "fatal: unable to start %s" % repo_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700740 print >>sys.stderr, "fatal: %s" % e
741 sys.exit(148)
742
743
744if __name__ == '__main__':
745 main(sys.argv[1:])