OLD | NEW |
---|---|
1 # Copyright 2017 The LUCI Authors. All rights reserved. | 1 # Copyright 2017 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
5 from recipe_engine.fetch import CommitMetadata | 5 from recipe_engine.fetch import CommitMetadata |
6 | 6 |
7 class UnknownCommit(KeyError): | 7 class UnknownCommit(KeyError): |
8 pass | 8 pass |
9 | 9 |
10 class CommitList(object): | 10 class CommitList(object): |
11 """A seekable list of CommitMetadata objects for a single repo. | 11 """A seekable list of CommitMetadata objects for a single repo. |
12 | 12 |
13 This can also be used to obtain the list of commits 'rolled so far' for the | 13 This can also be used to obtain the list of commits 'rolled so far' for the |
14 purposes of generating a changelist. | 14 purposes of generating a changelist. |
15 """ | 15 """ |
16 | 16 |
17 def __init__(self, commit_list): | 17 def __init__(self, commit_list): |
18 """ | 18 """ |
19 Args: | 19 Args: |
20 commit_list (list(CommitMetadata)) - The list of CommitMetadata objects to | 20 commit_list (list(CommitMetadata)) - The list of CommitMetadata objects to |
21 use. | 21 use. |
22 """ | 22 """ |
23 assert commit_list, 'commit_list is empty' | 23 assert commit_list, 'commit_list is empty' |
24 assert all(isinstance(c, CommitMetadata) for c in commit_list) | 24 assert all(isinstance(c, CommitMetadata) for c in commit_list) |
25 self._commits = list(commit_list) | 25 self._commits = list(commit_list) |
26 self._cur_idx = 0 | 26 self._cur_idx = 0 |
27 self._next_roll_candidate_idx = None | |
27 | 28 |
28 # This maps from commit hash -> index in _commits. | 29 # This maps from commit hash -> index in _commits. |
29 self._rev_idx = {} | 30 self._rev_idx = {} |
30 | 31 |
31 # This maps dep_project_id -> dep_commit -> set(idxs) | 32 # This maps dep_project_id -> dep_commit -> set(idxs) |
32 self._dep_idx = {} | 33 self._dep_idx = {} |
33 for i, c in enumerate(commit_list): | 34 for i, c in enumerate(commit_list): |
34 self._rev_idx[c.revision] = i | 35 self._rev_idx[c.revision] = i |
35 | 36 |
36 for dep_project_id, dep in c.spec.deps.iteritems(): | 37 for dep_project_id, dep in c.spec.deps.iteritems(): |
(...skipping 25 matching lines...) Expand all Loading... | |
62 def next(self): | 63 def next(self): |
63 """Gets the next CommitMetadata without advancing the current index. | 64 """Gets the next CommitMetadata without advancing the current index. |
64 | 65 |
65 Returns the next CommitMetadata or None, if there is no next CommitMetadata. | 66 Returns the next CommitMetadata or None, if there is no next CommitMetadata. |
66 """ | 67 """ |
67 nxt_idx = self._cur_idx+1 | 68 nxt_idx = self._cur_idx+1 |
68 if nxt_idx >= len(self._commits): | 69 if nxt_idx >= len(self._commits): |
69 return None | 70 return None |
70 return self._commits[nxt_idx] | 71 return self._commits[nxt_idx] |
71 | 72 |
73 @property | |
74 def next_roll_candidate(self): | |
75 """Gets the next CommitMetadata and distance with roll_candidate==True | |
76 without advancing the current index. | |
77 | |
78 Returns (CommitMetadata, <distance>), or (None, None) if there is no next | |
79 roll_candidate. | |
80 """ | |
81 nxt_idx = self._next_roll_candidate_idx | |
dnj
2017/04/20 18:16:21
"nxt_idx" always == self._next_roll_candidate_idx.
iannucci
2017/04/20 21:53:20
Done.
| |
82 if nxt_idx is None or nxt_idx <= self._cur_idx: | |
83 nxt_idx = self._next_roll_candidate_idx = None | |
84 for i, commit in enumerate(self._commits[self._cur_idx+1:]): | |
85 if commit.roll_candidate: | |
86 nxt_idx = self._next_roll_candidate_idx = self._cur_idx + i + 1 | |
87 break | |
88 if nxt_idx is not None: | |
89 return self._commits[nxt_idx], nxt_idx - self._cur_idx | |
90 return (None, None) | |
91 | |
72 def advance(self): | 92 def advance(self): |
73 """Advances the current CommitMetadata by one. | 93 """Advances the current CommitMetadata by one. |
74 | 94 |
75 That is: CommitList.next becomes CommitList.current. | 95 That is: CommitList.next becomes CommitList.current. |
76 | 96 |
77 Returns the now-current CommitMetadata (or None, if there was no next | 97 Returns the now-current CommitMetadata (or None, if there was no next |
78 CommitMetadata). | 98 CommitMetadata). |
79 """ | 99 """ |
80 ret = self.next | 100 ret = self.next |
81 if ret: | 101 if ret: |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 Args: | 192 Args: |
173 target_commit (str) - the commit to obtain the changelist for. | 193 target_commit (str) - the commit to obtain the changelist for. |
174 | 194 |
175 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to | 195 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to |
176 the provided target_commit. | 196 the provided target_commit. |
177 | 197 |
178 Raises: | 198 Raises: |
179 UnknownCommit if target_commit is not found. | 199 UnknownCommit if target_commit is not found. |
180 """ | 200 """ |
181 return list(self._commits[:self._idx_of(target_commit)+1]) | 201 return list(self._commits[:self._idx_of(target_commit)+1]) |
OLD | NEW |