preparechangelog.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import logging
  29. import re
  30. import sys
  31. from webkitpy.common.checkout.changelog import ChangeLog
  32. from webkitpy.common.system.executive import ScriptError
  33. from webkitpy.tool.steps.abstractstep import AbstractStep
  34. from webkitpy.tool.steps.options import Options
  35. _log = logging.getLogger(__name__)
  36. class PrepareChangeLog(AbstractStep):
  37. @classmethod
  38. def options(cls):
  39. return AbstractStep.options() + [
  40. Options.quiet,
  41. Options.email,
  42. Options.git_commit,
  43. Options.update_changelogs,
  44. ]
  45. def _ensure_bug_url(self, state):
  46. if not state.get("bug_id"):
  47. return
  48. bug_id = state.get("bug_id")
  49. changelogs = self.cached_lookup(state, "changelogs")
  50. for changelog_path in changelogs:
  51. changelog = ChangeLog(changelog_path, self._tool.filesystem)
  52. if not changelog.latest_entry().bug_id():
  53. changelog.set_short_description_and_bug_url(
  54. self.cached_lookup(state, "bug_title"),
  55. self._tool.bugs.bug_url_for_bug_id(bug_id))
  56. def _resolve_existing_entry(self, changelog_path):
  57. # When this is called, the top entry in the ChangeLog was just created
  58. # by prepare-ChangeLog, as an clean updated version of the one below it.
  59. with self._tool.filesystem.open_text_file_for_reading(changelog_path) as changelog_file:
  60. entries_gen = ChangeLog.parse_entries_from_file(changelog_file)
  61. entries = zip(entries_gen, range(2))
  62. if not len(entries):
  63. raise Exception("Expected to find at least two ChangeLog entries in %s but found none." % changelog_path)
  64. if len(entries) == 1:
  65. # If we get here, it probably means we've just rolled over to a
  66. # new CL file, so we don't have anything to resolve.
  67. return
  68. (new_entry, _), (old_entry, _) = entries
  69. final_entry = self._merge_entries(old_entry, new_entry)
  70. changelog = ChangeLog(changelog_path, self._tool.filesystem)
  71. changelog.delete_entries(2)
  72. changelog.prepend_text(final_entry)
  73. def _merge_entries(self, old_entry, new_entry):
  74. final_entry = old_entry.contents()
  75. final_entry = final_entry.replace(old_entry.date(), new_entry.date(), 1)
  76. new_bug_desc = new_entry.bug_description()
  77. old_bug_desc = old_entry.bug_description()
  78. if new_bug_desc and old_bug_desc and new_bug_desc != old_bug_desc:
  79. final_entry = final_entry.replace(old_bug_desc, new_bug_desc)
  80. new_touched = new_entry.touched_functions()
  81. old_touched = old_entry.touched_functions()
  82. if new_touched != old_touched:
  83. if old_entry.is_touched_files_text_clean():
  84. final_entry = final_entry.replace(old_entry.touched_files_text(), new_entry.touched_files_text())
  85. else:
  86. final_entry += "\n" + new_entry.touched_files_text()
  87. return final_entry + "\n"
  88. def run(self, state):
  89. if self.cached_lookup(state, "changelogs"):
  90. self._ensure_bug_url(state)
  91. if not self._options.update_changelogs:
  92. return
  93. args = self._tool.deprecated_port().prepare_changelog_command()
  94. if state.get("bug_id"):
  95. args.append("--bug=%s" % state["bug_id"])
  96. args.append("--description=%s" % self.cached_lookup(state, 'bug_title'))
  97. if self._options.email:
  98. args.append("--email=%s" % self._options.email)
  99. if self._tool.scm().supports_local_commits():
  100. args.append("--merge-base=%s" % self._tool.scm().merge_base(self._options.git_commit))
  101. args.extend(self._changed_files(state))
  102. try:
  103. output = self._tool.executive.run_and_throw_if_fail(args, self._options.quiet, cwd=self._tool.scm().checkout_root)
  104. except ScriptError, e:
  105. _log.error("Unable to prepare ChangeLogs.")
  106. sys.exit(1)
  107. # These are the ChangeLog entries added by prepare-Changelog
  108. changelogs = re.findall(r'Editing the (\S*/ChangeLog) file.', output)
  109. changelogs = set(self._tool.filesystem.join(self._tool.scm().checkout_root, f) for f in changelogs)
  110. for changelog in changelogs & set(self.cached_lookup(state, "changelogs")):
  111. self._resolve_existing_entry(changelog)
  112. self.did_modify_checkout(state)