blob: 61eadd541937365168f4758bc0a2154f8ddb90f9 [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
16import os
17import re
18import sys
19
20from command import Command
21
22CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
23
24class Download(Command):
25 common = True
26 helpSummary = "Download and checkout a change"
27 helpUsage = """
28%prog {project change[/patchset]}...
29"""
30 helpDescription = """
31The '%prog' command downloads a change from the review system and
32makes it available in your project's local working directory.
33"""
34
35 def _Options(self, p):
36 pass
37
38 def _ParseChangeIds(self, args):
Thiago Farina840ed0f2009-09-09 00:41:34 -040039 if not args:
40 self.Usage()
41
Shawn O. Pearce632768b2008-10-23 11:58:52 -070042 to_get = []
43 project = None
44
45 for a in args:
46 m = CHANGE_RE.match(a)
47 if m:
48 if not project:
49 self.Usage()
50 chg_id = int(m.group(1))
51 if m.group(2):
52 ps_id = int(m.group(2))
53 else:
54 ps_id = 1
55 to_get.append((project, chg_id, ps_id))
56 else:
57 project = self.GetProjects([a])[0]
58 return to_get
59
60 def Execute(self, opt, args):
61 for project, change_id, ps_id in self._ParseChangeIds(args):
62 dl = project.DownloadPatchSet(change_id, ps_id)
63 if not dl:
64 print >>sys.stderr, \
65 '[%s] change %d/%d not found' \
66 % (project.name, change_id, ps_id)
67 sys.exit(1)
68
69 if not dl.commits:
70 print >>sys.stderr, \
71 '[%s] change %d/%d has already been merged' \
72 % (project.name, change_id, ps_id)
73 continue
74
75 if len(dl.commits) > 1:
76 print >>sys.stderr, \
77 '[%s] %d/%d depends on %d unmerged changes:' \
78 % (project.name, change_id, ps_id, len(dl.commits))
79 for c in dl.commits:
80 print >>sys.stderr, ' %s' % (c)
81 project._Checkout(dl.commit)