style: clean up style hook code a bit
I've renamed the check_whitespace operation to check_style. You're going to need to change your .hg/hgrc file. While you're at it, add a pre-qrefresh hook please.
This commit is contained in:
parent
ae7e67f334
commit
3c8cc170d2
2 changed files with 15 additions and 16 deletions
|
@ -177,8 +177,8 @@ or your personal .hgrc
|
||||||
style = %s/util/style.py
|
style = %s/util/style.py
|
||||||
|
|
||||||
[hooks]
|
[hooks]
|
||||||
pretxncommit.style = python:style.check_whitespace
|
pretxncommit.style = python:style.check_style
|
||||||
pre-qrefresh.style = python:style.check_whitespace
|
pre-qrefresh.style = python:style.check_style
|
||||||
""" % (main.root)
|
""" % (main.root)
|
||||||
|
|
||||||
mercurial_bin_not_found = """
|
mercurial_bin_not_found = """
|
||||||
|
|
|
@ -36,6 +36,7 @@ sys.path.insert(0, os.path.dirname(__file__))
|
||||||
|
|
||||||
from file_types import lang_type
|
from file_types import lang_type
|
||||||
|
|
||||||
|
tabsize = 8
|
||||||
lead = re.compile(r'^([ \t]+)')
|
lead = re.compile(r'^([ \t]+)')
|
||||||
trail = re.compile(r'([ \t]+)$')
|
trail = re.compile(r'([ \t]+)$')
|
||||||
any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
|
any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
|
||||||
|
@ -69,7 +70,7 @@ def checkwhite(filename):
|
||||||
if not checkwhite_line(line):
|
if not checkwhite_line(line):
|
||||||
yield line,num + 1
|
yield line,num + 1
|
||||||
|
|
||||||
def fixwhite_line(line, tabsize):
|
def fixwhite_line(line):
|
||||||
if lead.search(line):
|
if lead.search(line):
|
||||||
newline = ''
|
newline = ''
|
||||||
for i,c in enumerate(line):
|
for i,c in enumerate(line):
|
||||||
|
@ -85,7 +86,7 @@ def fixwhite_line(line, tabsize):
|
||||||
|
|
||||||
return line.rstrip() + '\n'
|
return line.rstrip() + '\n'
|
||||||
|
|
||||||
def fixwhite(filename, tabsize, fixonly=None):
|
def fixwhite(filename, fixonly=None):
|
||||||
if lang_type(filename) not in whitespace_types:
|
if lang_type(filename) not in whitespace_types:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -224,7 +225,7 @@ def modified_lines(old_data, new_data, max_lines):
|
||||||
break
|
break
|
||||||
return modified
|
return modified
|
||||||
|
|
||||||
def do_check_whitespace(ui, repo, *files, **args):
|
def do_check_style(ui, repo, *files, **args):
|
||||||
"""check files for proper m5 style guidelines"""
|
"""check files for proper m5 style guidelines"""
|
||||||
from mercurial import mdiff, util
|
from mercurial import mdiff, util
|
||||||
|
|
||||||
|
@ -234,7 +235,7 @@ def do_check_whitespace(ui, repo, *files, **args):
|
||||||
def skip(name):
|
def skip(name):
|
||||||
return files and name in files
|
return files and name in files
|
||||||
|
|
||||||
def prompt(name, fixonly=None):
|
def prompt(name, func, fixonly=None):
|
||||||
if args.get('auto', False):
|
if args.get('auto', False):
|
||||||
result = 'f'
|
result = 'f'
|
||||||
else:
|
else:
|
||||||
|
@ -246,7 +247,7 @@ def do_check_whitespace(ui, repo, *files, **args):
|
||||||
if result == 'a':
|
if result == 'a':
|
||||||
return True
|
return True
|
||||||
elif result == 'f':
|
elif result == 'f':
|
||||||
fixwhite(repo.wjoin(name), args['tabsize'], fixonly)
|
func(repo.wjoin(name), fixonly)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -264,7 +265,7 @@ def do_check_whitespace(ui, repo, *files, **args):
|
||||||
ok = False
|
ok = False
|
||||||
|
|
||||||
if not ok:
|
if not ok:
|
||||||
if prompt(fname):
|
if prompt(fname, fixwhite):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -337,10 +338,10 @@ def check_hook(hooktype):
|
||||||
raise AttributeError, \
|
raise AttributeError, \
|
||||||
"This hook is not meant for %s" % hooktype
|
"This hook is not meant for %s" % hooktype
|
||||||
|
|
||||||
def check_whitespace(ui, repo, hooktype, **kwargs):
|
def check_style(ui, repo, hooktype, **kwargs):
|
||||||
check_hook(hooktype)
|
check_hook(hooktype)
|
||||||
args = { 'tabsize' : 8 }
|
args = {}
|
||||||
return do_check_whitespace(ui, repo, **args)
|
return do_check_style(ui, repo, **args)
|
||||||
|
|
||||||
def check_format(ui, repo, hooktype, **kwargs):
|
def check_format(ui, repo, hooktype, **kwargs):
|
||||||
check_hook(hooktype)
|
check_hook(hooktype)
|
||||||
|
@ -355,10 +356,9 @@ except ImportError:
|
||||||
|
|
||||||
cmdtable = {
|
cmdtable = {
|
||||||
'^m5style' :
|
'^m5style' :
|
||||||
( do_check_whitespace,
|
( do_check_style,
|
||||||
[ ('a', 'auto', False, _("automatically fix whitespace")),
|
[ ('a', 'auto', False, _("automatically fix whitespace")) ],
|
||||||
('t', 'tabsize', 8, _("Number of spaces TAB indents")) ],
|
_('hg m5style [-a] [FILE]...')),
|
||||||
_('hg m5style [-a] [-t <tabsize>] [FILE]...')),
|
|
||||||
'^m5format' :
|
'^m5format' :
|
||||||
( do_check_format,
|
( do_check_format,
|
||||||
[ ],
|
[ ],
|
||||||
|
@ -393,7 +393,6 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
code = 1
|
code = 1
|
||||||
verbose = 1
|
verbose = 1
|
||||||
tabsize = 8
|
|
||||||
for opt,arg in opts:
|
for opt,arg in opts:
|
||||||
if opt == '-n':
|
if opt == '-n':
|
||||||
code = None
|
code = None
|
||||||
|
|
Loading…
Reference in a new issue