| 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 | |
| 28 | 27 |
| 29 # This maps from commit hash -> index in _commits. | 28 # This maps from commit hash -> index in _commits. |
| 30 self._rev_idx = {} | 29 self._rev_idx = {} |
| 31 | 30 |
| 32 # This maps dep_project_id -> dep_commit -> set(idxs) | 31 # This maps dep_project_id -> dep_commit -> set(idxs) |
| 33 self._dep_idx = {} | 32 self._dep_idx = {} |
| 34 for i, c in enumerate(commit_list): | 33 for i, c in enumerate(commit_list): |
| 35 self._rev_idx[c.revision] = i | 34 self._rev_idx[c.revision] = i |
| 36 | 35 |
| 37 for dep_project_id, dep in c.spec.deps.iteritems(): | 36 for dep_project_id, dep in c.spec.deps.iteritems(): |
| (...skipping 25 matching lines...) Expand all Loading... |
| 63 def next(self): | 62 def next(self): |
| 64 """Gets the next CommitMetadata without advancing the current index. | 63 """Gets the next CommitMetadata without advancing the current index. |
| 65 | 64 |
| 66 Returns the next CommitMetadata or None, if there is no next CommitMetadata. | 65 Returns the next CommitMetadata or None, if there is no next CommitMetadata. |
| 67 """ | 66 """ |
| 68 nxt_idx = self._cur_idx+1 | 67 nxt_idx = self._cur_idx+1 |
| 69 if nxt_idx >= len(self._commits): | 68 if nxt_idx >= len(self._commits): |
| 70 return None | 69 return None |
| 71 return self._commits[nxt_idx] | 70 return self._commits[nxt_idx] |
| 72 | 71 |
| 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 | |
| 95 def advance(self): | 72 def advance(self): |
| 96 """Advances the current CommitMetadata by one. | 73 """Advances the current CommitMetadata by one. |
| 97 | 74 |
| 98 That is: CommitList.next becomes CommitList.current. | 75 That is: CommitList.next becomes CommitList.current. |
| 99 | 76 |
| 100 Returns the now-current CommitMetadata (or None, if there was no next | 77 Returns the now-current CommitMetadata (or None, if there was no next |
| 101 CommitMetadata). | 78 CommitMetadata). |
| 102 """ | 79 """ |
| 103 ret = self.next | 80 ret = self.next |
| 104 if ret: | 81 if ret: |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 Args: | 172 Args: |
| 196 target_commit (str) - the commit to obtain the changelist for. | 173 target_commit (str) - the commit to obtain the changelist for. |
| 197 | 174 |
| 198 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to | 175 Returns list(CommitMetadata) - The CommitMetadata objects corresponding to |
| 199 the provided target_commit. | 176 the provided target_commit. |
| 200 | 177 |
| 201 Raises: | 178 Raises: |
| 202 UnknownCommit if target_commit is not found. | 179 UnknownCommit if target_commit is not found. |
| 203 """ | 180 """ |
| 204 return list(self._commits[:self._idx_of(target_commit)+1]) | 181 return list(self._commits[:self._idx_of(target_commit)+1]) |
| OLD | NEW |