scons: Update automatic hook install for pre-commit

This replaces the old hooks with the pre-commit check. If Python
pre-commit has not been installed an error is thrown asking the user to
install the requirements via pip.

Change-Id: I2d42f42624e10d38d0da39b473f0363db128ce1c
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/62553
Tested-by: kokoro <noreply+kokoro@google.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
This commit is contained in:
Bobby R. Bruce
2022-08-18 16:08:37 -07:00
committed by Bobby Bruce
parent 68c00268cc
commit 5a88dcfdef
2 changed files with 53 additions and 44 deletions

View File

@@ -38,17 +38,21 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from asyncio import subprocess
import os
import sys
import subprocess
import gem5_scons.util
import SCons.Script
git_style_message = """
You're missing the gem5 style or commit message hook. These hooks help
to ensure that your code follows gem5's style rules on git commit.
This script will now install the hook in your .git/hooks/ directory.
Press enter to continue, or ctrl-c to abort: """
You're missing the pre-commit/commit-msg hooks. These hook help to ensure your
code follows gem5's style rules on git commit and your commit messages follow
our commit message requirements. This script will now install these hooks in
your .git/hooks/ directory.
Press enter to continue, or ctrl-c to abort:
"""
def install_style_hooks(env):
@@ -68,42 +72,6 @@ def install_style_hooks(env):
hook = git_hooks.File(hook_name)
return hook.exists()
def hook_install(hook_name, script):
hook = git_hooks.File(hook_name)
if hook.exists():
print(
"Warning: Can't install %s, hook already exists." % hook_name
)
return
if hook.islink():
print("Warning: Removing broken symlink for hook %s." % hook_name)
os.unlink(hook.get_abspath())
if not git_hooks.exists():
os.mkdir(git_hooks.get_abspath())
git_hooks.clear()
abs_symlink_hooks = git_hooks.islink() and os.path.isabs(
os.readlink(git_hooks.get_abspath())
)
# Use a relative symlink if the hooks live in the source directory,
# and the hooks directory is not a symlink to an absolute path.
if hook.is_under(env.Dir("#")) and not abs_symlink_hooks:
script_path = os.path.relpath(
os.path.realpath(script.get_abspath()),
os.path.realpath(hook.Dir(".").get_abspath()),
)
else:
script_path = script.get_abspath()
try:
os.symlink(script_path, hook.get_abspath())
except:
print("Error updating git %s hook" % hook_name)
raise
if hook_exists("pre-commit") and hook_exists("commit-msg"):
return
@@ -117,11 +85,9 @@ def install_style_hooks(env):
print("Input exception, exiting scons.\n")
sys.exit(1)
git_style_script = env.Dir("#util").File("git-pre-commit.py")
git_msg_script = env.Dir("#ext").File("git-commit-msg")
pre_commit_install = env.Dir("#util").File("pre-commit-install.sh")
hook_install("pre-commit", git_style_script)
hook_install("commit-msg", git_msg_script)
subprocess.call(str(pre_commit_install), shell=True)
def generate(env):

43
util/pre-commit-install.sh Executable file
View File

@@ -0,0 +1,43 @@
#!/bin/bash
# Copyright (c) 2022 The Regents of the University of California
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
GEM5_ROOT="${DIR}/.."
cd ${GEM5_ROOT}
if ! command -v pre-commit &> /dev/null
then
echo "Cannot find 'pre-commit'. Please ensure all Python requirements are "
echo "installed. This can be done via `pip install -r requirements.txt`."
exit 1
fi
pre-commit install -t pre-commit -t commit-msg