Chromium Code Reviews| 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.""" | 8 """A git-command for integrating reviews on Rietveld.""" |
| 9 | 9 |
| 10 from distutils.version import LooseVersion | 10 from distutils.version import LooseVersion |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 import owners_finder | 44 import owners_finder |
| 45 | 45 |
| 46 __version__ = '1.0' | 46 __version__ = '1.0' |
| 47 | 47 |
| 48 DEFAULT_SERVER = 'https://codereview.appspot.com' | 48 DEFAULT_SERVER = 'https://codereview.appspot.com' |
| 49 POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' | 49 POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' |
| 50 DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' | 50 DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' |
| 51 GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingGit' | 51 GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingGit' |
| 52 CHANGE_ID = 'Change-Id:' | 52 CHANGE_ID = 'Change-Id:' |
| 53 | 53 |
| 54 # Valid extensions for files we want to lint. | |
| 55 DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)" | |
| 56 DEFAULT_LINT_IGNORE_REGEX = r"$^" | |
| 57 | |
| 54 # Shortcut since it quickly becomes redundant. | 58 # Shortcut since it quickly becomes redundant. |
| 55 Fore = colorama.Fore | 59 Fore = colorama.Fore |
| 56 | 60 |
| 57 # Initialized in main() | 61 # Initialized in main() |
| 58 settings = None | 62 settings = None |
| 59 | 63 |
| 60 | 64 |
| 61 def DieWithError(message): | 65 def DieWithError(message): |
| 62 print >> sys.stderr, message | 66 print >> sys.stderr, message |
| 63 sys.exit(1) | 67 sys.exit(1) |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 if self.is_gerrit is None: | 419 if self.is_gerrit is None: |
| 416 self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True) | 420 self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True) |
| 417 return self.is_gerrit | 421 return self.is_gerrit |
| 418 | 422 |
| 419 def GetGitEditor(self): | 423 def GetGitEditor(self): |
| 420 """Return the editor specified in the git config, or None if none is.""" | 424 """Return the editor specified in the git config, or None if none is.""" |
| 421 if self.git_editor is None: | 425 if self.git_editor is None: |
| 422 self.git_editor = self._GetConfig('core.editor', error_ok=True) | 426 self.git_editor = self._GetConfig('core.editor', error_ok=True) |
| 423 return self.git_editor or None | 427 return self.git_editor or None |
| 424 | 428 |
| 429 def GetLintRegex(self): | |
| 430 return (self._GetRietveldConfig('cpplint-regex', error_ok=True) or | |
| 431 DEFAULT_LINT_REGEX) | |
| 432 | |
| 433 def GetLintIgnoreRegex(self): | |
| 434 return (self._GetRietveldConfig('cpplint-ignore-regex', error_ok=True) or | |
| 435 DEFAULT_LINT_IGNORE_REGEX) | |
| 436 | |
| 425 def _GetRietveldConfig(self, param, **kwargs): | 437 def _GetRietveldConfig(self, param, **kwargs): |
| 426 return self._GetConfig('rietveld.' + param, **kwargs) | 438 return self._GetConfig('rietveld.' + param, **kwargs) |
| 427 | 439 |
| 428 def _GetConfig(self, param, **kwargs): | 440 def _GetConfig(self, param, **kwargs): |
| 429 self.LazyUpdateIfNeeded() | 441 self.LazyUpdateIfNeeded() |
| 430 return RunGit(['config', param], **kwargs).strip() | 442 return RunGit(['config', param], **kwargs).strip() |
| 431 | 443 |
| 432 | 444 |
| 433 def ShortBranchName(branch): | 445 def ShortBranchName(branch): |
| 434 """Convert a name like 'refs/heads/foo' to just 'foo'.""" | 446 """Convert a name like 'refs/heads/foo' to just 'foo'.""" |
| (...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1036 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok) | 1048 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok) |
| 1037 | 1049 |
| 1038 SetProperty('server', 'CODE_REVIEW_SERVER') | 1050 SetProperty('server', 'CODE_REVIEW_SERVER') |
| 1039 # Only server setting is required. Other settings can be absent. | 1051 # Only server setting is required. Other settings can be absent. |
| 1040 # In that case, we ignore errors raised during option deletion attempt. | 1052 # In that case, we ignore errors raised during option deletion attempt. |
| 1041 SetProperty('cc', 'CC_LIST', unset_error_ok=True) | 1053 SetProperty('cc', 'CC_LIST', unset_error_ok=True) |
| 1042 SetProperty('private', 'PRIVATE', unset_error_ok=True) | 1054 SetProperty('private', 'PRIVATE', unset_error_ok=True) |
| 1043 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True) | 1055 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True) |
| 1044 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True) | 1056 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True) |
| 1045 SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True) | 1057 SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True) |
| 1058 SetProperty('cpplint-regex', 'LINT_REGEX', unset_error_ok=True) | |
| 1059 SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True) | |
| 1046 | 1060 |
| 1047 if 'GERRIT_HOST' in keyvals: | 1061 if 'GERRIT_HOST' in keyvals: |
| 1048 RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) | 1062 RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) |
| 1049 | 1063 |
| 1050 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: | 1064 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: |
| 1051 #should be of the form | 1065 #should be of the form |
| 1052 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof | 1066 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof |
| 1053 #ORIGIN_URL_CONFIG: http://src.chromium.org/git | 1067 #ORIGIN_URL_CONFIG: http://src.chromium.org/git |
| 1054 RunGit(['config', keyvals['PUSH_URL_CONFIG'], | 1068 RunGit(['config', keyvals['PUSH_URL_CONFIG'], |
| 1055 keyvals['ORIGIN_URL_CONFIG']]) | 1069 keyvals['ORIGIN_URL_CONFIG']]) |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1358 log_args = [args[0] + '..'] | 1372 log_args = [args[0] + '..'] |
| 1359 elif len(args) == 1 and args[0].endswith('...'): | 1373 elif len(args) == 1 and args[0].endswith('...'): |
| 1360 log_args = [args[0][:-1]] | 1374 log_args = [args[0][:-1]] |
| 1361 elif len(args) == 2: | 1375 elif len(args) == 2: |
| 1362 log_args = [args[0] + '..' + args[1]] | 1376 log_args = [args[0] + '..' + args[1]] |
| 1363 else: | 1377 else: |
| 1364 log_args = args[:] # Hope for the best! | 1378 log_args = args[:] # Hope for the best! |
| 1365 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) | 1379 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) |
| 1366 | 1380 |
| 1367 | 1381 |
| 1382 def CMDlint(parser, args): | |
| 1383 """Runs cpplint on the current changelist.""" | |
| 1384 _, args = parser.parse_args(args) | |
| 1385 | |
| 1386 # Access to a protected member _XX of a client class | |
| 1387 # pylint: disable=W0212 | |
| 1388 try: | |
| 1389 import cpplint | |
| 1390 import cpplint_chromium | |
| 1391 except ImportError: | |
| 1392 print "You need to install cpplint.py to lint C++ files." | |
|
iannucci
2014/02/11 07:50:01
No URL?
Lei Zhang
2014/02/11 07:58:04
I just copied this over from gcl.py. I'll come up
| |
| 1393 return 1 | |
| 1394 | |
| 1395 # Change the current working directory before calling lint so that it | |
| 1396 # shows the correct base. | |
| 1397 previous_cwd = os.getcwd() | |
| 1398 os.chdir(settings.GetRoot()) | |
|
iannucci
2014/02/11 07:50:01
arg.... there's really no way to pass a base direc
Lei Zhang
2014/02/11 07:58:04
Again, almost blindly copied over from gcl.py. It
Lei Zhang
2014/02/11 22:20:52
--root doesn't do what I think it does, so unless
| |
| 1399 try: | |
| 1400 cl = Changelist() | |
| 1401 change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None) | |
| 1402 files = [f.LocalPath() for f in change.AffectedFiles()] | |
| 1403 | |
| 1404 # Process cpplints arguments if any. | |
| 1405 filenames = cpplint.ParseArguments(args + files) | |
| 1406 | |
| 1407 white_regex = re.compile(settings.GetLintRegex()) | |
| 1408 black_regex = re.compile(settings.GetLintIgnoreRegex()) | |
| 1409 extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] | |
| 1410 for filename in filenames: | |
| 1411 if white_regex.match(filename): | |
| 1412 if black_regex.match(filename): | |
| 1413 print "Ignoring file %s" % filename | |
| 1414 else: | |
| 1415 cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, | |
| 1416 extra_check_functions) | |
| 1417 else: | |
| 1418 print "Skipping file %s" % filename | |
| 1419 finally: | |
| 1420 os.chdir(previous_cwd) | |
| 1421 print "Total errors found: %d\n" % cpplint._cpplint_state.error_count | |
| 1422 return 0 | |
|
iannucci
2014/02/11 07:50:01
this should probably return 0 iff cpplint._cpplint
Lei Zhang
2014/02/11 07:58:04
Sure, I can do that. Strangely, gcl lint always re
| |
| 1423 | |
| 1424 | |
| 1368 def CMDpresubmit(parser, args): | 1425 def CMDpresubmit(parser, args): |
| 1369 """Runs presubmit tests on the current changelist.""" | 1426 """Runs presubmit tests on the current changelist.""" |
| 1370 parser.add_option('-u', '--upload', action='store_true', | 1427 parser.add_option('-u', '--upload', action='store_true', |
| 1371 help='Run upload hook instead of the push/dcommit hook') | 1428 help='Run upload hook instead of the push/dcommit hook') |
| 1372 parser.add_option('-f', '--force', action='store_true', | 1429 parser.add_option('-f', '--force', action='store_true', |
| 1373 help='Run checks even if tree is dirty') | 1430 help='Run checks even if tree is dirty') |
| 1374 (options, args) = parser.parse_args(args) | 1431 (options, args) = parser.parse_args(args) |
| 1375 | 1432 |
| 1376 if not options.force and is_dirty_git_tree('presubmit'): | 1433 if not options.force and is_dirty_git_tree('presubmit'): |
| 1377 print 'use --force to check even if tree is dirty.' | 1434 print 'use --force to check even if tree is dirty.' |
| (...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2429 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' | 2486 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 2430 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) | 2487 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
| 2431 | 2488 |
| 2432 | 2489 |
| 2433 if __name__ == '__main__': | 2490 if __name__ == '__main__': |
| 2434 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2491 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 2435 # unit testing. | 2492 # unit testing. |
| 2436 fix_encoding.fix_encoding() | 2493 fix_encoding.fix_encoding() |
| 2437 colorama.init() | 2494 colorama.init() |
| 2438 sys.exit(main(sys.argv[1:])) | 2495 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |