cherry_pick.py 3.3 KB

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