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 | |
| 16 | import os |
| 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") |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 39 | |
| 40 | def _ParseChangeIds(self, args): |
Thiago Farina | de8b2c4 | 2009-09-09 00:41:34 -0400 | [diff] [blame] | 41 | if not args: |
| 42 | self.Usage() |
| 43 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 44 | to_get = [] |
| 45 | project = None |
| 46 | |
| 47 | for a in args: |
| 48 | m = CHANGE_RE.match(a) |
| 49 | if m: |
| 50 | if not project: |
| 51 | self.Usage() |
| 52 | chg_id = int(m.group(1)) |
| 53 | if m.group(2): |
| 54 | ps_id = int(m.group(2)) |
| 55 | else: |
| 56 | ps_id = 1 |
| 57 | to_get.append((project, chg_id, ps_id)) |
| 58 | else: |
| 59 | project = self.GetProjects([a])[0] |
| 60 | return to_get |
| 61 | |
| 62 | def Execute(self, opt, args): |
| 63 | for project, change_id, ps_id in self._ParseChangeIds(args): |
| 64 | dl = project.DownloadPatchSet(change_id, ps_id) |
| 65 | if not dl: |
| 66 | print >>sys.stderr, \ |
| 67 | '[%s] change %d/%d not found' \ |
| 68 | % (project.name, change_id, ps_id) |
| 69 | sys.exit(1) |
| 70 | |
| 71 | if not dl.commits: |
| 72 | print >>sys.stderr, \ |
| 73 | '[%s] change %d/%d has already been merged' \ |
| 74 | % (project.name, change_id, ps_id) |
| 75 | continue |
| 76 | |
| 77 | if len(dl.commits) > 1: |
| 78 | print >>sys.stderr, \ |
| 79 | '[%s] %d/%d depends on %d unmerged changes:' \ |
| 80 | % (project.name, change_id, ps_id, len(dl.commits)) |
| 81 | for c in dl.commits: |
| 82 | print >>sys.stderr, ' %s' % (c) |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame^] | 83 | if opt.cherrypick: |
| 84 | project._CherryPick(dl.commit) |
| 85 | else: |
| 86 | project._Checkout(dl.commit) |