blob: dde972866c8361f4ef4580cf4dcbf04fa90eb370 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
3# Copyright (C) 2008 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
Conley Owensd21720d2012-04-16 11:02:21 -070019import platform
Conley Owens971de8e2012-04-16 10:36:08 -070020import re
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')
Anthony King7993f3c2015-06-03 17:21:56 +010030 urllib.parse = 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
Mike Frysinger82caef62020-02-11 18:51:08 -050037from git_command import git_require, MIN_GIT_VERSION_SOFT, MIN_GIT_VERSION_HARD
Renaud Paquaya65adf72016-11-03 10:37:53 -070038import platform_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039
David Pursehouse819827a2020-02-12 15:20:19 +090040
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080041class Init(InteractiveCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042 common = True
43 helpSummary = "Initialize repo in the current directory"
44 helpUsage = """
45%prog [options]
46"""
47 helpDescription = """
48The '%prog' command is run once to install and initialize repo.
49The latest repo source code and manifest collection is downloaded
50from the server and is installed in the .repo/ directory in the
51current working directory.
52
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070053The optional -b argument can be used to select the manifest branch
54to checkout and use. If no branch is specified, master is assumed.
55
56The optional -m argument can be used to specify an alternate manifest
57to be used. If no manifest is specified, the manifest default.xml
58will be used.
59
Shawn O. Pearce88443382010-10-08 10:02:09 +020060The --reference option can be used to point to a directory that
61has the content of a --mirror sync. This will make the working
62directory use as much data as possible from the local reference
63directory when fetching from the server. This will make the sync
64go a lot faster by reducing data traffic on the network.
65
Nikolai Merinov09f0abb2018-10-19 15:07:05 +050066The --dissociate option can be used to borrow the objects from
67the directory specified with the --reference option only to reduce
68network transfer, and stop borrowing from them after a first clone
69is made by making necessary local copies of borrowed objects.
70
Hu xiuyun9711a982015-12-11 11:16:41 +080071The --no-clone-bundle option disables any attempt to use
72$URL/clone.bundle to bootstrap a new Git repository from a
73resumeable bundle file on a content delivery network. This
74may be necessary if there are problems with the local Python
75HTTP client or proxy configuration, but the Git binary works.
Shawn O. Pearce88443382010-10-08 10:02:09 +020076
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040077# Switching Manifest Branches
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070078
79To switch to another manifest branch, `repo init -b otherbranch`
80may be used in an existing client. However, as this only updates the
81manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
82to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083"""
84
Mike Frysinger66098f72020-02-05 00:01:59 -050085 def _Options(self, p, gitc_init=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086 # Logging
87 g = p.add_option_group('Logging options')
88 g.add_option('-q', '--quiet',
89 dest="quiet", action="store_true", default=False,
90 help="be quiet")
91
92 # Manifest
93 g = p.add_option_group('Manifest options')
94 g.add_option('-u', '--manifest-url',
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -080095 dest='manifest_url',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 help='manifest repository location', metavar='URL')
97 g.add_option('-b', '--manifest-branch',
98 dest='manifest_branch',
99 help='manifest branch or revision', metavar='REVISION')
Mike Frysinger66098f72020-02-05 00:01:59 -0500100 cbr_opts = ['--current-branch']
101 # The gitc-init subcommand allocates -c itself, but a lot of init users
102 # want -c, so try to satisfy both as best we can.
Dan Willemsen93293ca2020-02-06 17:00:00 -0800103 if not gitc_init:
Mike Frysinger66098f72020-02-05 00:01:59 -0500104 cbr_opts += ['-c']
105 g.add_option(*cbr_opts,
Naseer Ahmedf4dda9a2016-12-01 18:49:54 -0500106 dest='current_branch_only', action='store_true',
107 help='fetch only current manifest branch from server')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108 g.add_option('-m', '--manifest-name',
109 dest='manifest_name', default='default.xml',
110 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800111 g.add_option('--mirror',
112 dest='mirror', action='store_true',
David Pursehouse3d07da82012-08-15 14:22:08 +0900113 help='create a replica of the remote repositories '
114 'rather than a client working directory')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200115 g.add_option('--reference',
116 dest='reference',
117 help='location of mirror directory', metavar='DIR')
Nikolai Merinov09f0abb2018-10-19 15:07:05 +0500118 g.add_option('--dissociate',
119 dest='dissociate', action='store_true',
120 help='dissociate from reference mirrors after clone')
Doug Anderson30d45292011-05-04 15:01:04 -0700121 g.add_option('--depth', type='int', default=None,
122 dest='depth',
123 help='create a shallow clone with given depth; see git clone')
Xin Li745be2e2019-06-03 11:24:30 -0700124 g.add_option('--partial-clone', action='store_true',
125 dest='partial_clone',
126 help='perform partial clone (https://git-scm.com/'
127 'docs/gitrepository-layout#_code_partialclone_code)')
128 g.add_option('--clone-filter', action='store', default='blob:none',
129 dest='clone_filter',
130 help='filter for use with --partial-clone [default: %default]')
Julien Campergue335f5ef2013-10-16 11:02:35 +0200131 g.add_option('--archive',
132 dest='archive', action='store_true',
133 help='checkout an archive instead of a git repository for '
134 'each project. See git archive.')
Martin Kellye4e94d22017-03-21 16:05:12 -0700135 g.add_option('--submodules',
136 dest='submodules', action='store_true',
137 help='sync any submodules associated with the manifest repo')
Colin Cross5acde752012-03-28 20:15:45 -0700138 g.add_option('-g', '--groups',
David Holmer0a1c6a12012-11-14 19:19:00 -0500139 dest='groups', default='default',
140 help='restrict manifest projects to ones with specified '
141 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]',
Colin Cross5acde752012-03-28 20:15:45 -0700142 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700143 g.add_option('-p', '--platform',
144 dest='platform', default='auto',
Conley Owensbb1b5f52012-08-13 13:11:18 -0700145 help='restrict manifest projects to ones with a specified '
Conley Owensd21720d2012-04-16 11:02:21 -0700146 'platform group [auto|all|none|linux|darwin|...]',
147 metavar='PLATFORM')
Hu xiuyun9711a982015-12-11 11:16:41 +0800148 g.add_option('--no-clone-bundle',
149 dest='no_clone_bundle', action='store_true',
150 help='disable use of /clone.bundle on HTTP/HTTPS')
Naseer Ahmedf4dda9a2016-12-01 18:49:54 -0500151 g.add_option('--no-tags',
152 dest='no_tags', action='store_true',
153 help="don't fetch tags in the manifest")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154
155 # Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700156 g = p.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157 g.add_option('--repo-url',
158 dest='repo_url',
159 help='repo repository location', metavar='URL')
160 g.add_option('--repo-branch',
161 dest='repo_branch',
162 help='repo branch or revision', metavar='REVISION')
163 g.add_option('--no-repo-verify',
164 dest='no_repo_verify', action='store_true',
165 help='do not verify repo source code')
166
Victor Boivie841be342011-04-05 11:31:10 +0200167 # Other
168 g = p.add_option_group('Other options')
169 g.add_option('--config-name',
170 dest='config_name', action="store_true", default=False,
171 help='Always prompt for name/e-mail')
172
David Pursehouse3f5ea0b2012-11-17 03:13:09 +0900173 def _RegisteredEnvironmentOptions(self):
174 return {'REPO_MANIFEST_URL': 'manifest_url',
175 'REPO_MIRROR_LOCATION': 'reference'}
176
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700177 def _SyncManifest(self, opt):
178 m = self.manifest.manifestProject
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700179 is_new = not m.Exists
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700180
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700181 if is_new:
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800182 if not opt.manifest_url:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700183 print('fatal: manifest url (-u) is required.', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700184 sys.exit(1)
185
186 if not opt.quiet:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700187 print('Get %s' % GitConfig.ForUser().UrlInsteadOf(opt.manifest_url),
188 file=sys.stderr)
Victor Boivie2b30e3a2012-10-05 12:37:58 +0200189
190 # The manifest project object doesn't keep track of the path on the
191 # server where this git is located, so let's save that here.
192 mirrored_manifest_git = None
193 if opt.reference:
Anthony King7993f3c2015-06-03 17:21:56 +0100194 manifest_git_path = urllib.parse.urlparse(opt.manifest_url).path[1:]
Victor Boivie2b30e3a2012-10-05 12:37:58 +0200195 mirrored_manifest_git = os.path.join(opt.reference, manifest_git_path)
196 if not mirrored_manifest_git.endswith(".git"):
197 mirrored_manifest_git += ".git"
198 if not os.path.exists(mirrored_manifest_git):
Samuel Holland5f0e57d2018-01-22 11:00:24 -0600199 mirrored_manifest_git = os.path.join(opt.reference,
200 '.repo/manifests.git')
Victor Boivie2b30e3a2012-10-05 12:37:58 +0200201
202 m._InitGitDir(mirror_git=mirrored_manifest_git)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700203
204 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700205 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700206 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700207 m.revisionExpr = 'refs/heads/master'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208 else:
209 if opt.manifest_branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700210 m.revisionExpr = opt.manifest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 else:
212 m.PreSync()
213
Nasser Grainawid92464e2019-05-21 10:41:35 -0600214 self._ConfigureDepth(opt)
215
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 if opt.manifest_url:
217 r = m.GetRemote(m.remote.name)
218 r.url = opt.manifest_url
219 r.ResetFetch()
220 r.Save()
221
David Pursehouse1d947b32012-10-25 12:23:11 +0900222 groups = re.split(r'[,\s]+', opt.groups)
Pascal Muetschardc2a64dd2015-10-22 13:26:36 -0700223 all_platforms = ['linux', 'darwin', 'windows']
Conley Owensd21720d2012-04-16 11:02:21 -0700224 platformize = lambda x: 'platform-' + x
225 if opt.platform == 'auto':
226 if (not opt.mirror and
David Pursehouseabdf7502020-02-12 14:58:39 +0900227 not m.config.GetString('repo.mirror') == 'true'):
Conley Owensd21720d2012-04-16 11:02:21 -0700228 groups.append(platformize(platform.system().lower()))
229 elif opt.platform == 'all':
Colin Cross54657272012-04-23 13:39:48 -0700230 groups.extend(map(platformize, all_platforms))
Conley Owensd21720d2012-04-16 11:02:21 -0700231 elif opt.platform in all_platforms:
Pascal Muetschardc2a64dd2015-10-22 13:26:36 -0700232 groups.append(platformize(opt.platform))
Conley Owensd21720d2012-04-16 11:02:21 -0700233 elif opt.platform != 'none':
Sarah Owenscecd1d82012-11-01 22:59:27 -0700234 print('fatal: invalid platform flag', file=sys.stderr)
Conley Owensd21720d2012-04-16 11:02:21 -0700235 sys.exit(1)
236
Conley Owens971de8e2012-04-16 10:36:08 -0700237 groups = [x for x in groups if x]
238 groupstr = ','.join(groups)
David Holmer0a1c6a12012-11-14 19:19:00 -0500239 if opt.platform == 'auto' and groupstr == 'default,platform-' + platform.system().lower():
Conley Owens971de8e2012-04-16 10:36:08 -0700240 groupstr = None
241 m.config.SetString('manifest.groups', groupstr)
Colin Cross5acde752012-03-28 20:15:45 -0700242
Shawn O. Pearce88443382010-10-08 10:02:09 +0200243 if opt.reference:
244 m.config.SetString('repo.reference', opt.reference)
245
Nikolai Merinov09f0abb2018-10-19 15:07:05 +0500246 if opt.dissociate:
247 m.config.SetString('repo.dissociate', 'true')
248
Julien Campergue335f5ef2013-10-16 11:02:35 +0200249 if opt.archive:
250 if is_new:
251 m.config.SetString('repo.archive', 'true')
252 else:
253 print('fatal: --archive is only supported when initializing a new '
254 'workspace.', file=sys.stderr)
255 print('Either delete the .repo folder in this workspace, or initialize '
256 'in another location.', file=sys.stderr)
257 sys.exit(1)
258
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800259 if opt.mirror:
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700260 if is_new:
261 m.config.SetString('repo.mirror', 'true')
262 else:
David Pursehouse25470982012-11-21 14:41:58 +0900263 print('fatal: --mirror is only supported when initializing a new '
264 'workspace.', file=sys.stderr)
265 print('Either delete the .repo folder in this workspace, or initialize '
266 'in another location.', file=sys.stderr)
Shawn O. Pearce5470df62009-03-09 18:51:58 -0700267 sys.exit(1)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800268
Xin Li745be2e2019-06-03 11:24:30 -0700269 if opt.partial_clone:
270 if opt.mirror:
271 print('fatal: --mirror and --partial-clone are mutually exclusive',
272 file=sys.stderr)
273 sys.exit(1)
274 m.config.SetString('repo.partialclone', 'true')
275 if opt.clone_filter:
276 m.config.SetString('repo.clonefilter', opt.clone_filter)
277 else:
278 opt.clone_filter = None
279
Martin Kellye4e94d22017-03-21 16:05:12 -0700280 if opt.submodules:
281 m.config.SetString('repo.submodules', 'true')
282
Hu xiuyun9711a982015-12-11 11:16:41 +0800283 if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet,
David Pursehouseabdf7502020-02-12 14:58:39 +0900284 clone_bundle=not opt.no_clone_bundle,
285 current_branch_only=opt.current_branch_only,
286 no_tags=opt.no_tags, submodules=opt.submodules,
287 clone_filter=opt.clone_filter):
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700288 r = m.GetRemote(m.remote.name)
Sarah Owenscecd1d82012-11-01 22:59:27 -0700289 print('fatal: cannot obtain manifest %s' % r.url, file=sys.stderr)
Doug Anderson2630dd92011-04-07 13:36:30 -0700290
291 # Better delete the manifest git dir if we created it; otherwise next
292 # time (when user fixes problems) we won't go through the "is_new" logic.
293 if is_new:
Renaud Paquaya65adf72016-11-03 10:37:53 -0700294 platform_utils.rmtree(m.gitdir)
Shawn O. Pearce1fc99f42009-03-17 08:06:18 -0700295 sys.exit(1)
296
Florian Vallee5d016502012-06-07 17:19:26 +0200297 if opt.manifest_branch:
Martin Kelly224a31a2017-07-10 14:46:25 -0700298 m.MetaBranchSwitch(submodules=opt.submodules)
Florian Vallee5d016502012-06-07 17:19:26 +0200299
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700300 syncbuf = SyncBuffer(m.config)
Martin Kellye4e94d22017-03-21 16:05:12 -0700301 m.Sync_LocalHalf(syncbuf, submodules=opt.submodules)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700302 syncbuf.Finish()
303
Shawn O. Pearcedf018832009-03-17 08:15:27 -0700304 if is_new or m.CurrentBranch is None:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700305 if not m.StartBranch('default'):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700306 print('fatal: cannot create default in manifest', file=sys.stderr)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700307 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700308
309 def _LinkManifest(self, name):
310 if not name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700311 print('fatal: manifest name (-m) is required.', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700312 sys.exit(1)
313
314 try:
315 self.manifest.Link(name)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700316 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700317 print("fatal: manifest '%s' not available" % name, file=sys.stderr)
318 print('fatal: %s' % str(e), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319 sys.exit(1)
320
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700321 def _Prompt(self, prompt, value):
Mike Frysingerab85fe72019-07-04 17:35:11 -0400322 print('%-10s [%s]: ' % (prompt, value), end='')
323 # TODO: When we require Python 3, use flush=True w/print above.
324 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700326 if a == '':
327 return value
328 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700329
Victor Boivie841be342011-04-05 11:31:10 +0200330 def _ShouldConfigureUser(self):
331 gc = self.manifest.globalConfig
332 mp = self.manifest.manifestProject
333
334 # If we don't have local settings, get from global.
335 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
336 if not gc.Has('user.name') or not gc.Has('user.email'):
337 return True
338
339 mp.config.SetString('user.name', gc.GetString('user.name'))
340 mp.config.SetString('user.email', gc.GetString('user.email'))
341
Sarah Owenscecd1d82012-11-01 22:59:27 -0700342 print()
343 print('Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
344 mp.config.GetString('user.email')))
345 print('If you want to change this, please re-run \'repo init\' with --config-name')
Victor Boivie841be342011-04-05 11:31:10 +0200346 return False
347
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700348 def _ConfigureUser(self):
349 mp = self.manifest.manifestProject
350
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700351 while True:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700352 print()
David Pursehouse54a4e602020-02-12 14:31:05 +0900353 name = self._Prompt('Your Name', mp.UserName)
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700354 email = self._Prompt('Your Email', mp.UserEmail)
355
Sarah Owenscecd1d82012-11-01 22:59:27 -0700356 print()
357 print('Your identity is: %s <%s>' % (name, email))
Mike Frysingerab85fe72019-07-04 17:35:11 -0400358 print('is this correct [y/N]? ', end='')
359 # TODO: When we require Python 3, use flush=True w/print above.
360 sys.stdout.flush()
David Pursehousefc241242012-11-14 09:19:39 +0900361 a = sys.stdin.readline().strip().lower()
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700362 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700363 break
364
365 if name != mp.UserName:
366 mp.config.SetString('user.name', name)
367 if email != mp.UserEmail:
368 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369
370 def _HasColorSet(self, gc):
371 for n in ['ui', 'diff', 'status']:
372 if gc.Has('color.%s' % n):
373 return True
374 return False
375
376 def _ConfigureColor(self):
377 gc = self.manifest.globalConfig
378 if self._HasColorSet(gc):
379 return
380
381 class _Test(Coloring):
382 def __init__(self):
383 Coloring.__init__(self, gc, 'test color display')
384 self._on = True
385 out = _Test()
386
Sarah Owenscecd1d82012-11-01 22:59:27 -0700387 print()
388 print("Testing colorized output (for 'repo diff', 'repo status'):")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700389
David Pursehouse8f62fb72012-11-14 12:09:38 +0900390 for c in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700391 out.write(' ')
392 out.printer(fg=c)(' %-6s ', c)
393 out.write(' ')
394 out.printer(fg='white', bg='black')(' %s ' % 'white')
395 out.nl()
396
David Pursehouse8f62fb72012-11-14 12:09:38 +0900397 for c in ['bold', 'dim', 'ul', 'reverse']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700398 out.write(' ')
399 out.printer(fg='black', attr=c)(' %-6s ', c)
400 out.nl()
401
Mike Frysingerab85fe72019-07-04 17:35:11 -0400402 print('Enable color display in this user account (y/N)? ', end='')
403 # TODO: When we require Python 3, use flush=True w/print above.
404 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700405 a = sys.stdin.readline().strip().lower()
406 if a in ('y', 'yes', 't', 'true', 'on'):
407 gc.SetString('color.ui', 'auto')
408
Doug Anderson30d45292011-05-04 15:01:04 -0700409 def _ConfigureDepth(self, opt):
410 """Configure the depth we'll sync down.
411
412 Args:
413 opt: Options from optparse. We care about opt.depth.
414 """
415 # Opt.depth will be non-None if user actually passed --depth to repo init.
416 if opt.depth is not None:
417 if opt.depth > 0:
418 # Positive values will set the depth.
419 depth = str(opt.depth)
420 else:
421 # Negative numbers will clear the depth; passing None to SetString
422 # will do that.
423 depth = None
424
425 # We store the depth in the main manifest project.
426 self.manifest.manifestProject.config.SetString('repo.depth', depth)
427
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800428 def _DisplayResult(self):
429 if self.manifest.IsMirror:
430 init_type = 'mirror '
431 else:
432 init_type = ''
433
Sarah Owenscecd1d82012-11-01 22:59:27 -0700434 print()
435 print('repo %shas been initialized in %s'
436 % (init_type, self.manifest.topdir))
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800437
438 current_dir = os.getcwd()
439 if current_dir != self.manifest.topdir:
David Pursehouse35765962013-01-29 09:49:48 +0900440 print('If this is not the directory in which you want to initialize '
Sarah Owenscecd1d82012-11-01 22:59:27 -0700441 'repo, please run:')
442 print(' rm -r %s/.repo' % self.manifest.topdir)
443 print('and try again.')
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800444
Mike Frysingerae6cb082019-08-27 01:10:59 -0400445 def ValidateOptions(self, opt, args):
Victor Boivie297e7c62012-10-05 14:50:05 +0200446 if opt.reference:
Samuel Hollandbaa00092018-01-22 10:57:29 -0600447 opt.reference = os.path.expanduser(opt.reference)
Victor Boivie297e7c62012-10-05 14:50:05 +0200448
Julien Campergue335f5ef2013-10-16 11:02:35 +0200449 # Check this here, else manifest will be tagged "not new" and init won't be
450 # possible anymore without removing the .repo/manifests directory.
451 if opt.archive and opt.mirror:
Mike Frysingerae6cb082019-08-27 01:10:59 -0400452 self.OptionParser.error('--mirror and --archive cannot be used together.')
453
454 def Execute(self, opt, args):
Mike Frysinger82caef62020-02-11 18:51:08 -0500455 git_require(MIN_GIT_VERSION_HARD, fail=True)
456 if not git_require(MIN_GIT_VERSION_SOFT):
457 print('repo: warning: git-%s+ will soon be required; please upgrade your '
458 'version of git to maintain support.'
459 % ('.'.join(str(x) for x in MIN_GIT_VERSION_SOFT),),
460 file=sys.stderr)
Julien Campergue335f5ef2013-10-16 11:02:35 +0200461
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700462 self._SyncManifest(opt)
463 self._LinkManifest(opt.manifest_name)
464
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700465 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Victor Boivie841be342011-04-05 11:31:10 +0200466 if opt.config_name or self._ShouldConfigureUser():
467 self._ConfigureUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700468 self._ConfigureColor()
469
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800470 self._DisplayResult()