blob: 384af78170db10a074bd234452357ee3c8b337b3 [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 = """
Nicolas Cornu7482a962017-06-29 09:15:54 +020029%prog {[project] change[/patchset]}...
Shawn O. Pearce632768b2008-10-23 11:58:52 -070030"""
31 helpDescription = """
32The '%prog' command downloads a change from the review system and
33makes it available in your project's local working directory.
Nicolas Cornu7482a962017-06-29 09:15:54 +020034If no project is specified try to use current directory as a project.
Shawn O. Pearce632768b2008-10-23 11:58:52 -070035"""
36
37 def _Options(self, p):
David Pursehouse8f62fb72012-11-14 12:09:38 +090038 p.add_option('-c', '--cherry-pick',
Pierre Tardye5a21222011-03-24 16:28:18 +010039 dest='cherrypick', action='store_true',
40 help="cherry-pick instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090041 p.add_option('-r', '--revert',
Erwan Mahea94f1622011-08-19 13:56:09 +020042 dest='revert', action='store_true',
43 help="revert instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090044 p.add_option('-f', '--ff-only',
Pierre Tardy3d125942012-05-04 12:18:12 +020045 dest='ffonly', action='store_true',
46 help="force fast-forward merge")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070047
48 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040049 if not args:
50 self.Usage()
51
Shawn O. Pearce632768b2008-10-23 11:58:52 -070052 to_get = []
53 project = None
54
55 for a in args:
56 m = CHANGE_RE.match(a)
57 if m:
58 if not project:
Nicolas Cornu7482a962017-06-29 09:15:54 +020059 project = self.GetProjects(".")[0]
Shawn O. Pearce632768b2008-10-23 11:58:52 -070060 chg_id = int(m.group(1))
61 if m.group(2):
62 ps_id = int(m.group(2))
63 else:
64 ps_id = 1
Akshay Vermacf7c0832018-03-15 21:56:30 +053065 regex = r'refs/changes/%2.2d/%d/(\d+)' % (chg_id % 100, chg_id)
66 output = project._LsRemote()
67 if output:
68 rcomp = re.compile(regex, re.I)
69 for line in output.splitlines():
70 match = rcomp.search(line)
71 if match:
72 ps_id = max(int(match.group(1)), ps_id)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070073 to_get.append((project, chg_id, ps_id))
74 else:
75 project = self.GetProjects([a])[0]
76 return to_get
77
78 def Execute(self, opt, args):
79 for project, change_id, ps_id in self._ParseChangeIds(args):
80 dl = project.DownloadPatchSet(change_id, ps_id)
81 if not dl:
Sarah Owenscecd1d82012-11-01 22:59:27 -070082 print('[%s] change %d/%d not found'
83 % (project.name, change_id, ps_id),
84 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070085 sys.exit(1)
86
Erwan Mahea94f1622011-08-19 13:56:09 +020087 if not opt.revert and not dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -070088 print('[%s] change %d/%d has already been merged'
89 % (project.name, change_id, ps_id),
90 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070091 continue
92
93 if len(dl.commits) > 1:
Sarah Owenscecd1d82012-11-01 22:59:27 -070094 print('[%s] %d/%d depends on %d unmerged changes:' \
95 % (project.name, change_id, ps_id, len(dl.commits)),
96 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070097 for c in dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -070098 print(' %s' % (c), file=sys.stderr)
Pierre Tardye5a21222011-03-24 16:28:18 +010099 if opt.cherrypick:
Rob Ward18291012014-02-02 11:42:05 +0000100 try:
101 project._CherryPick(dl.commit)
102 except GitError:
103 print('[%s] Could not complete the cherry-pick of %s' \
104 % (project.name, dl.commit), file=sys.stderr)
Scott Anderson0936aea2014-10-17 15:37:12 -0400105 sys.exit(1)
Rob Ward18291012014-02-02 11:42:05 +0000106
Erwan Mahea94f1622011-08-19 13:56:09 +0200107 elif opt.revert:
108 project._Revert(dl.commit)
Pierre Tardy3d125942012-05-04 12:18:12 +0200109 elif opt.ffonly:
110 project._FastForward(dl.commit, ffonly=True)
Pierre Tardye5a21222011-03-24 16:28:18 +0100111 else:
112 project._Checkout(dl.commit)