blob: 1f7dffdc6a05ed5e6524c58afde9e53f4795bfdc [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)
Than McIntoshdb757042015-06-01 11:17:13 -040079 p.stdin.close()
Victor Boivied572a132010-11-11 20:36:39 +010080 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070081 print("error: Failed to update commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010082 sys.exit(1)
83
84 else:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090085 print('NOTE: When committing (please see above) and editing the commit '
Sarah Owenscecd1d82012-11-01 22:59:27 -070086 'message, please remove the old Change-Id-line and add:')
Conley Owens23bd3a12013-02-12 13:46:14 -080087 print(self._GetReference(sha1), file=sys.stderr)
88 print(file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010089
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)