diff options
author | LaMont Jones <lamontjones@google.com> | 2022-03-29 23:01:18 +0000 |
---|---|---|
committer | LaMont Jones <lamontjones@google.com> | 2022-04-01 15:48:04 +0000 |
commit | 9b03f15e8e870866b26699f696af1884100f51b5 (patch) | |
tree | 20a464d35d2c022ab5b0d05de109b4477fd7e1c5 /subcmds/init.py | |
parent | 9b72cf2ba5c00bee726aa4bddbb84be554294284 (diff) | |
download | git-repo-9b03f15e8e870866b26699f696af1884100f51b5.tar.gz |
project: add ManifestProject.Sync()
Move the logic to sync a ManifestProject out of subcmds/init.py
Change-Id: Ia9d00f3da1dc3c5dada84c4d19cf9802c2346cb0
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/334140
Tested-by: LaMont Jones <lamontjones@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/init.py')
-rw-r--r-- | subcmds/init.py | 266 |
1 files changed, 26 insertions, 240 deletions
diff --git a/subcmds/init.py b/subcmds/init.py index b9775a34..b6c891ac 100644 --- a/subcmds/init.py +++ b/subcmds/init.py | |||
@@ -128,230 +128,35 @@ to update the working directory files. | |||
128 | sys.exit(1) | 128 | sys.exit(1) |
129 | 129 | ||
130 | def _SyncManifest(self, opt): | 130 | def _SyncManifest(self, opt): |
131 | m = self.manifest.manifestProject | 131 | """Call manifestProject.Sync with arguments from opt. |
132 | is_new = not m.Exists | ||
133 | |||
134 | # If repo has already been initialized, we take -u with the absence of | ||
135 | # --standalone-manifest to mean "transition to a standard repo set up", | ||
136 | # which necessitates starting fresh. | ||
137 | # If --standalone-manifest is set, we always tear everything down and start | ||
138 | # anew. | ||
139 | if not is_new: | ||
140 | was_standalone_manifest = m.config.GetString('manifest.standalone') | ||
141 | if was_standalone_manifest and not opt.manifest_url: | ||
142 | print('fatal: repo was initialized with a standlone manifest, ' | ||
143 | 'cannot be re-initialized without --manifest-url/-u') | ||
144 | sys.exit(1) | ||
145 | |||
146 | if opt.standalone_manifest or (was_standalone_manifest and | ||
147 | opt.manifest_url): | ||
148 | m.config.ClearCache() | ||
149 | if m.gitdir and os.path.exists(m.gitdir): | ||
150 | platform_utils.rmtree(m.gitdir) | ||
151 | if m.worktree and os.path.exists(m.worktree): | ||
152 | platform_utils.rmtree(m.worktree) | ||
153 | |||
154 | is_new = not m.Exists | ||
155 | if is_new: | ||
156 | if not opt.manifest_url: | ||
157 | print('fatal: manifest url is required.', file=sys.stderr) | ||
158 | sys.exit(1) | ||
159 | |||
160 | if not opt.quiet: | ||
161 | print('Downloading manifest from %s' % | ||
162 | (GitConfig.ForUser().UrlInsteadOf(opt.manifest_url),), | ||
163 | file=sys.stderr) | ||
164 | |||
165 | # The manifest project object doesn't keep track of the path on the | ||
166 | # server where this git is located, so let's save that here. | ||
167 | mirrored_manifest_git = None | ||
168 | if opt.reference: | ||
169 | manifest_git_path = urllib.parse.urlparse(opt.manifest_url).path[1:] | ||
170 | mirrored_manifest_git = os.path.join(opt.reference, manifest_git_path) | ||
171 | if not mirrored_manifest_git.endswith(".git"): | ||
172 | mirrored_manifest_git += ".git" | ||
173 | if not os.path.exists(mirrored_manifest_git): | ||
174 | mirrored_manifest_git = os.path.join(opt.reference, | ||
175 | '.repo/manifests.git') | ||
176 | |||
177 | m._InitGitDir(mirror_git=mirrored_manifest_git) | ||
178 | |||
179 | # If standalone_manifest is set, mark the project as "standalone" -- we'll | ||
180 | # still do much of the manifests.git set up, but will avoid actual syncs to | ||
181 | # a remote. | ||
182 | standalone_manifest = False | ||
183 | if opt.standalone_manifest: | ||
184 | standalone_manifest = True | ||
185 | m.config.SetString('manifest.standalone', opt.manifest_url) | ||
186 | elif not opt.manifest_url and not opt.manifest_branch: | ||
187 | # If -u is set and --standalone-manifest is not, then we're not in | ||
188 | # standalone mode. Otherwise, use config to infer what we were in the last | ||
189 | # init. | ||
190 | standalone_manifest = bool(m.config.GetString('manifest.standalone')) | ||
191 | if not standalone_manifest: | ||
192 | m.config.SetString('manifest.standalone', None) | ||
193 | |||
194 | self._ConfigureDepth(opt) | ||
195 | |||
196 | # Set the remote URL before the remote branch as we might need it below. | ||
197 | if opt.manifest_url: | ||
198 | r = m.GetRemote(m.remote.name) | ||
199 | r.url = opt.manifest_url | ||
200 | r.ResetFetch() | ||
201 | r.Save() | ||
202 | |||
203 | if not standalone_manifest: | ||
204 | if opt.manifest_branch: | ||
205 | if opt.manifest_branch == 'HEAD': | ||
206 | opt.manifest_branch = m.ResolveRemoteHead() | ||
207 | if opt.manifest_branch is None: | ||
208 | print('fatal: unable to resolve HEAD', file=sys.stderr) | ||
209 | sys.exit(1) | ||
210 | m.revisionExpr = opt.manifest_branch | ||
211 | else: | ||
212 | if is_new: | ||
213 | default_branch = m.ResolveRemoteHead() | ||
214 | if default_branch is None: | ||
215 | # If the remote doesn't have HEAD configured, default to master. | ||
216 | default_branch = 'refs/heads/master' | ||
217 | m.revisionExpr = default_branch | ||
218 | else: | ||
219 | m.PreSync() | ||
220 | |||
221 | groups = re.split(r'[,\s]+', opt.groups) | ||
222 | all_platforms = ['linux', 'darwin', 'windows'] | ||
223 | platformize = lambda x: 'platform-' + x | ||
224 | if opt.platform == 'auto': | ||
225 | if (not opt.mirror and | ||
226 | not m.config.GetString('repo.mirror') == 'true'): | ||
227 | groups.append(platformize(platform.system().lower())) | ||
228 | elif opt.platform == 'all': | ||
229 | groups.extend(map(platformize, all_platforms)) | ||
230 | elif opt.platform in all_platforms: | ||
231 | groups.append(platformize(opt.platform)) | ||
232 | elif opt.platform != 'none': | ||
233 | print('fatal: invalid platform flag', file=sys.stderr) | ||
234 | sys.exit(1) | ||
235 | |||
236 | groups = [x for x in groups if x] | ||
237 | groupstr = ','.join(groups) | ||
238 | if opt.platform == 'auto' and groupstr == self.manifest.GetDefaultGroupsStr(): | ||
239 | groupstr = None | ||
240 | m.config.SetString('manifest.groups', groupstr) | ||
241 | |||
242 | if opt.reference: | ||
243 | m.config.SetString('repo.reference', opt.reference) | ||
244 | |||
245 | if opt.dissociate: | ||
246 | m.config.SetBoolean('repo.dissociate', opt.dissociate) | ||
247 | |||
248 | if opt.worktree: | ||
249 | if opt.mirror: | ||
250 | print('fatal: --mirror and --worktree are incompatible', | ||
251 | file=sys.stderr) | ||
252 | sys.exit(1) | ||
253 | if opt.submodules: | ||
254 | print('fatal: --submodules and --worktree are incompatible', | ||
255 | file=sys.stderr) | ||
256 | sys.exit(1) | ||
257 | m.config.SetBoolean('repo.worktree', opt.worktree) | ||
258 | if is_new: | ||
259 | m.use_git_worktrees = True | ||
260 | print('warning: --worktree is experimental!', file=sys.stderr) | ||
261 | |||
262 | if opt.archive: | ||
263 | if is_new: | ||
264 | m.config.SetBoolean('repo.archive', opt.archive) | ||
265 | else: | ||
266 | print('fatal: --archive is only supported when initializing a new ' | ||
267 | 'workspace.', file=sys.stderr) | ||
268 | print('Either delete the .repo folder in this workspace, or initialize ' | ||
269 | 'in another location.', file=sys.stderr) | ||
270 | sys.exit(1) | ||
271 | |||
272 | if opt.mirror: | ||
273 | if is_new: | ||
274 | m.config.SetBoolean('repo.mirror', opt.mirror) | ||
275 | else: | ||
276 | print('fatal: --mirror is only supported when initializing a new ' | ||
277 | 'workspace.', file=sys.stderr) | ||
278 | print('Either delete the .repo folder in this workspace, or initialize ' | ||
279 | 'in another location.', file=sys.stderr) | ||
280 | sys.exit(1) | ||
281 | 132 | ||
282 | if opt.partial_clone is not None: | 133 | Args: |
283 | if opt.mirror: | 134 | opt: options from optparse. |
284 | print('fatal: --mirror and --partial-clone are mutually exclusive', | 135 | """ |
285 | file=sys.stderr) | 136 | if not self.manifest.manifestProject.Sync( |
286 | sys.exit(1) | 137 | manifest_url=opt.manifest_url, |
287 | m.config.SetBoolean('repo.partialclone', opt.partial_clone) | 138 | manifest_branch=opt.manifest_branch, |
288 | if opt.clone_filter: | 139 | standalone_manifest=opt.standalone_manifest, |
289 | m.config.SetString('repo.clonefilter', opt.clone_filter) | 140 | groups=opt.groups, |
290 | elif m.config.GetBoolean('repo.partialclone'): | 141 | platform=opt.platform, |
291 | opt.clone_filter = m.config.GetString('repo.clonefilter') | 142 | mirror=opt.mirror, |
292 | else: | 143 | dissociate=opt.dissociate, |
293 | opt.clone_filter = None | 144 | reference=opt.reference, |
294 | 145 | worktree=opt.worktree, | |
295 | if opt.partial_clone_exclude is not None: | 146 | submodules=opt.submodules, |
296 | m.config.SetString('repo.partialcloneexclude', opt.partial_clone_exclude) | 147 | archive=opt.archive, |
297 | 148 | partial_clone=opt.partial_clone, | |
298 | if opt.clone_bundle is None: | 149 | clone_filter=opt.clone_filter, |
299 | opt.clone_bundle = False if opt.partial_clone else True | 150 | partial_clone_exclude=opt.partial_clone_exclude, |
300 | else: | 151 | clone_bundle=opt.clone_bundle, |
301 | m.config.SetBoolean('repo.clonebundle', opt.clone_bundle) | 152 | git_lfs=opt.git_lfs, |
302 | 153 | use_superproject=opt.use_superproject, | |
303 | if opt.submodules: | 154 | verbose=opt.verbose, |
304 | m.config.SetBoolean('repo.submodules', opt.submodules) | 155 | current_branch_only=opt.current_branch_only, |
305 | 156 | tags=opt.tags, | |
306 | if opt.git_lfs is not None: | 157 | depth=opt.depth): |
307 | if opt.git_lfs: | ||
308 | git_require((2, 17, 0), fail=True, msg='Git LFS support') | ||
309 | |||
310 | m.config.SetBoolean('repo.git-lfs', opt.git_lfs) | ||
311 | if not is_new: | ||
312 | print('warning: Changing --git-lfs settings will only affect new project checkouts.\n' | ||
313 | ' Existing projects will require manual updates.\n', file=sys.stderr) | ||
314 | |||
315 | if opt.use_superproject is not None: | ||
316 | m.config.SetBoolean('repo.superproject', opt.use_superproject) | ||
317 | |||
318 | if standalone_manifest: | ||
319 | if is_new: | ||
320 | manifest_name = 'default.xml' | ||
321 | manifest_data = fetch.fetch_file(opt.manifest_url, verbose=opt.verbose) | ||
322 | dest = os.path.join(m.worktree, manifest_name) | ||
323 | os.makedirs(os.path.dirname(dest), exist_ok=True) | ||
324 | with open(dest, 'wb') as f: | ||
325 | f.write(manifest_data) | ||
326 | return | ||
327 | |||
328 | if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet, verbose=opt.verbose, | ||
329 | clone_bundle=opt.clone_bundle, | ||
330 | current_branch_only=opt.current_branch_only, | ||
331 | tags=opt.tags, submodules=opt.submodules, | ||
332 | clone_filter=opt.clone_filter, | ||
333 | partial_clone_exclude=self.manifest.PartialCloneExclude): | ||
334 | r = m.GetRemote(m.remote.name) | ||
335 | print('fatal: cannot obtain manifest %s' % r.url, file=sys.stderr) | ||
336 | |||
337 | # Better delete the manifest git dir if we created it; otherwise next | ||
338 | # time (when user fixes problems) we won't go through the "is_new" logic. | ||
339 | if is_new: | ||
340 | platform_utils.rmtree(m.gitdir) | ||
341 | sys.exit(1) | 158 | sys.exit(1) |
342 | 159 | ||
343 | if opt.manifest_branch: | ||
344 | m.MetaBranchSwitch(submodules=opt.submodules) | ||
345 | |||
346 | syncbuf = SyncBuffer(m.config) | ||
347 | m.Sync_LocalHalf(syncbuf, submodules=opt.submodules) | ||
348 | syncbuf.Finish() | ||
349 | |||
350 | if is_new or m.CurrentBranch is None: | ||
351 | if not m.StartBranch('default'): | ||
352 | print('fatal: cannot create default in manifest', file=sys.stderr) | ||
353 | sys.exit(1) | ||
354 | |||
355 | def _LinkManifest(self, name): | 160 | def _LinkManifest(self, name): |
356 | if not name: | 161 | if not name: |
357 | print('fatal: manifest name (-m) is required.', file=sys.stderr) | 162 | print('fatal: manifest name (-m) is required.', file=sys.stderr) |
@@ -455,25 +260,6 @@ to update the working directory files. | |||
455 | if a in ('y', 'yes', 't', 'true', 'on'): | 260 | if a in ('y', 'yes', 't', 'true', 'on'): |
456 | gc.SetString('color.ui', 'auto') | 261 | gc.SetString('color.ui', 'auto') |
457 | 262 | ||
458 | def _ConfigureDepth(self, opt): | ||
459 | """Configure the depth we'll sync down. | ||
460 | |||
461 | Args: | ||
462 | opt: Options from optparse. We care about opt.depth. | ||
463 | """ | ||
464 | # Opt.depth will be non-None if user actually passed --depth to repo init. | ||
465 | if opt.depth is not None: | ||
466 | if opt.depth > 0: | ||
467 | # Positive values will set the depth. | ||
468 | depth = str(opt.depth) | ||
469 | else: | ||
470 | # Negative numbers will clear the depth; passing None to SetString | ||
471 | # will do that. | ||
472 | depth = None | ||
473 | |||
474 | # We store the depth in the main manifest project. | ||
475 | self.manifest.manifestProject.config.SetString('repo.depth', depth) | ||
476 | |||
477 | def _DisplayResult(self, opt): | 263 | def _DisplayResult(self, opt): |
478 | if self.manifest.IsMirror: | 264 | if self.manifest.IsMirror: |
479 | init_type = 'mirror ' | 265 | init_type = 'mirror ' |