blob: 098d8b430fddbf8f13e5e6d396ac7988366b85bd [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
Sarah Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Shawn O. Pearce632768b2008-10-23 11:58:52 -070017import re
18import sys
19
20from command import Command
Rob Ward18291012014-02-02 11:42:05 +000021from error import GitError
Shawn O. Pearce632768b2008-10-23 11:58:52 -070022
23CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
24
25class Download(Command):
26 common = True
27 helpSummary = "Download and checkout a change"
28 helpUsage = """
29%prog {project change[/patchset]}...
30"""
31 helpDescription = """
32The '%prog' command downloads a change from the review system and
33makes it available in your project's local working directory.
34"""
35
36 def _Options(self, p):
David Pursehouse8f62fb72012-11-14 12:09:38 +090037 p.add_option('-c', '--cherry-pick',
Pierre Tardye5a21222011-03-24 16:28:18 +010038 dest='cherrypick', action='store_true',
39 help="cherry-pick instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090040 p.add_option('-r', '--revert',
Erwan Mahea94f1622011-08-19 13:56:09 +020041 dest='revert', action='store_true',
42 help="revert instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090043 p.add_option('-f', '--ff-only',
Pierre Tardy3d125942012-05-04 12:18:12 +020044 dest='ffonly', action='store_true',
45 help="force fast-forward merge")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070046
47 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040048 if not args:
49 self.Usage()
50
Shawn O. Pearce632768b2008-10-23 11:58:52 -070051 to_get = []
52 project = None
53
54 for a in args:
55 m = CHANGE_RE.match(a)
56 if m:
57 if not project:
58 self.Usage()
59 chg_id = int(m.group(1))
60 if m.group(2):
61 ps_id = int(m.group(2))
62 else:
63 ps_id = 1
64 to_get.append((project, chg_id, ps_id))
65 else:
66 project = self.GetProjects([a])[0]
67 return to_get
68
69 def Execute(self, opt, args):
70 for project, change_id, ps_id in self._ParseChangeIds(args):
71 dl = project.DownloadPatchSet(change_id, ps_id)
72 if not dl:
Sarah Owenscecd1d82012-11-01 22:59:27 -070073 print('[%s] change %d/%d not found'
74 % (project.name, change_id, ps_id),
75 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070076 sys.exit(1)
77
Erwan Mahea94f1622011-08-19 13:56:09 +020078 if not opt.revert and not dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -070079 print('[%s] change %d/%d has already been merged'
80 % (project.name, change_id, ps_id),
81 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070082 continue
83
84 if len(dl.commits) > 1:
Sarah Owenscecd1d82012-11-01 22:59:27 -070085 print('[%s] %d/%d depends on %d unmerged changes:' \
86 % (project.name, change_id, ps_id, len(dl.commits)),
87 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070088 for c in dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -070089 print(' %s' % (c), file=sys.stderr)
Pierre Tardye5a21222011-03-24 16:28:18 +010090 if opt.cherrypick:
Rob Ward18291012014-02-02 11:42:05 +000091 try:
92 project._CherryPick(dl.commit)
93 except GitError:
94 print('[%s] Could not complete the cherry-pick of %s' \
95 % (project.name, dl.commit), file=sys.stderr)
96
Erwan Mahea94f1622011-08-19 13:56:09 +020097 elif opt.revert:
98 project._Revert(dl.commit)
Pierre Tardy3d125942012-05-04 12:18:12 +020099 elif opt.ffonly:
100 project._FastForward(dl.commit, ffonly=True)
Pierre Tardye5a21222011-03-24 16:28:18 +0100101 else:
102 project._Checkout(dl.commit)