blob: b73de71c1a3a77c7c5f29941dc21360705dc6867 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Sarah Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Conley Owensd21720d2012-04-16 11:02:21 -070018import platform
Conley Owens971de8e2012-04-16 10:36:08 -070019import re
Doug Anderson2630dd92011-04-07 13:36:30 -070020import shutil
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import sys
David Pursehouse59bbb582013-05-17 10:49:33 +090022
23from pyversion import is_python3
24if is_python3():
Victor Boivie2b30e3a2012-10-05 12:37:58 +020025 import urllib.parse
David Pursehouse59bbb582013-05-17 10:49:33 +090026else:
Victor Boivie2b30e3a2012-10-05 12:37:58 +020027 import imp
28 import urlparse
29 urllib = imp.new_module('urllib')
30 urllib.parse = urlparse.urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
32from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080033from command import InteractiveCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034from error import ManifestParseError
Jonathan Nieder93719792015-03-17 11:29:58 -070035from project import SyncBuffer
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070036from git_config import GitConfig
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -070037from git_command import git_require, MIN_GIT_VERSION
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080039class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 common = True
41 helpSummary = "Initialize repo in the current directory"
42 helpUsage = """
43%prog [options]
44"""
45 helpDescription = """
46The '%prog' command is run once to install and initialize repo.
47The latest repo source code and manifest collection is downloaded
48from the server and is installed in the .repo/ directory in the
49current working directory.
50
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070051The optional -b argument can be used to select the manifest branch
52to checkout and use. If no branch is specified, master is assumed.
53
54The optional -m argument can be used to specify an alternate manifest
55to be used. If no manifest is specified, the manifest default.xml
56will be used.
57
Shawn O. Pearce88443382010-10-08 10:02:09 +020058The --reference option can be used to point to a directory that
59has the content of a --mirror sync. This will make the working
60directory use as much data as possible from the local reference
61directory when fetching from the server. This will make the sync
62go a lot faster by reducing data traffic on the network.
63
64
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070065Switching Manifest Branches
66---------------------------
67
68To switch to another manifest branch, `repo init -b otherbranch`
69may be used in an existing client. However, as this only updates the
70manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
71to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070072"""
73
74 def _Options(self, p):
75 # Logging
76 g = p.add_option_group('Logging options')
77 g.add_option('-q', '--quiet',
78 dest="quiet", action="store_true", default=False,
79 help="be quiet")
80
81 # Manifest
82 g = p.add_option_group('Manifest options')
83 g.add_option('-u', '--manifest-url',
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -080084 dest='manifest_url',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085 help='manifest repository location', metavar='URL')
86 g.add_option('-b', '--manifest-branch',
87 dest='manifest_branch',
88 help='manifest branch or revision', metavar='REVISION')
89 g.add_option('-m', '--manifest-name',
90 dest='manifest_name', default='default.xml',
91 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -080092 g.add_option('--mirror',
93 dest='mirror', action='store_true',
David Pursehouse3d07da82012-08-15 14:22:08 +090094 help='create a replica of the remote repositories '
95 'rather than a client working directory')
Shawn O. Pearce88443382010-10-08 10:02:09 +020096 g.add_option('--reference',
97 dest='reference',
98 help='location of mirror directory', metavar='DIR')
Doug Anderson30d45292011-05-04 15:01:04 -070099 g.add_option('--depth', type='int', default=None,
100 dest='depth',
101 help='create a shallow clone with given depth; see git clone')
Julien Campergue335f5ef2013-10-16 11:02:35 +0200102 g.add_option('--archive',
103 dest='archive', action='store_true',
104 help='checkout an archive instead of a git repository for '
105 'each project. See git archive.')
Colin Cross5acde752012-03-28 20:15:45 -0700106 g.add_option('-g', '--groups',
David Holmer0a1c6a12012-11-14 19:19:00 -0500107 dest='groups', default='default',
108 help='restrict manifest projects to ones with specified '
109 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]',
Colin Cross5acde752012-03-28 20:15:45 -0700110 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700111 g.add_option('-p', '--platform',
112 dest='platform', default='auto',
Conley Owensbb1b5f52012-08-13 13:11:18 -0700113 help='restrict manifest projects to ones with a specified '
Conley Owensd21720d2012-04-16 11:02:21 -0700114 'platform group [auto|all|none|linux|darwin|...]',
115 metavar='PLATFORM')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116
117 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700118 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 g.add_option('--repo-url',
120 dest='repo_url',
121 help='repo repository location', metavar='URL')
122 g.add_option('--repo-branch',
123 dest='repo_branch',
124 help='repo branch or revision', metavar='REVISION')
125 g.add_option('--no-repo-verify',
126 dest='no_repo_verify', action='store_true',
127 help='do not verify repo source code')
128
Victor Boivie841be342011-04-05 11:31:10 +0200129 # Other
130 g = p.add_option_group('Other options')
131 g.add_option('--config-name',
132 dest='config_name', action="store_true", default=False,
133 help='Always prompt for name/e-mail')
134
David Pursehouse3f5ea0b2012-11-17 03:13:09 +0900135 def _RegisteredEnvironmentOptions(self):
136 return {'REPO_MANIFEST_URL': 'manifest_url',
137 'REPO_MIRROR_LOCATION': 'reference'}
138
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700139 def _SyncManifest(self, opt):
140 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700141 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700143 if is_new:
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800144 if not opt.manifest_url:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700145 print('fatal: manifest url (-u) is required.', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 sys.exit(1)
147
148 if not opt.quiet:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700149 print('Get %s' % GitConfig.ForUser().UrlInsteadOf(opt.manifest_url),
150 file=sys.stderr)
Victor Boivie2b30e3a2012-10-05 12:37:58 +0200151
152 # The manifest project object doesn't keep track of the path on the
153 # server where this git is located, so let's save that here.
154 mirrored_manifest_git = None
155 if opt.reference:
156 manifest_git_path = urllib.parse(opt.manifest_url).path[1:]
157 mirrored_manifest_git = os.path.join(opt.reference, manifest_git_path)
158 if not mirrored_manifest_git.endswith(".git"):
159 mirrored_manifest_git += ".git"
160 if not os.path.exists(mirrored_manifest_git):
161 mirrored_manifest_git = os.path.join(opt.reference + '/.repo/manifests.git')
162
163 m._InitGitDir(mirror_git=mirrored_manifest_git)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164
165 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700166 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700168 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700169 else:
170 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700171 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172 else:
173 m.PreSync()
174
175 if opt.manifest_url:
176 r = m.GetRemote(m.remote.name)
177 r.url = opt.manifest_url
178 r.ResetFetch()
179 r.Save()
180
David Pursehouse1d947b32012-10-25 12:23:11 +0900181 groups = re.split(r'[,\s]+', opt.groups)
Conley Owensd21720d2012-04-16 11:02:21 -0700182 all_platforms = ['linux', 'darwin']
183 platformize = lambda x: 'platform-' + x
184 if opt.platform == 'auto':
185 if (not opt.mirror and
186 not m.config.GetString('repo.mirror') == 'true'):
187 groups.append(platformize(platform.system().lower()))
188 elif opt.platform == 'all':
Colin Cross54657272012-04-23 13:39:48 -0700189 groups.extend(map(platformize, all_platforms))
Conley Owensd21720d2012-04-16 11:02:21 -0700190 elif opt.platform in all_platforms:
191 groups.extend(platformize(opt.platform))
192 elif opt.platform != 'none':
Sarah Owenscecd1d82012-11-01 22:59:27 -0700193 print('fatal: invalid platform flag', file=sys.stderr)
Conley Owensd21720d2012-04-16 11:02:21 -0700194 sys.exit(1)
195
Conley Owens971de8e2012-04-16 10:36:08 -0700196 groups = [x for x in groups if x]
197 groupstr = ','.join(groups)
David Holmer0a1c6a12012-11-14 19:19:00 -0500198 if opt.platform == 'auto' and groupstr == 'default,platform-' + platform.system().lower():
Conley Owens971de8e2012-04-16 10:36:08 -0700199 groupstr = None
200 m.config.SetString('manifest.groups', groupstr)
Colin Cross5acde752012-03-28 20:15:45 -0700201
Shawn O. Pearce88443382010-10-08 10:02:09 +0200202 if opt.reference:
203 m.config.SetString('repo.reference', opt.reference)
204
Julien Campergue335f5ef2013-10-16 11:02:35 +0200205 if opt.archive:
206 if is_new:
207 m.config.SetString('repo.archive', 'true')
208 else:
209 print('fatal: --archive is only supported when initializing a new '
210 'workspace.', file=sys.stderr)
211 print('Either delete the .repo folder in this workspace, or initialize '
212 'in another location.', file=sys.stderr)
213 sys.exit(1)
214
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800215 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700216 if is_new:
217 m.config.SetString('repo.mirror', 'true')
218 else:
David Pursehouse25470982012-11-21 14:41:58 +0900219 print('fatal: --mirror is only supported when initializing a new '
220 'workspace.', file=sys.stderr)
221 print('Either delete the .repo folder in this workspace, or initialize '
222 'in another location.', file=sys.stderr)
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700223 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800224
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700225 if not m.Sync_NetworkHalf(is_new=is_new):
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700226 r = m.GetRemote(m.remote.name)
Sarah Owenscecd1d82012-11-01 22:59:27 -0700227 print('fatal: cannot obtain manifest %s' % r.url, file=sys.stderr)
Doug Anderson2630dd92011-04-07 13:36:30 -0700228
229 # Better delete the manifest git dir if we created it; otherwise next
230 # time (when user fixes problems) we won't go through the "is_new" logic.
231 if is_new:
232 shutil.rmtree(m.gitdir)
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700233 sys.exit(1)
234
Florian Vallee5d016502012-06-07 17:19:26 +0200235 if opt.manifest_branch:
Anthony King7bdac712014-07-16 12:56:40 +0100236 m.MetaBranchSwitch()
Florian Vallee5d016502012-06-07 17:19:26 +0200237
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700238 syncbuf = SyncBuffer(m.config)
239 m.Sync_LocalHalf(syncbuf)
240 syncbuf.Finish()
241
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700242 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700243 if not m.StartBranch('default'):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700244 print('fatal: cannot create default in manifest', file=sys.stderr)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700245 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700246
247 def _LinkManifest(self, name):
248 if not name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700249 print('fatal: manifest name (-m) is required.', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700250 sys.exit(1)
251
252 try:
253 self.manifest.Link(name)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700254 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700255 print("fatal: manifest '%s' not available" % name, file=sys.stderr)
256 print('fatal: %s' % str(e), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700257 sys.exit(1)
258
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700259 def _Prompt(self, prompt, value):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700260 sys.stdout.write('%-10s [%s]: ' % (prompt, value))
261 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700262 if a == '':
263 return value
264 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700265
Victor Boivie841be342011-04-05 11:31:10 +0200266 def _ShouldConfigureUser(self):
267 gc = self.manifest.globalConfig
268 mp = self.manifest.manifestProject
269
270 # If we don't have local settings, get from global.
271 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
272 if not gc.Has('user.name') or not gc.Has('user.email'):
273 return True
274
275 mp.config.SetString('user.name', gc.GetString('user.name'))
276 mp.config.SetString('user.email', gc.GetString('user.email'))
277
Sarah Owenscecd1d82012-11-01 22:59:27 -0700278 print()
279 print('Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
280 mp.config.GetString('user.email')))
281 print('If you want to change this, please re-run \'repo init\' with --config-name')
Victor Boivie841be342011-04-05 11:31:10 +0200282 return False
283
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700284 def _ConfigureUser(self):
285 mp = self.manifest.manifestProject
286
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700287 while True:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700288 print()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700289 name = self._Prompt('Your Name', mp.UserName)
290 email = self._Prompt('Your Email', mp.UserEmail)
291
Sarah Owenscecd1d82012-11-01 22:59:27 -0700292 print()
293 print('Your identity is: %s <%s>' % (name, email))
Mike Frysingere9311272011-08-11 15:46:43 -0400294 sys.stdout.write('is this correct [y/N]? ')
David Pursehousefc241242012-11-14 09:19:39 +0900295 a = sys.stdin.readline().strip().lower()
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700296 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700297 break
298
299 if name != mp.UserName:
300 mp.config.SetString('user.name', name)
301 if email != mp.UserEmail:
302 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700303
304 def _HasColorSet(self, gc):
305 for n in ['ui', 'diff', 'status']:
306 if gc.Has('color.%s' % n):
307 return True
308 return False
309
310 def _ConfigureColor(self):
311 gc = self.manifest.globalConfig
312 if self._HasColorSet(gc):
313 return
314
315 class _Test(Coloring):
316 def __init__(self):
317 Coloring.__init__(self, gc, 'test color display')
318 self._on = True
319 out = _Test()
320
Sarah Owenscecd1d82012-11-01 22:59:27 -0700321 print()
322 print("Testing colorized output (for 'repo diff', 'repo status'):")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323
David Pursehouse8f62fb72012-11-14 12:09:38 +0900324 for c in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 out.write(' ')
326 out.printer(fg=c)(' %-6s ', c)
327 out.write(' ')
328 out.printer(fg='white', bg='black')(' %s ' % 'white')
329 out.nl()
330
David Pursehouse8f62fb72012-11-14 12:09:38 +0900331 for c in ['bold', 'dim', 'ul', 'reverse']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332 out.write(' ')
333 out.printer(fg='black', attr=c)(' %-6s ', c)
334 out.nl()
335
Mike Frysingere9311272011-08-11 15:46:43 -0400336 sys.stdout.write('Enable color display in this user account (y/N)? ')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700337 a = sys.stdin.readline().strip().lower()
338 if a in ('y', 'yes', 't', 'true', 'on'):
339 gc.SetString('color.ui', 'auto')
340
Doug Anderson30d45292011-05-04 15:01:04 -0700341 def _ConfigureDepth(self, opt):
342 """Configure the depth we'll sync down.
343
344 Args:
345 opt: Options from optparse. We care about opt.depth.
346 """
347 # Opt.depth will be non-None if user actually passed --depth to repo init.
348 if opt.depth is not None:
349 if opt.depth > 0:
350 # Positive values will set the depth.
351 depth = str(opt.depth)
352 else:
353 # Negative numbers will clear the depth; passing None to SetString
354 # will do that.
355 depth = None
356
357 # We store the depth in the main manifest project.
358 self.manifest.manifestProject.config.SetString('repo.depth', depth)
359
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800360 def _DisplayResult(self):
361 if self.manifest.IsMirror:
362 init_type = 'mirror '
363 else:
364 init_type = ''
365
Sarah Owenscecd1d82012-11-01 22:59:27 -0700366 print()
367 print('repo %shas been initialized in %s'
368 % (init_type, self.manifest.topdir))
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800369
370 current_dir = os.getcwd()
371 if current_dir != self.manifest.topdir:
David Pursehouse35765962013-01-29 09:49:48 +0900372 print('If this is not the directory in which you want to initialize '
Sarah Owenscecd1d82012-11-01 22:59:27 -0700373 'repo, please run:')
374 print(' rm -r %s/.repo' % self.manifest.topdir)
375 print('and try again.')
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800376
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700377 def Execute(self, opt, args):
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700378 git_require(MIN_GIT_VERSION, fail=True)
Victor Boivie297e7c62012-10-05 14:50:05 +0200379
380 if opt.reference:
381 opt.reference = os.path.expanduser(opt.reference)
382
Julien Campergue335f5ef2013-10-16 11:02:35 +0200383 # Check this here, else manifest will be tagged "not new" and init won't be
384 # possible anymore without removing the .repo/manifests directory.
385 if opt.archive and opt.mirror:
386 print('fatal: --mirror and --archive cannot be used together.',
387 file=sys.stderr)
388 sys.exit(1)
389
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700390 self._SyncManifest(opt)
391 self._LinkManifest(opt.manifest_name)
392
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700393 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Victor Boivie841be342011-04-05 11:31:10 +0200394 if opt.config_name or self._ShouldConfigureUser():
395 self._ConfigureUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700396 self._ConfigureColor()
397
Doug Anderson30d45292011-05-04 15:01:04 -0700398 self._ConfigureDepth(opt)
399
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800400 self._DisplayResult()