The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 17 | import os |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import sys |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 19 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | from command import Command |
Chad Jones | 87636f2 | 2012-06-14 16:53:40 -0700 | [diff] [blame] | 21 | from git_config import IsId |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | from git_command import git |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 23 | import gitc_utils |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 24 | from progress import Progress |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 25 | from project import SyncBuffer |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
| 27 | class Start(Command): |
| 28 | common = True |
| 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 | """ |
| 37 | |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 38 | def _Options(self, p): |
| 39 | p.add_option('--all', |
| 40 | dest='all', action='store_true', |
| 41 | help='begin branch in all projects') |
| 42 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | def Execute(self, opt, args): |
| 44 | if not args: |
| 45 | self.Usage() |
| 46 | |
| 47 | nb = args[0] |
| 48 | if not git.check_ref_format('heads/%s' % nb): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 49 | print("error: '%s' is not a valid name" % nb, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | sys.exit(1) |
| 51 | |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 52 | err = [] |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 53 | projects = [] |
| 54 | if not opt.all: |
| 55 | projects = args[1:] |
| 56 | if len(projects) < 1: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 57 | print("error: at least one project must be specified", file=sys.stderr) |
Ficus Kirkpatrick | 6f6cd77 | 2009-04-22 17:27:12 -0700 | [diff] [blame] | 58 | sys.exit(1) |
| 59 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 60 | if self.gitc_manifest: |
| 61 | all_projects = self.GetProjects(projects, manifest=self.gitc_manifest, |
| 62 | missing_ok=True) |
| 63 | for project in all_projects: |
| 64 | if project.old_revision: |
| 65 | project.already_synced = True |
| 66 | else: |
| 67 | project.already_synced = False |
| 68 | project.old_revision = project.revisionExpr |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 69 | project.revisionExpr = None |
| 70 | # Save the GITC manifest. |
| 71 | gitc_utils.save_manifest(self.gitc_manifest) |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 72 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 73 | all_projects = self.GetProjects(projects, |
| 74 | missing_ok=bool(self.gitc_manifest)) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 75 | pm = Progress('Starting %s' % nb, len(all_projects)) |
| 76 | for project in all_projects: |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 77 | pm.update() |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame^] | 78 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 79 | if self.gitc_manifest: |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame^] | 80 | gitc_project = self.gitc_manifest.paths[project.relpath] |
| 81 | # Sync projects that have not been opened. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 82 | if not gitc_project.already_synced: |
| 83 | proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir, |
| 84 | project.relpath) |
| 85 | project.worktree = proj_localdir |
| 86 | if not os.path.exists(proj_localdir): |
| 87 | os.makedirs(proj_localdir) |
| 88 | project.Sync_NetworkHalf() |
| 89 | sync_buf = SyncBuffer(self.manifest.manifestProject.config) |
| 90 | project.Sync_LocalHalf(sync_buf) |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame^] | 91 | project.revisionId = gitc_project.old_revision |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 92 | |
Chad Jones | 87636f2 | 2012-06-14 16:53:40 -0700 | [diff] [blame] | 93 | # If the current revision is a specific SHA1 then we can't push back |
Max Liu | 5fb8ed2 | 2014-06-23 14:48:16 +0800 | [diff] [blame] | 94 | # to it; so substitute with dest_branch if defined, or with manifest |
| 95 | # default revision instead. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 96 | branch_merge = '' |
Chad Jones | 87636f2 | 2012-06-14 16:53:40 -0700 | [diff] [blame] | 97 | if IsId(project.revisionExpr): |
Max Liu | 5fb8ed2 | 2014-06-23 14:48:16 +0800 | [diff] [blame] | 98 | if project.dest_branch: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 99 | branch_merge = project.dest_branch |
Max Liu | 5fb8ed2 | 2014-06-23 14:48:16 +0800 | [diff] [blame] | 100 | else: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 101 | branch_merge = self.manifest.default.revisionExpr |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame^] | 102 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 103 | if not project.StartBranch(nb, branch_merge=branch_merge): |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 104 | err.append(project) |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 105 | pm.end() |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 106 | |
| 107 | if err: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 108 | for p in err: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 109 | print("error: %s/: cannot start %s" % (p.relpath, nb), |
| 110 | file=sys.stderr) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 111 | sys.exit(1) |