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 |
Rob Ward | 1829101 | 2014-02-02 11:42:05 +0000 | [diff] [blame] | 19 | from error import GitError |
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): |
| 25 | common = True |
| 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 | |
| 51 | def _ParseChangeIds(self, 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] |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 63 | chg_id = int(m.group(1)) |
| 64 | if m.group(2): |
| 65 | ps_id = int(m.group(2)) |
| 66 | else: |
| 67 | ps_id = 1 |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 68 | refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id) |
| 69 | output = project._LsRemote(refs + '*') |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 70 | if output: |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 71 | regex = refs + r'(\d+)' |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 72 | rcomp = re.compile(regex, re.I) |
| 73 | for line in output.splitlines(): |
| 74 | match = rcomp.search(line) |
| 75 | if match: |
| 76 | ps_id = max(int(match.group(1)), ps_id) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 77 | to_get.append((project, chg_id, ps_id)) |
| 78 | else: |
| 79 | project = self.GetProjects([a])[0] |
| 80 | return to_get |
| 81 | |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 82 | def ValidateOptions(self, opt, args): |
| 83 | if opt.record_origin: |
| 84 | if not opt.cherrypick: |
| 85 | self.OptionParser.error('-x only makes sense with --cherry-pick') |
| 86 | |
| 87 | if opt.ffonly: |
| 88 | self.OptionParser.error('-x and --ff are mutually exclusive options') |
| 89 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 90 | def Execute(self, opt, args): |
| 91 | for project, change_id, ps_id in self._ParseChangeIds(args): |
| 92 | dl = project.DownloadPatchSet(change_id, ps_id) |
| 93 | if not dl: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 94 | print('[%s] change %d/%d not found' |
| 95 | % (project.name, change_id, ps_id), |
| 96 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 97 | sys.exit(1) |
| 98 | |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 99 | if not opt.revert and not dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 100 | print('[%s] change %d/%d has already been merged' |
| 101 | % (project.name, change_id, ps_id), |
| 102 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 103 | continue |
| 104 | |
| 105 | if len(dl.commits) > 1: |
David Pursehouse | 42339d7 | 2020-02-12 14:37:15 +0900 | [diff] [blame] | 106 | print('[%s] %d/%d depends on %d unmerged changes:' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 107 | % (project.name, change_id, ps_id, len(dl.commits)), |
| 108 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 109 | for c in dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 110 | print(' %s' % (c), file=sys.stderr) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 111 | |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 112 | if opt.cherrypick: |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 113 | mode = 'cherry-pick' |
| 114 | elif opt.revert: |
| 115 | mode = 'revert' |
| 116 | elif opt.ffonly: |
| 117 | mode = 'fast-forward merge' |
| 118 | else: |
| 119 | mode = 'checkout' |
| 120 | |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame] | 121 | # We'll combine the branch+checkout operation, but all the rest need a |
| 122 | # dedicated branch start. |
| 123 | if opt.branch and mode != 'checkout': |
| 124 | project.StartBranch(opt.branch) |
| 125 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 126 | try: |
| 127 | if opt.cherrypick: |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 128 | project._CherryPick(dl.commit, ffonly=opt.ffonly, |
| 129 | record_origin=opt.record_origin) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 130 | elif opt.revert: |
| 131 | project._Revert(dl.commit) |
| 132 | elif opt.ffonly: |
| 133 | project._FastForward(dl.commit, ffonly=True) |
| 134 | else: |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame] | 135 | if opt.branch: |
| 136 | project.StartBranch(opt.branch, revision=dl.commit) |
| 137 | else: |
| 138 | project._Checkout(dl.commit) |
Rob Ward | 1829101 | 2014-02-02 11:42:05 +0000 | [diff] [blame] | 139 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 140 | except GitError: |
| 141 | print('[%s] Could not complete the %s of %s' |
| 142 | % (project.name, mode, dl.commit), file=sys.stderr) |
| 143 | sys.exit(1) |