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) |
| 79 | if p.Wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 80 | print("error: Failed to update commit message", file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 81 | sys.exit(1) |
| 82 | |
| 83 | else: |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 84 | print('NOTE: When committing (please see above) and editing the commit ' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 85 | 'message, please remove the old Change-Id-line and add:') |
Conley Owens | 23bd3a1 | 2013-02-12 13:46:14 -0800 | [diff] [blame] | 86 | print(self._GetReference(sha1), file=sys.stderr) |
| 87 | print(file=sys.stderr) |
Victor Boivie | d572a13 | 2010-11-11 20:36:39 +0100 | [diff] [blame] | 88 | |
| 89 | def _IsChangeId(self, line): |
| 90 | return CHANGE_ID_RE.match(line) |
| 91 | |
| 92 | def _GetReference(self, sha1): |
| 93 | return "(cherry picked from commit %s)" % sha1 |
| 94 | |
| 95 | def _StripHeader(self, commit_msg): |
| 96 | lines = commit_msg.splitlines() |
| 97 | return "\n".join(lines[lines.index("")+1:]) |
| 98 | |
| 99 | def _Reformat(self, old_msg, sha1): |
| 100 | new_msg = [] |
| 101 | |
| 102 | for line in old_msg.splitlines(): |
| 103 | if not self._IsChangeId(line): |
| 104 | new_msg.append(line) |
| 105 | |
| 106 | # Add a blank line between the message and the change id/reference |
| 107 | try: |
| 108 | if new_msg[-1].strip() != "": |
| 109 | new_msg.append("") |
| 110 | except IndexError: |
| 111 | pass |
| 112 | |
| 113 | new_msg.append(self._GetReference(sha1)) |
| 114 | return "\n".join(new_msg) |