blob: 43215f91ac2578bd3c2ae08051a7c37b28a58772 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Victor Boivied572a132010-11-11 20:36:39 +01002#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
David Pursehousee00aa6b2012-09-11 14:33:51 +090018import re
19import sys
Victor Boivied572a132010-11-11 20:36:39 +010020from command import Command
21from git_command import GitCommand
22
23CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$')
24
25class CherryPick(Command):
26 common = True
27 helpSummary = "Cherry-pick a change."
28 helpUsage = """
29%prog <sha1>
30"""
31 helpDescription = """
32'%prog' cherry-picks a change from one branch to another.
33The change id will be updated, and a reference to the old
34change id will be added.
35"""
36
37 def _Options(self, p):
38 pass
39
40 def Execute(self, opt, args):
41 if len(args) != 1:
42 self.Usage()
43
44 reference = args[0]
45
46 p = GitCommand(None,
47 ['rev-parse', '--verify', reference],
48 capture_stdout = True,
49 capture_stderr = True)
50 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070051 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010052 sys.exit(1)
53 sha1 = p.stdout.strip()
54
55 p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
56 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070057 print("error: Failed to retrieve old commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010058 sys.exit(1)
59 old_msg = self._StripHeader(p.stdout)
60
61 p = GitCommand(None,
62 ['cherry-pick', sha1],
63 capture_stdout = True,
64 capture_stderr = True)
65 status = p.Wait()
66
Sarah Owenscecd1d82012-11-01 22:59:27 -070067 print(p.stdout, file=sys.stdout)
68 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010069
70 if status == 0:
71 # The cherry-pick was applied correctly. We just need to edit the
72 # commit message.
73 new_msg = self._Reformat(old_msg, sha1)
74
75 p = GitCommand(None, ['commit', '--amend', '-F', '-'],
76 provide_stdin = True,
77 capture_stdout = True,
78 capture_stderr = True)
79 p.stdin.write(new_msg)
Than McIntoshdb757042015-06-01 11:17:13 -040080 p.stdin.close()
Victor Boivied572a132010-11-11 20:36:39 +010081 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070082 print("error: Failed to update commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010083 sys.exit(1)
84
85 else:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090086 print('NOTE: When committing (please see above) and editing the commit '
Sarah Owenscecd1d82012-11-01 22:59:27 -070087 'message, please remove the old Change-Id-line and add:')
Conley Owens23bd3a12013-02-12 13:46:14 -080088 print(self._GetReference(sha1), file=sys.stderr)
89 print(file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010090
91 def _IsChangeId(self, line):
92 return CHANGE_ID_RE.match(line)
93
94 def _GetReference(self, sha1):
95 return "(cherry picked from commit %s)" % sha1
96
97 def _StripHeader(self, commit_msg):
98 lines = commit_msg.splitlines()
99 return "\n".join(lines[lines.index("")+1:])
100
101 def _Reformat(self, old_msg, sha1):
102 new_msg = []
103
104 for line in old_msg.splitlines():
105 if not self._IsChangeId(line):
106 new_msg.append(line)
107
108 # Add a blank line between the message and the change id/reference
109 try:
110 if new_msg[-1].strip() != "":
111 new_msg.append("")
112 except IndexError:
113 pass
114
115 new_msg.append(self._GetReference(sha1))
116 return "\n".join(new_msg)