#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2019 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 to
#     ".git/hooks/pre-commit"; e.g.,
#     (
#         cd $WM_PROJECT_DIR/.git/hooks &&
#         ln -sf ../../bin/tools/pre-commit-hook pre-commit
#     )
#
#     Hook receives: (nothing)
#
#     Checks for:
#     - tabs
#     - trailing whitespace and non-standard line endings
#     - lines longer that than 80 characters
#     - non-standard code patterns, > > and NULL
#     - mismatched header #ifndef/#define names
#     - incorrect copyright statements
#
# Note
#     Using "git commit --no-verify" it is possible to override the hook.
#
#------------------------------------------------------------------------------

. ./bin/tools/HookFunctions || exit 1

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

# Get the list of files to check
readarray -t files < <(git diff-index --cached --name-only "$against" --)

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

# Do checks
checkAll --cached true "${files[@]}" && exit 0 || exit 1

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