| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # Copyright (C) 2008 Evan Martin <martine@danga.com> | 6 # Copyright (C) 2008 Evan Martin <martine@danga.com> |
| 7 | 7 |
| 8 """A git-command for integrating reviews on Rietveld and Gerrit.""" | 8 """A git-command for integrating reviews on Rietveld and Gerrit.""" |
| 9 | 9 |
| 10 from distutils.version import LooseVersion | 10 from distutils.version import LooseVersion |
| (...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 if codereview: | 887 if codereview: |
| 888 codereview = codereview.lower() | 888 codereview = codereview.lower() |
| 889 if codereview == 'gerrit': | 889 if codereview == 'gerrit': |
| 890 self._codereview_impl = _GerritChangelistImpl(self, **kwargs) | 890 self._codereview_impl = _GerritChangelistImpl(self, **kwargs) |
| 891 elif codereview == 'rietveld': | 891 elif codereview == 'rietveld': |
| 892 self._codereview_impl = _RietveldChangelistImpl(self, **kwargs) | 892 self._codereview_impl = _RietveldChangelistImpl(self, **kwargs) |
| 893 else: | 893 else: |
| 894 assert codereview in ('rietveld', 'gerrit') | 894 assert codereview in ('rietveld', 'gerrit') |
| 895 return | 895 return |
| 896 | 896 |
| 897 # Automatic selection based on issue number set for a current branch. | 897 # Automatic selection. |
| 898 # Rietveld takes precedence over Gerrit. | |
| 899 assert not self.issue | 898 assert not self.issue |
| 900 # Whether we find issue or not, we are doing the lookup. | 899 # Check if this branch is associated with Rietveld => Rieveld. |
| 901 self.lookedup_issue = True | 900 self._codereview_impl = _RietveldChangelistImpl(self, **kwargs) |
| 902 for cls in [_RietveldChangelistImpl, _GerritChangelistImpl]: | 901 if self.GetIssue(force_lookup=True): |
| 903 setting = cls.IssueSetting(self.GetBranch()) | 902 return |
| 904 issue = RunGit(['config', setting], error_ok=True).strip() | |
| 905 if issue: | |
| 906 self._codereview_impl = cls(self, **kwargs) | |
| 907 self.issue = int(issue) | |
| 908 return | |
| 909 | 903 |
| 910 # No issue is set for this branch, so decide based on repo-wide settings. | 904 tmp_rietveld = self._codereview_impl # Save Rietveld object. |
| 911 return self._load_codereview_impl( | 905 |
| 912 codereview='gerrit' if settings.GetIsGerrit() else 'rietveld') | 906 # Check if this branch has Gerrit issue associated => Gerrit. |
| 907 self._codereview_impl = _GerritChangelistImpl(self, **kwargs) |
| 908 if self.GetIssue(force_lookup=True): |
| 909 return |
| 910 |
| 911 # OK, no issue is set for this branch. |
| 912 # If Gerrit is set repo-wide => Gerrit. |
| 913 if settings.GetIsGerrit(): |
| 914 return |
| 915 |
| 916 self._codereview_impl = tmp_rietveld |
| 917 return |
| 913 | 918 |
| 914 | 919 |
| 915 def GetCCList(self): | 920 def GetCCList(self): |
| 916 """Return the users cc'd on this CL. | 921 """Return the users cc'd on this CL. |
| 917 | 922 |
| 918 Return is a string suitable for passing to gcl with the --cc flag. | 923 Return is a string suitable for passing to gcl with the --cc flag. |
| 919 """ | 924 """ |
| 920 if self.cc is None: | 925 if self.cc is None: |
| 921 base_cc = settings.GetDefaultCCList() | 926 base_cc = settings.GetDefaultCCList() |
| 922 more_cc = ','.join(self.watchers) | 927 more_cc = ','.join(self.watchers) |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1119 remote, _ = self.GetRemoteBranch() | 1124 remote, _ = self.GetRemoteBranch() |
| 1120 url = RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip() | 1125 url = RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip() |
| 1121 | 1126 |
| 1122 # If URL is pointing to a local directory, it is probably a git cache. | 1127 # If URL is pointing to a local directory, it is probably a git cache. |
| 1123 if os.path.isdir(url): | 1128 if os.path.isdir(url): |
| 1124 url = RunGit(['config', 'remote.%s.url' % remote], | 1129 url = RunGit(['config', 'remote.%s.url' % remote], |
| 1125 error_ok=True, | 1130 error_ok=True, |
| 1126 cwd=url).strip() | 1131 cwd=url).strip() |
| 1127 return url | 1132 return url |
| 1128 | 1133 |
| 1129 def GetIssue(self): | 1134 def GetIssue(self, force_lookup=False): |
| 1130 """Returns the issue number as a int or None if not set.""" | 1135 """Returns the issue number as a int or None if not set.""" |
| 1131 if self.issue is None and not self.lookedup_issue: | 1136 if force_lookup or (self.issue is None and not self.lookedup_issue): |
| 1132 issue = RunGit(['config', | 1137 issue = RunGit(['config', self._codereview_impl.IssueSetting()], |
| 1133 self._codereview_impl.IssueSetting(self.GetBranch())], | |
| 1134 error_ok=True).strip() | 1138 error_ok=True).strip() |
| 1135 self.issue = int(issue) or None if issue else None | 1139 self.issue = int(issue) or None if issue else None |
| 1136 self.lookedup_issue = True | 1140 self.lookedup_issue = True |
| 1137 return self.issue | 1141 return self.issue |
| 1138 | 1142 |
| 1139 def GetIssueURL(self): | 1143 def GetIssueURL(self): |
| 1140 """Get the URL for a particular issue.""" | 1144 """Get the URL for a particular issue.""" |
| 1141 issue = self.GetIssue() | 1145 issue = self.GetIssue() |
| 1142 if not issue: | 1146 if not issue: |
| 1143 return None | 1147 return None |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1169 if patchset: | 1173 if patchset: |
| 1170 RunGit(['config', patchset_setting, str(patchset)]) | 1174 RunGit(['config', patchset_setting, str(patchset)]) |
| 1171 self.patchset = patchset | 1175 self.patchset = patchset |
| 1172 else: | 1176 else: |
| 1173 RunGit(['config', '--unset', patchset_setting], | 1177 RunGit(['config', '--unset', patchset_setting], |
| 1174 stderr=subprocess2.PIPE, error_ok=True) | 1178 stderr=subprocess2.PIPE, error_ok=True) |
| 1175 self.patchset = None | 1179 self.patchset = None |
| 1176 | 1180 |
| 1177 def SetIssue(self, issue=None): | 1181 def SetIssue(self, issue=None): |
| 1178 """Set this branch's issue. If issue isn't given, clears the issue.""" | 1182 """Set this branch's issue. If issue isn't given, clears the issue.""" |
| 1179 issue_setting = self._codereview_impl.IssueSetting(self.GetBranch()) | 1183 issue_setting = self._codereview_impl.IssueSetting() |
| 1180 codereview_setting = self._codereview_impl.GetCodereviewServerSetting() | 1184 codereview_setting = self._codereview_impl.GetCodereviewServerSetting() |
| 1181 if issue: | 1185 if issue: |
| 1182 self.issue = issue | 1186 self.issue = issue |
| 1183 RunGit(['config', issue_setting, str(issue)]) | 1187 RunGit(['config', issue_setting, str(issue)]) |
| 1184 codereview_server = self._codereview_impl.GetCodereviewServer() | 1188 codereview_server = self._codereview_impl.GetCodereviewServer() |
| 1185 if codereview_server: | 1189 if codereview_server: |
| 1186 RunGit(['config', codereview_setting, codereview_server]) | 1190 RunGit(['config', codereview_setting, codereview_server]) |
| 1187 else: | 1191 else: |
| 1188 current_issue = self.GetIssue() | 1192 current_issue = self.GetIssue() |
| 1189 if current_issue: | 1193 if current_issue: |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1301 raise NotImplementedError() | 1305 raise NotImplementedError() |
| 1302 | 1306 |
| 1303 def FetchDescription(self): | 1307 def FetchDescription(self): |
| 1304 """Fetches and returns description from the codereview server.""" | 1308 """Fetches and returns description from the codereview server.""" |
| 1305 raise NotImplementedError() | 1309 raise NotImplementedError() |
| 1306 | 1310 |
| 1307 def GetCodereviewServerSetting(self): | 1311 def GetCodereviewServerSetting(self): |
| 1308 """Returns git config setting for the codereview server.""" | 1312 """Returns git config setting for the codereview server.""" |
| 1309 raise NotImplementedError() | 1313 raise NotImplementedError() |
| 1310 | 1314 |
| 1311 @staticmethod | 1315 def IssueSetting(self): |
| 1312 def IssueSetting(branch): | 1316 """Returns name of git config setting which stores issue number.""" |
| 1313 """Returns name of git config setting which stores issue number for a given | |
| 1314 branch.""" | |
| 1315 raise NotImplementedError() | 1317 raise NotImplementedError() |
| 1316 | 1318 |
| 1317 def PatchsetSetting(self): | 1319 def PatchsetSetting(self): |
| 1318 """Returns name of git config setting which stores issue number.""" | 1320 """Returns name of git config setting which stores issue number.""" |
| 1319 raise NotImplementedError() | 1321 raise NotImplementedError() |
| 1320 | 1322 |
| 1321 def GetRieveldObjForPresubmit(self): | 1323 def GetRieveldObjForPresubmit(self): |
| 1322 # This is an unfortunate Rietveld-embeddedness in presubmit. | 1324 # This is an unfortunate Rietveld-embeddedness in presubmit. |
| 1323 # For non-Rietveld codereviews, this probably should return a dummy object. | 1325 # For non-Rietveld codereviews, this probably should return a dummy object. |
| 1324 raise NotImplementedError() | 1326 raise NotImplementedError() |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1489 | 1491 |
| 1490 def RpcServer(self): | 1492 def RpcServer(self): |
| 1491 """Returns an upload.RpcServer() to access this review's rietveld instance. | 1493 """Returns an upload.RpcServer() to access this review's rietveld instance. |
| 1492 """ | 1494 """ |
| 1493 if not self._rpc_server: | 1495 if not self._rpc_server: |
| 1494 self._rpc_server = rietveld.CachingRietveld( | 1496 self._rpc_server = rietveld.CachingRietveld( |
| 1495 self.GetCodereviewServer(), | 1497 self.GetCodereviewServer(), |
| 1496 self._auth_config or auth.make_auth_config()) | 1498 self._auth_config or auth.make_auth_config()) |
| 1497 return self._rpc_server | 1499 return self._rpc_server |
| 1498 | 1500 |
| 1499 @staticmethod | 1501 def IssueSetting(self): |
| 1500 def IssueSetting(branch): | 1502 """Return the git setting that stores this change's issue.""" |
| 1501 return 'branch.%s.rietveldissue' % branch | 1503 return 'branch.%s.rietveldissue' % self.GetBranch() |
| 1502 | 1504 |
| 1503 def PatchsetSetting(self): | 1505 def PatchsetSetting(self): |
| 1504 """Return the git setting that stores this change's most recent patchset.""" | 1506 """Return the git setting that stores this change's most recent patchset.""" |
| 1505 return 'branch.%s.rietveldpatchset' % self.GetBranch() | 1507 return 'branch.%s.rietveldpatchset' % self.GetBranch() |
| 1506 | 1508 |
| 1507 def GetCodereviewServerSetting(self): | 1509 def GetCodereviewServerSetting(self): |
| 1508 """Returns the git setting that stores this change's rietveld server.""" | 1510 """Returns the git setting that stores this change's rietveld server.""" |
| 1509 branch = self.GetBranch() | 1511 branch = self.GetBranch() |
| 1510 if branch: | 1512 if branch: |
| 1511 return 'branch.%s.rietveldserver' % branch | 1513 return 'branch.%s.rietveldserver' % branch |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1541 self._gerrit_host = urlparse.urlparse(self._gerrit_server).netloc | 1543 self._gerrit_host = urlparse.urlparse(self._gerrit_server).netloc |
| 1542 if not self._gerrit_server: | 1544 if not self._gerrit_server: |
| 1543 # We assume repo to be hosted on Gerrit, and hence Gerrit server | 1545 # We assume repo to be hosted on Gerrit, and hence Gerrit server |
| 1544 # has "-review" suffix for lowest level subdomain. | 1546 # has "-review" suffix for lowest level subdomain. |
| 1545 parts = urlparse.urlparse(self.GetRemoteUrl()).netloc.split('.') | 1547 parts = urlparse.urlparse(self.GetRemoteUrl()).netloc.split('.') |
| 1546 parts[0] = parts[0] + '-review' | 1548 parts[0] = parts[0] + '-review' |
| 1547 self._gerrit_host = '.'.join(parts) | 1549 self._gerrit_host = '.'.join(parts) |
| 1548 self._gerrit_server = 'https://%s' % self._gerrit_host | 1550 self._gerrit_server = 'https://%s' % self._gerrit_host |
| 1549 return self._gerrit_server | 1551 return self._gerrit_server |
| 1550 | 1552 |
| 1551 @staticmethod | 1553 def IssueSetting(self): |
| 1552 def IssueSetting(branch): | 1554 """Return the git setting that stores this change's issue.""" |
| 1553 return 'branch.%s.gerritissue' % branch | 1555 return 'branch.%s.gerritissue' % self.GetBranch() |
| 1554 | 1556 |
| 1555 def PatchsetSetting(self): | 1557 def PatchsetSetting(self): |
| 1556 """Return the git setting that stores this change's most recent patchset.""" | 1558 """Return the git setting that stores this change's most recent patchset.""" |
| 1557 return 'branch.%s.gerritpatchset' % self.GetBranch() | 1559 return 'branch.%s.gerritpatchset' % self.GetBranch() |
| 1558 | 1560 |
| 1559 def GetCodereviewServerSetting(self): | 1561 def GetCodereviewServerSetting(self): |
| 1560 """Returns the git setting that stores this change's Gerrit server.""" | 1562 """Returns the git setting that stores this change's Gerrit server.""" |
| 1561 branch = self.GetBranch() | 1563 branch = self.GetBranch() |
| 1562 if branch: | 1564 if branch: |
| 1563 return 'branch.%s.gerritserver' % branch | 1565 return 'branch.%s.gerritserver' % branch |
| (...skipping 2703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4267 if __name__ == '__main__': | 4269 if __name__ == '__main__': |
| 4268 # These affect sys.stdout so do it outside of main() to simplify mocks in | 4270 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 4269 # unit testing. | 4271 # unit testing. |
| 4270 fix_encoding.fix_encoding() | 4272 fix_encoding.fix_encoding() |
| 4271 colorama.init() | 4273 colorama.init() |
| 4272 try: | 4274 try: |
| 4273 sys.exit(main(sys.argv[1:])) | 4275 sys.exit(main(sys.argv[1:])) |
| 4274 except KeyboardInterrupt: | 4276 except KeyboardInterrupt: |
| 4275 sys.stderr.write('interrupted\n') | 4277 sys.stderr.write('interrupted\n') |
| 4276 sys.exit(1) | 4278 sys.exit(1) |
| OLD | NEW |