blob: f79f485be08832c196556c8cfc69920564f99388 [file] [log] [blame]
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001#
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
16import os
17import re
18import sys
19
20from command import Command
21
22CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
23
24class Download(Command):
25 common = True
26 helpSummary = "Download and checkout a change"
27 helpUsage = """
28%prog {project change[/patchset]}...
29"""
30 helpDescription = """
31The '%prog' command downloads a change from the review system and
32makes it available in your project's local working directory.
33"""
34
35 def _Options(self, p):
Pierre Tardye5a21222011-03-24 16:28:18 +010036 p.add_option('-c','--cherry-pick',
37 dest='cherrypick', action='store_true',
38 help="cherry-pick instead of checkout")
Erwan Mahea94f1622011-08-19 13:56:09 +020039 p.add_option('-r','--revert',
40 dest='revert', action='store_true',
41 help="revert instead of checkout")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070042
43 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040044 if not args:
45 self.Usage()
46
Shawn O. Pearce632768b2008-10-23 11:58:52 -070047 to_get = []
48 project = None
49
50 for a in args:
51 m = CHANGE_RE.match(a)
52 if m:
53 if not project:
54 self.Usage()
55 chg_id = int(m.group(1))
56 if m.group(2):
57 ps_id = int(m.group(2))
58 else:
59 ps_id = 1
60 to_get.append((project, chg_id, ps_id))
61 else:
62 project = self.GetProjects([a])[0]
63 return to_get
64
65 def Execute(self, opt, args):
66 for project, change_id, ps_id in self._ParseChangeIds(args):
67 dl = project.DownloadPatchSet(change_id, ps_id)
68 if not dl:
69 print >>sys.stderr, \
70 '[%s] change %d/%d not found' \
71 % (project.name, change_id, ps_id)
72 sys.exit(1)
73
Erwan Mahea94f1622011-08-19 13:56:09 +020074 if not opt.revert and not dl.commits:
Shawn O. Pearce632768b2008-10-23 11:58:52 -070075 print >>sys.stderr, \
76 '[%s] change %d/%d has already been merged' \
77 % (project.name, change_id, ps_id)
78 continue
79
80 if len(dl.commits) > 1:
81 print >>sys.stderr, \
82 '[%s] %d/%d depends on %d unmerged changes:' \
83 % (project.name, change_id, ps_id, len(dl.commits))
84 for c in dl.commits:
85 print >>sys.stderr, ' %s' % (c)
Pierre Tardye5a21222011-03-24 16:28:18 +010086 if opt.cherrypick:
87 project._CherryPick(dl.commit)
Erwan Mahea94f1622011-08-19 13:56:09 +020088 elif opt.revert:
89 project._Revert(dl.commit)
Pierre Tardye5a21222011-03-24 16:28:18 +010090 else:
91 project._Checkout(dl.commit)