Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1 | # 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 Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 15 | import functools |
| 16 | import multiprocessing |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 17 | import sys |
Mike Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 18 | |
| 19 | from command import Command, DEFAULT_LOCAL_JOBS, WORKER_BATCH_SIZE |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 20 | from progress import Progress |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 21 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 22 | |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 23 | class Checkout(Command): |
| 24 | common = True |
| 25 | helpSummary = "Checkout a branch for development" |
| 26 | helpUsage = """ |
| 27 | %prog <branchname> [<project>...] |
Shawn O. Pearce | d33f43a | 2009-04-13 12:11:31 -0700 | [diff] [blame] | 28 | """ |
| 29 | helpDescription = """ |
| 30 | The '%prog' command checks out an existing branch that was previously |
| 31 | created by 'repo start'. |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | d33f43a | 2009-04-13 12:11:31 -0700 | [diff] [blame] | 33 | The command is equivalent to: |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 34 | |
Shawn O. Pearce | d33f43a | 2009-04-13 12:11:31 -0700 | [diff] [blame] | 35 | repo forall [<project>...] -c git checkout <branchname> |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 36 | """ |
Mike Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 37 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 38 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 39 | def ValidateOptions(self, opt, args): |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 40 | if not args: |
| 41 | self.Usage() |
| 42 | |
Mike Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 43 | def _ExecuteOne(self, nb, project): |
| 44 | """Checkout one project.""" |
| 45 | return (project.CheckoutBranch(nb), project) |
| 46 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 47 | def Execute(self, opt, args): |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 48 | nb = args[0] |
| 49 | err = [] |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 50 | success = [] |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 51 | all_projects = self.GetProjects(args[1:]) |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 52 | |
Mike Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 53 | 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 Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 61 | |
Mike Frysinger | 151701e | 2021-04-13 15:07:21 -0400 | [diff] [blame] | 62 | pm = Progress('Checkout %s' % nb, len(all_projects), quiet=opt.quiet) |
Mike Frysinger | 6093d99 | 2021-02-24 12:17:31 -0500 | [diff] [blame] | 63 | # 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. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 72 | pm.end() |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 73 | |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 74 | if err: |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 75 | for p in err: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 76 | print("error: %s/: cannot checkout %s" % (p.relpath, nb), |
| 77 | file=sys.stderr) |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 78 | sys.exit(1) |
| 79 | elif not success: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 80 | print('error: no project has branch %s' % nb, file=sys.stderr) |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 81 | sys.exit(1) |