2008-10-21 14:00:00 +00:00
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-02-19 04:37:33 +00:00
|
|
|
import functools
|
2023-08-22 01:20:32 +00:00
|
|
|
from typing import NamedTuple
|
2015-08-20 19:19:28 +00:00
|
|
|
|
2023-08-22 01:20:32 +00:00
|
|
|
from command import Command
|
|
|
|
from command import DEFAULT_LOCAL_JOBS
|
|
|
|
from error import RepoExitError
|
2008-10-21 14:00:00 +00:00
|
|
|
from git_command import git
|
2023-08-22 01:20:32 +00:00
|
|
|
from git_config import IsImmutable
|
2009-04-18 21:53:39 +00:00
|
|
|
from progress import Progress
|
2023-05-26 19:44:50 +00:00
|
|
|
from project import Project
|
2023-09-06 18:01:20 +00:00
|
|
|
from repo_logging import RepoLogger
|
|
|
|
|
|
|
|
|
|
|
|
logger = RepoLogger(__file__)
|
2023-08-08 21:12:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExecuteOneResult(NamedTuple):
|
|
|
|
project: Project
|
|
|
|
error: Exception
|
|
|
|
|
|
|
|
|
|
|
|
class StartError(RepoExitError):
|
|
|
|
"""Exit error for failed start command."""
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
class Start(Command):
|
2023-03-11 06:46:20 +00:00
|
|
|
COMMON = True
|
|
|
|
helpSummary = "Start a new branch for development"
|
|
|
|
helpUsage = """
|
2009-04-23 00:27:12 +00:00
|
|
|
%prog <newbranchname> [--all | <project>...]
|
2009-04-18 18:19:01 +00:00
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
helpDescription = """
|
2009-04-18 18:19:01 +00:00
|
|
|
'%prog' begins a new branch of development, starting from the
|
|
|
|
revision specified in the manifest.
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
|
|
|
|
|
|
|
def _Options(self, p):
|
|
|
|
p.add_option(
|
|
|
|
"--all",
|
|
|
|
dest="all",
|
|
|
|
action="store_true",
|
|
|
|
help="begin branch in all projects",
|
|
|
|
)
|
|
|
|
p.add_option(
|
|
|
|
"-r",
|
|
|
|
"--rev",
|
|
|
|
"--revision",
|
|
|
|
dest="revision",
|
|
|
|
help="point branch at this revision instead of upstream",
|
|
|
|
)
|
|
|
|
p.add_option(
|
|
|
|
"--head",
|
|
|
|
"--HEAD",
|
|
|
|
dest="revision",
|
|
|
|
action="store_const",
|
|
|
|
const="HEAD",
|
|
|
|
help="abbreviation for --rev HEAD",
|
|
|
|
)
|
|
|
|
|
|
|
|
def ValidateOptions(self, opt, args):
|
|
|
|
if not args:
|
|
|
|
self.Usage()
|
|
|
|
|
|
|
|
nb = args[0]
|
|
|
|
if not git.check_ref_format("heads/%s" % nb):
|
|
|
|
self.OptionParser.error("'%s' is not a valid name" % nb)
|
|
|
|
|
|
|
|
def _ExecuteOne(self, revision, nb, project):
|
|
|
|
"""Start one project."""
|
|
|
|
# If the current revision is immutable, such as a SHA1, a tag or
|
|
|
|
# a change, then we can't push back to it. Substitute with
|
|
|
|
# dest_branch, if defined; or with manifest default revision instead.
|
|
|
|
branch_merge = ""
|
2023-08-08 21:12:53 +00:00
|
|
|
error = None
|
2023-03-11 06:46:20 +00:00
|
|
|
if IsImmutable(project.revisionExpr):
|
|
|
|
if project.dest_branch:
|
|
|
|
branch_merge = project.dest_branch
|
|
|
|
else:
|
|
|
|
branch_merge = self.manifest.default.revisionExpr
|
|
|
|
|
|
|
|
try:
|
2023-08-08 21:12:53 +00:00
|
|
|
project.StartBranch(
|
2023-03-11 06:46:20 +00:00
|
|
|
nb, branch_merge=branch_merge, revision=revision
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2023-09-06 18:01:20 +00:00
|
|
|
logger.error("error: unable to checkout %s: %s", project.name, e)
|
2023-08-08 21:12:53 +00:00
|
|
|
error = e
|
|
|
|
return ExecuteOneResult(project, error)
|
2023-03-11 06:46:20 +00:00
|
|
|
|
|
|
|
def Execute(self, opt, args):
|
|
|
|
nb = args[0]
|
2023-08-08 21:12:53 +00:00
|
|
|
err_projects = []
|
2023-03-11 06:46:20 +00:00
|
|
|
err = []
|
|
|
|
projects = []
|
|
|
|
if not opt.all:
|
|
|
|
projects = args[1:]
|
|
|
|
if len(projects) < 1:
|
|
|
|
projects = ["."] # start it in the local project by default
|
|
|
|
|
|
|
|
all_projects = self.GetProjects(
|
|
|
|
projects,
|
|
|
|
all_manifests=not opt.this_manifest_only,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _ProcessResults(_pool, pm, results):
|
2023-08-08 21:12:53 +00:00
|
|
|
for result in results:
|
|
|
|
if result.error:
|
|
|
|
err_projects.append(result.project)
|
|
|
|
err.append(result.error)
|
2023-05-04 04:48:43 +00:00
|
|
|
pm.update(msg="")
|
2023-03-11 06:46:20 +00:00
|
|
|
|
|
|
|
self.ExecuteInParallel(
|
|
|
|
opt.jobs,
|
|
|
|
functools.partial(self._ExecuteOne, opt.revision, nb),
|
|
|
|
all_projects,
|
|
|
|
callback=_ProcessResults,
|
|
|
|
output=Progress(
|
2023-09-29 15:04:49 +00:00
|
|
|
f"Starting {nb}", len(all_projects), quiet=opt.quiet
|
2023-03-11 06:46:20 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-08-08 21:12:53 +00:00
|
|
|
if err_projects:
|
|
|
|
for p in err_projects:
|
2023-09-06 18:01:20 +00:00
|
|
|
logger.error(
|
|
|
|
"error: %s/: cannot start %s",
|
|
|
|
p.RelPath(local=opt.this_manifest_only),
|
|
|
|
nb,
|
2023-03-11 06:46:20 +00:00
|
|
|
)
|
2023-05-13 00:00:46 +00:00
|
|
|
msg_fmt = "cannot start %d project(s)"
|
2023-08-08 21:12:53 +00:00
|
|
|
self.git_event_log.ErrorEvent(
|
|
|
|
msg_fmt % (len(err_projects)), msg_fmt
|
|
|
|
)
|
|
|
|
raise StartError(aggregate_errors=err)
|