5a052ee6a294014b6c2881b1c4fc9f7820a4401d
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / steps / preparechangelog.py
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
29 import logging
30 import re
31 import sys
32
33 from webkitpy.common.checkout.changelog import ChangeLog
34 from webkitpy.common.system.executive import ScriptError
35 from webkitpy.tool.steps.abstractstep import AbstractStep
36 from webkitpy.tool.steps.options import Options
37
38 _log = logging.getLogger(__name__)
39
40
41 class PrepareChangeLog(AbstractStep):
42     @classmethod
43     def options(cls):
44         return AbstractStep.options() + [
45             Options.quiet,
46             Options.email,
47             Options.git_commit,
48         ]
49
50     def _ensure_bug_url(self, state):
51         if not state.get("bug_id"):
52             return
53         bug_id = state.get("bug_id")
54         changelogs = self.cached_lookup(state, "changelogs")
55         for changelog_path in changelogs:
56             changelog = ChangeLog(changelog_path, self._tool.filesystem)
57             if not changelog.latest_entry().bug_id():
58                 changelog.set_short_description_and_bug_url(
59                     self.cached_lookup(state, "bug_title"),
60                     self._tool.bugs.bug_url_for_bug_id(bug_id))
61
62     def _resolve_existing_entry(self, changelog_path):
63         # When this is called, the top entry in the ChangeLog was just created
64         # by prepare-ChangeLog, as an clean updated version of the one below it.
65         with self._tool.filesystem.open_text_file_for_reading(changelog_path) as changelog_file:
66             entries_gen = ChangeLog.parse_entries_from_file(changelog_file)
67             entries = zip(entries_gen, range(2))
68
69         if not len(entries):
70             raise Exception("Expected to find at least two ChangeLog entries in %s but found none." % changelog_path)
71         if len(entries) == 1:
72             # If we get here, it probably means we've just rolled over to a
73             # new CL file, so we don't have anything to resolve.
74             return
75
76         (new_entry, _), (old_entry, _) = entries
77         final_entry = self._merge_entries(old_entry, new_entry)
78
79         changelog = ChangeLog(changelog_path, self._tool.filesystem)
80         changelog.delete_entries(2)
81         changelog.prepend_text(final_entry)
82
83     def _merge_entries(self, old_entry, new_entry):
84         final_entry = old_entry.contents()
85
86         new_date_line = new_entry.date_line()
87         old_date_line = old_entry.date_line()
88         if new_date_line != old_date_line:
89             final_entry = final_entry.replace(old_date_line, new_date_line)
90
91         new_bug_desc = new_entry.bug_description()
92         old_bug_desc = old_entry.bug_description()
93         if new_bug_desc and old_bug_desc and new_bug_desc != old_bug_desc:
94             final_entry = final_entry.replace(old_bug_desc, new_bug_desc)
95
96         new_touched = new_entry.touched_functions()
97         old_touched = old_entry.touched_functions()
98         if new_touched != old_touched:
99             if old_entry.is_touched_files_text_clean():
100                 final_entry = final_entry.replace(old_entry.touched_files_text(), new_entry.touched_files_text())
101             else:
102                 final_entry += "\n" + new_entry.touched_files_text()
103
104         return final_entry + "\n"
105
106     def run(self, state):
107         if self.cached_lookup(state, "changelogs"):
108             self._ensure_bug_url(state)
109
110         args = self._tool.deprecated_port().prepare_changelog_command()
111         if state.get("bug_id"):
112             args.append("--bug=%s" % state["bug_id"])
113             args.append("--description=%s" % self.cached_lookup(state, 'bug_title'))
114         if self._options.email:
115             args.append("--email=%s" % self._options.email)
116
117         if self._tool.scm().supports_local_commits():
118             args.append("--merge-base=%s" % self._tool.scm().merge_base(self._options.git_commit))
119
120         args.extend(self._changed_files(state))
121
122         try:
123             output = self._tool.executive.run_and_throw_if_fail(args, self._options.quiet, cwd=self._tool.scm().checkout_root)
124         except ScriptError, e:
125             _log.error("Unable to prepare ChangeLogs.")
126             sys.exit(1)
127
128         # These are the ChangeLog entries added by prepare-Changelog
129         changelogs = re.findall(r'Editing the (\S*/ChangeLog) file.', output)
130         changelogs = set(self._tool.filesystem.join(self._tool.scm().checkout_root, f) for f in changelogs)
131         for changelog in changelogs & set(self.cached_lookup(state, "changelogs")):
132             self._resolve_existing_entry(changelog)
133
134         self.did_modify_checkout(state)