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