git-pre-commit.py revision 11467
14997Sgblack@eecs.umich.edu#!/usr/bin/env python
25268Sksewell@umich.edu#
35254Sksewell@umich.edu# Copyright (c) 2016 ARM Limited
45254Sksewell@umich.edu# All rights reserved
54997Sgblack@eecs.umich.edu#
65254Sksewell@umich.edu# The license below extends only to copyright in the software and shall
75254Sksewell@umich.edu# not be construed as granting a license to any other intellectual
85254Sksewell@umich.edu# property including but not limited to intellectual property relating
95254Sksewell@umich.edu# to a hardware implementation of the functionality of the software
105254Sksewell@umich.edu# licensed hereunder.  You may use the software subject to the license
115254Sksewell@umich.edu# terms below provided that you ensure that this notice is replicated
125254Sksewell@umich.edu# unmodified and in its entirety in all distributions of the software,
135254Sksewell@umich.edu# modified or unmodified, in source code or in binary form.
145254Sksewell@umich.edu#
155254Sksewell@umich.edu# Redistribution and use in source and binary forms, with or without
164997Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
175254Sksewell@umich.edu# met: redistributions of source code must retain the above copyright
185254Sksewell@umich.edu# notice, this list of conditions and the following disclaimer;
195254Sksewell@umich.edu# redistributions in binary form must reproduce the above copyright
205254Sksewell@umich.edu# notice, this list of conditions and the following disclaimer in the
215254Sksewell@umich.edu# documentation and/or other materials provided with the distribution;
225254Sksewell@umich.edu# neither the name of the copyright holders nor the names of its
235254Sksewell@umich.edu# contributors may be used to endorse or promote products derived from
245254Sksewell@umich.edu# this software without specific prior written permission.
255254Sksewell@umich.edu#
265254Sksewell@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
275254Sksewell@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
284997Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
295268Sksewell@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
305268Sksewell@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
315268Sksewell@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
328696Sguodeyuan@tsinghua.org.cn# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
338696Sguodeyuan@tsinghua.org.cn# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
344997Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
354997Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3611793Sbrandon.potter@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3711793Sbrandon.potter@amd.com#
385222Sksewell@umich.edu# Authors: Andreas Sandberg
395222Sksewell@umich.edu
404997Sgblack@eecs.umich.eduimport os
418229Snate@binkert.orgimport sys
428229Snate@binkert.org
435222Sksewell@umich.edufrom style.repo import GitRepo
445222Sksewell@umich.edufrom style.verifiers import all_verifiers, all_regions
455222Sksewell@umich.edufrom style.style import StdioUI, check_ignores
465222Sksewell@umich.edu
475222Sksewell@umich.eduimport argparse
485222Sksewell@umich.edu
498232Snate@binkert.orgparser = argparse.ArgumentParser(
508232Snate@binkert.org    description="gem5 git style checker hook")
515224Sksewell@umich.edu
525222Sksewell@umich.eduparser.add_argument("--verbose", "-v", action="store_true",
538229Snate@binkert.org                    help="Produce verbose output")
544997Sgblack@eecs.umich.edu
555222Sksewell@umich.eduargs = parser.parse_args()
565222Sksewell@umich.edu
575019Sgblack@eecs.umich.edugit = GitRepo()
585222Sksewell@umich.edu
595222Sksewell@umich.eduopts = {}
605222Sksewell@umich.edurepo_base = git.repo_base()
615222Sksewell@umich.eduui = StdioUI()
625019Sgblack@eecs.umich.edu
635222Sksewell@umich.eduos.chdir(repo_base)
645358Sgblack@eecs.umich.edufailing_files = set()
655222Sksewell@umich.edu
666378Sgblack@eecs.umich.edufor status, fname in git.status(filter="MA", cached=True):
676378Sgblack@eecs.umich.edu    if args.verbose:
686378Sgblack@eecs.umich.edu        print "Checking %s..." % fname
695222Sksewell@umich.edu    if check_ignores(fname):
705222Sksewell@umich.edu        continue
715222Sksewell@umich.edu    if status == "M":
725222Sksewell@umich.edu        regions = git.staged_regions(fname)
735222Sksewell@umich.edu    else:
745222Sksewell@umich.edu        regions = all_regions
755222Sksewell@umich.edu
765222Sksewell@umich.edu    verifiers = [ v(ui, opts, base=repo_base) for v in all_verifiers ]
775222Sksewell@umich.edu    for v in verifiers:
785222Sksewell@umich.edu        if v.check(fname, regions):
795222Sksewell@umich.edu            failing_files.add(fname)
805222Sksewell@umich.edu
815222Sksewell@umich.eduif failing_files:
826378Sgblack@eecs.umich.edu    print >> sys.stderr
835222Sksewell@umich.edu    print >> sys.stderr, "Style checker failed for the following files:"
845222Sksewell@umich.edu    for f in failing_files:
855222Sksewell@umich.edu        print >> sys.stderr, "\t%s" % f
865222Sksewell@umich.edu    print >> sys.stderr
876378Sgblack@eecs.umich.edu    print >> sys.stderr, \
885222Sksewell@umich.edu        "Please run the style checker manually to fix the offending files.\n" \
895222Sksewell@umich.edu        "To check your modifications, run: util/style.py -m"
905222Sksewell@umich.edu    sys.exit(1)
915222Sksewell@umich.edu