blob: c0c47dd996a03dca9959476ff79efdbf51b11ae3 [file] [log] [blame]
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearce632768b2008-10-23 11:58:52 -070015import re
16import sys
17
18from command import Command
Rob Ward18291012014-02-02 11:42:05 +000019from error import GitError
Shawn O. Pearce632768b2008-10-23 11:58:52 -070020
21CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
22
David Pursehouse819827a2020-02-12 15:20:19 +090023
Shawn O. Pearce632768b2008-10-23 11:58:52 -070024class Download(Command):
25 common = True
26 helpSummary = "Download and checkout a change"
27 helpUsage = """
Nicolas Cornu7482a962017-06-29 09:15:54 +020028%prog {[project] change[/patchset]}...
Shawn O. Pearce632768b2008-10-23 11:58:52 -070029"""
30 helpDescription = """
31The '%prog' command downloads a change from the review system and
32makes it available in your project's local working directory.
Nicolas Cornu7482a962017-06-29 09:15:54 +020033If no project is specified try to use current directory as a project.
Shawn O. Pearce632768b2008-10-23 11:58:52 -070034"""
35
36 def _Options(self, p):
Mike Frysinger78964472020-03-22 13:54:55 -040037 p.add_option('-b', '--branch',
38 help='create a new branch first')
David Pursehouse8f62fb72012-11-14 12:09:38 +090039 p.add_option('-c', '--cherry-pick',
Pierre Tardye5a21222011-03-24 16:28:18 +010040 dest='cherrypick', action='store_true',
41 help="cherry-pick instead of checkout")
Mike Frysinger915fda12020-03-22 12:15:20 -040042 p.add_option('-x', '--record-origin', action='store_true',
43 help='pass -x when cherry-picking')
David Pursehouse8f62fb72012-11-14 12:09:38 +090044 p.add_option('-r', '--revert',
Erwan Mahea94f1622011-08-19 13:56:09 +020045 dest='revert', action='store_true',
46 help="revert instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090047 p.add_option('-f', '--ff-only',
Pierre Tardy3d125942012-05-04 12:18:12 +020048 dest='ffonly', action='store_true',
49 help="force fast-forward merge")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070050
51 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040052 if not args:
53 self.Usage()
54
Shawn O. Pearce632768b2008-10-23 11:58:52 -070055 to_get = []
56 project = None
57
58 for a in args:
59 m = CHANGE_RE.match(a)
60 if m:
61 if not project:
Nicolas Cornu7482a962017-06-29 09:15:54 +020062 project = self.GetProjects(".")[0]
Shawn O. Pearce632768b2008-10-23 11:58:52 -070063 chg_id = int(m.group(1))
64 if m.group(2):
65 ps_id = int(m.group(2))
66 else:
67 ps_id = 1
Akshay Verma0f2e45a2018-03-24 12:27:05 +053068 refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
69 output = project._LsRemote(refs + '*')
Akshay Vermacf7c0832018-03-15 21:56:30 +053070 if output:
Akshay Verma0f2e45a2018-03-24 12:27:05 +053071 regex = refs + r'(\d+)'
Akshay Vermacf7c0832018-03-15 21:56:30 +053072 rcomp = re.compile(regex, re.I)
73 for line in output.splitlines():
74 match = rcomp.search(line)
75 if match:
76 ps_id = max(int(match.group(1)), ps_id)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070077 to_get.append((project, chg_id, ps_id))
78 else:
79 project = self.GetProjects([a])[0]
80 return to_get
81
Mike Frysinger915fda12020-03-22 12:15:20 -040082 def ValidateOptions(self, opt, args):
83 if opt.record_origin:
84 if not opt.cherrypick:
85 self.OptionParser.error('-x only makes sense with --cherry-pick')
86
87 if opt.ffonly:
88 self.OptionParser.error('-x and --ff are mutually exclusive options')
89
Shawn O. Pearce632768b2008-10-23 11:58:52 -070090 def Execute(self, opt, args):
91 for project, change_id, ps_id in self._ParseChangeIds(args):
92 dl = project.DownloadPatchSet(change_id, ps_id)
93 if not dl:
Sarah Owenscecd1d82012-11-01 22:59:27 -070094 print('[%s] change %d/%d not found'
95 % (project.name, change_id, ps_id),
96 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070097 sys.exit(1)
98
Erwan Mahea94f1622011-08-19 13:56:09 +020099 if not opt.revert and not dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700100 print('[%s] change %d/%d has already been merged'
101 % (project.name, change_id, ps_id),
102 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700103 continue
104
105 if len(dl.commits) > 1:
David Pursehouse42339d72020-02-12 14:37:15 +0900106 print('[%s] %d/%d depends on %d unmerged changes:'
Sarah Owenscecd1d82012-11-01 22:59:27 -0700107 % (project.name, change_id, ps_id, len(dl.commits)),
108 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700109 for c in dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700110 print(' %s' % (c), file=sys.stderr)
Mike Frysinger05097c62020-03-22 13:36:27 -0400111
Pierre Tardye5a21222011-03-24 16:28:18 +0100112 if opt.cherrypick:
Mike Frysinger05097c62020-03-22 13:36:27 -0400113 mode = 'cherry-pick'
114 elif opt.revert:
115 mode = 'revert'
116 elif opt.ffonly:
117 mode = 'fast-forward merge'
118 else:
119 mode = 'checkout'
120
Mike Frysinger78964472020-03-22 13:54:55 -0400121 # We'll combine the branch+checkout operation, but all the rest need a
122 # dedicated branch start.
123 if opt.branch and mode != 'checkout':
124 project.StartBranch(opt.branch)
125
Mike Frysinger05097c62020-03-22 13:36:27 -0400126 try:
127 if opt.cherrypick:
Mike Frysinger915fda12020-03-22 12:15:20 -0400128 project._CherryPick(dl.commit, ffonly=opt.ffonly,
129 record_origin=opt.record_origin)
Mike Frysinger05097c62020-03-22 13:36:27 -0400130 elif opt.revert:
131 project._Revert(dl.commit)
132 elif opt.ffonly:
133 project._FastForward(dl.commit, ffonly=True)
134 else:
Mike Frysinger78964472020-03-22 13:54:55 -0400135 if opt.branch:
136 project.StartBranch(opt.branch, revision=dl.commit)
137 else:
138 project._Checkout(dl.commit)
Rob Ward18291012014-02-02 11:42:05 +0000139
Mike Frysinger05097c62020-03-22 13:36:27 -0400140 except GitError:
141 print('[%s] Could not complete the %s of %s'
142 % (project.name, mode, dl.commit), file=sys.stderr)
143 sys.exit(1)