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