blob: 6b71a8fad71257bacc867136940cdb2387d4e419 [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
16import multiprocessing
Wink Saville02d79452009-04-10 13:01:24 -070017import sys
Mike Frysinger6093d992021-02-24 12:17:31 -050018
19from command import Command, DEFAULT_LOCAL_JOBS, WORKER_BATCH_SIZE
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070020from progress import Progress
Wink Saville02d79452009-04-10 13:01:24 -070021
David Pursehouse819827a2020-02-12 15:20:19 +090022
Wink Saville02d79452009-04-10 13:01:24 -070023class Checkout(Command):
24 common = True
25 helpSummary = "Checkout a branch for development"
26 helpUsage = """
27%prog <branchname> [<project>...]
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070028"""
29 helpDescription = """
30The '%prog' command checks out an existing branch that was previously
31created by 'repo start'.
Wink Saville02d79452009-04-10 13:01:24 -070032
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070033The command is equivalent to:
Wink Saville02d79452009-04-10 13:01:24 -070034
Shawn O. Pearced33f43a2009-04-13 12:11:31 -070035 repo forall [<project>...] -c git checkout <branchname>
Wink Saville02d79452009-04-10 13:01:24 -070036"""
Mike Frysinger6093d992021-02-24 12:17:31 -050037 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
Wink Saville02d79452009-04-10 13:01:24 -070038
Mike Frysingerae6cb082019-08-27 01:10:59 -040039 def ValidateOptions(self, opt, args):
Wink Saville02d79452009-04-10 13:01:24 -070040 if not args:
41 self.Usage()
42
Mike Frysinger6093d992021-02-24 12:17:31 -050043 def _ExecuteOne(self, nb, project):
44 """Checkout one project."""
45 return (project.CheckoutBranch(nb), project)
46
Mike Frysingerae6cb082019-08-27 01:10:59 -040047 def Execute(self, opt, args):
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070048 nb = args[0]
49 err = []
Doug Anderson3ba5f952011-04-07 12:51:04 -070050 success = []
David Pursehouse5c6eeac2012-10-11 16:44:48 +090051 all_projects = self.GetProjects(args[1:])
Wink Saville02d79452009-04-10 13:01:24 -070052
Mike Frysinger6093d992021-02-24 12:17:31 -050053 def _ProcessResults(results):
54 for status, project in results:
55 if status is not None:
56 if status:
57 success.append(project)
58 else:
59 err.append(project)
60 pm.update()
Doug Anderson3ba5f952011-04-07 12:51:04 -070061
Mike Frysinger151701e2021-04-13 15:07:21 -040062 pm = Progress('Checkout %s' % nb, len(all_projects), quiet=opt.quiet)
Mike Frysinger6093d992021-02-24 12:17:31 -050063 # NB: Multiprocessing is heavy, so don't spin it up for one job.
64 if len(all_projects) == 1 or opt.jobs == 1:
65 _ProcessResults(self._ExecuteOne(nb, x) for x in all_projects)
66 else:
67 with multiprocessing.Pool(opt.jobs) as pool:
68 results = pool.imap_unordered(
69 functools.partial(self._ExecuteOne, nb), all_projects,
70 chunksize=WORKER_BATCH_SIZE)
71 _ProcessResults(results)
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070072 pm.end()
Wink Saville02d79452009-04-10 13:01:24 -070073
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070074 if err:
Doug Anderson3ba5f952011-04-07 12:51:04 -070075 for p in err:
Sarah Owenscecd1d82012-11-01 22:59:27 -070076 print("error: %s/: cannot checkout %s" % (p.relpath, nb),
77 file=sys.stderr)
Doug Anderson3ba5f952011-04-07 12:51:04 -070078 sys.exit(1)
79 elif not success:
Sarah Owenscecd1d82012-11-01 22:59:27 -070080 print('error: no project has branch %s' % nb, file=sys.stderr)
Shawn O. Pearce89e717d2009-04-18 15:04:41 -070081 sys.exit(1)