cherry_pick.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2010 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. import sys
  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 ValidateOptions(self, opt, args):
  33. if len(args) != 1:
  34. self.Usage()
  35. def Execute(self, opt, args):
  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(p.stderr, file=sys.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("error: Failed to retrieve old commit message", file=sys.stderr)
  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(p.stdout, file=sys.stdout)
  56. print(p.stderr, file=sys.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. p.stdin.close()
  67. if p.Wait() != 0:
  68. print("error: Failed to update commit message", file=sys.stderr)
  69. sys.exit(1)
  70. else:
  71. print('NOTE: When committing (please see above) and editing the commit '
  72. 'message, please remove the old Change-Id-line and add:')
  73. print(self._GetReference(sha1), file=sys.stderr)
  74. print(file=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)