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