REGRESSION(r259585) Text decoration color with value currentColor miscomputed in...
[WebKit-https.git] / Tools / Scripts / webkitpy / common / checkout / checkout.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 sys
30
31 if sys.version_info > (3, 0):
32     from functools import reduce
33
34 from webkitpy.common.config import urls
35 from webkitpy.common.checkout.changelog import ChangeLog, parse_bug_id_from_changelog
36 from webkitpy.common.checkout.commitinfo import CommitInfo
37 from webkitpy.common.checkout.scm import CommitMessage
38 from webkitpy.common.memoized import memoized
39 from webkitpy.common.system.executive import ScriptError
40 from webkitpy.common.unicode_compatibility import StringIO
41
42
43 # This class represents the WebKit-specific parts of the checkout (like ChangeLogs).
44 # FIXME: Move a bunch of ChangeLog-specific processing from SCM to this object.
45 # NOTE: All paths returned from this class should be absolute.
46 class Checkout(object):
47     def __init__(self, scm, executive=None, filesystem=None):
48         self._scm = scm
49         # FIXME: We shouldn't be grabbing at private members on scm.
50         self._executive = executive or self._scm._executive
51         self._filesystem = filesystem or self._scm._filesystem
52
53     def scripts_directory(self):
54         return self._filesystem.join(self._scm.checkout_root, "Tools", "Scripts")
55
56     def script_path(self, script_name):
57         return self._filesystem.join(self.scripts_directory(), script_name)
58
59     def is_path_to_changelog(self, path):
60         return self._filesystem.basename(path) == "ChangeLog"
61
62     def _latest_entry_for_changelog_at_revision(self, changelog_path, revision):
63         changelog_contents = self._scm.contents_at_revision(changelog_path, revision)
64         # contents_at_revision returns a byte array (str()), but we know
65         # that ChangeLog files are utf-8.  parse_latest_entry_from_file
66         # expects a file-like object which vends unicode(), so we decode here.
67         # Old revisions of Sources/WebKit/wx/ChangeLog have some invalid utf8 characters.
68         changelog_file = StringIO(changelog_contents.decode("utf-8", "ignore"))
69         return ChangeLog.parse_latest_entry_from_file(changelog_file)
70
71     def changelog_entries_for_revision(self, revision, changed_files=None):
72         if not changed_files:
73             changed_files = self._scm.changed_files_for_revision(revision)
74         # FIXME: This gets confused if ChangeLog files are moved, as
75         # deletes are still "changed files" per changed_files_for_revision.
76         # FIXME: For now we hack around this by caching any exceptions
77         # which result from having deleted files included the changed_files list.
78         changelog_entries = []
79         for path in changed_files:
80             if not self.is_path_to_changelog(path):
81                 continue
82             try:
83                 changelog_entries.append(self._latest_entry_for_changelog_at_revision(path, revision))
84             except ScriptError:
85                 pass
86         return changelog_entries
87
88     def _changelog_data_for_revision(self, revision):
89         changed_files = self._scm.changed_files_for_revision(revision)
90         changelog_entries = self.changelog_entries_for_revision(revision, changed_files=changed_files)
91         # Assume for now that the first entry has everything we need:
92         # FIXME: This will throw an exception if there were no ChangeLogs.
93         if not len(changelog_entries):
94             return None
95         changelog_entry = changelog_entries[0]
96         return {
97             "bug_id": changelog_entry.bug_id(),
98             "author_name": changelog_entry.author_name(),
99             "author_email": changelog_entry.author_email(),
100             "author": changelog_entry.author(),
101             "bug_description": changelog_entry.bug_description(),
102             "reviewer_text": changelog_entry.reviewer_text(),
103             "reviewer": changelog_entry.reviewer(),
104             "contents": changelog_entry.contents(),
105             "changed_files": changed_files,
106         }
107
108     @memoized
109     def commit_info_for_revision(self, revision):
110         committer_email = self._scm.committer_email_for_revision(revision)
111         changelog_data = self._changelog_data_for_revision(revision)
112         if not changelog_data:
113             return None
114         return CommitInfo(revision, committer_email, changelog_data)
115
116     def bug_id_for_revision(self, revision):
117         return self.commit_info_for_revision(revision).bug_id()
118
119     def _modified_files_matching_predicate(self, git_commit, predicate, changed_files=None):
120         # SCM returns paths relative to scm.checkout_root
121         # Callers (especially those using the ChangeLog class) may
122         # expect absolute paths, so this method returns absolute paths.
123         if not changed_files:
124             changed_files = self._scm.changed_files(git_commit)
125         return list(filter(predicate, list(map(self._scm.absolute_path, changed_files))))
126
127     def modified_changelogs(self, git_commit, changed_files=None):
128         return self._modified_files_matching_predicate(git_commit, self.is_path_to_changelog, changed_files=changed_files)
129
130     def modified_non_changelogs(self, git_commit, changed_files=None):
131         return self._modified_files_matching_predicate(git_commit, lambda path: not self.is_path_to_changelog(path), changed_files=changed_files)
132
133     def commit_message_for_this_commit(self, git_commit, changed_files=None, return_stderr=False):
134         changelog_paths = self.modified_changelogs(git_commit, changed_files)
135         if not len(changelog_paths):
136             raise ScriptError(message="Found no modified ChangeLogs, cannot create a commit message.\n"
137                               "All changes require a ChangeLog.  See:\n %s" % urls.contribution_guidelines)
138
139         message_text = self._scm.run([self.script_path('commit-log-editor'), '--print-log'] + changelog_paths, return_stderr=return_stderr)
140         return CommitMessage(message_text.splitlines())
141
142     def recent_commit_infos_for_files(self, paths):
143         revisions = set(sum(map(self._scm.revisions_changing_file, paths), []))
144         # Remove a None entry from the set. This can happen if a revision does have an associated ChangeLog entry (e.g. r185745).
145         return set(map(self.commit_info_for_revision, revisions)) - set([None])
146
147     def suggested_reviewers(self, git_commit, changed_files=None):
148         changed_files = self.modified_non_changelogs(git_commit, changed_files)
149         commit_infos = sorted(self.recent_commit_infos_for_files(changed_files), key=lambda info: info.revision(), reverse=True)
150         reviewers = filter(lambda person: person and person.can_review, sum(map(lambda info: [info.reviewer(), info.author()], commit_infos), []))
151         unique_reviewers = reduce(lambda suggestions, reviewer: suggestions + [reviewer if reviewer not in suggestions else None], reviewers, [])
152         return list(filter(lambda reviewer: reviewer, unique_reviewers))
153
154     def bug_id_for_this_commit(self, git_commit, changed_files=None):
155         try:
156             return parse_bug_id_from_changelog(self.commit_message_for_this_commit(git_commit, changed_files).message())
157         except ScriptError as e:
158             pass  # We might not have ChangeLogs.
159
160     def apply_patch(self, patch):
161         # It's possible that the patch was not made from the root directory.
162         # We should detect and handle that case.
163         # FIXME: Move _scm.script_path here once we get rid of all the dependencies.
164         # --force (continue after errors) is the common case, so we always use it.
165         args = [self.script_path('svn-apply'), "--force"]
166         if patch.reviewer():
167             args += ['--reviewer', patch.reviewer().full_name]
168         self._executive.run_command(args, input=patch.contents(), cwd=self._scm.checkout_root)
169
170     def apply_reverse_diff(self, revision):
171         self._scm.apply_reverse_diff(revision)
172
173         # We revert the ChangeLogs because removing lines from a ChangeLog
174         # doesn't make sense.  ChangeLogs are append only.
175         changelog_paths = self.modified_changelogs(git_commit=None)
176         if len(changelog_paths):
177             self._scm.revert_files(changelog_paths)
178
179         conflicts = self._scm.conflicted_files()
180         if len(conflicts):
181             raise ScriptError(message="Failed to apply reverse diff for revision %s because of the following conflicts:\n%s" % (revision, "\n".join(conflicts)))
182
183     def apply_reverse_diffs(self, revision_list):
184         for revision in sorted(revision_list, reverse=True):
185             self.apply_reverse_diff(revision)