util: git pre-commit hook to check staged files

This patch updates the git-pre-commit hook to check the files as they
will be after the commit, instead of as they are currently, this way we
prevent the undesired situation:
    - unstylish modification of a file
    - stage said file for commit
    - try to commit and fail due to style
    - fix style, forgetting staging changes
    - try to commit and fail, as although the changes staged are not
      styly, the current content of the file is.

Change-Id: I5cc3f783375d9e4162e310e176103ebbf0a59023
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
[andreas.sandberg@arm.com: Rebased ontop of latest gem5]
This commit is contained in:
Rekai Gonzalez Alberquilla
2016-11-25 10:31:21 +00:00
parent b0856ab3b1
commit ac29b6c6fc
2 changed files with 72 additions and 24 deletions

View File

@@ -37,7 +37,9 @@
#
# Authors: Andreas Sandberg
from tempfile import TemporaryFile
import os
import subprocess
import sys
from style.repo import GitRepo
@@ -62,6 +64,7 @@ ui = StdioUI()
os.chdir(repo_base)
failing_files = set()
staged_mismatch = set()
for status, fname in git.status(filter="MA", cached=True):
if args.verbose:
@@ -73,18 +76,41 @@ for status, fname in git.status(filter="MA", cached=True):
else:
regions = all_regions
# Show they appropriate object and dump it to a file
status = git.file_from_index(fname)
f = TemporaryFile()
f.write(status)
verifiers = [ v(ui, opts, base=repo_base) for v in all_verifiers ]
for v in verifiers:
if not v.skip(fname) and v.check(fname, regions):
f.seek(0)
# It is prefered that the first check is silent as it is in the
# staged file. If the check fails, then we will do it non-silently
# on the current file, reporting meaningful shortcomings
if not v.skip(fname) and v.check(fname, regions, fobj=f, silent=True):
failing_files.add(fname)
if not v.check(fname, regions):
staged_mismatch.add(fname)
f.close()
if failing_files:
print >> sys.stderr
print >> sys.stderr, "Style checker failed for the following files:"
for f in failing_files:
print >> sys.stderr, "\t%s" % f
print >> sys.stderr
print >> sys.stderr, \
if len(failing_files) > len(staged_mismatch):
print >> sys.stderr
print >> sys.stderr, "Style checker failed for the following files:"
for f in failing_files:
if f not in staged_mismatch:
print >> sys.stderr, "\t%s" % f
print >> sys.stderr
print >> sys.stderr, \
"Please run the style checker manually to fix the offending files.\n" \
"To check your modifications, run: util/style.py -m"
print >> sys.stderr
if staged_mismatch:
print >> sys.stderr, \
"It looks like you have forgotten to stage your fixes for commit in\n"\
"the following files: "
for f in staged_mismatch:
print >> sys.stderr, "\t%s" % f
print >> sys.stderr, "Please `git --add' them"
sys.exit(1)