blob: 12d99526511630f239a0e762770b9f75cea392b4 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Shawn O. Pearce632768b2008-10-23 11:58:52 -07002#
3# Copyright (C) 2008 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Shawn O. Pearce632768b2008-10-23 11:58:52 -070018import re
19import sys
20
21from command import Command
Rob Ward18291012014-02-02 11:42:05 +000022from error import GitError
Shawn O. Pearce632768b2008-10-23 11:58:52 -070023
24CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
25
David Pursehouse819827a2020-02-12 15:20:19 +090026
Shawn O. Pearce632768b2008-10-23 11:58:52 -070027class Download(Command):
28 common = True
29 helpSummary = "Download and checkout a change"
30 helpUsage = """
Nicolas Cornu7482a962017-06-29 09:15:54 +020031%prog {[project] change[/patchset]}...
Shawn O. Pearce632768b2008-10-23 11:58:52 -070032"""
33 helpDescription = """
34The '%prog' command downloads a change from the review system and
35makes it available in your project's local working directory.
Nicolas Cornu7482a962017-06-29 09:15:54 +020036If no project is specified try to use current directory as a project.
Shawn O. Pearce632768b2008-10-23 11:58:52 -070037"""
38
39 def _Options(self, p):
David Pursehouse8f62fb72012-11-14 12:09:38 +090040 p.add_option('-c', '--cherry-pick',
Pierre Tardye5a21222011-03-24 16:28:18 +010041 dest='cherrypick', action='store_true',
42 help="cherry-pick instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090043 p.add_option('-r', '--revert',
Erwan Mahea94f1622011-08-19 13:56:09 +020044 dest='revert', action='store_true',
45 help="revert instead of checkout")
David Pursehouse8f62fb72012-11-14 12:09:38 +090046 p.add_option('-f', '--ff-only',
Pierre Tardy3d125942012-05-04 12:18:12 +020047 dest='ffonly', action='store_true',
48 help="force fast-forward merge")
Shawn O. Pearce632768b2008-10-23 11:58:52 -070049
50 def _ParseChangeIds(self, args):
Thiago Farinade8b2c42009-09-09 00:41:34 -040051 if not args:
52 self.Usage()
53
Shawn O. Pearce632768b2008-10-23 11:58:52 -070054 to_get = []
55 project = None
56
57 for a in args:
58 m = CHANGE_RE.match(a)
59 if m:
60 if not project:
Nicolas Cornu7482a962017-06-29 09:15:54 +020061 project = self.GetProjects(".")[0]
Shawn O. Pearce632768b2008-10-23 11:58:52 -070062 chg_id = int(m.group(1))
63 if m.group(2):
64 ps_id = int(m.group(2))
65 else:
66 ps_id = 1
Akshay Verma0f2e45a2018-03-24 12:27:05 +053067 refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
68 output = project._LsRemote(refs + '*')
Akshay Vermacf7c0832018-03-15 21:56:30 +053069 if output:
Akshay Verma0f2e45a2018-03-24 12:27:05 +053070 regex = refs + r'(\d+)'
Akshay Vermacf7c0832018-03-15 21:56:30 +053071 rcomp = re.compile(regex, re.I)
72 for line in output.splitlines():
73 match = rcomp.search(line)
74 if match:
75 ps_id = max(int(match.group(1)), ps_id)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070076 to_get.append((project, chg_id, ps_id))
77 else:
78 project = self.GetProjects([a])[0]
79 return to_get
80
81 def Execute(self, opt, args):
82 for project, change_id, ps_id in self._ParseChangeIds(args):
83 dl = project.DownloadPatchSet(change_id, ps_id)
84 if not dl:
Sarah Owenscecd1d82012-11-01 22:59:27 -070085 print('[%s] change %d/%d not found'
86 % (project.name, change_id, ps_id),
87 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070088 sys.exit(1)
89
Erwan Mahea94f1622011-08-19 13:56:09 +020090 if not opt.revert and not dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -070091 print('[%s] change %d/%d has already been merged'
92 % (project.name, change_id, ps_id),
93 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -070094 continue
95
96 if len(dl.commits) > 1:
David Pursehouse42339d72020-02-12 14:37:15 +090097 print('[%s] %d/%d depends on %d unmerged changes:'
Sarah Owenscecd1d82012-11-01 22:59:27 -070098 % (project.name, change_id, ps_id, len(dl.commits)),
99 file=sys.stderr)
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700100 for c in dl.commits:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700101 print(' %s' % (c), file=sys.stderr)
Pierre Tardye5a21222011-03-24 16:28:18 +0100102 if opt.cherrypick:
Rob Ward18291012014-02-02 11:42:05 +0000103 try:
Mike Frysingerea431762020-03-22 12:14:01 -0400104 project._CherryPick(dl.commit, ffonly=opt.ffonly)
Rob Ward18291012014-02-02 11:42:05 +0000105 except GitError:
David Pursehouse42339d72020-02-12 14:37:15 +0900106 print('[%s] Could not complete the cherry-pick of %s'
Rob Ward18291012014-02-02 11:42:05 +0000107 % (project.name, dl.commit), file=sys.stderr)
Scott Anderson0936aea2014-10-17 15:37:12 -0400108 sys.exit(1)
Rob Ward18291012014-02-02 11:42:05 +0000109
Erwan Mahea94f1622011-08-19 13:56:09 +0200110 elif opt.revert:
111 project._Revert(dl.commit)
Pierre Tardy3d125942012-05-04 12:18:12 +0200112 elif opt.ffonly:
113 project._FastForward(dl.commit, ffonly=True)
Pierre Tardye5a21222011-03-24 16:28:18 +0100114 else:
115 project._Checkout(dl.commit)