OLD | NEW |
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import base64 | 5 import base64 |
6 import xml.dom.minidom as minidom | 6 import xml.dom.minidom as minidom |
7 from xml.parsers.expat import ExpatError | 7 from xml.parsers.expat import ExpatError |
8 | 8 |
9 import crash_utils | 9 import crash_utils |
10 from repository_parser_interface import ParserInterface | 10 from repository_parser_interface import ParserInterface |
11 | 11 |
12 FILE_CHANGE_TYPE_MAP = { | 12 FILE_CHANGE_TYPE_MAP = { |
13 'add': 'A', | 13 'add': 'A', |
| 14 'copy': 'C', |
14 'delete': 'D', | 15 'delete': 'D', |
15 'modify': 'M' | 16 'modify': 'M', |
| 17 'rename': 'R' |
16 } | 18 } |
17 | 19 |
18 | 20 |
| 21 def _ConvertToFileChangeType(file_action): |
| 22 # TODO(stgao): verify impact on code that checks the file change type. |
| 23 return file_action[0].upper() |
| 24 |
| 25 |
19 class GitParser(ParserInterface): | 26 class GitParser(ParserInterface): |
20 """Parser for Git repository in googlesource. | 27 """Parser for Git repository in googlesource. |
21 | 28 |
22 Attributes: | 29 Attributes: |
23 parsed_deps: A map from component path to its repository name, regression, | 30 parsed_deps: A map from component path to its repository name, regression, |
24 etc. | 31 etc. |
25 url_parts_map: A map from url type to its url parts. This parts are added | 32 url_parts_map: A map from url type to its url parts. This parts are added |
26 the base url to form different urls. | 33 the base url to form different urls. |
27 """ | 34 """ |
28 | 35 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 103 |
97 # Go through changed files, they are in li. | 104 # Go through changed files, they are in li. |
98 lis = ul.getElementsByTagName('li') | 105 lis = ul.getElementsByTagName('li') |
99 for li in lis: | 106 for li in lis: |
100 # Retrieve path and action of the changed file | 107 # Retrieve path and action of the changed file |
101 file_path = li.getElementsByTagName('a')[0].firstChild.nodeValue | 108 file_path = li.getElementsByTagName('a')[0].firstChild.nodeValue |
102 file_change_type = li.getElementsByTagName('span')[ | 109 file_change_type = li.getElementsByTagName('span')[ |
103 0].getAttribute('class') | 110 0].getAttribute('class') |
104 | 111 |
105 # Normalize file action so that it is same as SVN parser. | 112 # Normalize file action so that it is same as SVN parser. |
106 file_change_type = FILE_CHANGE_TYPE_MAP[file_change_type] | 113 file_change_type = _ConvertToFileChangeType(file_change_type) |
107 | 114 |
108 # Add the changed file to the map. | 115 # Add the changed file to the map. |
109 if file_path not in file_to_revision_map: | 116 if file_path not in file_to_revision_map: |
110 file_to_revision_map[file_path] = [] | 117 file_to_revision_map[file_path] = [] |
111 file_to_revision_map[file_path].append((githash, file_change_type)) | 118 file_to_revision_map[file_path].append((githash, file_change_type)) |
112 | 119 |
113 # Add this revision object to the map. | 120 # Add this revision object to the map. |
114 revision_map[githash] = revision | 121 revision_map[githash] = revision |
115 | 122 |
116 # Parse one revision for the start range, because googlesource does not | 123 # Parse one revision for the start range, because googlesource does not |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 revision['author'] = json_revision['author']['name'] | 185 revision['author'] = json_revision['author']['name'] |
179 revision['message'] = json_revision['message'] | 186 revision['message'] = json_revision['message'] |
180 revision['url'] = url | 187 revision['url'] = url |
181 | 188 |
182 # Iterate through the changed files. | 189 # Iterate through the changed files. |
183 for diff in json_revision['tree_diff']: | 190 for diff in json_revision['tree_diff']: |
184 file_path = diff['new_path'] | 191 file_path = diff['new_path'] |
185 file_change_type = diff['type'] | 192 file_change_type = diff['type'] |
186 | 193 |
187 # Normalize file action so that it fits with svn_repository_parser. | 194 # Normalize file action so that it fits with svn_repository_parser. |
188 file_change_type = FILE_CHANGE_TYPE_MAP[file_change_type] | 195 file_change_type = _ConvertToFileChangeType(file_change_type) |
189 | 196 |
190 # Add the file to the map. | 197 # Add the file to the map. |
191 if file_path not in file_to_revision_map: | 198 if file_path not in file_to_revision_map: |
192 file_to_revision_map[file_path] = [] | 199 file_to_revision_map[file_path] = [] |
193 file_to_revision_map[file_path].append((githash, file_change_type)) | 200 file_to_revision_map[file_path].append((githash, file_change_type)) |
194 | 201 |
195 # Add this CL to the map. | 202 # Add this CL to the map. |
196 revision_map[githash] = revision | 203 revision_map[githash] = revision |
197 | 204 |
198 return | 205 return |
199 | 206 |
200 def ParseLineDiff(self, path, component, file_change_type, githash): | 207 def ParseLineDiff(self, path, component, file_change_type, githash): |
201 changed_line_numbers = [] | 208 changed_line_numbers = [] |
202 changed_line_contents = [] | 209 changed_line_contents = [] |
203 base_url = self.component_to_url_map[component]['repository'] | 210 base_url = self.component_to_url_map[component]['repository'] |
204 backup_url = (base_url + self.url_parts_map['revision_url']) % githash | 211 backup_url = (base_url + self.url_parts_map['revision_url']) % githash |
205 | 212 |
206 # If the file is added (not modified), treat it as if it is not changed. | 213 # If the file is added (not modified), treat it as if it is not changed. |
207 if file_change_type == 'A': | 214 if file_change_type in ('A', 'C', 'R'): |
| 215 # TODO(stgao): Maybe return whole file change for Add, Rename, and Copy? |
208 return (backup_url, changed_line_numbers, changed_line_contents) | 216 return (backup_url, changed_line_numbers, changed_line_contents) |
209 | 217 |
210 # Retrieves the diff data from URL, and if it fails, return emptry lines. | 218 # Retrieves the diff data from URL, and if it fails, return emptry lines. |
211 url = (base_url + self.url_parts_map['diff_url']) % (githash, path) | 219 url = (base_url + self.url_parts_map['diff_url']) % (githash, path) |
212 data = crash_utils.GetDataFromURL(url + '?format=text') | 220 data = crash_utils.GetDataFromURL(url + '?format=text') |
213 if not data: | 221 if not data: |
214 return (backup_url, changed_line_numbers, changed_line_contents) | 222 return (backup_url, changed_line_numbers, changed_line_contents) |
215 | 223 |
216 # Decode the returned object to line diff info | 224 # Decode the returned object to line diff info |
217 diff = base64.b64decode(data).splitlines() | 225 diff = base64.b64decode(data).splitlines() |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 revision_url = base_url + revision_url_parts | 280 revision_url = base_url + revision_url_parts |
273 # TODO(jeun): Add a way to get content from JSON object. | 281 # TODO(jeun): Add a way to get content from JSON object. |
274 content = None | 282 content = None |
275 | 283 |
276 (revision_info, _) = self.ParseChangelog(component, revision, revision) | 284 (revision_info, _) = self.ParseChangelog(component, revision, revision) |
277 message = revision_info[revision]['message'] | 285 message = revision_info[revision]['message'] |
278 return (content, revision, author, revision_url, message) | 286 return (content, revision, author, revision_url, message) |
279 | 287 |
280 # Return none if the region does not exist. | 288 # Return none if the region does not exist. |
281 return None | 289 return None |
OLD | NEW |