Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 18 | import os |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import sys |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 20 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | from command import Command |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 22 | from git_config import IsImmutable |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | from git_command import git |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 24 | import gitc_utils |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 25 | from progress import Progress |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 26 | from project import SyncBuffer |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame^] | 28 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | class Start(Command): |
| 30 | common = True |
| 31 | helpSummary = "Start a new branch for development" |
| 32 | helpUsage = """ |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 33 | %prog <newbranchname> [--all | <project>...] |
Shawn O. Pearce | 06e556d | 2009-04-18 11:19:01 -0700 | [diff] [blame] | 34 | """ |
| 35 | helpDescription = """ |
| 36 | '%prog' begins a new branch of development, starting from the |
| 37 | revision specified in the manifest. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | """ |
| 39 | |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 40 | def _Options(self, p): |
| 41 | p.add_option('--all', |
| 42 | dest='all', action='store_true', |
| 43 | help='begin branch in all projects') |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 44 | p.add_option('-r', '--rev', '--revision', dest='revision', |
| 45 | help='point branch at this revision instead of upstream') |
| 46 | p.add_option('--head', dest='revision', action='store_const', const='HEAD', |
| 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 | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 57 | def Execute(self, opt, args): |
| 58 | nb = args[0] |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 59 | err = [] |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 60 | projects = [] |
| 61 | if not opt.all: |
| 62 | projects = args[1:] |
| 63 | if len(projects) < 1: |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 64 | projects = ['.'] # start it in the local project by default |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 65 | |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 66 | all_projects = self.GetProjects(projects, |
| 67 | missing_ok=bool(self.gitc_manifest)) |
| 68 | |
| 69 | # This must happen after we find all_projects, since GetProjects may need |
| 70 | # the local directory, which will disappear once we save the GITC manifest. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 71 | if self.gitc_manifest: |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 72 | gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest, |
| 73 | missing_ok=True) |
| 74 | for project in gitc_projects: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 75 | if project.old_revision: |
| 76 | project.already_synced = True |
| 77 | else: |
| 78 | project.already_synced = False |
| 79 | project.old_revision = project.revisionExpr |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 80 | project.revisionExpr = None |
| 81 | # Save the GITC manifest. |
| 82 | gitc_utils.save_manifest(self.gitc_manifest) |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 83 | |
Dan Willemsen | 04197a5 | 2015-10-07 16:53:10 -0700 | [diff] [blame] | 84 | # Make sure we have a valid CWD |
| 85 | if not os.path.exists(os.getcwd()): |
| 86 | os.chdir(self.manifest.topdir) |
| 87 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 88 | pm = Progress('Starting %s' % nb, len(all_projects)) |
| 89 | for project in all_projects: |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 90 | pm.update() |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 91 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 92 | if self.gitc_manifest: |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 93 | gitc_project = self.gitc_manifest.paths[project.relpath] |
| 94 | # Sync projects that have not been opened. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 95 | if not gitc_project.already_synced: |
| 96 | proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir, |
| 97 | project.relpath) |
| 98 | project.worktree = proj_localdir |
| 99 | if not os.path.exists(proj_localdir): |
| 100 | os.makedirs(proj_localdir) |
| 101 | project.Sync_NetworkHalf() |
| 102 | sync_buf = SyncBuffer(self.manifest.manifestProject.config) |
| 103 | project.Sync_LocalHalf(sync_buf) |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 104 | project.revisionId = gitc_project.old_revision |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 105 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 106 | # If the current revision is immutable, such as a SHA1, a tag or |
| 107 | # a change, then we can't push back to it. Substitute with |
| 108 | # dest_branch, if defined; or with manifest default revision instead. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 109 | branch_merge = '' |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 110 | if IsImmutable(project.revisionExpr): |
Max Liu | 5fb8ed2 | 2014-06-23 14:48:16 +0800 | [diff] [blame] | 111 | if project.dest_branch: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 112 | branch_merge = project.dest_branch |
Max Liu | 5fb8ed2 | 2014-06-23 14:48:16 +0800 | [diff] [blame] | 113 | else: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 114 | branch_merge = self.manifest.default.revisionExpr |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 115 | |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 116 | if not project.StartBranch( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 117 | nb, branch_merge=branch_merge, revision=opt.revision): |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 118 | err.append(project) |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 119 | pm.end() |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 120 | |
| 121 | if err: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 122 | for p in err: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 123 | print("error: %s/: cannot start %s" % (p.relpath, nb), |
| 124 | file=sys.stderr) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 125 | sys.exit(1) |