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 # Compute and cache the next roll candidate index. |
| 82 nxt_idx = self._next_roll_candidate_idx |
| 83 if nxt_idx is None or nxt_idx <= self._cur_idx: |
| 84 nxt_idx = None |
| 85 for i, commit in enumerate(self._commits[self._cur_idx+1:]): |
| 86 if commit.roll_candidate: |
| 87 nxt_idx = self._cur_idx + i + 1 |
| 88 break |
| 89 self._next_roll_candidate_idx = nxt_idx |
| 90 |
| 91 if nxt_idx is not None: |
| 92 return self._commits[nxt_idx], nxt_idx - self._cur_idx |
| 93 return (None, None) |
| 94 |
72 def advance(self): | 95 def advance(self): |
73 """Advances the current CommitMetadata by one. | 96 """Advances the current CommitMetadata by one. |
74 | 97 |
75 That is: CommitList.next becomes CommitList.current. | 98 That is: CommitList.next becomes CommitList.current. |
76 | 99 |
77 Returns the now-current CommitMetadata (or None, if there was no next | 100 Returns the now-current CommitMetadata (or None, if there was no next |
78 CommitMetadata). | 101 CommitMetadata). |
79 """ | 102 """ |
80 ret = self.next | 103 ret = self.next |
81 if ret: | 104 if ret: |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 Args: | 195 Args: |
173 target_commit (str) - the commit to obtain the changelist for. | 196 target_commit (str) - the commit to obtain the changelist for. |
174 | 197 |
175 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to | 198 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to |
176 the provided target_commit. | 199 the provided target_commit. |
177 | 200 |
178 Raises: | 201 Raises: |
179 UnknownCommit if target_commit is not found. | 202 UnknownCommit if target_commit is not found. |
180 """ | 203 """ |
181 return list(self._commits[:self._idx_of(target_commit)+1]) | 204 return list(self._commits[:self._idx_of(target_commit)+1]) |
OLD | NEW |