The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # 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 | |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 15 | import functools |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 16 | import os |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import sys |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 18 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 19 | from command import Command, DEFAULT_LOCAL_JOBS |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 20 | from git_config import IsImmutable |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | from git_command import git |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 22 | import gitc_utils |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 23 | from progress import Progress |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 24 | from project import SyncBuffer |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 26 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | class Start(Command): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame^] | 28 | COMMON = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | helpSummary = "Start a new branch for development" |
| 30 | helpUsage = """ |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 31 | %prog <newbranchname> [--all | <project>...] |
Shawn O. Pearce | 06e556d | 2009-04-18 11:19:01 -0700 | [diff] [blame] | 32 | """ |
| 33 | helpDescription = """ |
| 34 | '%prog' begins a new branch of development, starting from the |
| 35 | revision specified in the manifest. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | """ |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 37 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 39 | def _Options(self, p): |
| 40 | p.add_option('--all', |
| 41 | dest='all', action='store_true', |
| 42 | help='begin branch in all projects') |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 43 | p.add_option('-r', '--rev', '--revision', dest='revision', |
| 44 | help='point branch at this revision instead of upstream') |
Mike Frysinger | d1e4fa7 | 2021-03-23 20:27:29 -0400 | [diff] [blame] | 45 | p.add_option('--head', '--HEAD', |
| 46 | dest='revision', action='store_const', const='HEAD', |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 47 | help='abbreviation for --rev HEAD') |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 48 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 49 | def ValidateOptions(self, opt, args): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | if not args: |
| 51 | self.Usage() |
| 52 | |
| 53 | nb = args[0] |
| 54 | if not git.check_ref_format('heads/%s' % nb): |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 55 | self.OptionParser.error("'%s' is not a valid name" % nb) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 57 | def _ExecuteOne(self, revision, nb, project): |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 58 | """Start one project.""" |
| 59 | # If the current revision is immutable, such as a SHA1, a tag or |
| 60 | # a change, then we can't push back to it. Substitute with |
| 61 | # dest_branch, if defined; or with manifest default revision instead. |
| 62 | branch_merge = '' |
| 63 | if IsImmutable(project.revisionExpr): |
| 64 | if project.dest_branch: |
| 65 | branch_merge = project.dest_branch |
| 66 | else: |
| 67 | branch_merge = self.manifest.default.revisionExpr |
| 68 | |
| 69 | try: |
| 70 | ret = project.StartBranch( |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 71 | nb, branch_merge=branch_merge, revision=revision) |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 72 | except Exception as e: |
| 73 | print('error: unable to checkout %s: %s' % (project.name, e), file=sys.stderr) |
| 74 | ret = False |
| 75 | return (ret, project) |
| 76 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 77 | def Execute(self, opt, args): |
| 78 | nb = args[0] |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 79 | err = [] |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 80 | projects = [] |
| 81 | if not opt.all: |
| 82 | projects = args[1:] |
| 83 | if len(projects) < 1: |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 84 | projects = ['.'] # start it in the local project by default |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 85 | |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 86 | all_projects = self.GetProjects(projects, |
| 87 | missing_ok=bool(self.gitc_manifest)) |
| 88 | |
| 89 | # This must happen after we find all_projects, since GetProjects may need |
| 90 | # the local directory, which will disappear once we save the GITC manifest. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 91 | if self.gitc_manifest: |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 92 | gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest, |
| 93 | missing_ok=True) |
| 94 | for project in gitc_projects: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 95 | if project.old_revision: |
| 96 | project.already_synced = True |
| 97 | else: |
| 98 | project.already_synced = False |
| 99 | project.old_revision = project.revisionExpr |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 100 | project.revisionExpr = None |
| 101 | # Save the GITC manifest. |
| 102 | gitc_utils.save_manifest(self.gitc_manifest) |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 103 | |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 104 | # Make sure we have a valid CWD |
| 105 | if not os.path.exists(os.getcwd()): |
| 106 | os.chdir(self.manifest.topdir) |
| 107 | |
Mike Frysinger | 151701e | 2021-04-13 15:07:21 -0400 | [diff] [blame] | 108 | pm = Progress('Syncing %s' % nb, len(all_projects), quiet=opt.quiet) |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 109 | for project in all_projects: |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 110 | gitc_project = self.gitc_manifest.paths[project.relpath] |
| 111 | # Sync projects that have not been opened. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 112 | if not gitc_project.already_synced: |
| 113 | proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir, |
| 114 | project.relpath) |
| 115 | project.worktree = proj_localdir |
| 116 | if not os.path.exists(proj_localdir): |
| 117 | os.makedirs(proj_localdir) |
| 118 | project.Sync_NetworkHalf() |
| 119 | sync_buf = SyncBuffer(self.manifest.manifestProject.config) |
| 120 | project.Sync_LocalHalf(sync_buf) |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 121 | project.revisionId = gitc_project.old_revision |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 122 | pm.update() |
| 123 | pm.end() |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 124 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 125 | def _ProcessResults(_pool, pm, results): |
Mike Frysinger | 8dbc07a | 2021-02-18 23:37:33 -0500 | [diff] [blame] | 126 | for (result, project) in results: |
| 127 | if not result: |
| 128 | err.append(project) |
| 129 | pm.update() |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 130 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 131 | self.ExecuteInParallel( |
| 132 | opt.jobs, |
| 133 | functools.partial(self._ExecuteOne, opt.revision, nb), |
| 134 | all_projects, |
| 135 | callback=_ProcessResults, |
| 136 | output=Progress('Starting %s' % (nb,), len(all_projects), quiet=opt.quiet)) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 137 | |
| 138 | if err: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 139 | for p in err: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 140 | print("error: %s/: cannot start %s" % (p.relpath, nb), |
| 141 | file=sys.stderr) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 142 | sys.exit(1) |