blob: cae0e342026557d53757290c04c581536137b841 [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
Mike Frysinger3599cc32020-02-29 02:53:41 -050027from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028
David Pursehouse819827a2020-02-12 15:20:19 +090029
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080030class Init(InteractiveCommand, MirrorSafeCommand):
Mike Frysinger4f210542021-06-14 16:05:19 -040031 COMMON = True
LaMont Jones409407a2022-04-05 21:21:56 +000032 MULTI_MANIFEST_SUPPORT = True
Mike Frysinger401c6f02021-02-18 15:20:15 -050033 helpSummary = "Initialize a repo client checkout in the current directory"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034 helpUsage = """
Mike Frysinger401c6f02021-02-18 15:20:15 -050035%prog [options] [manifest url]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036"""
37 helpDescription = """
38The '%prog' command is run once to install and initialize repo.
39The latest repo source code and manifest collection is downloaded
40from the server and is installed in the .repo/ directory in the
41current working directory.
42
Mike Frysinger401c6f02021-02-18 15:20:15 -050043When creating a new checkout, the manifest URL is the only required setting.
44It may be specified using the --manifest-url option, or as the first optional
45argument.
46
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070047The optional -b argument can be used to select the manifest branch
Mike Frysinger50a81de2020-09-06 15:51:21 -040048to checkout and use. If no branch is specified, the remote's default
Mike Frysinger23882b32021-02-23 15:43:07 -050049branch is used. This is equivalent to using -b HEAD.
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070050
51The optional -m argument can be used to specify an alternate manifest
52to be used. If no manifest is specified, the manifest default.xml
53will be used.
54
Jack Neusc474c9c2021-07-26 23:08:54 +000055If the --standalone-manifest argument is set, the manifest will be downloaded
56directly from the specified --manifest-url as a static file (rather than
57setting up a manifest git checkout). With --standalone-manifest, the manifest
58will be fully static and will not be re-downloaded during subsesquent
59`repo init` and `repo sync` calls.
60
Shawn O. Pearce88443382010-10-08 10:02:09 +020061The --reference option can be used to point to a directory that
62has the content of a --mirror sync. This will make the working
63directory use as much data as possible from the local reference
64directory when fetching from the server. This will make the sync
65go a lot faster by reducing data traffic on the network.
66
Nikolai Merinov09f0abb2018-10-19 15:07:05 +050067The --dissociate option can be used to borrow the objects from
68the directory specified with the --reference option only to reduce
69network transfer, and stop borrowing from them after a first clone
70is made by making necessary local copies of borrowed objects.
71
Hu xiuyun9711a982015-12-11 11:16:41 +080072The --no-clone-bundle option disables any attempt to use
73$URL/clone.bundle to bootstrap a new Git repository from a
74resumeable bundle file on a content delivery network. This
75may be necessary if there are problems with the local Python
76HTTP client or proxy configuration, but the Git binary works.
Shawn O. Pearce88443382010-10-08 10:02:09 +020077
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040078# Switching Manifest Branches
Shawn O. Pearce77bb4af2009-04-18 11:33:32 -070079
80To switch to another manifest branch, `repo init -b otherbranch`
81may be used in an existing client. However, as this only updates the
82manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
83to update the working directory files.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084"""
85
Mike Frysinger9180a072021-04-13 14:57:40 -040086 def _CommonOptions(self, p):
87 """Disable due to re-use of Wrapper()."""
88
Mike Frysinger66098f72020-02-05 00:01:59 -050089 def _Options(self, p, gitc_init=False):
Mike Frysinger9a734a32021-04-08 19:14:15 -040090 Wrapper().InitParser(p, gitc_init=gitc_init)
LaMont Jonescc879a92021-11-18 22:40:18 +000091 m = p.add_option_group('Multi-manifest')
LaMont Jonesbdcba7d2022-04-11 22:50:11 +000092 m.add_option('--outer-manifest', action='store_true', default=True,
LaMont Jonescc879a92021-11-18 22:40:18 +000093 help='operate starting at the outermost manifest')
94 m.add_option('--no-outer-manifest', dest='outer_manifest',
LaMont Jonesbdcba7d2022-04-11 22:50:11 +000095 action='store_false', help='do not operate on outer manifests')
LaMont Jonescc879a92021-11-18 22:40:18 +000096 m.add_option('--this-manifest-only', action='store_true', default=None,
97 help='only operate on this (sub)manifest')
98 m.add_option('--no-this-manifest-only', '--all-manifests',
99 dest='this_manifest_only', action='store_false',
100 help='operate on this manifest and its submanifests')
Victor Boivie841be342011-04-05 11:31:10 +0200101
David Pursehouse3f5ea0b2012-11-17 03:13:09 +0900102 def _RegisteredEnvironmentOptions(self):
103 return {'REPO_MANIFEST_URL': 'manifest_url',
104 'REPO_MIRROR_LOCATION': 'reference'}
105
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 def _SyncManifest(self, opt):
LaMont Jones9b03f152022-03-29 23:01:18 +0000107 """Call manifestProject.Sync with arguments from opt.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108
LaMont Jones9b03f152022-03-29 23:01:18 +0000109 Args:
110 opt: options from optparse.
111 """
112 if not self.manifest.manifestProject.Sync(
113 manifest_url=opt.manifest_url,
114 manifest_branch=opt.manifest_branch,
115 standalone_manifest=opt.standalone_manifest,
116 groups=opt.groups,
117 platform=opt.platform,
118 mirror=opt.mirror,
119 dissociate=opt.dissociate,
120 reference=opt.reference,
121 worktree=opt.worktree,
122 submodules=opt.submodules,
123 archive=opt.archive,
124 partial_clone=opt.partial_clone,
125 clone_filter=opt.clone_filter,
126 partial_clone_exclude=opt.partial_clone_exclude,
127 clone_bundle=opt.clone_bundle,
128 git_lfs=opt.git_lfs,
129 use_superproject=opt.use_superproject,
130 verbose=opt.verbose,
131 current_branch_only=opt.current_branch_only,
132 tags=opt.tags,
LaMont Jones409407a2022-04-05 21:21:56 +0000133 depth=opt.depth,
LaMont Jones55ee3042022-04-06 17:10:21 +0000134 git_event_log=self.git_event_log,
LaMont Jones409407a2022-04-05 21:21:56 +0000135 manifest_name=opt.manifest_name):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136 sys.exit(1)
137
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700138 def _Prompt(self, prompt, value):
Mike Frysingerab85fe72019-07-04 17:35:11 -0400139 print('%-10s [%s]: ' % (prompt, value), end='')
140 # TODO: When we require Python 3, use flush=True w/print above.
141 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700143 if a == '':
144 return value
145 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146
Mike Frysinger3d27c712022-08-19 06:48:23 -0400147 def _ShouldConfigureUser(self, opt, existing_checkout):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400148 gc = self.client.globalConfig
Victor Boivie841be342011-04-05 11:31:10 +0200149 mp = self.manifest.manifestProject
150
151 # If we don't have local settings, get from global.
152 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
153 if not gc.Has('user.name') or not gc.Has('user.email'):
154 return True
155
156 mp.config.SetString('user.name', gc.GetString('user.name'))
157 mp.config.SetString('user.email', gc.GetString('user.email'))
158
Mike Frysinger3d27c712022-08-19 06:48:23 -0400159 if not opt.quiet and not existing_checkout or opt.verbose:
Mike Frysinger0b888912020-02-21 22:48:40 -0500160 print()
161 print('Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
162 mp.config.GetString('user.email')))
163 print("If you want to change this, please re-run 'repo init' with --config-name")
Victor Boivie841be342011-04-05 11:31:10 +0200164 return False
165
Mike Frysinger0b888912020-02-21 22:48:40 -0500166 def _ConfigureUser(self, opt):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 mp = self.manifest.manifestProject
168
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700169 while True:
Mike Frysinger0b888912020-02-21 22:48:40 -0500170 if not opt.quiet:
171 print()
David Pursehouse54a4e602020-02-12 14:31:05 +0900172 name = self._Prompt('Your Name', mp.UserName)
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700173 email = self._Prompt('Your Email', mp.UserEmail)
174
Mike Frysinger0b888912020-02-21 22:48:40 -0500175 if not opt.quiet:
176 print()
Sarah Owenscecd1d82012-11-01 22:59:27 -0700177 print('Your identity is: %s <%s>' % (name, email))
Mike Frysingerab85fe72019-07-04 17:35:11 -0400178 print('is this correct [y/N]? ', end='')
179 # TODO: When we require Python 3, use flush=True w/print above.
180 sys.stdout.flush()
David Pursehousefc241242012-11-14 09:19:39 +0900181 a = sys.stdin.readline().strip().lower()
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700182 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700183 break
184
185 if name != mp.UserName:
186 mp.config.SetString('user.name', name)
187 if email != mp.UserEmail:
188 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700189
190 def _HasColorSet(self, gc):
191 for n in ['ui', 'diff', 'status']:
192 if gc.Has('color.%s' % n):
193 return True
194 return False
195
196 def _ConfigureColor(self):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400197 gc = self.client.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198 if self._HasColorSet(gc):
199 return
200
201 class _Test(Coloring):
202 def __init__(self):
203 Coloring.__init__(self, gc, 'test color display')
204 self._on = True
205 out = _Test()
206
Sarah Owenscecd1d82012-11-01 22:59:27 -0700207 print()
208 print("Testing colorized output (for 'repo diff', 'repo status'):")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209
David Pursehouse8f62fb72012-11-14 12:09:38 +0900210 for c in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 out.write(' ')
212 out.printer(fg=c)(' %-6s ', c)
213 out.write(' ')
214 out.printer(fg='white', bg='black')(' %s ' % 'white')
215 out.nl()
216
David Pursehouse8f62fb72012-11-14 12:09:38 +0900217 for c in ['bold', 'dim', 'ul', 'reverse']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700218 out.write(' ')
219 out.printer(fg='black', attr=c)(' %-6s ', c)
220 out.nl()
221
Mike Frysingerab85fe72019-07-04 17:35:11 -0400222 print('Enable color display in this user account (y/N)? ', end='')
223 # TODO: When we require Python 3, use flush=True w/print above.
224 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 a = sys.stdin.readline().strip().lower()
226 if a in ('y', 'yes', 't', 'true', 'on'):
227 gc.SetString('color.ui', 'auto')
228
Mike Frysinger0b888912020-02-21 22:48:40 -0500229 def _DisplayResult(self, opt):
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800230 if self.manifest.IsMirror:
231 init_type = 'mirror '
232 else:
233 init_type = ''
234
Mike Frysinger0b888912020-02-21 22:48:40 -0500235 if not opt.quiet:
236 print()
237 print('repo %shas been initialized in %s' %
238 (init_type, self.manifest.topdir))
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800239
240 current_dir = os.getcwd()
241 if current_dir != self.manifest.topdir:
David Pursehouse35765962013-01-29 09:49:48 +0900242 print('If this is not the directory in which you want to initialize '
Sarah Owenscecd1d82012-11-01 22:59:27 -0700243 'repo, please run:')
Mateus Azisbddc9642022-07-21 08:13:09 -0700244 print(' rm -r %s' % os.path.join(self.manifest.topdir, '.repo'))
Sarah Owenscecd1d82012-11-01 22:59:27 -0700245 print('and try again.')
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800246
Mike Frysingerae6cb082019-08-27 01:10:59 -0400247 def ValidateOptions(self, opt, args):
Victor Boivie297e7c62012-10-05 14:50:05 +0200248 if opt.reference:
Samuel Hollandbaa00092018-01-22 10:57:29 -0600249 opt.reference = os.path.expanduser(opt.reference)
Victor Boivie297e7c62012-10-05 14:50:05 +0200250
Julien Campergue335f5ef2013-10-16 11:02:35 +0200251 # Check this here, else manifest will be tagged "not new" and init won't be
252 # possible anymore without removing the .repo/manifests directory.
Raman Tenneti6bd89aa2021-11-16 11:48:09 -0800253 if opt.mirror:
254 if opt.archive:
255 self.OptionParser.error('--mirror and --archive cannot be used '
256 'together.')
257 if opt.use_superproject is not None:
258 self.OptionParser.error('--mirror and --use-superproject cannot be '
259 'used together.')
LaMont Jones5fa912b2022-04-14 14:41:13 +0000260 if opt.archive and opt.use_superproject is not None:
261 self.OptionParser.error('--archive and --use-superproject cannot be used '
262 '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
Mike Frysinger488d54d2022-08-19 06:47:35 -0400314 # Provide a short notice that we're reinitializing an existing checkout.
315 # Sometimes developers might not realize that they're in one, or that
316 # repo doesn't do nested checkouts.
317 existing_checkout = self.manifest.manifestProject.Exists
318 if not opt.quiet and existing_checkout:
319 print('repo: reusing existing repo client checkout in', self.manifest.topdir)
320
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700321 self._SyncManifest(opt)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800322
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700323 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Mike Frysinger3d27c712022-08-19 06:48:23 -0400324 if opt.config_name or self._ShouldConfigureUser(opt, existing_checkout):
Mike Frysinger0b888912020-02-21 22:48:40 -0500325 self._ConfigureUser(opt)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700326 self._ConfigureColor()
327
Mike Frysinger0b888912020-02-21 22:48:40 -0500328 self._DisplayResult(opt)