#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2018 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     pre-commit-hook
#
# Description
#     pre-commit hook for git.
#     Copy or link this file as ".git/hooks/pre-commit"
#
#     Eg,
#     (
#         cd $WM_PROJECT_DIR/.git/hooks &&
#         ln -sf ../../bin/tools/pre-commit-hook pre-commit
#     )
#
#     Hook receives: empty
#
#     Checks for
#     - trailing whitespace and non-standard line endings
#     - illegal code, e.g. <TAB>
#     - columns greater than 80 for *.[CH] files
#     - non-standard code patterns
#     - missmatched header #ifndef/#define names
#     - incorrect copyright statements
#
# Note
#     Using "git commit --no-verify" it is possible to override the hook.
#
#     By supplying arguments to the hook, it can also be used to manually
#     test the specified files/directories for standards conformance.
#
#------------------------------------------------------------------------------

. bin/tools/HookFunctions || exit 1

hookName="pre-commit"


#-----------------------------------------------------------------------------
# Check content that will be added by this commit
#

# Clear
unset fileList
unset badFiles

# Get the commit to test against
if git rev-parse --verify HEAD > /dev/null 2>&1
then
    against=HEAD
else
    # Git's "empty" object
    against=$(git hash-object -t tree /dev/null)

fi

# Get the list of files to check
if [ "$#" -gt 0 ]
then
    # Called with arguments for the files/directories to be tested
    case "$1" in
    -h | -help)
        die "interactive usage: supply list of files/directories to check"
        ;;
    esac
    fileList=$(git ls-files -- $@ 2>/dev/null)
else
    # Called with out arguments to test staged changes
    fileList=$(git diff-index --cached --name-only $against --)
fi

# If no files have changed then the checks are not needed. This usage can
# correspond to a 'git commit --amend'
[ -n "$fileList" ] || exit 0


#------------------------------------------------------------------------------
# Main code. Do all checks.
#

# Builtin whitespace check to avoid trailing space, including CR-LF endings
bad=$(git diff-index --cached --check $against --) || die "$bad"

# Check for illegal code, e.g. <TAB>, etc
checkIllegalCode

# Ensure code conforms to 80 columns max
checkLineLengthNonDirective

# Check for non-standard code patterns
checkNonStandardCodePatterns

# Check if #ifndef/#define bounds are named correctly
checkHeaderIfndefNames

# Check banner
checkBanner

# Check copyright
checkCopyright

exit 0

#------------------------------------------------------------------------------
