blob: 3ad8210925c6e00f6ca1c2e673f40586dae785b2 [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
David Pursehouse819827a2020-02-12 15:20:19 +090025
Victor Boivied572a132010-11-11 20:36:39 +010026class CherryPick(Command):
27 common = True
28 helpSummary = "Cherry-pick a change."
29 helpUsage = """
30%prog <sha1>
31"""
32 helpDescription = """
33'%prog' cherry-picks a change from one branch to another.
34The change id will be updated, and a reference to the old
35change id will be added.
36"""
37
38 def _Options(self, p):
39 pass
40
Mike Frysingerae6cb082019-08-27 01:10:59 -040041 def ValidateOptions(self, opt, args):
Victor Boivied572a132010-11-11 20:36:39 +010042 if len(args) != 1:
43 self.Usage()
44
Mike Frysingerae6cb082019-08-27 01:10:59 -040045 def Execute(self, opt, args):
Victor Boivied572a132010-11-11 20:36:39 +010046 reference = args[0]
47
48 p = GitCommand(None,
49 ['rev-parse', '--verify', reference],
David Pursehousee5913ae2020-02-12 13:56:59 +090050 capture_stdout=True,
51 capture_stderr=True)
Victor Boivied572a132010-11-11 20:36:39 +010052 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070053 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010054 sys.exit(1)
55 sha1 = p.stdout.strip()
56
57 p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
58 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070059 print("error: Failed to retrieve old commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010060 sys.exit(1)
61 old_msg = self._StripHeader(p.stdout)
62
63 p = GitCommand(None,
64 ['cherry-pick', sha1],
David Pursehousee5913ae2020-02-12 13:56:59 +090065 capture_stdout=True,
66 capture_stderr=True)
Victor Boivied572a132010-11-11 20:36:39 +010067 status = p.Wait()
68
Sarah Owenscecd1d82012-11-01 22:59:27 -070069 print(p.stdout, file=sys.stdout)
70 print(p.stderr, file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010071
72 if status == 0:
73 # The cherry-pick was applied correctly. We just need to edit the
74 # commit message.
75 new_msg = self._Reformat(old_msg, sha1)
76
77 p = GitCommand(None, ['commit', '--amend', '-F', '-'],
David Pursehousee5913ae2020-02-12 13:56:59 +090078 provide_stdin=True,
79 capture_stdout=True,
80 capture_stderr=True)
Victor Boivied572a132010-11-11 20:36:39 +010081 p.stdin.write(new_msg)
Than McIntoshdb757042015-06-01 11:17:13 -040082 p.stdin.close()
Victor Boivied572a132010-11-11 20:36:39 +010083 if p.Wait() != 0:
Sarah Owenscecd1d82012-11-01 22:59:27 -070084 print("error: Failed to update commit message", file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010085 sys.exit(1)
86
87 else:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090088 print('NOTE: When committing (please see above) and editing the commit '
Sarah Owenscecd1d82012-11-01 22:59:27 -070089 'message, please remove the old Change-Id-line and add:')
Conley Owens23bd3a12013-02-12 13:46:14 -080090 print(self._GetReference(sha1), file=sys.stderr)
91 print(file=sys.stderr)
Victor Boivied572a132010-11-11 20:36:39 +010092
93 def _IsChangeId(self, line):
94 return CHANGE_ID_RE.match(line)
95
96 def _GetReference(self, sha1):
97 return "(cherry picked from commit %s)" % sha1
98
99 def _StripHeader(self, commit_msg):
100 lines = commit_msg.splitlines()
David Pursehouse54a4e602020-02-12 14:31:05 +0900101 return "\n".join(lines[lines.index("") + 1:])
Victor Boivied572a132010-11-11 20:36:39 +0100102
103 def _Reformat(self, old_msg, sha1):
104 new_msg = []
105
106 for line in old_msg.splitlines():
107 if not self._IsChangeId(line):
108 new_msg.append(line)
109
110 # Add a blank line between the message and the change id/reference
111 try:
112 if new_msg[-1].strip() != "":
113 new_msg.append("")
114 except IndexError:
115 pass
116
117 new_msg.append(self._GetReference(sha1))
118 return "\n".join(new_msg)