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