Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: git_cl.py

Issue 134223010: Implement git cl lint. (Closed) Base URL: svn://chrome-svn/chrome/trunk/tools/depot_tools/
Patch Set: rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/git_cl_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 if self.is_gerrit is None: 420 if self.is_gerrit is None:
417 self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True) 421 self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True)
418 return self.is_gerrit 422 return self.is_gerrit
419 423
420 def GetGitEditor(self): 424 def GetGitEditor(self):
421 """Return the editor specified in the git config, or None if none is.""" 425 """Return the editor specified in the git config, or None if none is."""
422 if self.git_editor is None: 426 if self.git_editor is None:
423 self.git_editor = self._GetConfig('core.editor', error_ok=True) 427 self.git_editor = self._GetConfig('core.editor', error_ok=True)
424 return self.git_editor or None 428 return self.git_editor or None
425 429
430 def GetLintRegex(self):
431 return (self._GetRietveldConfig('cpplint-regex', error_ok=True) or
432 DEFAULT_LINT_REGEX)
433
434 def GetLintIgnoreRegex(self):
435 return (self._GetRietveldConfig('cpplint-ignore-regex', error_ok=True) or
436 DEFAULT_LINT_IGNORE_REGEX)
437
426 def _GetRietveldConfig(self, param, **kwargs): 438 def _GetRietveldConfig(self, param, **kwargs):
427 return self._GetConfig('rietveld.' + param, **kwargs) 439 return self._GetConfig('rietveld.' + param, **kwargs)
428 440
429 def _GetConfig(self, param, **kwargs): 441 def _GetConfig(self, param, **kwargs):
430 self.LazyUpdateIfNeeded() 442 self.LazyUpdateIfNeeded()
431 return RunGit(['config', param], **kwargs).strip() 443 return RunGit(['config', param], **kwargs).strip()
432 444
433 445
434 def ShortBranchName(branch): 446 def ShortBranchName(branch):
435 """Convert a name like 'refs/heads/foo' to just 'foo'.""" 447 """Convert a name like 'refs/heads/foo' to just 'foo'."""
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok) 1049 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
1038 1050
1039 SetProperty('server', 'CODE_REVIEW_SERVER') 1051 SetProperty('server', 'CODE_REVIEW_SERVER')
1040 # Only server setting is required. Other settings can be absent. 1052 # Only server setting is required. Other settings can be absent.
1041 # In that case, we ignore errors raised during option deletion attempt. 1053 # In that case, we ignore errors raised during option deletion attempt.
1042 SetProperty('cc', 'CC_LIST', unset_error_ok=True) 1054 SetProperty('cc', 'CC_LIST', unset_error_ok=True)
1043 SetProperty('private', 'PRIVATE', unset_error_ok=True) 1055 SetProperty('private', 'PRIVATE', unset_error_ok=True)
1044 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True) 1056 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
1045 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True) 1057 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
1046 SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True) 1058 SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True)
1059 SetProperty('cpplint-regex', 'LINT_REGEX', unset_error_ok=True)
1060 SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True)
1047 1061
1048 if 'GERRIT_HOST' in keyvals: 1062 if 'GERRIT_HOST' in keyvals:
1049 RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) 1063 RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
1050 1064
1051 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: 1065 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
1052 #should be of the form 1066 #should be of the form
1053 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof 1067 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof
1054 #ORIGIN_URL_CONFIG: http://src.chromium.org/git 1068 #ORIGIN_URL_CONFIG: http://src.chromium.org/git
1055 RunGit(['config', keyvals['PUSH_URL_CONFIG'], 1069 RunGit(['config', keyvals['PUSH_URL_CONFIG'],
1056 keyvals['ORIGIN_URL_CONFIG']]) 1070 keyvals['ORIGIN_URL_CONFIG']])
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1359 log_args = [args[0] + '..'] 1373 log_args = [args[0] + '..']
1360 elif len(args) == 1 and args[0].endswith('...'): 1374 elif len(args) == 1 and args[0].endswith('...'):
1361 log_args = [args[0][:-1]] 1375 log_args = [args[0][:-1]]
1362 elif len(args) == 2: 1376 elif len(args) == 2:
1363 log_args = [args[0] + '..' + args[1]] 1377 log_args = [args[0] + '..' + args[1]]
1364 else: 1378 else:
1365 log_args = args[:] # Hope for the best! 1379 log_args = args[:] # Hope for the best!
1366 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) 1380 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
1367 1381
1368 1382
1383 def CMDlint(parser, args):
1384 """Runs cpplint on the current changelist."""
1385 _, args = parser.parse_args(args)
1386
1387 # Access to a protected member _XX of a client class
1388 # pylint: disable=W0212
1389 try:
1390 import cpplint
1391 import cpplint_chromium
1392 except ImportError:
1393 print "Your depot_tools is missing cpplint.py and/or cpplint_chromium.py."
1394 return 1
1395
1396 # Change the current working directory before calling lint so that it
1397 # shows the correct base.
1398 previous_cwd = os.getcwd()
1399 os.chdir(settings.GetRoot())
1400 try:
1401 cl = Changelist()
1402 change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None)
1403 files = [f.LocalPath() for f in change.AffectedFiles()]
1404
1405 # Process cpplints arguments if any.
1406 filenames = cpplint.ParseArguments(args + files)
1407
1408 white_regex = re.compile(settings.GetLintRegex())
1409 black_regex = re.compile(settings.GetLintIgnoreRegex())
1410 extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace]
1411 for filename in filenames:
1412 if white_regex.match(filename):
1413 if black_regex.match(filename):
1414 print "Ignoring file %s" % filename
1415 else:
1416 cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level,
1417 extra_check_functions)
1418 else:
1419 print "Skipping file %s" % filename
1420 finally:
1421 os.chdir(previous_cwd)
1422 print "Total errors found: %d\n" % cpplint._cpplint_state.error_count
1423 if cpplint._cpplint_state.error_count != 0:
1424 return 1
1425 return 0
1426
1427
1369 def CMDpresubmit(parser, args): 1428 def CMDpresubmit(parser, args):
1370 """Runs presubmit tests on the current changelist.""" 1429 """Runs presubmit tests on the current changelist."""
1371 parser.add_option('-u', '--upload', action='store_true', 1430 parser.add_option('-u', '--upload', action='store_true',
1372 help='Run upload hook instead of the push/dcommit hook') 1431 help='Run upload hook instead of the push/dcommit hook')
1373 parser.add_option('-f', '--force', action='store_true', 1432 parser.add_option('-f', '--force', action='store_true',
1374 help='Run checks even if tree is dirty') 1433 help='Run checks even if tree is dirty')
1375 (options, args) = parser.parse_args(args) 1434 (options, args) = parser.parse_args(args)
1376 1435
1377 if not options.force and is_dirty_git_tree('presubmit'): 1436 if not options.force and is_dirty_git_tree('presubmit'):
1378 print 'use --force to check even if tree is dirty.' 1437 print 'use --force to check even if tree is dirty.'
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
2467 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' 2526 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
2468 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) 2527 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
2469 2528
2470 2529
2471 if __name__ == '__main__': 2530 if __name__ == '__main__':
2472 # These affect sys.stdout so do it outside of main() to simplify mocks in 2531 # These affect sys.stdout so do it outside of main() to simplify mocks in
2473 # unit testing. 2532 # unit testing.
2474 fix_encoding.fix_encoding() 2533 fix_encoding.fix_encoding()
2475 colorama.init() 2534 colorama.init()
2476 sys.exit(main(sys.argv[1:])) 2535 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | tests/git_cl_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698