blob: 99f30dce410c62dda9a25fa1f217f393e5f41241 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
Conley Owensd21720d2012-04-16 11:02:21 -070016import platform
Conley Owens971de8e2012-04-16 10:36:08 -070017import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
Mike Frysingeracf63b22019-06-13 02:24:21 -040019import urllib.parse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020
21from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080022from command import InteractiveCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from error import ManifestParseError
Jonathan Nieder93719792015-03-17 11:29:58 -070024from project import SyncBuffer
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070025from git_config import GitConfig
Mike Frysinger82caef62020-02-11 18:51:08 -050026from git_command import git_require, MIN_GIT_VERSION_SOFT, MIN_GIT_VERSION_HARD
Jack Neusc474c9c2021-07-26 23:08:54 +000027import fetch
Renaud Paquaya65adf72016-11-03 10:37:53 -070028import platform_utils
Mike Frysinger3599cc32020-02-29 02:53:41 -050029from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
David Pursehouse819827a2020-02-12 15:20:19 +090031
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080032class Init(InteractiveCommand, MirrorSafeCommand):
Mike Frysinger4f210542021-06-14 16:05:19 -040033 COMMON = True
LaMont Jones409407a2022-04-05 21:21:56 +000034 MULTI_MANIFEST_SUPPORT = True
Mike Frysinger401c6f02021-02-18 15:20:15 -050035 helpSummary = "Initialize a repo client checkout in the current directory"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036 helpUsage = """
Mike Frysinger401c6f02021-02-18 15:20:15 -050037%prog [options] [manifest url]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038"""
39 helpDescription = """
40The '%prog' command is run once to install and initialize repo.
41The latest repo source code and manifest collection is downloaded
42from the server and is installed in the .repo/ directory in the
43current working directory.
44
Mike Frysinger401c6f02021-02-18 15:20:15 -050045When creating a new checkout, the manifest URL is the only required setting.
46It may be specified using the --manifest-url option, or as the first optional
47argument.
48
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070049The optional -b argument can be used to select the manifest branch
Mike Frysinger50a81de2020-09-06 15:51:21 -040050to checkout and use. If no branch is specified, the remote's default
Mike Frysinger23882b32021-02-23 15:43:07 -050051branch is used. This is equivalent to using -b HEAD.
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070052
53The optional -m argument can be used to specify an alternate manifest
54to be used. If no manifest is specified, the manifest default.xml
55will be used.
56
Jack Neusc474c9c2021-07-26 23:08:54 +000057If the --standalone-manifest argument is set, the manifest will be downloaded
58directly from the specified --manifest-url as a static file (rather than
59setting up a manifest git checkout). With --standalone-manifest, the manifest
60will be fully static and will not be re-downloaded during subsesquent
61`repo init` and `repo sync` calls.
62
Shawn O. Pearce88443382010-10-08 10:02:09 +020063The --reference option can be used to point to a directory that
64has the content of a --mirror sync. This will make the working
65directory use as much data as possible from the local reference
66directory when fetching from the server. This will make the sync
67go a lot faster by reducing data traffic on the network.
68
Nikolai Merinov09f0abb2018-10-19 15:07:05 +050069The --dissociate option can be used to borrow the objects from
70the directory specified with the --reference option only to reduce
71network transfer, and stop borrowing from them after a first clone
72is made by making necessary local copies of borrowed objects.
73
Hu xiuyun9711a982015-12-11 11:16:41 +080074The --no-clone-bundle option disables any attempt to use
75$URL/clone.bundle to bootstrap a new Git repository from a
76resumeable bundle file on a content delivery network. This
77may be necessary if there are problems with the local Python
78HTTP client or proxy configuration, but the Git binary works.
Shawn O. Pearce88443382010-10-08 10:02:09 +020079
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040080# Switching Manifest Branches
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070081
82To switch to another manifest branch, `repo init -b otherbranch`
83may be used in an existing client. However, as this only updates the
84manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
85to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086"""
87
Mike Frysinger9180a072021-04-13 14:57:40 -040088 def _CommonOptions(self, p):
89 """Disable due to re-use of Wrapper()."""
90
Mike Frysinger66098f72020-02-05 00:01:59 -050091 def _Options(self, p, gitc_init=False):
Mike Frysinger9a734a32021-04-08 19:14:15 -040092 Wrapper().InitParser(p, gitc_init=gitc_init)
LaMont Jonescc879a92021-11-18 22:40:18 +000093 m = p.add_option_group('Multi-manifest')
94 m.add_option('--outer-manifest', action='store_true',
95 help='operate starting at the outermost manifest')
96 m.add_option('--no-outer-manifest', dest='outer_manifest',
97 action='store_false', default=None,
98 help='do not operate on outer manifests')
99 m.add_option('--this-manifest-only', action='store_true', default=None,
100 help='only operate on this (sub)manifest')
101 m.add_option('--no-this-manifest-only', '--all-manifests',
102 dest='this_manifest_only', action='store_false',
103 help='operate on this manifest and its submanifests')
Victor Boivie841be342011-04-05 11:31:10 +0200104
David Pursehouse3f5ea0b2012-11-17 03:13:09 +0900105 def _RegisteredEnvironmentOptions(self):
106 return {'REPO_MANIFEST_URL': 'manifest_url',
107 'REPO_MIRROR_LOCATION': 'reference'}
108
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109 def _SyncManifest(self, opt):
LaMont Jones9b03f152022-03-29 23:01:18 +0000110 """Call manifestProject.Sync with arguments from opt.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111
LaMont Jones9b03f152022-03-29 23:01:18 +0000112 Args:
113 opt: options from optparse.
114 """
115 if not self.manifest.manifestProject.Sync(
116 manifest_url=opt.manifest_url,
117 manifest_branch=opt.manifest_branch,
118 standalone_manifest=opt.standalone_manifest,
119 groups=opt.groups,
120 platform=opt.platform,
121 mirror=opt.mirror,
122 dissociate=opt.dissociate,
123 reference=opt.reference,
124 worktree=opt.worktree,
125 submodules=opt.submodules,
126 archive=opt.archive,
127 partial_clone=opt.partial_clone,
128 clone_filter=opt.clone_filter,
129 partial_clone_exclude=opt.partial_clone_exclude,
130 clone_bundle=opt.clone_bundle,
131 git_lfs=opt.git_lfs,
132 use_superproject=opt.use_superproject,
133 verbose=opt.verbose,
134 current_branch_only=opt.current_branch_only,
135 tags=opt.tags,
LaMont Jones409407a2022-04-05 21:21:56 +0000136 depth=opt.depth,
LaMont Jones55ee3042022-04-06 17:10:21 +0000137 git_event_log=self.git_event_log,
LaMont Jones409407a2022-04-05 21:21:56 +0000138 manifest_name=opt.manifest_name):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700139 sys.exit(1)
140
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700141 def _Prompt(self, prompt, value):
Mike Frysingerab85fe72019-07-04 17:35:11 -0400142 print('%-10s [%s]: ' % (prompt, value), end='')
143 # TODO: When we require Python 3, use flush=True w/print above.
144 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700145 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700146 if a == '':
147 return value
148 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149
Mike Frysinger0b888912020-02-21 22:48:40 -0500150 def _ShouldConfigureUser(self, opt):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400151 gc = self.client.globalConfig
Victor Boivie841be342011-04-05 11:31:10 +0200152 mp = self.manifest.manifestProject
153
154 # If we don't have local settings, get from global.
155 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
156 if not gc.Has('user.name') or not gc.Has('user.email'):
157 return True
158
159 mp.config.SetString('user.name', gc.GetString('user.name'))
160 mp.config.SetString('user.email', gc.GetString('user.email'))
161
Mike Frysinger0b888912020-02-21 22:48:40 -0500162 if not opt.quiet:
163 print()
164 print('Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
165 mp.config.GetString('user.email')))
166 print("If you want to change this, please re-run 'repo init' with --config-name")
Victor Boivie841be342011-04-05 11:31:10 +0200167 return False
168
Mike Frysinger0b888912020-02-21 22:48:40 -0500169 def _ConfigureUser(self, opt):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700170 mp = self.manifest.manifestProject
171
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700172 while True:
Mike Frysinger0b888912020-02-21 22:48:40 -0500173 if not opt.quiet:
174 print()
David Pursehouse54a4e602020-02-12 14:31:05 +0900175 name = self._Prompt('Your Name', mp.UserName)
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700176 email = self._Prompt('Your Email', mp.UserEmail)
177
Mike Frysinger0b888912020-02-21 22:48:40 -0500178 if not opt.quiet:
179 print()
Sarah Owenscecd1d82012-11-01 22:59:27 -0700180 print('Your identity is: %s <%s>' % (name, email))
Mike Frysingerab85fe72019-07-04 17:35:11 -0400181 print('is this correct [y/N]? ', end='')
182 # TODO: When we require Python 3, use flush=True w/print above.
183 sys.stdout.flush()
David Pursehousefc241242012-11-14 09:19:39 +0900184 a = sys.stdin.readline().strip().lower()
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700185 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700186 break
187
188 if name != mp.UserName:
189 mp.config.SetString('user.name', name)
190 if email != mp.UserEmail:
191 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192
193 def _HasColorSet(self, gc):
194 for n in ['ui', 'diff', 'status']:
195 if gc.Has('color.%s' % n):
196 return True
197 return False
198
199 def _ConfigureColor(self):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400200 gc = self.client.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201 if self._HasColorSet(gc):
202 return
203
204 class _Test(Coloring):
205 def __init__(self):
206 Coloring.__init__(self, gc, 'test color display')
207 self._on = True
208 out = _Test()
209
Sarah Owenscecd1d82012-11-01 22:59:27 -0700210 print()
211 print("Testing colorized output (for 'repo diff', 'repo status'):")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700212
David Pursehouse8f62fb72012-11-14 12:09:38 +0900213 for c in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700214 out.write(' ')
215 out.printer(fg=c)(' %-6s ', c)
216 out.write(' ')
217 out.printer(fg='white', bg='black')(' %s ' % 'white')
218 out.nl()
219
David Pursehouse8f62fb72012-11-14 12:09:38 +0900220 for c in ['bold', 'dim', 'ul', 'reverse']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700221 out.write(' ')
222 out.printer(fg='black', attr=c)(' %-6s ', c)
223 out.nl()
224
Mike Frysingerab85fe72019-07-04 17:35:11 -0400225 print('Enable color display in this user account (y/N)? ', end='')
226 # TODO: When we require Python 3, use flush=True w/print above.
227 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700228 a = sys.stdin.readline().strip().lower()
229 if a in ('y', 'yes', 't', 'true', 'on'):
230 gc.SetString('color.ui', 'auto')
231
Mike Frysinger0b888912020-02-21 22:48:40 -0500232 def _DisplayResult(self, opt):
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800233 if self.manifest.IsMirror:
234 init_type = 'mirror '
235 else:
236 init_type = ''
237
Mike Frysinger0b888912020-02-21 22:48:40 -0500238 if not opt.quiet:
239 print()
240 print('repo %shas been initialized in %s' %
241 (init_type, self.manifest.topdir))
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800242
243 current_dir = os.getcwd()
244 if current_dir != self.manifest.topdir:
David Pursehouse35765962013-01-29 09:49:48 +0900245 print('If this is not the directory in which you want to initialize '
Sarah Owenscecd1d82012-11-01 22:59:27 -0700246 'repo, please run:')
247 print(' rm -r %s/.repo' % self.manifest.topdir)
248 print('and try again.')
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800249
Mike Frysingerae6cb082019-08-27 01:10:59 -0400250 def ValidateOptions(self, opt, args):
Victor Boivie297e7c62012-10-05 14:50:05 +0200251 if opt.reference:
Samuel Hollandbaa00092018-01-22 10:57:29 -0600252 opt.reference = os.path.expanduser(opt.reference)
Victor Boivie297e7c62012-10-05 14:50:05 +0200253
Julien Campergue335f5ef2013-10-16 11:02:35 +0200254 # Check this here, else manifest will be tagged "not new" and init won't be
255 # possible anymore without removing the .repo/manifests directory.
Raman Tenneti6bd89aa2021-11-16 11:48:09 -0800256 if opt.mirror:
257 if opt.archive:
258 self.OptionParser.error('--mirror and --archive cannot be used '
259 'together.')
260 if opt.use_superproject is not None:
261 self.OptionParser.error('--mirror and --use-superproject cannot be '
262 'used together.')
Mike Frysingerae6cb082019-08-27 01:10:59 -0400263
Raman Tenneti4a478ed2021-11-17 18:38:24 -0800264 if opt.standalone_manifest and (opt.manifest_branch or
265 opt.manifest_name != 'default.xml'):
Jack Neusc474c9c2021-07-26 23:08:54 +0000266 self.OptionParser.error('--manifest-branch and --manifest-name cannot'
267 ' be used with --standalone-manifest.')
268
Mike Frysinger0578ebf2020-08-27 01:50:12 -0400269 if args:
Mike Frysinger401c6f02021-02-18 15:20:15 -0500270 if opt.manifest_url:
271 self.OptionParser.error(
272 '--manifest-url option and URL argument both specified: only use '
273 'one to select the manifest URL.')
274
275 opt.manifest_url = args.pop(0)
276
277 if args:
278 self.OptionParser.error('too many arguments to init')
Mike Frysinger0578ebf2020-08-27 01:50:12 -0400279
Mike Frysingerae6cb082019-08-27 01:10:59 -0400280 def Execute(self, opt, args):
Mike Frysinger82caef62020-02-11 18:51:08 -0500281 git_require(MIN_GIT_VERSION_HARD, fail=True)
282 if not git_require(MIN_GIT_VERSION_SOFT):
283 print('repo: warning: git-%s+ will soon be required; please upgrade your '
284 'version of git to maintain support.'
285 % ('.'.join(str(x) for x in MIN_GIT_VERSION_SOFT),),
286 file=sys.stderr)
Julien Campergue335f5ef2013-10-16 11:02:35 +0200287
Mike Frysinger7936ce82020-02-29 02:53:41 -0500288 rp = self.manifest.repoProject
289
290 # Handle new --repo-url requests.
291 if opt.repo_url:
292 remote = rp.GetRemote('origin')
293 remote.url = opt.repo_url
294 remote.Save()
295
Mike Frysinger3599cc32020-02-29 02:53:41 -0500296 # Handle new --repo-rev requests.
297 if opt.repo_rev:
298 wrapper = Wrapper()
Mike Frysinger4aa85842022-01-25 02:10:28 -0500299 try:
300 remote_ref, rev = wrapper.check_repo_rev(
301 rp.gitdir, opt.repo_rev, repo_verify=opt.repo_verify, quiet=opt.quiet)
302 except wrapper.CloneFailure:
303 print('fatal: double check your --repo-rev setting.', file=sys.stderr)
304 sys.exit(1)
Mike Frysinger3599cc32020-02-29 02:53:41 -0500305 branch = rp.GetBranch('default')
306 branch.merge = remote_ref
Mike Frysinger5e2f32f2020-12-05 22:57:19 -0500307 rp.work_git.reset('--hard', rev)
Mike Frysinger3599cc32020-02-29 02:53:41 -0500308 branch.Save()
309
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500310 if opt.worktree:
311 # Older versions of git supported worktree, but had dangerous gc bugs.
312 git_require((2, 15, 0), fail=True, msg='git gc worktree corruption')
313
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700314 self._SyncManifest(opt)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800315
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700316 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Mike Frysinger0b888912020-02-21 22:48:40 -0500317 if opt.config_name or self._ShouldConfigureUser(opt):
318 self._ConfigureUser(opt)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319 self._ConfigureColor()
320
Mike Frysinger0b888912020-02-21 22:48:40 -0500321 self._DisplayResult(opt)