#! /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.
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)
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"""
- from mercurial import mdiff, util
- auto = args.get('auto', False)
- if auto:
- auto = 'f'
- ui = MercurialUI(hgui, hgui.verbose, auto)
+def do_check_style(hgui, repo, *pats, **opts):
+ """check files for proper m5 style guidelines
- if files:
- files = frozenset(files)
+ 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).
- 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
+ The --all option can be specified to include clean files and check
+ modified files in their entirety.
+ """
+ from mercurial import mdiff, util
+
+ 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')
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:
- wctx = repo.workingctx()
- except:
- from mercurial import context
- wctx = context.workingctx(repo)
+ 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
return arg
cmdtable = {
- '^m5style' :
- ( do_check_style,
- [ ('a', 'auto', False, _("automatically fix whitespace")) ],
- _('hg m5style [-a] [FILE]...')),
+ '^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,
[ ],