OLD | NEW |
---|---|
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 collections | 5 import collections |
6 | 6 |
7 from dashboard.common import namespaced_stored_object | 7 from dashboard.common import namespaced_stored_object |
8 from dashboard.services import gitiles_service | 8 from dashboard.services import gitiles_service |
9 | 9 |
10 | 10 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 gitiles_service.CommitInfo(commit.repository_url, commit.git_hash) | 94 gitiles_service.CommitInfo(commit.repository_url, commit.git_hash) |
95 except gitiles_service.NotFoundError as e: | 95 except gitiles_service.NotFoundError as e: |
96 raise KeyError(str(e)) | 96 raise KeyError(str(e)) |
97 | 97 |
98 return commit | 98 return commit |
99 | 99 |
100 @classmethod | 100 @classmethod |
101 def Midpoint(cls, commit_a, commit_b): | 101 def Midpoint(cls, commit_a, commit_b): |
102 """Return a Commit halfway between the two given Commits. | 102 """Return a Commit halfway between the two given Commits. |
103 | 103 |
104 If the range has an even number of Commits, the midpoint is the Commit just | |
105 before the halfway point. The range includes both commit_a and commit_b; | |
106 i.e. if commit_a and commit_b are adjacent or the same, the midpoint is | |
107 commit_a. | |
108 | |
104 Uses Gitiles to look up the commit range. | 109 Uses Gitiles to look up the commit range. |
105 | 110 |
106 Args: | 111 Args: |
107 commit_a: The first Commit in the range. | 112 commit_a: The first Commit in the range. |
108 commit_b: The last Commit in the range. | 113 commit_b: The last Commit in the range. |
109 | 114 |
110 Returns: | 115 Returns: |
111 A new Commit representing the midpoint. | 116 A tuple of (Commit, (left, right)). left and right are the distances |
112 The commit before the midpoint if the range has an even number of commits. | 117 between the midpoint and commit_a and commit_b, respectively. |
113 commit_a if the Commits are the same or adjacent. | |
114 | 118 |
115 Raises: | 119 Raises: |
116 NonLinearError: The Commits are in different repositories or commit_a does | 120 NonLinearError: The Commits are in different repositories or commit_a does |
117 not come before commit_b. | 121 not come before commit_b. |
118 """ | 122 """ |
119 if commit_a == commit_b: | 123 if commit_a == commit_b: |
120 return commit_a | 124 return commit_a, (0, 0) |
121 | 125 |
122 if commit_a.repository != commit_b.repository: | 126 if commit_a.repository != commit_b.repository: |
123 raise NonLinearError('Repositories differ between Commits: %s vs %s' % | 127 raise NonLinearError('Repositories differ between Commits: %s vs %s' % |
124 (commit_a.repository, commit_b.repository)) | 128 (commit_a.repository, commit_b.repository)) |
125 | 129 |
126 commits = gitiles_service.CommitRange(commit_a.repository_url, | 130 commits = gitiles_service.CommitRange(commit_a.repository_url, |
127 commit_a.git_hash, commit_b.git_hash) | 131 commit_a.git_hash, commit_b.git_hash) |
128 # We don't handle NotFoundErrors because we assume that all Commits either | 132 # We don't handle NotFoundErrors because we assume that all Commits either |
129 # came from this method or were already validated elsewhere. | 133 # came from this method or were already validated elsewhere. |
130 if len(commits) == 0: | 134 if len(commits) == 0: |
131 raise NonLinearError('Commit "%s" does not come before commit "%s".' % | 135 raise NonLinearError('Commit "%s" does not come before commit "%s".' % |
132 commit_a, commit_b) | 136 commit_a, commit_b) |
133 if len(commits) == 1: | 137 # Include both commit_a and commit_b in the range. |
perezju
2017/09/19 15:58:05
nit: Add a short reminder that the list of commits
| |
134 return commit_a | 138 commits.append({'commit': commit_a.git_hash}) |
perezju
2017/09/19 15:58:05
I'm wondering whether gitiles_service.CommitRange
| |
135 commits.pop(0) # Remove commit_b from the range. | |
136 | 139 |
137 return cls(commit_a.repository, commits[len(commits) / 2]['commit']) | 140 midpoint_index = len(commits) / 2 |
141 midpoint = cls(commit_a.repository, commits[midpoint_index]['commit']) | |
142 return midpoint, (len(commits) - midpoint_index - 1, midpoint_index) | |
138 | 143 |
139 | 144 |
140 def _Repository(repository_url): | 145 def _Repository(repository_url): |
141 if repository_url.endswith('.git'): | 146 if repository_url.endswith('.git'): |
142 repository_url = repository_url[:-4] | 147 repository_url = repository_url[:-4] |
143 | 148 |
144 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) | 149 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) |
145 for repo_label, repo_info in repositories.iteritems(): | 150 for repo_label, repo_info in repositories.iteritems(): |
146 if repository_url == repo_info['repository_url']: | 151 if repository_url == repo_info['repository_url']: |
147 return repo_label | 152 return repo_label |
148 | 153 |
149 raise KeyError('Unknown repository URL: ' + repository_url) | 154 raise KeyError('Unknown repository URL: ' + repository_url) |
150 | 155 |
151 | 156 |
152 def _AddRepository(repository_url): | 157 def _AddRepository(repository_url): |
153 if repository_url.endswith('.git'): | 158 if repository_url.endswith('.git'): |
154 repository_url = repository_url[:-4] | 159 repository_url = repository_url[:-4] |
155 | 160 |
156 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) | 161 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) |
157 repository = repository_url.split('/')[-1] | 162 repository = repository_url.split('/')[-1] |
158 | 163 |
159 if repository in repositories: | 164 if repository in repositories: |
160 raise AssertionError("Attempted to add a repository that's already in the " | 165 raise AssertionError("Attempted to add a repository that's already in the " |
161 'Datastore: %s: %s' % (repository, repository_url)) | 166 'Datastore: %s: %s' % (repository, repository_url)) |
162 | 167 |
163 repositories[repository] = {'repository_url': repository_url} | 168 repositories[repository] = {'repository_url': repository_url} |
164 namespaced_stored_object.Set(_REPOSITORIES_KEY, repositories) | 169 namespaced_stored_object.Set(_REPOSITORIES_KEY, repositories) |
165 | 170 |
166 return repository | 171 return repository |
OLD | NEW |