2010-11-11 19:36:39 +00:00
|
|
|
# Copyright (C) 2010 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2012-09-11 05:33:51 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2010-11-11 19:36:39 +00:00
|
|
|
from command import Command
|
|
|
|
from git_command import GitCommand
|
2023-08-08 21:12:53 +00:00
|
|
|
from error import GitError
|
2010-11-11 19:36:39 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
CHANGE_ID_RE = re.compile(r"^\s*Change-Id: I([0-9a-f]{40})\s*$")
|
2010-11-11 19:36:39 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2010-11-11 19:36:39 +00:00
|
|
|
class CherryPick(Command):
|
2023-03-11 06:46:20 +00:00
|
|
|
COMMON = True
|
|
|
|
helpSummary = "Cherry-pick a change."
|
|
|
|
helpUsage = """
|
2010-11-11 19:36:39 +00:00
|
|
|
%prog <sha1>
|
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
helpDescription = """
|
2010-11-11 19:36:39 +00:00
|
|
|
'%prog' cherry-picks a change from one branch to another.
|
|
|
|
The change id will be updated, and a reference to the old
|
|
|
|
change id will be added.
|
|
|
|
"""
|
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
|
|
|
if len(args) != 1:
|
|
|
|
self.Usage()
|
|
|
|
|
|
|
|
def Execute(self, opt, args):
|
|
|
|
reference = args[0]
|
|
|
|
|
|
|
|
p = GitCommand(
|
|
|
|
None,
|
|
|
|
["rev-parse", "--verify", reference],
|
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True,
|
2023-08-08 21:12:53 +00:00
|
|
|
verify_command=True,
|
2023-03-11 06:46:20 +00:00
|
|
|
)
|
2023-08-08 21:12:53 +00:00
|
|
|
try:
|
|
|
|
p.Wait()
|
|
|
|
except GitError:
|
2023-03-11 06:46:20 +00:00
|
|
|
print(p.stderr, file=sys.stderr)
|
2023-08-08 21:12:53 +00:00
|
|
|
raise
|
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
sha1 = p.stdout.strip()
|
|
|
|
|
2023-08-08 21:12:53 +00:00
|
|
|
p = GitCommand(
|
|
|
|
None,
|
|
|
|
["cat-file", "commit", sha1],
|
|
|
|
capture_stdout=True,
|
|
|
|
verify_command=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
p.Wait()
|
|
|
|
except GitError:
|
2023-03-11 06:46:20 +00:00
|
|
|
print(
|
|
|
|
"error: Failed to retrieve old commit message", file=sys.stderr
|
|
|
|
)
|
2023-08-08 21:12:53 +00:00
|
|
|
raise
|
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
old_msg = self._StripHeader(p.stdout)
|
|
|
|
|
|
|
|
p = GitCommand(
|
|
|
|
None,
|
|
|
|
["cherry-pick", sha1],
|
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True,
|
2023-08-08 21:12:53 +00:00
|
|
|
verify_command=True,
|
2023-03-11 06:46:20 +00:00
|
|
|
)
|
2023-08-08 21:12:53 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
p.Wait()
|
|
|
|
except GitError as e:
|
|
|
|
print(str(e))
|
|
|
|
print(
|
|
|
|
"NOTE: When committing (please see above) and editing the "
|
|
|
|
"commit message, please remove the old Change-Id-line and "
|
|
|
|
"add:"
|
|
|
|
)
|
|
|
|
print(self._GetReference(sha1), file=sys.stderr)
|
|
|
|
print(file=sys.stderr)
|
|
|
|
raise
|
2023-03-11 06:46:20 +00:00
|
|
|
|
|
|
|
if p.stdout:
|
|
|
|
print(p.stdout.strip(), file=sys.stdout)
|
|
|
|
if p.stderr:
|
|
|
|
print(p.stderr.strip(), file=sys.stderr)
|
|
|
|
|
2023-08-08 21:12:53 +00:00
|
|
|
# The cherry-pick was applied correctly. We just need to edit
|
|
|
|
# the commit message.
|
|
|
|
new_msg = self._Reformat(old_msg, sha1)
|
2023-03-11 06:46:20 +00:00
|
|
|
|
2023-08-08 21:12:53 +00:00
|
|
|
p = GitCommand(
|
|
|
|
None,
|
|
|
|
["commit", "--amend", "-F", "-"],
|
|
|
|
input=new_msg,
|
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True,
|
|
|
|
verify_command=True,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
p.Wait()
|
|
|
|
except GitError:
|
2023-03-11 06:46:20 +00:00
|
|
|
print(
|
2023-08-08 21:12:53 +00:00
|
|
|
"error: Failed to update commit message",
|
|
|
|
file=sys.stderr,
|
2023-03-11 06:46:20 +00:00
|
|
|
)
|
2023-08-08 21:12:53 +00:00
|
|
|
raise
|
2023-03-11 06:46:20 +00:00
|
|
|
|
|
|
|
def _IsChangeId(self, line):
|
|
|
|
return CHANGE_ID_RE.match(line)
|
|
|
|
|
|
|
|
def _GetReference(self, sha1):
|
|
|
|
return "(cherry picked from commit %s)" % sha1
|
|
|
|
|
|
|
|
def _StripHeader(self, commit_msg):
|
|
|
|
lines = commit_msg.splitlines()
|
|
|
|
return "\n".join(lines[lines.index("") + 1 :])
|
|
|
|
|
|
|
|
def _Reformat(self, old_msg, sha1):
|
|
|
|
new_msg = []
|
|
|
|
|
|
|
|
for line in old_msg.splitlines():
|
|
|
|
if not self._IsChangeId(line):
|
|
|
|
new_msg.append(line)
|
|
|
|
|
|
|
|
# Add a blank line between the message and the change id/reference.
|
|
|
|
try:
|
|
|
|
if new_msg[-1].strip() != "":
|
|
|
|
new_msg.append("")
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
new_msg.append(self._GetReference(sha1))
|
|
|
|
return "\n".join(new_msg)
|