blob: 81d997e0f4d53614c1a51e6f8950229ade552cfc [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
Mike Frysinger31fabee2021-02-28 22:03:04 -050019from error import GitError, NoSuchProjectError
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]
Mike Frysinger31fabee2021-02-28 22:03:04 -050063 print('Defaulting to cwd project', project.name)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070064 chg_id = int(m.group(1))
65 if m.group(2):
66 ps_id = int(m.group(2))
67 else:
68 ps_id = 1
Akshay Verma0f2e45a2018-03-24 12:27:05 +053069 refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
70 output = project._LsRemote(refs + '*')
Akshay Vermacf7c0832018-03-15 21:56:30 +053071 if output:
Akshay Verma0f2e45a2018-03-24 12:27:05 +053072 regex = refs + r'(\d+)'
Akshay Vermacf7c0832018-03-15 21:56:30 +053073 rcomp = re.compile(regex, re.I)
74 for line in output.splitlines():
75 match = rcomp.search(line)
76 if match:
77 ps_id = max(int(match.group(1)), ps_id)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070078 to_get.append((project, chg_id, ps_id))
79 else:
Mike Frysinger31fabee2021-02-28 22:03:04 -050080 projects = self.GetProjects([a])
81 if len(projects) > 1:
82 # If the cwd is one of the projects, assume they want that.
83 try:
84 project = self.GetProjects('.')[0]
85 except NoSuchProjectError:
86 project = None
87 if project not in projects:
88 print('error: %s matches too many projects; please re-run inside '
89 'the project checkout.' % (a,), file=sys.stderr)
90 for project in projects:
91 print(' %s/ @ %s' % (project.relpath, project.revisionExpr),
92 file=sys.stderr)
93 sys.exit(1)
94 else:
95 project = projects[0]
96 print('Defaulting to cwd project', project.name)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070097 return to_get
98
Mike Frysinger915fda12020-03-22 12:15:20 -040099 def ValidateOptions(self, opt, args):
100 if opt.record_origin:
101 if not opt.cherrypick:
102 self.OptionParser.error('-x only makes sense with --cherry-pick')
103
104 if opt.ffonly:
105 self.OptionParser.error('-x and --ff are mutually exclusive options')
106
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700107 def Execute(self, opt, args):
108 for project, change_id, ps_id in self._ParseChangeIds(args):
109 dl = project.DownloadPatchSet(change_id, ps_id)
110 if not dl:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700111 print('[%s] change %d/%d not found'
112 % (project.name, change_id, ps_id),
113 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700114 sys.exit(1)
115
Erwan Mahea94f1622011-08-19 13:56:09 +0200116 if not opt.revert and not dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700117 print('[%s] change %d/%d has already been merged'
118 % (project.name, change_id, ps_id),
119 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700120 continue
121
122 if len(dl.commits) > 1:
David Pursehouse42339d72020-02-12 14:37:15 +0900123 print('[%s] %d/%d depends on %d unmerged changes:'
Sarah Owenscecd1d82012-11-01 22:59:27 -0700124 % (project.name, change_id, ps_id, len(dl.commits)),
125 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700126 for c in dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700127 print(' %s' % (c), file=sys.stderr)
Mike Frysinger05097c62020-03-22 13:36:27 -0400128
Pierre Tardye5a21222011-03-24 16:28:18 +0100129 if opt.cherrypick:
Mike Frysinger05097c62020-03-22 13:36:27 -0400130 mode = 'cherry-pick'
131 elif opt.revert:
132 mode = 'revert'
133 elif opt.ffonly:
134 mode = 'fast-forward merge'
135 else:
136 mode = 'checkout'
137
Mike Frysinger78964472020-03-22 13:54:55 -0400138 # We'll combine the branch+checkout operation, but all the rest need a
139 # dedicated branch start.
140 if opt.branch and mode != 'checkout':
141 project.StartBranch(opt.branch)
142
Mike Frysinger05097c62020-03-22 13:36:27 -0400143 try:
144 if opt.cherrypick:
Mike Frysinger915fda12020-03-22 12:15:20 -0400145 project._CherryPick(dl.commit, ffonly=opt.ffonly,
146 record_origin=opt.record_origin)
Mike Frysinger05097c62020-03-22 13:36:27 -0400147 elif opt.revert:
148 project._Revert(dl.commit)
149 elif opt.ffonly:
150 project._FastForward(dl.commit, ffonly=True)
151 else:
Mike Frysinger78964472020-03-22 13:54:55 -0400152 if opt.branch:
153 project.StartBranch(opt.branch, revision=dl.commit)
154 else:
155 project._Checkout(dl.commit)
Rob Ward18291012014-02-02 11:42:05 +0000156
Mike Frysinger05097c62020-03-22 13:36:27 -0400157 except GitError:
158 print('[%s] Could not complete the %s of %s'
159 % (project.name, mode, dl.commit), file=sys.stderr)
160 sys.exit(1)