blob: 0abe90d0877f029f0c35f3d9e53a98372d98c1a3 [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
Shawn O. Pearce632768b2008-10-23 11:58:52 -070016import re
17import sys
18
19from command import Command
20
21CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
22
23class Download(Command):
24 common = True
25 helpSummary = "Download and checkout a change"
26 helpUsage = """
27%prog {project change[/patchset]}...
28"""
29 helpDescription = """
30The '%prog' command downloads a change from the review system and
31makes it available in your project's local working directory.
32"""
33
34 def _Options(self, p):
Pierre Tardye5a21222011-03-24 16:28:18 +010035 p.add_option('-c','--cherry-pick',
36 dest='cherrypick', action='store_true',
37 help="cherry-pick instead of checkout")
Erwan Mahea94f1622011-08-19 13:56:09 +020038 p.add_option('-r','--revert',
39 dest='revert', action='store_true',
40 help="revert instead of checkout")
Pierre Tardy3d125942012-05-04 12:18:12 +020041 p.add_option('-f','--ff-only',
42 dest='ffonly', action='store_true',
43 help="force fast-forward merge")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070044
45 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040046 if not args:
47 self.Usage()
48
Shawn O. Pearce632768b2008-10-23 11:58:52 -070049 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 Mahea94f1622011-08-19 13:56:09 +020076 if not opt.revert and not dl.commits:
Shawn O. Pearce632768b2008-10-23 11:58:52 -070077 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 Tardye5a21222011-03-24 16:28:18 +010088 if opt.cherrypick:
89 project._CherryPick(dl.commit)
Erwan Mahea94f1622011-08-19 13:56:09 +020090 elif opt.revert:
91 project._Revert(dl.commit)
Pierre Tardy3d125942012-05-04 12:18:12 +020092 elif opt.ffonly:
93 project._FastForward(dl.commit, ffonly=True)
Pierre Tardye5a21222011-03-24 16:28:18 +010094 else:
95 project._Checkout(dl.commit)