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