blob: c3ec303e8f6fd56ee845bbef1c04cb61dd117166 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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 Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Simran Basib9a1b732015-08-20 12:19:28 -070017import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
Simran Basib9a1b732015-08-20 12:19:28 -070019
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020from command import Command
Zac Livingston9ead97b2017-06-13 08:29:04 -060021from git_config import IsImmutable
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from git_command import git
Simran Basib9a1b732015-08-20 12:19:28 -070023import gitc_utils
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070024from progress import Progress
Simran Basib9a1b732015-08-20 12:19:28 -070025from project import SyncBuffer
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
27class Start(Command):
28 common = True
29 helpSummary = "Start a new branch for development"
30 helpUsage = """
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070031%prog <newbranchname> [--all | <project>...]
Shawn O. Pearce06e556d2009-04-18 11:19:01 -070032"""
33 helpDescription = """
34'%prog' begins a new branch of development, starting from the
35revision specified in the manifest.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036"""
37
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070038 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 Projectcf31fe92008-10-21 07:00:00 -070043 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 Owenscecd1d82012-11-01 22:59:27 -070049 print("error: '%s' is not a valid name" % nb, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070050 sys.exit(1)
51
Shawn O. Pearce0a389e92009-04-10 16:21:18 -070052 err = []
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070053 projects = []
54 if not opt.all:
55 projects = args[1:]
56 if len(projects) < 1:
Vadim Bendeburye5c0ea02014-09-05 10:26:31 -070057 projects = ['.',] # start it in the local project by default
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070058
Dan Willemsen04197a52015-10-07 16:53:10 -070059 all_projects = self.GetProjects(projects,
60 missing_ok=bool(self.gitc_manifest))
61
62 # This must happen after we find all_projects, since GetProjects may need
63 # the local directory, which will disappear once we save the GITC manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070064 if self.gitc_manifest:
Dan Willemsen04197a52015-10-07 16:53:10 -070065 gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
66 missing_ok=True)
67 for project in gitc_projects:
Simran Basib9a1b732015-08-20 12:19:28 -070068 if project.old_revision:
69 project.already_synced = True
70 else:
71 project.already_synced = False
72 project.old_revision = project.revisionExpr
Simran Basib9a1b732015-08-20 12:19:28 -070073 project.revisionExpr = None
74 # Save the GITC manifest.
75 gitc_utils.save_manifest(self.gitc_manifest)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070076
Dan Willemsen04197a52015-10-07 16:53:10 -070077 # Make sure we have a valid CWD
78 if not os.path.exists(os.getcwd()):
79 os.chdir(self.manifest.topdir)
80
David Pursehouse8a68ff92012-09-24 12:15:13 +090081 pm = Progress('Starting %s' % nb, len(all_projects))
82 for project in all_projects:
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070083 pm.update()
Dan Willemsen5ea32d12015-09-08 13:27:20 -070084
Simran Basib9a1b732015-08-20 12:19:28 -070085 if self.gitc_manifest:
Dan Willemsen5ea32d12015-09-08 13:27:20 -070086 gitc_project = self.gitc_manifest.paths[project.relpath]
87 # Sync projects that have not been opened.
Simran Basib9a1b732015-08-20 12:19:28 -070088 if not gitc_project.already_synced:
89 proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
90 project.relpath)
91 project.worktree = proj_localdir
92 if not os.path.exists(proj_localdir):
93 os.makedirs(proj_localdir)
94 project.Sync_NetworkHalf()
95 sync_buf = SyncBuffer(self.manifest.manifestProject.config)
96 project.Sync_LocalHalf(sync_buf)
Dan Willemsen5ea32d12015-09-08 13:27:20 -070097 project.revisionId = gitc_project.old_revision
Simran Basib9a1b732015-08-20 12:19:28 -070098
Zac Livingston9ead97b2017-06-13 08:29:04 -060099 # If the current revision is immutable, such as a SHA1, a tag or
100 # a change, then we can't push back to it. Substitute with
101 # dest_branch, if defined; or with manifest default revision instead.
Simran Basib9a1b732015-08-20 12:19:28 -0700102 branch_merge = ''
Zac Livingston9ead97b2017-06-13 08:29:04 -0600103 if IsImmutable(project.revisionExpr):
Max Liu5fb8ed22014-06-23 14:48:16 +0800104 if project.dest_branch:
Simran Basib9a1b732015-08-20 12:19:28 -0700105 branch_merge = project.dest_branch
Max Liu5fb8ed22014-06-23 14:48:16 +0800106 else:
Simran Basib9a1b732015-08-20 12:19:28 -0700107 branch_merge = self.manifest.default.revisionExpr
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700108
Simran Basib9a1b732015-08-20 12:19:28 -0700109 if not project.StartBranch(nb, branch_merge=branch_merge):
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700110 err.append(project)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700111 pm.end()
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700112
113 if err:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700114 for p in err:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700115 print("error: %s/: cannot start %s" % (p.relpath, nb),
116 file=sys.stderr)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700117 sys.exit(1)