From 3037fde5ebddf3d755d29b4f06f21e6ca122d484 Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Fri, 24 Sep 2021 14:11:13 -0400 Subject: Move commit gst-indent hook to the root This renable at meson setup time the installation of the gst-indent commit hook. The hooks were kept from gst-devtools as this set supports both C checks and Python checks. Part-of: --- scripts/git-hooks/multi-pre-commit.hook | 43 +++++++++++++++++ scripts/git-hooks/pre-commit-python.hook | 81 +++++++++++++++++++++++++++++++ scripts/git-hooks/pre-commit.hook | 83 ++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100755 scripts/git-hooks/multi-pre-commit.hook create mode 100755 scripts/git-hooks/pre-commit-python.hook create mode 100755 scripts/git-hooks/pre-commit.hook (limited to 'scripts') diff --git a/scripts/git-hooks/multi-pre-commit.hook b/scripts/git-hooks/multi-pre-commit.hook new file mode 100755 index 0000000000..a77d0da21f --- /dev/null +++ b/scripts/git-hooks/multi-pre-commit.hook @@ -0,0 +1,43 @@ +#!/bin/sh +# Git pre-commit hook that runs multiple hooks specified in $HOOKS. +# Make sure this script is executable. Bypass hooks with git commit --no-verify. + +# This file is inspired by a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + + +########################################################### +# SETTINGS: +# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder +# as this script. Hooks should return 0 if successful and nonzero to cancel the +# commit. They are executed in the order in which they are listed. +########################################################### + +HOOKS="scripts/git-hooks/pre-commit.hook scripts/git-hooks/pre-commit-python.hook" + +# exit on error +set -e + +echo $PWD + +for hook in $HOOKS +do + echo "Running hook: $hook" + # run hook if it exists + # if it returns with nonzero exit with 1 and thus abort the commit + if [ -f "$PWD/$hook" ]; then + "$PWD/$hook" + if [ $? != 0 ]; then + exit 1 + fi + else + echo "Error: file $hook not found." + echo "Aborting commit. Make sure the hook is at $PWD/$hook and executable." + echo "You can disable it by removing it from the list" + echo "You can skip all pre-commit hooks with --no-verify (not recommended)." + exit 1 + fi +done + diff --git a/scripts/git-hooks/pre-commit-python.hook b/scripts/git-hooks/pre-commit-python.hook new file mode 100755 index 0000000000..14fbc63bfd --- /dev/null +++ b/scripts/git-hooks/pre-commit-python.hook @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +import os +import subprocess +import sys +import tempfile + +NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \ + "Your code is not fully pycodestyle compliant and contains"\ + " the following coding style issues:\n\n" + +NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \ + "Please fix these errors and commit again, you can do so "\ + "from the root directory automatically like this, assuming the whole "\ + "file is to be commited:" + +NO_PYCODESTYLE_MESSAGE = \ + "You should install the pycodestyle style checker to be able"\ + " to commit in this repo.\nIt allows us to garantee that "\ + "anything that is commited respects the pycodestyle coding style "\ + "standard.\nYou can install it:\n"\ + " * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\ + " * on fedora: #yum install python3-pycodestyle \n"\ + " * on arch: #pacman -S python-pycodestyle \n"\ + " * or `pip install --user pycodestyle`" + + +def system(*args, **kwargs): + kwargs.setdefault('stdout', subprocess.PIPE) + proc = subprocess.Popen(args, **kwargs) + out, err = proc.communicate() + if isinstance(out, bytes): + out = out.decode() + return out + + +def copy_files_to_tmp_dir(files): + tempdir = tempfile.mkdtemp() + for name in files: + filename = os.path.join(tempdir, name) + filepath = os.path.dirname(filename) + if not os.path.exists(filepath): + os.makedirs(filepath) + with open(filename, 'w') as f: + system('git', 'show', ':' + name, stdout=f) + + return tempdir + + +def main(): + modified_files = system('git', 'diff-index', '--cached', + '--name-only', 'HEAD', '--diff-filter=ACMR').split("\n")[:-1] + non_compliant_files = [] + output_message = None + + for modified_file in modified_files: + try: + if not modified_file.endswith(".py"): + continue + pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E402,E501,E128,W605,W503', modified_file) + if pycodestyle_errors: + if output_message is None: + output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE + output_message += pycodestyle_errors + non_compliant_files.append(modified_file) + except OSError as e: + output_message = NO_PYCODESTYLE_MESSAGE + break + + if output_message: + print(output_message) + if non_compliant_files: + print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST) + for non_compliant_file in non_compliant_files: + print("autopep8 -i ", non_compliant_file, "; git add ", + non_compliant_file) + print("git commit") + sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/scripts/git-hooks/pre-commit.hook b/scripts/git-hooks/pre-commit.hook new file mode 100755 index 0000000000..3c1062b9e0 --- /dev/null +++ b/scripts/git-hooks/pre-commit.hook @@ -0,0 +1,83 @@ +#!/bin/sh +# +# Check that the code follows a consistant code style +# + +# Check for existence of indent, and error out if not present. +# On some *bsd systems the binary seems to be called gnunindent, +# so check for that first. + +version=`gnuindent --version 2>/dev/null` +if test "x$version" = "x"; then + version=`gindent --version 2>/dev/null` + if test "x$version" = "x"; then + version=`indent --version 2>/dev/null` + if test "x$version" = "x"; then + echo "GStreamer git pre-commit hook:" + echo "Did not find GNU indent, please install it before continuing." + exit 1 + else + INDENT=indent + fi + else + INDENT=gindent + fi +else + INDENT=gnuindent +fi + +case `$INDENT --version` in + GNU*) + ;; + default) + echo "GStreamer git pre-commit hook:" + echo "Did not find GNU indent, please install it before continuing." + echo "(Found $INDENT, but it doesn't seem to be GNU indent)" + exit 1 + ;; +esac + +INDENT_PARAMETERS="--braces-on-if-line \ + --case-brace-indentation0 \ + --case-indentation2 \ + --braces-after-struct-decl-line \ + --line-length80 \ + --no-tabs \ + --cuddle-else \ + --dont-line-up-parentheses \ + --continuation-indentation4 \ + --honour-newlines \ + --tab-size8 \ + --indent-level2 \ + --leave-preprocessor-space" + +echo "--Checking style--" +for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.c$"` ; do + # nf is the temporary checkout. This makes sure we check against the + # revision in the index (and not the checked out version). + nf=`git checkout-index --temp ${file} | cut -f 1` + newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1 + $INDENT ${INDENT_PARAMETERS} \ + $nf -o $newfile 2>> /dev/null + # FIXME: Call indent twice as it tends to do line-breaks + # different for every second call. + $INDENT ${INDENT_PARAMETERS} \ + $newfile 2>> /dev/null + diff -u -p "${nf}" "${newfile}" + r=$? + rm "${newfile}" + rm "${nf}" + if [ $r != 0 ] ; then +echo "=================================================================================================" +echo " Code style error in: $file " +echo " " +echo " Please fix before committing. Don't forget to run git add before trying to commit again. " +echo " If the whole file is to be committed, this should work (run from the top-level directory): " +echo " " +echo " gst-indent $file; git add $file; git commit" +echo " " +echo "=================================================================================================" + exit 1 + fi +done +echo "--Checking style pass--" -- cgit v1.2.1