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 |
| 21 | |
| 22 | CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$') |
| 23 | |
| 24 | class Download(Command): |
| 25 | common = True |
| 26 | helpSummary = "Download and checkout a change" |
| 27 | helpUsage = """ |
| 28 | %prog {project change[/patchset]}... |
| 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. |
| 33 | """ |
| 34 | |
| 35 | def _Options(self, p): |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 36 | p.add_option('-c','--cherry-pick', |
| 37 | dest='cherrypick', action='store_true', |
| 38 | help="cherry-pick instead of checkout") |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 39 | p.add_option('-r','--revert', |
| 40 | dest='revert', action='store_true', |
| 41 | help="revert instead of checkout") |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 42 | p.add_option('-f','--ff-only', |
| 43 | dest='ffonly', action='store_true', |
| 44 | help="force fast-forward merge") |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 45 | |
| 46 | def _ParseChangeIds(self, args): |
Thiago Farina | de8b2c4 | 2009-09-09 00:41:34 -0400 | [diff] [blame] | 47 | if not args: |
| 48 | self.Usage() |
| 49 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 50 | to_get = [] |
| 51 | project = None |
| 52 | |
| 53 | for a in args: |
| 54 | m = CHANGE_RE.match(a) |
| 55 | if m: |
| 56 | if not project: |
| 57 | self.Usage() |
| 58 | chg_id = int(m.group(1)) |
| 59 | if m.group(2): |
| 60 | ps_id = int(m.group(2)) |
| 61 | else: |
| 62 | ps_id = 1 |
| 63 | to_get.append((project, chg_id, ps_id)) |
| 64 | else: |
| 65 | project = self.GetProjects([a])[0] |
| 66 | return to_get |
| 67 | |
| 68 | def Execute(self, opt, args): |
| 69 | for project, change_id, ps_id in self._ParseChangeIds(args): |
| 70 | dl = project.DownloadPatchSet(change_id, ps_id) |
| 71 | if not dl: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 72 | print('[%s] change %d/%d not found' |
| 73 | % (project.name, change_id, ps_id), |
| 74 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 75 | sys.exit(1) |
| 76 | |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 77 | if not opt.revert and not dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 78 | print('[%s] change %d/%d has already been merged' |
| 79 | % (project.name, change_id, ps_id), |
| 80 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 81 | continue |
| 82 | |
| 83 | if len(dl.commits) > 1: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 84 | print('[%s] %d/%d depends on %d unmerged changes:' \ |
| 85 | % (project.name, change_id, ps_id, len(dl.commits)), |
| 86 | file=sys.stderr) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 87 | for c in dl.commits: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 88 | print(' %s' % (c), file=sys.stderr) |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 89 | if opt.cherrypick: |
| 90 | project._CherryPick(dl.commit) |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 91 | elif opt.revert: |
| 92 | project._Revert(dl.commit) |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 93 | elif opt.ffonly: |
| 94 | project._FastForward(dl.commit, ffonly=True) |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 95 | else: |
| 96 | project._Checkout(dl.commit) |