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