blob: 6e3951c9c92c1b71cb6f1894e96a309c766d7d66 [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')
92 m.add_option('--outer-manifest', action='store_true',
93 help='operate starting at the outermost manifest')
94 m.add_option('--no-outer-manifest', dest='outer_manifest',
95 action='store_false', default=None,
96 help='do not operate on outer manifests')
97 m.add_option('--this-manifest-only', action='store_true', default=None,
98 help='only operate on this (sub)manifest')
99 m.add_option('--no-this-manifest-only', '--all-manifests',
100 dest='this_manifest_only', action='store_false',
101 help='operate on this manifest and its submanifests')
Victor Boivie841be342011-04-05 11:31:10 +0200102
David Pursehouse3f5ea0b2012-11-17 03:13:09 +0900103 def _RegisteredEnvironmentOptions(self):
104 return {'REPO_MANIFEST_URL': 'manifest_url',
105 'REPO_MIRROR_LOCATION': 'reference'}
106
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107 def _SyncManifest(self, opt):
LaMont Jones9b03f152022-03-29 23:01:18 +0000108 """Call manifestProject.Sync with arguments from opt.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109
LaMont Jones9b03f152022-03-29 23:01:18 +0000110 Args:
111 opt: options from optparse.
112 """
113 if not self.manifest.manifestProject.Sync(
114 manifest_url=opt.manifest_url,
115 manifest_branch=opt.manifest_branch,
116 standalone_manifest=opt.standalone_manifest,
117 groups=opt.groups,
118 platform=opt.platform,
119 mirror=opt.mirror,
120 dissociate=opt.dissociate,
121 reference=opt.reference,
122 worktree=opt.worktree,
123 submodules=opt.submodules,
124 archive=opt.archive,
125 partial_clone=opt.partial_clone,
126 clone_filter=opt.clone_filter,
127 partial_clone_exclude=opt.partial_clone_exclude,
128 clone_bundle=opt.clone_bundle,
129 git_lfs=opt.git_lfs,
130 use_superproject=opt.use_superproject,
131 verbose=opt.verbose,
132 current_branch_only=opt.current_branch_only,
133 tags=opt.tags,
LaMont Jones409407a2022-04-05 21:21:56 +0000134 depth=opt.depth,
LaMont Jones55ee3042022-04-06 17:10:21 +0000135 git_event_log=self.git_event_log,
LaMont Jones409407a2022-04-05 21:21:56 +0000136 manifest_name=opt.manifest_name):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137 sys.exit(1)
138
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700139 def _Prompt(self, prompt, value):
Mike Frysingerab85fe72019-07-04 17:35:11 -0400140 print('%-10s [%s]: ' % (prompt, value), end='')
141 # TODO: When we require Python 3, use flush=True w/print above.
142 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 a = sys.stdin.readline().strip()
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700144 if a == '':
145 return value
146 return a
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700147
Mike Frysinger0b888912020-02-21 22:48:40 -0500148 def _ShouldConfigureUser(self, opt):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400149 gc = self.client.globalConfig
Victor Boivie841be342011-04-05 11:31:10 +0200150 mp = self.manifest.manifestProject
151
152 # If we don't have local settings, get from global.
153 if not mp.config.Has('user.name') or not mp.config.Has('user.email'):
154 if not gc.Has('user.name') or not gc.Has('user.email'):
155 return True
156
157 mp.config.SetString('user.name', gc.GetString('user.name'))
158 mp.config.SetString('user.email', gc.GetString('user.email'))
159
Mike Frysinger0b888912020-02-21 22:48:40 -0500160 if not opt.quiet:
161 print()
162 print('Your identity is: %s <%s>' % (mp.config.GetString('user.name'),
163 mp.config.GetString('user.email')))
164 print("If you want to change this, please re-run 'repo init' with --config-name")
Victor Boivie841be342011-04-05 11:31:10 +0200165 return False
166
Mike Frysinger0b888912020-02-21 22:48:40 -0500167 def _ConfigureUser(self, opt):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168 mp = self.manifest.manifestProject
169
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700170 while True:
Mike Frysinger0b888912020-02-21 22:48:40 -0500171 if not opt.quiet:
172 print()
David Pursehouse54a4e602020-02-12 14:31:05 +0900173 name = self._Prompt('Your Name', mp.UserName)
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700174 email = self._Prompt('Your Email', mp.UserEmail)
175
Mike Frysinger0b888912020-02-21 22:48:40 -0500176 if not opt.quiet:
177 print()
Sarah Owenscecd1d82012-11-01 22:59:27 -0700178 print('Your identity is: %s <%s>' % (name, email))
Mike Frysingerab85fe72019-07-04 17:35:11 -0400179 print('is this correct [y/N]? ', end='')
180 # TODO: When we require Python 3, use flush=True w/print above.
181 sys.stdout.flush()
David Pursehousefc241242012-11-14 09:19:39 +0900182 a = sys.stdin.readline().strip().lower()
Nico Sallembien6d7508b2010-04-01 11:03:53 -0700183 if a in ('yes', 'y', 't', 'true'):
Shawn O. Pearce37dbf2b2009-07-02 10:53:04 -0700184 break
185
186 if name != mp.UserName:
187 mp.config.SetString('user.name', name)
188 if email != mp.UserEmail:
189 mp.config.SetString('user.email', email)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190
191 def _HasColorSet(self, gc):
192 for n in ['ui', 'diff', 'status']:
193 if gc.Has('color.%s' % n):
194 return True
195 return False
196
197 def _ConfigureColor(self):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400198 gc = self.client.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700199 if self._HasColorSet(gc):
200 return
201
202 class _Test(Coloring):
203 def __init__(self):
204 Coloring.__init__(self, gc, 'test color display')
205 self._on = True
206 out = _Test()
207
Sarah Owenscecd1d82012-11-01 22:59:27 -0700208 print()
209 print("Testing colorized output (for 'repo diff', 'repo status'):")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210
David Pursehouse8f62fb72012-11-14 12:09:38 +0900211 for c in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700212 out.write(' ')
213 out.printer(fg=c)(' %-6s ', c)
214 out.write(' ')
215 out.printer(fg='white', bg='black')(' %s ' % 'white')
216 out.nl()
217
David Pursehouse8f62fb72012-11-14 12:09:38 +0900218 for c in ['bold', 'dim', 'ul', 'reverse']:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700219 out.write(' ')
220 out.printer(fg='black', attr=c)(' %-6s ', c)
221 out.nl()
222
Mike Frysingerab85fe72019-07-04 17:35:11 -0400223 print('Enable color display in this user account (y/N)? ', end='')
224 # TODO: When we require Python 3, use flush=True w/print above.
225 sys.stdout.flush()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700226 a = sys.stdin.readline().strip().lower()
227 if a in ('y', 'yes', 't', 'true', 'on'):
228 gc.SetString('color.ui', 'auto')
229
Mike Frysinger0b888912020-02-21 22:48:40 -0500230 def _DisplayResult(self, opt):
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800231 if self.manifest.IsMirror:
232 init_type = 'mirror '
233 else:
234 init_type = ''
235
Mike Frysinger0b888912020-02-21 22:48:40 -0500236 if not opt.quiet:
237 print()
238 print('repo %shas been initialized in %s' %
239 (init_type, self.manifest.topdir))
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800240
241 current_dir = os.getcwd()
242 if current_dir != self.manifest.topdir:
David Pursehouse35765962013-01-29 09:49:48 +0900243 print('If this is not the directory in which you want to initialize '
Sarah Owenscecd1d82012-11-01 22:59:27 -0700244 'repo, please run:')
245 print(' rm -r %s/.repo' % self.manifest.topdir)
246 print('and try again.')
Yang Zhenhui75cc3532012-10-23 15:41:54 +0800247
Mike Frysingerae6cb082019-08-27 01:10:59 -0400248 def ValidateOptions(self, opt, args):
Victor Boivie297e7c62012-10-05 14:50:05 +0200249 if opt.reference:
Samuel Hollandbaa00092018-01-22 10:57:29 -0600250 opt.reference = os.path.expanduser(opt.reference)
Victor Boivie297e7c62012-10-05 14:50:05 +0200251
Julien Campergue335f5ef2013-10-16 11:02:35 +0200252 # Check this here, else manifest will be tagged "not new" and init won't be
253 # possible anymore without removing the .repo/manifests directory.
Raman Tenneti6bd89aa2021-11-16 11:48:09 -0800254 if opt.mirror:
255 if opt.archive:
256 self.OptionParser.error('--mirror and --archive cannot be used '
257 'together.')
258 if opt.use_superproject is not None:
259 self.OptionParser.error('--mirror and --use-superproject cannot be '
260 'used together.')
LaMont Jones5fa912b2022-04-14 14:41:13 +0000261 if opt.archive and opt.use_superproject is not None:
262 self.OptionParser.error('--archive and --use-superproject cannot be used '
263 'together.')
Mike Frysingerae6cb082019-08-27 01:10:59 -0400264
Raman Tenneti4a478ed2021-11-17 18:38:24 -0800265 if opt.standalone_manifest and (opt.manifest_branch or
266 opt.manifest_name != 'default.xml'):
Jack Neusc474c9c2021-07-26 23:08:54 +0000267 self.OptionParser.error('--manifest-branch and --manifest-name cannot'
268 ' be used with --standalone-manifest.')
269
Mike Frysinger0578ebf2020-08-27 01:50:12 -0400270 if args:
Mike Frysinger401c6f02021-02-18 15:20:15 -0500271 if opt.manifest_url:
272 self.OptionParser.error(
273 '--manifest-url option and URL argument both specified: only use '
274 'one to select the manifest URL.')
275
276 opt.manifest_url = args.pop(0)
277
278 if args:
279 self.OptionParser.error('too many arguments to init')
Mike Frysinger0578ebf2020-08-27 01:50:12 -0400280
Mike Frysingerae6cb082019-08-27 01:10:59 -0400281 def Execute(self, opt, args):
Mike Frysinger82caef62020-02-11 18:51:08 -0500282 git_require(MIN_GIT_VERSION_HARD, fail=True)
283 if not git_require(MIN_GIT_VERSION_SOFT):
284 print('repo: warning: git-%s+ will soon be required; please upgrade your '
285 'version of git to maintain support.'
286 % ('.'.join(str(x) for x in MIN_GIT_VERSION_SOFT),),
287 file=sys.stderr)
Julien Campergue335f5ef2013-10-16 11:02:35 +0200288
Mike Frysinger7936ce82020-02-29 02:53:41 -0500289 rp = self.manifest.repoProject
290
291 # Handle new --repo-url requests.
292 if opt.repo_url:
293 remote = rp.GetRemote('origin')
294 remote.url = opt.repo_url
295 remote.Save()
296
Mike Frysinger3599cc32020-02-29 02:53:41 -0500297 # Handle new --repo-rev requests.
298 if opt.repo_rev:
299 wrapper = Wrapper()
Mike Frysinger4aa85842022-01-25 02:10:28 -0500300 try:
301 remote_ref, rev = wrapper.check_repo_rev(
302 rp.gitdir, opt.repo_rev, repo_verify=opt.repo_verify, quiet=opt.quiet)
303 except wrapper.CloneFailure:
304 print('fatal: double check your --repo-rev setting.', file=sys.stderr)
305 sys.exit(1)
Mike Frysinger3599cc32020-02-29 02:53:41 -0500306 branch = rp.GetBranch('default')
307 branch.merge = remote_ref
Mike Frysinger5e2f32f2020-12-05 22:57:19 -0500308 rp.work_git.reset('--hard', rev)
Mike Frysinger3599cc32020-02-29 02:53:41 -0500309 branch.Save()
310
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500311 if opt.worktree:
312 # Older versions of git supported worktree, but had dangerous gc bugs.
313 git_require((2, 15, 0), fail=True, msg='git gc worktree corruption')
314
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700315 self._SyncManifest(opt)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800316
Shawn O. Pearce8630f392009-03-19 10:17:12 -0700317 if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
Mike Frysinger0b888912020-02-21 22:48:40 -0500318 if opt.config_name or self._ShouldConfigureUser(opt):
319 self._ConfigureUser(opt)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700320 self._ConfigureColor()
321
Mike Frysinger0b888912020-02-21 22:48:40 -0500322 self._DisplayResult(opt)