git-pre-commit.py revision 11467
19651SAndreas.Sandberg@ARM.com#!/usr/bin/env python
29651SAndreas.Sandberg@ARM.com#
39651SAndreas.Sandberg@ARM.com# Copyright (c) 2016 ARM Limited
49651SAndreas.Sandberg@ARM.com# All rights reserved
59651SAndreas.Sandberg@ARM.com#
69651SAndreas.Sandberg@ARM.com# The license below extends only to copyright in the software and shall
79651SAndreas.Sandberg@ARM.com# not be construed as granting a license to any other intellectual
89651SAndreas.Sandberg@ARM.com# property including but not limited to intellectual property relating
99651SAndreas.Sandberg@ARM.com# to a hardware implementation of the functionality of the software
109651SAndreas.Sandberg@ARM.com# licensed hereunder.  You may use the software subject to the license
119651SAndreas.Sandberg@ARM.com# terms below provided that you ensure that this notice is replicated
129651SAndreas.Sandberg@ARM.com# unmodified and in its entirety in all distributions of the software,
139651SAndreas.Sandberg@ARM.com# modified or unmodified, in source code or in binary form.
149651SAndreas.Sandberg@ARM.com#
159651SAndreas.Sandberg@ARM.com# Redistribution and use in source and binary forms, with or without
169651SAndreas.Sandberg@ARM.com# modification, are permitted provided that the following conditions are
179651SAndreas.Sandberg@ARM.com# met: redistributions of source code must retain the above copyright
189651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer;
199651SAndreas.Sandberg@ARM.com# redistributions in binary form must reproduce the above copyright
209651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer in the
219651SAndreas.Sandberg@ARM.com# documentation and/or other materials provided with the distribution;
229651SAndreas.Sandberg@ARM.com# neither the name of the copyright holders nor the names of its
239651SAndreas.Sandberg@ARM.com# contributors may be used to endorse or promote products derived from
249651SAndreas.Sandberg@ARM.com# this software without specific prior written permission.
259651SAndreas.Sandberg@ARM.com#
269651SAndreas.Sandberg@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
279651SAndreas.Sandberg@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
289651SAndreas.Sandberg@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
299651SAndreas.Sandberg@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
309651SAndreas.Sandberg@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
319651SAndreas.Sandberg@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
329651SAndreas.Sandberg@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
339651SAndreas.Sandberg@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
349651SAndreas.Sandberg@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
359651SAndreas.Sandberg@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
369651SAndreas.Sandberg@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
379651SAndreas.Sandberg@ARM.com#
389651SAndreas.Sandberg@ARM.com# Authors: Andreas Sandberg
399651SAndreas.Sandberg@ARM.com
409651SAndreas.Sandberg@ARM.comimport os
419651SAndreas.Sandberg@ARM.comimport sys
429651SAndreas.Sandberg@ARM.com
439651SAndreas.Sandberg@ARM.comfrom style.repo import GitRepo
449651SAndreas.Sandberg@ARM.comfrom style.verifiers import all_verifiers, all_regions
459651SAndreas.Sandberg@ARM.comfrom style.style import StdioUI, check_ignores
469651SAndreas.Sandberg@ARM.com
479651SAndreas.Sandberg@ARM.comimport argparse
489651SAndreas.Sandberg@ARM.com
499760Sandreas@sandberg.pp.separser = argparse.ArgumentParser(
509651SAndreas.Sandberg@ARM.com    description="gem5 git style checker hook")
519651SAndreas.Sandberg@ARM.com
529683Sandreas@sandberg.pp.separser.add_argument("--verbose", "-v", action="store_true",
539753Sandreas@sandberg.pp.se                    help="Produce verbose output")
549651SAndreas.Sandberg@ARM.com
559651SAndreas.Sandberg@ARM.comargs = parser.parse_args()
569651SAndreas.Sandberg@ARM.com
579651SAndreas.Sandberg@ARM.comgit = GitRepo()
589651SAndreas.Sandberg@ARM.com
599651SAndreas.Sandberg@ARM.comopts = {}
609651SAndreas.Sandberg@ARM.comrepo_base = git.repo_base()
619753Sandreas@sandberg.pp.seui = StdioUI()
629753Sandreas@sandberg.pp.se
639651SAndreas.Sandberg@ARM.comos.chdir(repo_base)
649651SAndreas.Sandberg@ARM.comfailing_files = set()
659651SAndreas.Sandberg@ARM.com
669651SAndreas.Sandberg@ARM.comfor status, fname in git.status(filter="MA", cached=True):
679651SAndreas.Sandberg@ARM.com    if args.verbose:
689651SAndreas.Sandberg@ARM.com        print "Checking %s..." % fname
699651SAndreas.Sandberg@ARM.com    if check_ignores(fname):
709651SAndreas.Sandberg@ARM.com        continue
719651SAndreas.Sandberg@ARM.com    if status == "M":
729652SAndreas.Sandberg@ARM.com        regions = git.staged_regions(fname)
739652SAndreas.Sandberg@ARM.com    else:
749651SAndreas.Sandberg@ARM.com        regions = all_regions
759651SAndreas.Sandberg@ARM.com
769651SAndreas.Sandberg@ARM.com    verifiers = [ v(ui, opts, base=repo_base) for v in all_verifiers ]
779651SAndreas.Sandberg@ARM.com    for v in verifiers:
789892Sandreas@sandberg.pp.se        if v.check(fname, regions):
799655SAndreas.Sandberg@ARM.com            failing_files.add(fname)
809752Sandreas@sandberg.pp.se
819753Sandreas@sandberg.pp.seif failing_files:
829752Sandreas@sandberg.pp.se    print >> sys.stderr
839651SAndreas.Sandberg@ARM.com    print >> sys.stderr, "Style checker failed for the following files:"
849651SAndreas.Sandberg@ARM.com    for f in failing_files:
859651SAndreas.Sandberg@ARM.com        print >> sys.stderr, "\t%s" % f
869651SAndreas.Sandberg@ARM.com    print >> sys.stderr
879651SAndreas.Sandberg@ARM.com    print >> sys.stderr, \
889651SAndreas.Sandberg@ARM.com        "Please run the style checker manually to fix the offending files.\n" \
899651SAndreas.Sandberg@ARM.com        "To check your modifications, run: util/style.py -m"
909651SAndreas.Sandberg@ARM.com    sys.exit(1)
919651SAndreas.Sandberg@ARM.com