style: Fixup strange semantics in hg m5style

The 'hg m5style' command had some rather strange semantics. When
called without arguments, it applied the style checker to all added
files and modified regions of modified files. However, when providing
a list of files, it used that list as an ignore list instead of
specifically checking those files.

This patch makes the m5style command behave more like other Mercurial
commands where the arguments are used to specify which files to work
on instead of which files to ignore.
This commit is contained in:
Andreas Sandberg 2014-08-26 10:14:07 -04:00
parent 70176fecd1
commit 35bc5210d3

View file

@ -1,4 +1,16 @@
#! /usr/bin/env python
# Copyright (c) 2014 ARM Limited
# All rights reserved
#
# The license below extends only to copyright in the software and shall
# not be construed as granting a license to any other intellectual
# property including but not limited to intellectual property relating
# to a hardware implementation of the functionality of the software
# licensed hereunder. You may use the software subject to the license
# terms below provided that you ensure that this notice is replicated
# unmodified and in its entirety in all distributions of the software,
# modified or unmodified, in source code or in binary form.
#
# Copyright (c) 2006 The Regents of The University of Michigan
# Copyright (c) 2007,2011 The Hewlett-Packard Development Company
# All rights reserved.
@ -35,7 +47,7 @@ import sys
from os.path import dirname, join as joinpath
from itertools import count
from mercurial import bdiff, mdiff
from mercurial import bdiff, mdiff, commands
current_dir = dirname(__file__)
sys.path.insert(0, current_dir)
@ -378,27 +390,25 @@ def validate(filename, stats, verbose, exit_code):
msg(i, line, 'improper spacing after %s' % match.group(1))
bad()
def do_check_style(hgui, repo, *files, **args):
"""check files for proper m5 style guidelines"""
def do_check_style(hgui, repo, *pats, **opts):
"""check files for proper m5 style guidelines
Without an argument, checks all modified and added files for gem5
coding style violations. A list of files can be specified to limit
the checker to a subset of the repository. The style rules are
normally applied on a diff of the repository state (i.e., added
files are checked in their entirety while only modifications of
modified files are checked).
The --all option can be specified to include clean files and check
modified files in their entirety.
"""
from mercurial import mdiff, util
auto = args.get('auto', False)
if auto:
auto = 'f'
ui = MercurialUI(hgui, hgui.verbose, auto)
if files:
files = frozenset(files)
def skip(name):
# We never want to handle symlinks, so always skip them: If the location
# pointed to is a directory, skip it. If the location is a file inside
# the gem5 directory, it will be checked as a file, so symlink can be
# skipped. If the location is a file outside gem5, we don't want to
# check it anyway.
if os.path.islink(name):
return True
return files and name in files
opt_fix_white = opts.get('fix_white', False)
opt_all = opts.get('all', False)
ui = MercurialUI(hgui, hgui.verbose, opt_fix_white)
def prompt(name, func, regions=all_regions):
result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a')
@ -409,39 +419,40 @@ def do_check_style(hgui, repo, *files, **args):
return False
modified, added, removed, deleted, unknown, ignore, clean = repo.status()
whitespace = Whitespace(ui)
sorted_includes = SortedIncludes(ui)
for fname in added:
if skip(fname):
continue
fpath = joinpath(repo.root, fname)
if whitespace.apply(fpath, prompt):
return True
if sorted_includes.apply(fpath, prompt):
return True
# Import the match (repository file name matching helper)
# function. Different versions of Mercurial keep it in different
# modules and implement them differently.
try:
from mercurial import scmutil
m = scmutil.match(repo[None], pats, opts)
except ImportError:
from mercurial import cmdutil
m = cmdutil.match(repo, pats, opts)
modified, added, removed, deleted, unknown, ignore, clean = \
repo.status(match=m, clean=opt_all)
if not opt_all:
try:
wctx = repo.workingctx()
except:
from mercurial import context
wctx = context.workingctx(repo)
for fname in modified:
if skip(fname):
continue
files = [ (fn, all_regions) for fn in added ] + \
[ (fn, modregions(wctx, fn)) for fn in modified ]
else:
files = [ (fn, all_regions) for fn in added + modified + clean ]
whitespace = Whitespace(ui)
sorted_includes = SortedIncludes(ui)
for fname, mod_regions in files:
fpath = joinpath(repo.root, fname)
regions = modregions(wctx, fname)
if whitespace.apply(fpath, prompt, regions):
if whitespace.apply(fpath, prompt, mod_regions):
return True
if sorted_includes.apply(fpath, prompt, regions):
if sorted_includes.apply(fpath, prompt, mod_regions):
return True
return False
@ -499,9 +510,12 @@ except ImportError:
return arg
cmdtable = {
'^m5style' :
( do_check_style,
[ ('a', 'auto', False, _("automatically fix whitespace")) ],
'^m5style' : (
do_check_style, [
('w', 'fix-white', False, _("automatically fix whitespace")),
('a', 'all', False,
_("include clean files and unmodified parts of modified files")),
] + commands.walkopts,
_('hg m5style [-a] [FILE]...')),
'^m5format' :
( do_check_format,