diff options
Diffstat (limited to '')
-rwxr-xr-x | tools/pre-commit | 135 | ||||
-rw-r--r-- | tools/pre-commit-ignore.conf | 27 | ||||
-rwxr-xr-x | tools/pre-commit-ignore.py | 59 |
3 files changed, 221 insertions, 0 deletions
diff --git a/tools/pre-commit b/tools/pre-commit new file mode 100755 index 0000000..be74103 --- /dev/null +++ b/tools/pre-commit @@ -0,0 +1,135 @@ +#!/bin/sh +# Copyright 2013, Alexis La Goutte (See AUTHORS file) +# +# For git user: copy tools/pre-commit to .git/hooks/ folder and make it +# executable. To bypass it for a single commit, use the --no-verify argument. +# Using --no-verify will then fail during git review because of a missing +# ChangeID. Fix that by running git review -i. Do not use -i during normal +# operation. +# +# Alternatively, invoke it directly with the commit ID. Example for checking the +# last commit: +# +# tools/pre-commit HEAD~ +# +# Relative paths are also supported. For instance, if you are in epan/, then you +# could invoke `../tools/pre-commit HEAD` to check for changes to staged files. +# +# From +# http://mark-story.com/posts/view/using-git-commit-hooks-to-prevent-stupid-mistakes +# + +# If the commit identifier is not given, use HEAD instead. +COMMIT_ID="${1:-HEAD}" + +UNAME=$( uname -a ) + +case "$UNAME" in + *\ Msys) + pyvar="pythonw.exe" + ;; + *) + pyvar="python3" + ;; +esac + +PYBIN=${WS_GITHOOK_PYTHON:-$pyvar} + +# Path to hook script in the .git directory +hook_script=${GIT_DIR:-.git}/hooks/pre-commit + +# Always start in the root directory of the source tree, this allows for +# invocations via relative paths (such as ../tools/pre-commit): +if ! cd "$(git rev-parse --show-toplevel)" ; then + echo "Can't change to the top-level source directory." + exit 1 +fi + +# Check for newer (actually, different) versions of the pre-commit script +# (but only if invoked as hook, i.e. the commit ID is not given as argument). +if [ -z "$1" ] && [ -f "$hook_script" ]; then + if ! cmp -s "$hook_script" tools/pre-commit; then + echo "Pre-commit hook script is outdated, please update! (cp tools/pre-commit ${hook_script})" + fi +fi + +exit_status=0 + +COMMIT_FILES=$( git diff-index --cached --name-status "${COMMIT_ID}" | grep -v "^D" | cut -f2 | grep "\\.[ch]$" ) +DIAMETER_FILES=$( git diff-index --cached --name-status "${COMMIT_ID}" | grep -v "^D" | cut -f2 | grep diameter/ ) + +# Path to filter script in the tools directory +filter_script=${PWD}/tools/pre-commit-ignore.py +filter_conf=${PWD}/tools/pre-commit-ignore.conf + +if [ -f "$filter_script" ] && [ -f "$filter_conf" ]; then + CHECK_FILES=$( echo "$COMMIT_FILES" | "$PYBIN" "$filter_script" "$filter_conf" ) || exit +else + CHECK_FILES="$COMMIT_FILES" +fi + +bad_alloc_patterns=${PWD}/tools/detect_bad_alloc_patterns.py +echo "$COMMIT_FILES" | $PYBIN "$bad_alloc_patterns" + +# On windows python will output \r\n line endings - we don't want that. +# +# Do not use sed, as not all versions of sed support \r as meaning CR +# in a regexp - the only version that does so might be GNU sed; the +# GNU sed documentation says that only \n and \\ can be used in a +# portable script. +# +# The Single UNIX Specification says that tr supports \r; most if not +# all modern UN*Xes should support it. +CHECK_FILES=$( echo "$CHECK_FILES" | tr -d '\r' ) + +for FILE in $CHECK_FILES; do + # Skip some special cases + FILE_BASENAME="$( basename "$FILE" )" + # This should only be done on code that's part of one or more + # Wireshark programs; idl2wrs.c is a developer tool, not a + # Wireshark program, so these tests don't apply. + if test "$FILE_BASENAME" = "idl2wrs.c" + then + continue + fi + if test "$FILE_BASENAME" = "wmem_test.c" + then + continue + fi + + #Check if checkhf is good + ./tools/checkhf.pl "$FILE" || exit_status=1 + + #Check if checkAPIs is good + ./tools/checkAPIs.pl -p "$FILE" || exit_status=1 + + #Check if fix-encoding-args is good + ./tools/fix-encoding-args.pl "$FILE" || exit_status=1 + + #Check if checkfiltername is good + ./tools/checkfiltername.pl "$FILE" || exit_status=1 + + # If there are whitespace errors, print the offending file names and fail. (from git pre-commit.sample) + git diff-index --check --cached "${COMMIT_ID}" "$FILE" || exit_status=1 + +done + +if [ "x$DIAMETER_FILES" != x ] +then + ./tools/validate-diameter-xml.sh > /dev/null || exit_status=1 +fi + +exit $exit_status + +# +# Editor modelines +# +# Local Variables: +# c-basic-offset: 4 +# tab-width: 8 +# indent-tabs-mode: nil +# End: +# +# ex: set shiftwidth=4 tabstop=8 expandtab: +# :indentSize=4:tabSize=8:noTabs=true: +# diff --git a/tools/pre-commit-ignore.conf b/tools/pre-commit-ignore.conf new file mode 100644 index 0000000..09c40a8 --- /dev/null +++ b/tools/pre-commit-ignore.conf @@ -0,0 +1,27 @@ +# Files listed here are ignored by the git pre-commit hook for the purpose +# of checking for forbidden APIs and other dissector-specific glitches. +# +# Each line is compared against the output of 'git diff-index --name-only'. +# For example to skip checking this file add: +# +# tools/pre-commit-ignore.conf +# +# The pathname wildcards allowed are: '*', '?', character set '[abc]' or +# negated with '[!abc]'. + +cli_main.c +doc/packet-PROTOABBREV.c +epan/dissectors/asn1/*/*asn +epan/dissectors/asn1/*/packet-*-template.c +epan/dissectors/packet-http.c +epan/nghttp2/* +epan/wmem/wmem_strbuf.c +epan/wmem/wmem_strutil.c +epan/wslua/init_wslua.c +extcap/* +resources/stock_icons/* +mmdbresolve.c +packaging/* +tools/lemon/* +wsutil/file_util.h +wsutil/strptime.c diff --git a/tools/pre-commit-ignore.py b/tools/pre-commit-ignore.py new file mode 100755 index 0000000..63ecf3e --- /dev/null +++ b/tools/pre-commit-ignore.py @@ -0,0 +1,59 @@ +#!/bin/env python3 +# +# Wireshark - Network traffic analyzer +# By Gerald Combs <gerald@wireshark.org> +# Copyright 1998 Gerald Combs +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +import os +import fnmatch + +IGNORE_CONF = "pre-commit-ignore.conf" + +if len(sys.argv) > 2: + print("Usage: {0} [path/to/ignore.conf]".format(sys.argv[0])) + sys.exit(1) + +if len(sys.argv) == 2: + ignore_path = sys.argv[1] +else: + ignore_path = IGNORE_CONF + +# Function to load our patterns from 'path' for modified files +# to be ignored (skipping any comments) +def load_checkignore(path): + try: + with open(path) as f: + patterns = f.read() + except OSError as err: + sys.exit(str(err)) + ign = [l.strip() for l in patterns.splitlines()] + ign = [l for l in ign if l and not l.startswith("#")] + return ign + +ignore_list = load_checkignore(ignore_path) + +def ignore_match(f): + for p in ignore_list: + if fnmatch.fnmatchcase(f, p): + return True + return False + +for line in sys.stdin: + line = line.strip() + if not ignore_match(line): + print(line) + +# +# Editor modelines +# +# Local Variables: +# c-basic-offset: 4 +# indent-tabs-mode: nil +# End: +# +# ex: set shiftwidth=4 expandtab: +# :indentSize=4:noTabs=true: +# |