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