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