Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: appengine/findit/lib/gitiles/change_log.py

Issue 2538373003: [Culprit-Finder] Merge lib/ to libs/. (Closed)
Patch Set: . Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/findit/lib/gitiles/blame.py ('k') | appengine/findit/lib/gitiles/commit_util.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 from collections import namedtuple
6
7 from lib.gitiles.diff import ChangeType
8
9 # TODO(wrengr): it'd be better to have a class hierarchy here, so we can
10 # avoid playing around with None, and so the change_type can be stored
11 # once (in the class itself; rather than once per instance).
12 # TODO(http://crbug/644476): better name for this class; i.e., without
13 # the extraneous \"Info\" at the very least.
14 # TODO(http://crbug.com/659346): coverage tests for the smart constructors.
15 class FileChangeInfo(namedtuple('FileChangeInfo',
16 ['change_type', 'old_path', 'new_path'])):
17 """Represents a file change (add/delete/modify/rename/copy/etc)."""
18 __slots__ = ()
19
20 @classmethod
21 def Modify(cls, path): # pragma: no cover
22 return cls(ChangeType.MODIFY, path, path)
23
24 @classmethod
25 def Add(cls, path): # pragma: no cover
26 # Stay the same as gitile.
27 return cls(ChangeType.ADD, None, path)
28
29 @classmethod
30 def Delete(cls, path): # pragma: no cover
31 return cls(ChangeType.DELETE, path, None)
32
33 @classmethod
34 def Rename(cls, old_path, new_path): # pragma: no cover
35 return cls(ChangeType.RENAME, old_path, new_path)
36
37 @classmethod
38 def Copy(cls, old_path, new_path): # pragma: no cover
39 return cls(ChangeType.COPY, old_path, new_path)
40
41 @classmethod
42 def FromDict(cls, info):
43 return cls(info['change_type'].lower(), info['old_path'], info['new_path'])
44
45 def ToDict(self):
46 return {
47 'change_type': self.change_type,
48 'old_path': self.old_path,
49 'new_path': self.new_path
50 }
51
52
53 class ChangeLog(namedtuple('ChangeLog',
54 ['author_name', 'author_email', 'author_time', 'committer_name',
55 'committer_email', 'committer_time', 'revision', 'commit_position',
56 'message', 'touched_files', 'commit_url', 'code_review_url',
57 'reverted_revision'])):
58 """Represents the change log of a revision."""
59 __slots__ = ()
60
61 def __new__(cls, author_name, author_email, author_time, committer_name,
62 committer_email, committer_time, revision, commit_position,
63 message, touched_files, commit_url, code_review_url=None,
64 reverted_revision=None):
65 return super(cls, ChangeLog).__new__(cls,
66 author_name, author_email, author_time, committer_name,
67 committer_email, committer_time, revision, commit_position,
68 message, touched_files, commit_url, code_review_url,
69 reverted_revision)
70
71 def ToDict(self):
72 """Returns the change log as a JSON object."""
73 json_data = {
74 'author_name': self.author_name,
75 'author_email': self.author_email,
76 'author_time': self.author_time,
77 'committer_name': self.committer_name,
78 'committer_email': self.committer_email,
79 'committer_time': self.committer_time,
80 'revision': self.revision,
81 'commit_position': self.commit_position,
82 'touched_files': [],
83 'message': self.message,
84 'commit_url': self.commit_url,
85 'code_review_url': self.code_review_url,
86 'reverted_revision': self.reverted_revision,
87 }
88 for touched_file in self.touched_files:
89 json_data['touched_files'].append(touched_file.ToDict())
90 return json_data
91
92 @staticmethod
93 def FromDict(info):
94 """Returns a ChangeLog instance represented by the given JSON info."""
95 touched_files = []
96 for touched_file_info in info['touched_files']:
97 if isinstance(touched_file_info, dict):
98 touched_file_info = FileChangeInfo.FromDict(touched_file_info)
99 if not isinstance(touched_file_info, FileChangeInfo): # pragma: no cover
100 raise TypeError("expected FileChangeInfo but got %s"
101 % touched_file_info.__class__.__name__)
102 touched_files.append(touched_file_info)
103
104 return ChangeLog(
105 info['author_name'], info['author_email'], info['author_time'],
106 info['committer_name'], info['committer_email'], info['committer_time'],
107 info['revision'], info['commit_position'], info['message'],
108 touched_files, info['commit_url'], info['code_review_url'],
109 info['reverted_revision']
110 )
OLDNEW
« no previous file with comments | « appengine/findit/lib/gitiles/blame.py ('k') | appengine/findit/lib/gitiles/commit_util.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698