Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 18 | import re |
| 19 | import sys |
| 20 | |
| 21 | from command import Command |
Rob Ward | 1829101 | 2014-02-02 11:42:05 +0000 | [diff] [blame] | 22 | from error import GitError |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 23 | |
| 24 | CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$') |
| 25 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 26 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 27 | class Download(Command): |
| 28 | common = True |
| 29 | helpSummary = "Download and checkout a change" |
| 30 | helpUsage = """ |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 31 | %prog {[project] change[/patchset]}... |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 32 | """ |
| 33 | helpDescription = """ |
| 34 | The '%prog' command downloads a change from the review system and |
| 35 | makes it available in your project's local working directory. |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 36 | 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] | 37 | """ |
| 38 | |
| 39 | def _Options(self, p): |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame^] | 40 | p.add_option('-b', '--branch', |
| 41 | help='create a new branch first') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 42 | p.add_option('-c', '--cherry-pick', |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 43 | dest='cherrypick', action='store_true', |
| 44 | help="cherry-pick instead of checkout") |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 45 | p.add_option('-x', '--record-origin', action='store_true', |
| 46 | help='pass -x when cherry-picking') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 47 | p.add_option('-r', '--revert', |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 48 | dest='revert', action='store_true', |
| 49 | help="revert instead of checkout") |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 50 | p.add_option('-f', '--ff-only', |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 51 | dest='ffonly', action='store_true', |
| 52 | help="force fast-forward merge") |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 53 | |
| 54 | def _ParseChangeIds(self, args): |
Thiago Farina | de8b2c4 | 2009-09-09 00:41:34 -0400 | [diff] [blame] | 55 | if not args: |
| 56 | self.Usage() |
| 57 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 58 | to_get = [] |
| 59 | project = None |
| 60 | |
| 61 | for a in args: |
| 62 | m = CHANGE_RE.match(a) |
| 63 | if m: |
| 64 | if not project: |
Nicolas Cornu | 7482a96 | 2017-06-29 09:15:54 +0200 | [diff] [blame] | 65 | project = self.GetProjects(".")[0] |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 66 | chg_id = int(m.group(1)) |
| 67 | if m.group(2): |
| 68 | ps_id = int(m.group(2)) |
| 69 | else: |
| 70 | ps_id = 1 |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 71 | refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id) |
| 72 | output = project._LsRemote(refs + '*') |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 73 | if output: |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 74 | regex = refs + r'(\d+)' |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 75 | rcomp = re.compile(regex, re.I) |
| 76 | for line in output.splitlines(): |
| 77 | match = rcomp.search(line) |
| 78 | if match: |
| 79 | ps_id = max(int(match.group(1)), ps_id) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 80 | to_get.append((project, chg_id, ps_id)) |
| 81 | else: |
| 82 | project = self.GetProjects([a])[0] |
| 83 | return to_get |
| 84 | |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 85 | def ValidateOptions(self, opt, args): |
| 86 | if opt.record_origin: |
| 87 | if not opt.cherrypick: |
| 88 | self.OptionParser.error('-x only makes sense with --cherry-pick') |
| 89 | |
| 90 | if opt.ffonly: |
| 91 | self.OptionParser.error('-x and --ff are mutually exclusive options') |
| 92 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 93 | def Execute(self, opt, args): |
| 94 | for project, change_id, ps_id in self._ParseChangeIds(args): |
| 95 | dl = project.DownloadPatchSet(change_id, ps_id) |
| 96 | if not dl: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 97 | print('[%s] change %d/%d not found' |
| 98 | % (project.name, change_id, ps_id), |
| 99 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 100 | sys.exit(1) |
| 101 | |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 102 | if not opt.revert and not dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 103 | print('[%s] change %d/%d has already been merged' |
| 104 | % (project.name, change_id, ps_id), |
| 105 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 106 | continue |
| 107 | |
| 108 | if len(dl.commits) > 1: |
David Pursehouse | 42339d7 | 2020-02-12 14:37:15 +0900 | [diff] [blame] | 109 | print('[%s] %d/%d depends on %d unmerged changes:' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 110 | % (project.name, change_id, ps_id, len(dl.commits)), |
| 111 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 112 | for c in dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 113 | print(' %s' % (c), file=sys.stderr) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 114 | |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 115 | if opt.cherrypick: |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 116 | mode = 'cherry-pick' |
| 117 | elif opt.revert: |
| 118 | mode = 'revert' |
| 119 | elif opt.ffonly: |
| 120 | mode = 'fast-forward merge' |
| 121 | else: |
| 122 | mode = 'checkout' |
| 123 | |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame^] | 124 | # We'll combine the branch+checkout operation, but all the rest need a |
| 125 | # dedicated branch start. |
| 126 | if opt.branch and mode != 'checkout': |
| 127 | project.StartBranch(opt.branch) |
| 128 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 129 | try: |
| 130 | if opt.cherrypick: |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 131 | project._CherryPick(dl.commit, ffonly=opt.ffonly, |
| 132 | record_origin=opt.record_origin) |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 133 | elif opt.revert: |
| 134 | project._Revert(dl.commit) |
| 135 | elif opt.ffonly: |
| 136 | project._FastForward(dl.commit, ffonly=True) |
| 137 | else: |
Mike Frysinger | 7896447 | 2020-03-22 13:54:55 -0400 | [diff] [blame^] | 138 | if opt.branch: |
| 139 | project.StartBranch(opt.branch, revision=dl.commit) |
| 140 | else: |
| 141 | project._Checkout(dl.commit) |
Rob Ward | 1829101 | 2014-02-02 11:42:05 +0000 | [diff] [blame] | 142 | |
Mike Frysinger | 05097c6 | 2020-03-22 13:36:27 -0400 | [diff] [blame] | 143 | except GitError: |
| 144 | print('[%s] Could not complete the %s of %s' |
| 145 | % (project.name, mode, dl.commit), file=sys.stderr) |
| 146 | sys.exit(1) |