cherry_pick.py 3.3 KB

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