1 # Copyright (c) 2010 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
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
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):
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
51 def is_path_to_changelog(self, path):
52 return self._filesystem.basename(path) == "ChangeLog"
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)
63 def changelog_entries_for_revision(self, revision, changed_files=None):
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):
75 changelog_entries.append(self._latest_entry_for_changelog_at_revision(path, revision))
78 return changelog_entries
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):
87 changelog_entry = changelog_entries[0]
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,
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:
105 return CommitInfo(revision, committer_email, changelog_data)
107 def bug_id_for_revision(self, revision):
108 return self.commit_info_for_revision(revision).bug_id()
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))
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)
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)
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)
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())
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))
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))
144 def bug_id_for_this_commit(self, git_commit, changed_files=None):
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.
150 def chromium_deps(self):
151 return DEPS(self._scm.absolute_path(self._filesystem.join("Source", "WebKit", "chromium", "DEPS")))
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')]
159 args += ['--reviewer', patch.reviewer().full_name]
161 args.append('--force')
162 self._executive.run_command(args, input=patch.contents())
164 def apply_reverse_diff(self, revision):
165 self._scm.apply_reverse_diff(revision)
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)
173 conflicts = self._scm.conflicted_files()
175 raise ScriptError(message="Failed to apply reverse diff for revision %s because of the following conflicts:\n%s" % (revision, "\n".join(conflicts)))
177 def apply_reverse_diffs(self, revision_list):
178 for revision in sorted(revision_list, reverse=True):
179 self.apply_reverse_diff(revision)