#!/usr/bin/env bash
#
# To install this script, copy it to .git/hooks/pre-commit in local copy of
# tor git repo and make sure it has permission to execute.
#
# This is pre-commit git hook script that prevents commiting your changeset if
# it fails our code formatting, changelog entry formatting, module include
# rules, or best practices tracker.

workdir=$(git rev-parse --show-toplevel)

cd "$workdir" || exit 1

set -e

if [ $# -eq 0 ]; then
    # When called in pre-commit, check the files modified in this commit
    CHECK_FILTER="git diff --cached --name-only --diff-filter=ACMR"
    # Use the appropriate owned tor source list to filter the changed files
    if [ -d src/lib ]; then
        # This is the layout in 0.3.5
        CHECK_FILES="$($CHECK_FILTER \
                           src/lib/*/*.[ch] \
                           src/core/*/*.[ch] \
                           src/feature/*/*.[ch] \
                           src/app/*/*.[ch] \
                           src/test/*.[ch] \
                           src/test/*/*.[ch] \
                           src/tools/*.[ch] \
                      )"
    elif [ -d src/common ]; then
        # This was the layout before 0.3.5
        CHECK_FILES="$($CHECK_FILTER \
                           src/common/*/*.[ch] \
                           src/or/*/*.[ch] \
                           src/test/*.[ch] \
                           src/test/*/*.[ch] \
                           src/tools/*.[ch]
                      )"
    fi
else
    # When called in pre-push, concatenate the argument array
    # Fails on special characters in file names
    CHECK_FILES="$*"
fi

## General File Checks

if [ -n "$(ls ./changes/)" ]; then
    python scripts/maint/lintChanges.py ./changes/*
fi

if [ -e scripts/maint/checkShellScripts.sh ]; then
    scripts/maint/checkShellScripts.sh
fi

if [ ! "$CHECK_FILES" ]; then
    echo "No modified tor-owned source files, skipping further checks"
    exit 0
fi

## Owned Source File Checks

printf "Modified tor-owned source files:\\n%s\\n" "$CHECK_FILES"

# We want word splitting here, because file names are space separated
# shellcheck disable=SC2086
perl scripts/maint/checkSpace.pl -C \
     $CHECK_FILES

if test -e scripts/maint/practracker/includes.py; then
    python scripts/maint/practracker/includes.py
fi

# Only call practracker if ${PT_DIR}/.enable_practracker_in_hooks exists
# We do this check so that we can enable practracker in hooks in master, and
# disable it on maint branches
PT_DIR=scripts/maint/practracker

if [ -e "${PT_DIR}/practracker.py" ]; then
    if [ -e "${PT_DIR}/.enable_practracker_in_hooks" ]; then
        if ! python3 "${PT_DIR}/practracker.py" "$workdir"; then
            exit 1
        fi
    fi
fi

if [ -e scripts/coccinelle/check_cocci_parse.sh ]; then

    # Run a verbose cocci parse check on the changed files
    # (spatch is slow, so we don't want to check all the files.)
    #
    # We want word splitting here, because file names are space separated
    # shellcheck disable=SC2086
    VERBOSE=1 scripts/coccinelle/check_cocci_parse.sh \
              $CHECK_FILES
fi
