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