Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/scripts/pre-commit
901 views
#!/bin/sh

#  This file is part of t8code.
#  t8code is a C library to manage a collection (a forest) of multiple
#  connected adaptive space-trees of general element classes in parallel.
#
#  Copyright (C) 2023 the developers
#
#  t8code 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 2 of the License, or
#  (at your option) any later version.
#
#  t8code 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 t8code; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


#
# A hook script to verify what is about to be committed.
# This hook checks whether the code is properly indented using
# the provided indent script.
# Move into .git/hooks folder and name it "pre-commit".
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#

# This is the indent script in the project's directory

# Determine base directory of git repo
GIT_REPO_PATH=$(git rev-parse --show-toplevel)

# Set paths to check scripts
CHECK_INDENT=$GIT_REPO_PATH/scripts/check_if_file_indented.sh
CHECK_MACROS=$GIT_REPO_PATH/scripts/check_macros.sh

TYPOS=`which typos 2> /dev/null`
TYPOS_CONFIG_FILE=./.typos.toml

if [ -z "$TYPOS" ]
then
  # Exit if the spell checking script was not found
  echo "ERROR: typos not found."
  echo "Please install typos."
  echo "See https://github.com/crate-ci/typos#install"
  exit 1
fi

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2


nocontinue=0
for file in `git diff --cached --name-only`
do
  # only indent existing files, this is necessary since if we rename or delete
  # a file it is added to the committed files and we thus would try to indent a
  # nonexisting file.
  if [ ! -e $file ]
  then
    continue
  fi

  # Calling the typos script
  # We use a config file to exclude certain spellings and specific files.
  # Excluding specific files in the config files does only work when used together
  # with --force-exclude
  status=`$TYPOS --force-exclude --config $TYPOS_CONFIG_FILE $file`
  if ! [ -z "$status" ]; then
    echo "File $file contains typos."
    nocontinue=1
  fi 

  # We only indent .c, .cxx, .h and .hxx files
#-a ${file: -2} != ".h" ]
  FILE_ENDING="${file##*.}"
  if [ $FILE_ENDING = "c" -o $FILE_ENDING = "h" -o $FILE_ENDING = "cxx" -o $FILE_ENDING = "hxx" ]
  then
    echo "Checking file $file"
    $CHECK_INDENT $file > /dev/null 2>&1
    status=$?
    if test $status -ne 0 
    then
      echo "File $file is not indented."
      nocontinue=1
    fi

    # This script checks for the usage of #ifdef T8_ENABLE_ macros in the specified file.
    # If such macros are found, it suggests using #if T8_ENABLE_ instead and sets a flag to indicate the issue.
    # - $CHECK_MACROS: Command or script to check the macros in the file.
    # - $file: The file being checked.
    # - status: The exit status of the $CHECK_MACROS command.
    # - nocontinue: Flag set to 1 if the incorrect macro usage is found.
    $CHECK_MACROS $file
    status=$?
    if test $status -ne 0 
    then
      nocontinue=1
    fi
  fi
done

# exit 1 if there was a nonindented file
# this will abort the commit
exit $nocontinue