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 | |
| 16 | import sys, re, string, random, os |
| 17 | from command import Command |
| 18 | from git_command import GitCommand |
| 19 | |
| 20 | CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$') |
| 21 | |
| 22 | class CherryPick(Command): |
| 23 | common = True |
| 24 | helpSummary = "Cherry-pick a change." |
| 25 | helpUsage = """ |
| 26 | %prog <sha1> |
| 27 | """ |
| 28 | helpDescription = """ |
| 29 | '%prog' cherry-picks a change from one branch to another. |
| 30 | The change id will be updated, and a reference to the old |
| 31 | change id will be added. |
| 32 | """ |
| 33 | |
| 34 | def _Options(self, p): |
| 35 | pass |
| 36 | |
| 37 | def Execute(self, opt, args): |
| 38 | if len(args) != 1: |
| 39 | self.Usage() |
| 40 | |
| 41 | reference = args[0] |
| 42 | |
| 43 | p = GitCommand(None, |
| 44 | ['rev-parse', '--verify', reference], |
| 45 | capture_stdout = True, |
| 46 | capture_stderr = True) |
| 47 | if p.Wait() != 0: |
| 48 | print >>sys.stderr, p.stderr |
| 49 | sys.exit(1) |
| 50 | sha1 = p.stdout.strip() |
| 51 | |
| 52 | p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True) |
| 53 | if p.Wait() != 0: |
| 54 | print >>sys.stderr, "error: Failed to retrieve old commit message" |
| 55 | sys.exit(1) |
| 56 | old_msg = self._StripHeader(p.stdout) |
| 57 | |
| 58 | p = GitCommand(None, |
| 59 | ['cherry-pick', sha1], |
| 60 | capture_stdout = True, |
| 61 | capture_stderr = True) |
| 62 | status = p.Wait() |
| 63 | |
| 64 | print >>sys.stdout, p.stdout |
| 65 | print >>sys.stderr, p.stderr |
| 66 | |
| 67 | if status == 0: |
| 68 | # The cherry-pick was applied correctly. We just need to edit the |
| 69 | # commit message. |
| 70 | new_msg = self._Reformat(old_msg, sha1) |
| 71 | |
| 72 | p = GitCommand(None, ['commit', '--amend', '-F', '-'], |
| 73 | provide_stdin = True, |
| 74 | capture_stdout = True, |
| 75 | capture_stderr = True) |
| 76 | p.stdin.write(new_msg) |
| 77 | if p.Wait() != 0: |
| 78 | print >>sys.stderr, "error: Failed to update commit message" |
| 79 | sys.exit(1) |
| 80 | |
| 81 | else: |
| 82 | print >>sys.stderr, """\ |
| 83 | NOTE: When committing (please see above) and editing the commit message, |
| 84 | please remove the old Change-Id-line and add: |
| 85 | """ |
| 86 | print >>sys.stderr, self._GetReference(sha1) |
| 87 | print >>sys.stderr |
| 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) |