cherry_pick.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Execute(self, opt, args):
  36. if len(args) != 1:
  37. self.Usage()
  38. reference = args[0]
  39. p = GitCommand(None,
  40. ['rev-parse', '--verify', reference],
  41. capture_stdout = True,
  42. capture_stderr = True)
  43. if p.Wait() != 0:
  44. print(p.stderr, file=sys.stderr)
  45. sys.exit(1)
  46. sha1 = p.stdout.strip()
  47. p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True)
  48. if p.Wait() != 0:
  49. print("error: Failed to retrieve old commit message", file=sys.stderr)
  50. sys.exit(1)
  51. old_msg = self._StripHeader(p.stdout)
  52. p = GitCommand(None,
  53. ['cherry-pick', sha1],
  54. capture_stdout = True,
  55. capture_stderr = True)
  56. status = p.Wait()
  57. print(p.stdout, file=sys.stdout)
  58. print(p.stderr, file=sys.stderr)
  59. if status == 0:
  60. # The cherry-pick was applied correctly. We just need to edit the
  61. # commit message.
  62. new_msg = self._Reformat(old_msg, sha1)
  63. p = GitCommand(None, ['commit', '--amend', '-F', '-'],
  64. provide_stdin = True,
  65. capture_stdout = True,
  66. capture_stderr = True)
  67. p.stdin.write(new_msg)
  68. p.stdin.close()
  69. if p.Wait() != 0:
  70. print("error: Failed to update commit message", file=sys.stderr)
  71. sys.exit(1)
  72. else:
  73. print('NOTE: When committing (please see above) and editing the commit '
  74. 'message, please remove the old Change-Id-line and add:')
  75. print(self._GetReference(sha1), file=sys.stderr)
  76. print(file=sys.stderr)
  77. def _IsChangeId(self, line):
  78. return CHANGE_ID_RE.match(line)
  79. def _GetReference(self, sha1):
  80. return "(cherry picked from commit %s)" % sha1
  81. def _StripHeader(self, commit_msg):
  82. lines = commit_msg.splitlines()
  83. return "\n".join(lines[lines.index("")+1:])
  84. def _Reformat(self, old_msg, sha1):
  85. new_msg = []
  86. for line in old_msg.splitlines():
  87. if not self._IsChangeId(line):
  88. new_msg.append(line)
  89. # Add a blank line between the message and the change id/reference
  90. try:
  91. if new_msg[-1].strip() != "":
  92. new_msg.append("")
  93. except IndexError:
  94. pass
  95. new_msg.append(self._GetReference(sha1))
  96. return "\n".join(new_msg)