blob: a541a040355bff4a0289324c82cd8cbb9793e576 [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
Mike Frysingerae6cb082019-08-27 01:10:59 -040040 def ValidateOptions(self, opt, args):
Victor Boivied572a132010-11-11 20:36:39 +010041 if len(args) != 1:
42 self.Usage()
43
Mike Frysingerae6cb082019-08-27 01:10:59 -040044 def Execute(self, opt, args):
Victor Boivied572a132010-11-11 20:36:39 +010045 reference = args[0]
46
47 p = GitCommand(None,
48 ['rev-parse', '--verify', reference],
49 capture_stdout = True,
50 capture_stderr = True)
51 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070052 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010053 sys.exit(1)
54 sha1 = p.stdout.strip()
55
56 p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
57 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070058 print("error: Failed to retrieve old commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010059 sys.exit(1)
60 old_msg = self._StripHeader(p.stdout)
61
62 p = GitCommand(None,
63 ['cherry-pick', sha1],
64 capture_stdout = True,
65 capture_stderr = True)
66 status = p.Wait()
67
Sarah Owenscecd1d82012-11-01 22:59:27 -070068 print(p.stdout, file=sys.stdout)
69 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010070
71 if status == 0:
72 # The cherry-pick was applied correctly. We just need to edit the
73 # commit message.
74 new_msg = self._Reformat(old_msg, sha1)
75
76 p = GitCommand(None, ['commit', '--amend', '-F', '-'],
77 provide_stdin = True,
78 capture_stdout = True,
79 capture_stderr = True)
80 p.stdin.write(new_msg)
Than McIntoshdb757042015-06-01 11:17:13 -040081 p.stdin.close()
Victor Boivied572a132010-11-11 20:36:39 +010082 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070083 print("error: Failed to update commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010084 sys.exit(1)
85
86 else:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090087 print('NOTE: When committing (please see above) and editing the commit '
Sarah Owenscecd1d82012-11-01 22:59:27 -070088 'message, please remove the old Change-Id-line and add:')
Conley Owens23bd3a12013-02-12 13:46:14 -080089 print(self._GetReference(sha1), file=sys.stderr)
90 print(file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010091
92 def _IsChangeId(self, line):
93 return CHANGE_ID_RE.match(line)
94
95 def _GetReference(self, sha1):
96 return "(cherry picked from commit %s)" % sha1
97
98 def _StripHeader(self, commit_msg):
99 lines = commit_msg.splitlines()
100 return "\n".join(lines[lines.index("")+1:])
101
102 def _Reformat(self, old_msg, sha1):
103 new_msg = []
104
105 for line in old_msg.splitlines():
106 if not self._IsChangeId(line):
107 new_msg.append(line)
108
109 # Add a blank line between the message and the change id/reference
110 try:
111 if new_msg[-1].strip() != "":
112 new_msg.append("")
113 except IndexError:
114 pass
115
116 new_msg.append(self._GetReference(sha1))
117 return "\n".join(new_msg)