Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 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 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 15 | import re |
| 16 | import sys |
| 17 | |
| 18 | from command import Command |
Mike Frysinger | 31fabee | 2021-02-28 22:03:04 -0500 | [diff] [blame] | 19 | from error import GitError, NoSuchProjectError |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 20 | |
| 21 | CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$') |
| 22 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 23 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 24 | class Download(Command): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 25 | COMMON = True |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 26 | helpSummary = "Download and checkout a change" |
| 27 | helpUsage = """ |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 28 | %prog {[project] change[/patchset]}... |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 29 | """ |
| 30 | helpDescription = """ |
| 31 | The '%prog' command downloads a change from the review system and |
| 32 | makes it available in your project's local working directory. |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 33 | If no project is specified try to use current directory as a project. |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 34 | """ |
| 35 | |
| 36 | def _Options(self, p): |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame] | 37 | p.add_option('-b', '--branch', |
| 38 | help='create a new branch first') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 39 | p.add_option('-c', '--cherry-pick', |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 40 | dest='cherrypick', action='store_true', |
| 41 | help="cherry-pick instead of checkout") |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 42 | p.add_option('-x', '--record-origin', action='store_true', |
| 43 | help='pass -x when cherry-picking') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 44 | p.add_option('-r', '--revert', |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 45 | dest='revert', action='store_true', |
| 46 | help="revert instead of checkout") |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 47 | p.add_option('-f', '--ff-only', |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 48 | dest='ffonly', action='store_true', |
| 49 | help="force fast-forward merge") |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 50 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 51 | def _ParseChangeIds(self, opt, args): |
Thiago Farina | de8b2c4 | 2009-09-09 00:41:34 -0400 | [diff] [blame] | 52 | if not args: |
| 53 | self.Usage() |
| 54 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 55 | to_get = [] |
| 56 | project = None |
| 57 | |
| 58 | for a in args: |
| 59 | m = CHANGE_RE.match(a) |
| 60 | if m: |
| 61 | if not project: |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 62 | project = self.GetProjects(".")[0] |
Mike Frysinger | 31fabee | 2021-02-28 22:03:04 -0500 | [diff] [blame] | 63 | print('Defaulting to cwd project', project.name) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 64 | chg_id = int(m.group(1)) |
| 65 | if m.group(2): |
| 66 | ps_id = int(m.group(2)) |
| 67 | else: |
| 68 | ps_id = 1 |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 69 | refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id) |
| 70 | output = project._LsRemote(refs + '*') |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 71 | if output: |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 72 | regex = refs + r'(\d+)' |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 73 | rcomp = re.compile(regex, re.I) |
| 74 | for line in output.splitlines(): |
| 75 | match = rcomp.search(line) |
| 76 | if match: |
| 77 | ps_id = max(int(match.group(1)), ps_id) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 78 | to_get.append((project, chg_id, ps_id)) |
| 79 | else: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 80 | projects = self.GetProjects([a], all_manifests=not opt.this_manifest_only) |
Mike Frysinger | 31fabee | 2021-02-28 22:03:04 -0500 | [diff] [blame] | 81 | if len(projects) > 1: |
| 82 | # If the cwd is one of the projects, assume they want that. |
| 83 | try: |
| 84 | project = self.GetProjects('.')[0] |
| 85 | except NoSuchProjectError: |
| 86 | project = None |
| 87 | if project not in projects: |
| 88 | print('error: %s matches too many projects; please re-run inside ' |
| 89 | 'the project checkout.' % (a,), file=sys.stderr) |
| 90 | for project in projects: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 91 | print(' %s/ @ %s' % (project.RelPath(local=opt.this_manifest_only), |
| 92 | project.revisionExpr), file=sys.stderr) |
Mike Frysinger | 31fabee | 2021-02-28 22:03:04 -0500 | [diff] [blame] | 93 | sys.exit(1) |
| 94 | else: |
| 95 | project = projects[0] |
| 96 | print('Defaulting to cwd project', project.name) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 97 | return to_get |
| 98 | |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 99 | def ValidateOptions(self, opt, args): |
| 100 | if opt.record_origin: |
| 101 | if not opt.cherrypick: |
| 102 | self.OptionParser.error('-x only makes sense with --cherry-pick') |
| 103 | |
| 104 | if opt.ffonly: |
| 105 | self.OptionParser.error('-x and --ff are mutually exclusive options') |
| 106 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 107 | def Execute(self, opt, args): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 108 | for project, change_id, ps_id in self._ParseChangeIds(opt, args): |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 109 | dl = project.DownloadPatchSet(change_id, ps_id) |
| 110 | if not dl: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 111 | print('[%s] change %d/%d not found' |
| 112 | % (project.name, change_id, ps_id), |
| 113 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 114 | sys.exit(1) |
| 115 | |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 116 | if not opt.revert and not dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 117 | print('[%s] change %d/%d has already been merged' |
| 118 | % (project.name, change_id, ps_id), |
| 119 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 120 | continue |
| 121 | |
| 122 | if len(dl.commits) > 1: |
David Pursehouse | 42339d7 | 2020-02-12 14:37:15 +0900 | [diff] [blame] | 123 | print('[%s] %d/%d depends on %d unmerged changes:' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 124 | % (project.name, change_id, ps_id, len(dl.commits)), |
| 125 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 126 | for c in dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 127 | print(' %s' % (c), file=sys.stderr) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 128 | |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 129 | if opt.cherrypick: |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 130 | mode = 'cherry-pick' |
| 131 | elif opt.revert: |
| 132 | mode = 'revert' |
| 133 | elif opt.ffonly: |
| 134 | mode = 'fast-forward merge' |
| 135 | else: |
| 136 | mode = 'checkout' |
| 137 | |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame] | 138 | # We'll combine the branch+checkout operation, but all the rest need a |
| 139 | # dedicated branch start. |
| 140 | if opt.branch and mode != 'checkout': |
| 141 | project.StartBranch(opt.branch) |
| 142 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 143 | try: |
| 144 | if opt.cherrypick: |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 145 | project._CherryPick(dl.commit, ffonly=opt.ffonly, |
| 146 | record_origin=opt.record_origin) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 147 | elif opt.revert: |
| 148 | project._Revert(dl.commit) |
| 149 | elif opt.ffonly: |
| 150 | project._FastForward(dl.commit, ffonly=True) |
| 151 | else: |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame] | 152 | if opt.branch: |
| 153 | project.StartBranch(opt.branch, revision=dl.commit) |
| 154 | else: |
| 155 | project._Checkout(dl.commit) |
Rob Ward | 1829101 | 2014-02-02 11:42:05 +0000 | [diff] [blame] | 156 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 157 | except GitError: |
| 158 | print('[%s] Could not complete the %s of %s' |
| 159 | % (project.name, mode, dl.commit), file=sys.stderr) |
| 160 | sys.exit(1) |