diff options
-rw-r--r-- | project.py | 42 | ||||
-rw-r--r-- | subcmds/download.py | 78 |
2 files changed, 120 insertions, 0 deletions
@@ -45,6 +45,31 @@ def _info(fmt, *args): | |||
45 | def not_rev(r): | 45 | def not_rev(r): |
46 | return '^' + r | 46 | return '^' + r |
47 | 47 | ||
48 | class DownloadedChange(object): | ||
49 | _commit_cache = None | ||
50 | |||
51 | def __init__(self, project, base, change_id, ps_id, commit): | ||
52 | self.project = project | ||
53 | self.base = base | ||
54 | self.change_id = change_id | ||
55 | self.ps_id = ps_id | ||
56 | self.commit = commit | ||
57 | |||
58 | @property | ||
59 | def commits(self): | ||
60 | if self._commit_cache is None: | ||
61 | self._commit_cache = self.project.bare_git.rev_list( | ||
62 | '--abbrev=8', | ||
63 | '--abbrev-commit', | ||
64 | '--pretty=oneline', | ||
65 | '--reverse', | ||
66 | '--date-order', | ||
67 | not_rev(self.base), | ||
68 | self.commit, | ||
69 | '--') | ||
70 | return self._commit_cache | ||
71 | |||
72 | |||
48 | class ReviewableBranch(object): | 73 | class ReviewableBranch(object): |
49 | _commit_cache = None | 74 | _commit_cache = None |
50 | 75 | ||
@@ -612,6 +637,23 @@ class Project(object): | |||
612 | src = os.path.join(self.worktree, src) | 637 | src = os.path.join(self.worktree, src) |
613 | self.copyfiles.append(_CopyFile(src, dest)) | 638 | self.copyfiles.append(_CopyFile(src, dest)) |
614 | 639 | ||
640 | def DownloadPatchSet(self, change_id, patch_id): | ||
641 | """Download a single patch set of a single change to FETCH_HEAD. | ||
642 | """ | ||
643 | remote = self.GetRemote(self.remote.name) | ||
644 | |||
645 | cmd = ['fetch', remote.name] | ||
646 | cmd.append('refs/changes/%2.2d/%d/%d' \ | ||
647 | % (change_id % 100, change_id, patch_id)) | ||
648 | cmd.extend(map(lambda x: str(x), remote.fetch)) | ||
649 | if GitCommand(self, cmd, bare=True).Wait() != 0: | ||
650 | return None | ||
651 | return DownloadedChange(self, | ||
652 | remote.ToLocal(self.revision), | ||
653 | change_id, | ||
654 | patch_id, | ||
655 | self.bare_git.rev_parse('FETCH_HEAD')) | ||
656 | |||
615 | 657 | ||
616 | ## Branch Management ## | 658 | ## Branch Management ## |
617 | 659 | ||
diff --git a/subcmds/download.py b/subcmds/download.py new file mode 100644 index 00000000..a6f3aa45 --- /dev/null +++ b/subcmds/download.py | |||
@@ -0,0 +1,78 @@ | |||
1 | # | ||
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 | |||
16 | import os | ||
17 | import re | ||
18 | import sys | ||
19 | |||
20 | from command import Command | ||
21 | |||
22 | CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$') | ||
23 | |||
24 | class Download(Command): | ||
25 | common = True | ||
26 | helpSummary = "Download and checkout a change" | ||
27 | helpUsage = """ | ||
28 | %prog {project change[/patchset]}... | ||
29 | """ | ||
30 | helpDescription = """ | ||
31 | The '%prog' command downloads a change from the review system and | ||
32 | makes it available in your project's local working directory. | ||
33 | """ | ||
34 | |||
35 | def _Options(self, p): | ||
36 | pass | ||
37 | |||
38 | def _ParseChangeIds(self, args): | ||
39 | to_get = [] | ||
40 | project = None | ||
41 | |||
42 | for a in args: | ||
43 | m = CHANGE_RE.match(a) | ||
44 | if m: | ||
45 | if not project: | ||
46 | self.Usage() | ||
47 | chg_id = int(m.group(1)) | ||
48 | if m.group(2): | ||
49 | ps_id = int(m.group(2)) | ||
50 | else: | ||
51 | ps_id = 1 | ||
52 | to_get.append((project, chg_id, ps_id)) | ||
53 | else: | ||
54 | project = self.GetProjects([a])[0] | ||
55 | return to_get | ||
56 | |||
57 | def Execute(self, opt, args): | ||
58 | for project, change_id, ps_id in self._ParseChangeIds(args): | ||
59 | dl = project.DownloadPatchSet(change_id, ps_id) | ||
60 | if not dl: | ||
61 | print >>sys.stderr, \ | ||
62 | '[%s] change %d/%d not found' \ | ||
63 | % (project.name, change_id, ps_id) | ||
64 | sys.exit(1) | ||
65 | |||
66 | if not dl.commits: | ||
67 | print >>sys.stderr, \ | ||
68 | '[%s] change %d/%d has already been merged' \ | ||
69 | % (project.name, change_id, ps_id) | ||
70 | continue | ||
71 | |||
72 | if len(dl.commits) > 1: | ||
73 | print >>sys.stderr, \ | ||
74 | '[%s] %d/%d depends on %d unmerged changes:' \ | ||
75 | % (project.name, change_id, ps_id, len(dl.commits)) | ||
76 | for c in dl.commits: | ||
77 | print >>sys.stderr, ' %s' % (c) | ||
78 | project._Checkout(dl.commit) | ||