blob: 7a6d4c2014e92f030c0d5a4e3158aa1be6cca022 [file] [log] [blame]
Victor Boivied572a132010-11-11 20:36:39 +01001#
2# Copyright (C) 2010 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
David Pursehousee00aa6b2012-09-11 14:33:51 +090016import re
17import sys
Victor Boivied572a132010-11-11 20:36:39 +010018from command import Command
19from git_command import GitCommand
20
21CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$')
22
23class CherryPick(Command):
24 common = True
25 helpSummary = "Cherry-pick a change."
26 helpUsage = """
27%prog <sha1>
28"""
29 helpDescription = """
30'%prog' cherry-picks a change from one branch to another.
31The change id will be updated, and a reference to the old
32change id will be added.
33"""
34
35 def _Options(self, p):
36 pass
37
38 def Execute(self, opt, args):
39 if len(args) != 1:
40 self.Usage()
41
42 reference = args[0]
43
44 p = GitCommand(None,
45 ['rev-parse', '--verify', reference],
46 capture_stdout = True,
47 capture_stderr = True)
48 if p.Wait() != 0:
49 print >>sys.stderr, p.stderr
50 sys.exit(1)
51 sha1 = p.stdout.strip()
52
53 p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
54 if p.Wait() != 0:
55 print >>sys.stderr, "error: Failed to retrieve old commit message"
56 sys.exit(1)
57 old_msg = self._StripHeader(p.stdout)
58
59 p = GitCommand(None,
60 ['cherry-pick', sha1],
61 capture_stdout = True,
62 capture_stderr = True)
63 status = p.Wait()
64
65 print >>sys.stdout, p.stdout
66 print >>sys.stderr, p.stderr
67
68 if status == 0:
69 # The cherry-pick was applied correctly. We just need to edit the
70 # commit message.
71 new_msg = self._Reformat(old_msg, sha1)
72
73 p = GitCommand(None, ['commit', '--amend', '-F', '-'],
74 provide_stdin = True,
75 capture_stdout = True,
76 capture_stderr = True)
77 p.stdin.write(new_msg)
78 if p.Wait() != 0:
79 print >>sys.stderr, "error: Failed to update commit message"
80 sys.exit(1)
81
82 else:
83 print >>sys.stderr, """\
84NOTE: When committing (please see above) and editing the commit message,
85please remove the old Change-Id-line and add:
86"""
87 print >>sys.stderr, self._GetReference(sha1)
88 print >>sys.stderr
89
90 def _IsChangeId(self, line):
91 return CHANGE_ID_RE.match(line)
92
93 def _GetReference(self, sha1):
94 return "(cherry picked from commit %s)" % sha1
95
96 def _StripHeader(self, commit_msg):
97 lines = commit_msg.splitlines()
98 return "\n".join(lines[lines.index("")+1:])
99
100 def _Reformat(self, old_msg, sha1):
101 new_msg = []
102
103 for line in old_msg.splitlines():
104 if not self._IsChangeId(line):
105 new_msg.append(line)
106
107 # Add a blank line between the message and the change id/reference
108 try:
109 if new_msg[-1].strip() != "":
110 new_msg.append("")
111 except IndexError:
112 pass
113
114 new_msg.append(self._GetReference(sha1))
115 return "\n".join(new_msg)