blob: 5d4c9c015d555e90b3ba70287a3f4c8c0e99fb33 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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 Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Simran Basib9a1b732015-08-20 12:19:28 -070018import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
Simran Basib9a1b732015-08-20 12:19:28 -070020
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021from command import Command
Zac Livingston9ead97b2017-06-13 08:29:04 -060022from git_config import IsImmutable
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from git_command import git
Simran Basib9a1b732015-08-20 12:19:28 -070024import gitc_utils
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070025from progress import Progress
Simran Basib9a1b732015-08-20 12:19:28 -070026from project import SyncBuffer
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
28class Start(Command):
29 common = True
30 helpSummary = "Start a new branch for development"
31 helpUsage = """
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070032%prog <newbranchname> [--all | <project>...]
Shawn O. Pearce06e556d2009-04-18 11:19:01 -070033"""
34 helpDescription = """
35'%prog' begins a new branch of development, starting from the
36revision specified in the manifest.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037"""
38
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070039 def _Options(self, p):
40 p.add_option('--all',
41 dest='all', action='store_true',
42 help='begin branch in all projects')
43
Mike Frysingerae6cb082019-08-27 01:10:59 -040044 def ValidateOptions(self, opt, args):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070045 if not args:
46 self.Usage()
47
48 nb = args[0]
49 if not git.check_ref_format('heads/%s' % nb):
Mike Frysingerae6cb082019-08-27 01:10:59 -040050 self.OptionParser.error("'%s' is not a valid name" % nb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051
Mike Frysingerae6cb082019-08-27 01:10:59 -040052 def Execute(self, opt, args):
53 nb = args[0]
Shawn O. Pearce0a389e92009-04-10 16:21:18 -070054 err = []
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070055 projects = []
56 if not opt.all:
57 projects = args[1:]
58 if len(projects) < 1:
Vadim Bendeburye5c0ea02014-09-05 10:26:31 -070059 projects = ['.',] # start it in the local project by default
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070060
Dan Willemsen04197a52015-10-07 16:53:10 -070061 all_projects = self.GetProjects(projects,
62 missing_ok=bool(self.gitc_manifest))
63
64 # This must happen after we find all_projects, since GetProjects may need
65 # the local directory, which will disappear once we save the GITC manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070066 if self.gitc_manifest:
Dan Willemsen04197a52015-10-07 16:53:10 -070067 gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
68 missing_ok=True)
69 for project in gitc_projects:
Simran Basib9a1b732015-08-20 12:19:28 -070070 if project.old_revision:
71 project.already_synced = True
72 else:
73 project.already_synced = False
74 project.old_revision = project.revisionExpr
Simran Basib9a1b732015-08-20 12:19:28 -070075 project.revisionExpr = None
76 # Save the GITC manifest.
77 gitc_utils.save_manifest(self.gitc_manifest)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070078
Dan Willemsen04197a52015-10-07 16:53:10 -070079 # Make sure we have a valid CWD
80 if not os.path.exists(os.getcwd()):
81 os.chdir(self.manifest.topdir)
82
David Pursehouse8a68ff92012-09-24 12:15:13 +090083 pm = Progress('Starting %s' % nb, len(all_projects))
84 for project in all_projects:
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070085 pm.update()
Dan Willemsen5ea32d12015-09-08 13:27:20 -070086
Simran Basib9a1b732015-08-20 12:19:28 -070087 if self.gitc_manifest:
Dan Willemsen5ea32d12015-09-08 13:27:20 -070088 gitc_project = self.gitc_manifest.paths[project.relpath]
89 # Sync projects that have not been opened.
Simran Basib9a1b732015-08-20 12:19:28 -070090 if not gitc_project.already_synced:
91 proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
92 project.relpath)
93 project.worktree = proj_localdir
94 if not os.path.exists(proj_localdir):
95 os.makedirs(proj_localdir)
96 project.Sync_NetworkHalf()
97 sync_buf = SyncBuffer(self.manifest.manifestProject.config)
98 project.Sync_LocalHalf(sync_buf)
Dan Willemsen5ea32d12015-09-08 13:27:20 -070099 project.revisionId = gitc_project.old_revision
Simran Basib9a1b732015-08-20 12:19:28 -0700100
Zac Livingston9ead97b2017-06-13 08:29:04 -0600101 # If the current revision is immutable, such as a SHA1, a tag or
102 # a change, then we can't push back to it. Substitute with
103 # dest_branch, if defined; or with manifest default revision instead.
Simran Basib9a1b732015-08-20 12:19:28 -0700104 branch_merge = ''
Zac Livingston9ead97b2017-06-13 08:29:04 -0600105 if IsImmutable(project.revisionExpr):
Max Liu5fb8ed22014-06-23 14:48:16 +0800106 if project.dest_branch:
Simran Basib9a1b732015-08-20 12:19:28 -0700107 branch_merge = project.dest_branch
Max Liu5fb8ed22014-06-23 14:48:16 +0800108 else:
Simran Basib9a1b732015-08-20 12:19:28 -0700109 branch_merge = self.manifest.default.revisionExpr
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700110
Simran Basib9a1b732015-08-20 12:19:28 -0700111 if not project.StartBranch(nb, branch_merge=branch_merge):
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700112 err.append(project)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700113 pm.end()
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700114
115 if err:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700116 for p in err:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700117 print("error: %s/: cannot start %s" % (p.relpath, nb),
118 file=sys.stderr)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700119 sys.exit(1)