cherry_pick.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2010 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import re
  18. import sys
  19. from command import Command
  20. from git_command import GitCommand
  21. CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$')
  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. def _Options(self, p):
  34. pass
  35. def ValidateOptions(self, opt, args):
  36. if len(args) != 1:
  37. self.Usage()
  38. def Execute(self, opt, args):
  39. reference = args[0]
  40. p = GitCommand(None,
  41. ['rev-parse', '--verify', reference],
  42. capture_stdout=True,
  43. capture_stderr=True)
  44. if p.Wait() != 0:
  45. print(p.stderr, file=sys.stderr)
  46. sys.exit(1)
  47. sha1 = p.stdout.strip()
  48. p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
  49. if p.Wait() != 0:
  50. print("error: Failed to retrieve old commit message", file=sys.stderr)
  51. sys.exit(1)
  52. old_msg = self._StripHeader(p.stdout)
  53. p = GitCommand(None,
  54. ['cherry-pick', sha1],
  55. capture_stdout=True,
  56. capture_stderr=True)
  57. status = p.Wait()
  58. print(p.stdout, file=sys.stdout)
  59. print(p.stderr, file=sys.stderr)
  60. if status == 0:
  61. # The cherry-pick was applied correctly. We just need to edit the
  62. # commit message.
  63. new_msg = self._Reformat(old_msg, sha1)
  64. p = GitCommand(None, ['commit', '--amend', '-F', '-'],
  65. provide_stdin=True,
  66. capture_stdout=True,
  67. capture_stderr=True)
  68. p.stdin.write(new_msg)
  69. p.stdin.close()
  70. if p.Wait() != 0:
  71. print("error: Failed to update commit message", file=sys.stderr)
  72. sys.exit(1)
  73. else:
  74. print('NOTE: When committing (please see above) and editing the commit '
  75. 'message, please remove the old Change-Id-line and add:')
  76. print(self._GetReference(sha1), file=sys.stderr)
  77. print(file=sys.stderr)
  78. def _IsChangeId(self, line):
  79. return CHANGE_ID_RE.match(line)
  80. def _GetReference(self, sha1):
  81. return "(cherry picked from commit %s)" % sha1
  82. def _StripHeader(self, commit_msg):
  83. lines = commit_msg.splitlines()
  84. return "\n".join(lines[lines.index("") + 1:])
  85. def _Reformat(self, old_msg, sha1):
  86. new_msg = []
  87. for line in old_msg.splitlines():
  88. if not self._IsChangeId(line):
  89. new_msg.append(line)
  90. # Add a blank line between the message and the change id/reference
  91. try:
  92. if new_msg[-1].strip() != "":
  93. new_msg.append("")
  94. except IndexError:
  95. pass
  96. new_msg.append(self._GetReference(sha1))
  97. return "\n".join(new_msg)