Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 2 | # |
| 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 Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 18 | import re |
| 19 | import sys |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 20 | from command import Command |
| 21 | from git_command import GitCommand |
| 22 | |
| 23 | CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$') |
| 24 | |
| 25 | class 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. |
| 33 | The change id will be updated, and a reference to the old |
| 34 | change 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 Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 51 | print(p.stderr, file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 52 | 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 Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 57 | print("error: Failed to retrieve old commit message", file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 58 | 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 Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 67 | print(p.stdout, file=sys.stdout) |
| 68 | print(p.stderr, file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 69 | |
| 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 McIntosh | db75704 | 2015-06-01 11:17:13 -0400 | [diff] [blame] | 80 | p.stdin.close() |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 81 | if p.Wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 82 | print("error: Failed to update commit message", file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 83 | sys.exit(1) |
| 84 | |
| 85 | else: |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 86 | print('NOTE: When committing (please see above) and editing the commit ' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 87 | 'message, please remove the old Change-Id-line and add:') |
Conley Owens | 23bd3a1 | 2013-02-12 13:46:14 -0800 | [diff] [blame] | 88 | print(self._GetReference(sha1), file=sys.stderr) |
| 89 | print(file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 90 | |
| 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) |