Add hg commands for style check so you can check at times other than commit

This commit is contained in:
Nathan Binkert 2008-06-14 19:37:26 -07:00
parent ce43e46576
commit ed84388b85

View file

@ -1,5 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
# Copyright (c) 2007 The Regents of The University of Michigan # Copyright (c) 2006 The Regents of The University of Michigan
# Copyright (c) 2007 The Hewlett-Packard Development Company
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -260,35 +261,42 @@ def modified_lines(old_data, new_data, max_lines):
break break
return modified return modified
def check_whitespace(ui, repo, hooktype, node, parent1, parent2): def do_check_whitespace(ui, repo, *files, **args):
from mercurial import mdiff """check files for proper m5 style guidelines"""
from mercurial import mdiff, util
if hooktype != 'pretxncommit': if files:
raise AttributeError, \ files = frozenset(files)
"This hook is only meant for pretxncommit, not %s" % hooktype
def skip(name):
return files and name in files
tabsize = 8
verbose = ui.configbool('style', 'verbose', False)
def prompt(name, fixonly=None): def prompt(name, fixonly=None):
result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", "^[aif]$", "a") if args.get('auto', False):
result = 'f'
else:
result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", "^[aif]$", "a")
if result == 'a': if result == 'a':
return True return True
elif result == 'i': elif result == 'i':
pass pass
elif result == 'f': elif result == 'f':
fixwhite(repo.wjoin(name), tabsize, fixonly) fixwhite(repo.wjoin(name), args['tabsize'], fixonly)
else: else:
raise RepoError, "Invalid response: '%s'" % result raise util.Abort(_("Invalid response: '%s'") % result)
return False return False
modified, added, removed, deleted, unknown, ignore, clean = repo.status() modified, added, removed, deleted, unknown, ignore, clean = repo.status()
for fname in added: for fname in added:
if skip(fname):
continue
ok = True ok = True
for line,num in checkwhite(repo.wjoin(fname)): for line,num in checkwhite(repo.wjoin(fname)):
ui.write("invalid whitespace in %s:%d\n" % (fname, num)) ui.write("invalid whitespace in %s:%d\n" % (fname, num))
if verbose: if ui.verbose:
ui.write(">>%s<<\n" % line[-1]) ui.write(">>%s<<\n" % line[-1])
ok = False ok = False
@ -298,6 +306,9 @@ def check_whitespace(ui, repo, hooktype, node, parent1, parent2):
wctx = repo.workingctx() wctx = repo.workingctx()
for fname in modified: for fname in modified:
if skip(fname):
continue
if not whitespace_file(fname): if not whitespace_file(fname):
continue continue
@ -321,7 +332,7 @@ def check_whitespace(ui, repo, hooktype, node, parent1, parent2):
continue continue
ui.write("invalid whitespace: %s:%d\n" % (fname, i+1)) ui.write("invalid whitespace: %s:%d\n" % (fname, i+1))
if verbose: if ui.verbose:
ui.write(">>%s<<\n" % line[:-1]) ui.write(">>%s<<\n" % line[:-1])
fixonly.add(i) fixonly.add(i)
@ -329,6 +340,14 @@ def check_whitespace(ui, repo, hooktype, node, parent1, parent2):
if prompt(fname, fixonly): if prompt(fname, fixonly):
return True return True
def check_whitespace(ui, repo, hooktype, node, parent1, parent2):
if hooktype != 'pretxncommit':
raise AttributeError, \
"This hook is only meant for pretxncommit, not %s" % hooktype
args = { 'tabsize' : 8 }
do_check_whitespace(ui, repo, **args)
def check_format(ui, repo, hooktype, node, parent1, parent2): def check_format(ui, repo, hooktype, node, parent1, parent2):
if hooktype != 'pretxncommit': if hooktype != 'pretxncommit':
raise AttributeError, \ raise AttributeError, \
@ -350,10 +369,23 @@ def check_format(ui, repo, hooktype, node, parent1, parent2):
elif result.startswith('a'): elif result.startswith('a'):
return True return True
else: else:
raise RepoError, "Invalid response: '%s'" % result raise util.Abort(_("Invalid response: '%s'") % result)
return False return False
try:
from mercurial.i18n import _
except ImportError:
def _(arg):
return arg
cmdtable = {
'^m5style' :
( do_check_whitespace,
[ ('a', 'auto', False, _("automatically fix whitespace")),
('t', 'tabsize', 8, _("Number of spaces TAB indents")) ],
_('hg m5check [-t <tabsize>] [FILE]...')),
}
if __name__ == '__main__': if __name__ == '__main__':
import getopt import getopt