blob: 768b60271c0562c0120e3982509d9f0a1caad12a [file] [log] [blame]
Wink Saville02d79452009-04-10 13:01:24 -07001# Copyright (C) 2009 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Mike Frysinger6093d992021-02-24 12:17:31 -050015import functools
Wink Saville02d79452009-04-10 13:01:24 -070016import sys
Mike Frysinger6093d992021-02-24 12:17:31 -050017
Mike Frysingerb5d075d2021-03-01 00:56:38 -050018from command import Command, DEFAULT_LOCAL_JOBS
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070019from progress import Progress
Wink Saville02d79452009-04-10 13:01:24 -070020
David Pursehouse819827a2020-02-12 15:20:19 +090021
Wink Saville02d79452009-04-10 13:01:24 -070022class Checkout(Command):
Mike Frysinger4f210542021-06-14 16:05:19 -040023 COMMON = True
Wink Saville02d79452009-04-10 13:01:24 -070024 helpSummary = "Checkout a branch for development"
25 helpUsage = """
26%prog <branchname> [<project>...]
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070027"""
28 helpDescription = """
29The '%prog' command checks out an existing branch that was previously
30created by 'repo start'.
Wink Saville02d79452009-04-10 13:01:24 -070031
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070032The command is equivalent to:
Wink Saville02d79452009-04-10 13:01:24 -070033
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070034 repo forall [<project>...] -c git checkout <branchname>
Wink Saville02d79452009-04-10 13:01:24 -070035"""
Mike Frysinger6093d992021-02-24 12:17:31 -050036 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
Wink Saville02d79452009-04-10 13:01:24 -070037
Mike Frysingerae6cb082019-08-27 01:10:59 -040038 def ValidateOptions(self, opt, args):
Wink Saville02d79452009-04-10 13:01:24 -070039 if not args:
40 self.Usage()
41
Mike Frysinger6093d992021-02-24 12:17:31 -050042 def _ExecuteOne(self, nb, project):
43 """Checkout one project."""
44 return (project.CheckoutBranch(nb), project)
45
Mike Frysingerae6cb082019-08-27 01:10:59 -040046 def Execute(self, opt, args):
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070047 nb = args[0]
48 err = []
Doug Anderson3ba5f952011-04-07 12:51:04 -070049 success = []
LaMont Jonescc879a92021-11-18 22:40:18 +000050 all_projects = self.GetProjects(args[1:], all_manifests=not opt.this_manifest_only)
Wink Saville02d79452009-04-10 13:01:24 -070051
Mike Frysingerb5d075d2021-03-01 00:56:38 -050052 def _ProcessResults(_pool, pm, results):
Mike Frysinger6093d992021-02-24 12:17:31 -050053 for status, project in results:
54 if status is not None:
55 if status:
56 success.append(project)
57 else:
58 err.append(project)
59 pm.update()
Doug Anderson3ba5f952011-04-07 12:51:04 -070060
Mike Frysingerb5d075d2021-03-01 00:56:38 -050061 self.ExecuteInParallel(
62 opt.jobs,
63 functools.partial(self._ExecuteOne, nb),
64 all_projects,
65 callback=_ProcessResults,
66 output=Progress('Checkout %s' % (nb,), len(all_projects), quiet=opt.quiet))
Wink Saville02d79452009-04-10 13:01:24 -070067
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070068 if err:
Doug Anderson3ba5f952011-04-07 12:51:04 -070069 for p in err:
Sarah Owenscecd1d82012-11-01 22:59:27 -070070 print("error: %s/: cannot checkout %s" % (p.relpath, nb),
71 file=sys.stderr)
Doug Anderson3ba5f952011-04-07 12:51:04 -070072 sys.exit(1)
73 elif not success:
Sarah Owenscecd1d82012-11-01 22:59:27 -070074 print('error: no project has branch %s' % nb, file=sys.stderr)
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070075 sys.exit(1)