2009-04-10 20:01:24 +00:00
|
|
|
# Copyright (C) 2009 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-24 17:17:31 +00:00
|
|
|
import functools
|
2023-08-08 21:12:53 +00:00
|
|
|
from typing import NamedTuple
|
2023-08-22 01:20:32 +00:00
|
|
|
|
|
|
|
from command import Command
|
|
|
|
from command import DEFAULT_LOCAL_JOBS
|
|
|
|
from error import GitError
|
|
|
|
from error import RepoExitError
|
2009-04-18 22:04:41 +00:00
|
|
|
from progress import Progress
|
2023-09-14 08:46:44 +00:00
|
|
|
from repo_logging import RepoLogger
|
|
|
|
|
|
|
|
|
|
|
|
logger = RepoLogger(__file__)
|
2023-08-08 21:12:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CheckoutBranchResult(NamedTuple):
|
|
|
|
# Whether the Project is on the branch (i.e. branch exists and no errors)
|
|
|
|
result: bool
|
2024-10-22 13:04:41 +00:00
|
|
|
project_idx: int
|
2023-08-08 21:12:53 +00:00
|
|
|
error: Exception
|
|
|
|
|
|
|
|
|
|
|
|
class CheckoutCommandError(RepoExitError):
|
|
|
|
"""Exception thrown when checkout command fails."""
|
|
|
|
|
|
|
|
|
|
|
|
class MissingBranchError(RepoExitError):
|
|
|
|
"""Exception thrown when no project has specified branch."""
|
2009-04-10 20:01:24 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2009-04-10 20:01:24 +00:00
|
|
|
class Checkout(Command):
|
2023-03-11 06:46:20 +00:00
|
|
|
COMMON = True
|
|
|
|
helpSummary = "Checkout a branch for development"
|
|
|
|
helpUsage = """
|
2009-04-10 20:01:24 +00:00
|
|
|
%prog <branchname> [<project>...]
|
2009-04-13 19:11:31 +00:00
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
helpDescription = """
|
2009-04-13 19:11:31 +00:00
|
|
|
The '%prog' command checks out an existing branch that was previously
|
|
|
|
created by 'repo start'.
|
2009-04-10 20:01:24 +00:00
|
|
|
|
2009-04-13 19:11:31 +00:00
|
|
|
The command is equivalent to:
|
2009-04-10 20:01:24 +00:00
|
|
|
|
2009-04-13 19:11:31 +00:00
|
|
|
repo forall [<project>...] -c git checkout <branchname>
|
2009-04-10 20:01:24 +00:00
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
2009-04-10 20:01:24 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
|
|
|
if not args:
|
|
|
|
self.Usage()
|
2009-04-10 20:01:24 +00:00
|
|
|
|
2024-10-22 13:04:41 +00:00
|
|
|
@classmethod
|
|
|
|
def _ExecuteOne(cls, nb, project_idx):
|
2023-03-11 06:46:20 +00:00
|
|
|
"""Checkout one project."""
|
2023-08-08 21:12:53 +00:00
|
|
|
error = None
|
|
|
|
result = None
|
2024-10-22 13:04:41 +00:00
|
|
|
project = cls.get_parallel_context()["projects"][project_idx]
|
2023-08-08 21:12:53 +00:00
|
|
|
try:
|
|
|
|
result = project.CheckoutBranch(nb)
|
|
|
|
except GitError as e:
|
|
|
|
error = e
|
2024-10-22 13:04:41 +00:00
|
|
|
return CheckoutBranchResult(result, project_idx, error)
|
2021-02-24 17:17:31 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def Execute(self, opt, args):
|
|
|
|
nb = args[0]
|
|
|
|
err = []
|
2023-08-08 21:12:53 +00:00
|
|
|
err_projects = []
|
2023-03-11 06:46:20 +00:00
|
|
|
success = []
|
|
|
|
all_projects = self.GetProjects(
|
|
|
|
args[1:], all_manifests=not opt.this_manifest_only
|
|
|
|
)
|
2009-04-18 22:04:41 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def _ProcessResults(_pool, pm, results):
|
2023-08-08 21:12:53 +00:00
|
|
|
for result in results:
|
2024-10-22 13:04:41 +00:00
|
|
|
project = all_projects[result.project_idx]
|
2023-08-08 21:12:53 +00:00
|
|
|
if result.error is not None:
|
|
|
|
err.append(result.error)
|
2024-10-22 13:04:41 +00:00
|
|
|
err_projects.append(project)
|
2023-08-08 21:12:53 +00:00
|
|
|
elif result.result:
|
2024-10-22 13:04:41 +00:00
|
|
|
success.append(project)
|
2023-05-04 04:48:43 +00:00
|
|
|
pm.update(msg="")
|
2011-04-07 19:51:04 +00:00
|
|
|
|
2024-10-22 13:04:41 +00:00
|
|
|
with self.ParallelContext():
|
|
|
|
self.get_parallel_context()["projects"] = all_projects
|
|
|
|
self.ExecuteInParallel(
|
|
|
|
opt.jobs,
|
|
|
|
functools.partial(self._ExecuteOne, nb),
|
|
|
|
range(len(all_projects)),
|
|
|
|
callback=_ProcessResults,
|
|
|
|
output=Progress(
|
|
|
|
f"Checkout {nb}", len(all_projects), quiet=opt.quiet
|
|
|
|
),
|
|
|
|
)
|
2009-04-18 22:04:41 +00:00
|
|
|
|
2023-08-08 21:12:53 +00:00
|
|
|
if err_projects:
|
|
|
|
for p in err_projects:
|
2023-09-14 08:46:44 +00:00
|
|
|
logger.error("error: %s/: cannot checkout %s", p.relpath, nb)
|
2023-08-08 21:12:53 +00:00
|
|
|
raise CheckoutCommandError(aggregate_errors=err)
|
2023-03-11 06:46:20 +00:00
|
|
|
elif not success:
|
2023-08-08 21:12:53 +00:00
|
|
|
msg = f"error: no project has branch {nb}"
|
2023-09-14 08:46:44 +00:00
|
|
|
logger.error(msg)
|
2023-08-08 21:12:53 +00:00
|
|
|
raise MissingBranchError(msg)
|