Skip to content

Commit

Permalink
Use the paths of the unaliased binaries for the pre-commit hook
Browse files Browse the repository at this point in the history
Some commands might be already aliased to something else and not do what is expected
  • Loading branch information
kopperp authored and scopplestone committed Feb 13, 2024
1 parent b57c9c9 commit d6156b8
Showing 1 changed file with 97 additions and 34 deletions.
131 changes: 97 additions & 34 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
#!/bin/sh
#==================================================================================================================================
# Copyright (c) 2010 - 2022 Prof. Claus-Dieter Munz and Prof. Stefanos Fasoulas
#============================================================================================================ xX =================
# _____ _____ _______________ _______________ _______________ .xXXXXXXXx. X
# / /) / /) / _____ /) / _____ /) / _____ /) .XXXXXXXXXXXXXXx .XXXXx
# / // / // / /)___/ // / /)___/ // / /)___/ // .XXXXXXXXXXXXXXXXXXXXXXXXXx
# / //___/ // / // / // / //___/ // / //___/ // .XXXXXXXXXXXXXXXXXXXXXXX`
# / _____ // / // / // / __________// / __ __// .XX``XXXXXXXXXXXXXXXXX`
# / /)___/ // / // / // / /)_________) / /)_| |__) XX` `XXXXX` .X`
# / // / // / //___/ // / // / // | |_ XX XXX` .`
# /____// /____// /______________// /____// /____// |_____/) ,X` XXX`
# )____) )____) )______________) )____) )____) )_____) ,xX` .XX`
# xxX` XXx
# Copyright (C) 2023 Florian Hindenlang <[email protected]>
# Copyright (C) 2023 Stephen Copplestone <[email protected]>
# Copyright (C) 2023 Patrick Kopper <[email protected]>
# Copyright (C) 2017 Claus-Dieter Munz <[email protected]>
# This file is part of HOPR, a software for the generation of high-order meshes.
#
# This file is part of PICLas (https://github.com/piclas-framework/piclas). PICLas 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.
# HOPR 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.
#
# PICLas 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 v3.0 for more details.
# HOPR 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 PICLas. If not, see <http://www.gnu.org/licenses/>.
#==================================================================================================================================
# You should have received a copy of the GNU General Public License along with HOPR. If not, see <http://www.gnu.org/licenses/>.
#=================================================================================================================================

#===================================================================================================================================
# Check the number and total size of files about to be commited
#===================================================================================================================================

# Executables, command -v is safe with sh
BN_EXE=$(command -v basename)
DU_EXE=$(command -v du)
GIT_EXE=$(command -v git)
LS_EXE=$(command -v ls)
TR_EXE=$(command -v tr)

# Check if override is requested
if [ "$GIT_OVERRIDE_LIMITS" = "1" ]; then
echo 'Detected "GIT_OVERRIDE_LIMITS=1", overriding pre-commit check ...'
Expand All @@ -30,21 +50,29 @@ SIZETOTALLIMIT=1000000
SIZETOTALWARN=100000

# Number of existing files plus 100
NUMBERLIMIT=$(git ls-tree --full-tree -r --name-only HEAD | wc -l)
NUMBERLIMIT=$($GIT_EXE ls-tree --full-tree -r --name-only HEAD | wc -l)
NUMBERLIMIT=$(($NUMBERLIMIT + 100))
NUMBERWARN=100

# Local variables
ERROR=0
FILEWARN=0
FILEERR=0
FILEWARN=0
FILESUM=0
EXEWARN=0
FILESUMERR=0
FILESUMWARN=0
EXEERR=0
TABERR=0

EXELINE=''
TABLINE=''
SIZEWARNLINE=''
SIZEDENYLINE=''

# Check if file is opened in a terminal
if test -t 1; then
# Check if terminal supports color
NbrOfColors=$(which tput > /dev/null && tput colors)
NbrOfColors=$(command -v tput > /dev/null && tput colors)
if test -n "$NbrOfColors" && test "$NbrOfColors" -ge 8; then
NC="$(tput sgr0)"
RED="$(tput setaf 1)"
Expand All @@ -54,13 +82,14 @@ if test -t 1; then
fi

# Get a list of all staged files
CHANGED=$(git diff --staged --name-only)
CHANGED=$($GIT_EXE diff --staged --name-only)

# Check if any changes are present
if [ -n "$CHANGED" ]; then
# Sort found files by size (-S) in reverse ordering (-r)
SORTED=$(ls -Shsr "$CHANGED" 2> /dev/null)
NUMBER=$(git diff --staged --numstat | wc -l)
CHANGED=$(echo "$CHANGED" | $TR_EXE '\n' ' ')
SORTED=$($LS_EXE --sort=size -r $CHANGED 2> /dev/null)
NUMBER=$($GIT_EXE diff --staged --numstat | wc -l)

# Check the number of files
if [ "$NUMBER" -ge "$NUMBERLIMIT" ]; then
Expand All @@ -77,57 +106,91 @@ if [ -n "$CHANGED" ]; then
# -f True if FILE exists and is a regular file.
if [ -f "$file" ]; then
# -b, --bytes equivalent to '--apparent-size --block-size=1'
LINE=$(du -h "$file")
FILESIZE=$(du -b "$file" | cut -d ' ' -f1) # this is a tab, not a white space
LINE=$($LS_EXE -alhs "$file")
FILESIZE=$($DU_EXE -b "$file" | cut -d ' ' -f1) # this is a tab, not a white space

# Sum up the total file sizes
FILESUM=$(($FILESUM + $FILESIZE))

# Check the file size limit
if [ "$FILESIZE" -gt "$SIZESINGLELIMIT" ]; then
# Error header
if [ "$FILEERR" -eq 0 ]; then
printf "${RED}The following file(s) exceed the file size limit ($SIZESINGLELIMIT bytes)${NC}\n"
ERROR=1
FILEERR=1
fi
printf "$LINE\n"
SIZEDENYLINE="$LINE\n$SIZEDENYLINE"
# Check the file size warning
elif [ "$FILESIZE" -gt "$SIZESINGLEWARN" ]; then
# Error header
if [ "$FILEWARN" -eq 0 ]; then
printf "${YELLOW}The following large file(s) are about to be committed (>$SIZESINGLEWARN bytes)${NC}\n"
# Warning header
FILEWARN=1
fi
printf "$LINE\n"
SIZEWARNLINE="$LINE\n$SIZEWARNLINE"
fi
fi

# Check total file size
if [ "$FILESUM" -gt "$SIZETOTALLIMIT" ]; then
# Error header
printf "${RED}The total file size exceeds the size limit ($SIZETOTALLIMIT bytes)${NC}\n"
ERROR=1
FILESUMERR=1
# Check the file size warning
elif [ "$FILESUM" -gt "$SIZETOTALWARN" ]; then
# Error header
printf "${YELLOW}Total file size to be committed ($FILESUM bytes)${NC}\n"
FILESUMWARN=1
fi

# Check if file is executable
if [ -x "$file" ]; then
# Error header
if [ "$EXEWARN" -eq 0 ]; then
printf "${RED}The following file(s) are executable, which is not allowed. Remove the execute permission via 'chmod -x filename' and try again.${NC}\n"
EXEWARN=1
ERROR=1
EXEERR=1
LINE=$($LS_EXE -alhs "$file")
EXELINE="$LINE\n$EXELINE"
fi
LINE=$(ls -alFhs "$file")
printf "$LINE\n"

# Check for tab characters in specific file endings
name=$($BN_EXE -- "$file")
ext="${name##*.}"
name="${name%.*}"
if [ "$ext" = "txt" ] || [ "$ext" = "f90" ] || [ "$ext" = "ini" ]; then
# -P, --perl-regexp PATTERNS are Perl regular expressions
NbrOfLinesWithTabs=$(grep -P '\t' $file | wc -l)
if [ "${NbrOfLinesWithTabs}" -gt 0 ]; then
LINE=$($LS_EXE -alhs "$file")
ERROR=1
TABERR=1
TABLINE="$LINE\n$TABLINE"
fi
fi

done

if [ $FILEERR -ne 0 ]; then
printf "${RED}The following file(s) exceed the file size limit ($SIZESINGLELIMIT bytes)${NC}\n"
printf "$SIZEDENYLINE"
fi

if [ $FILEWARN -ne 0 ]; then
printf "${YELLOW}The following large file(s) are about to be committed (>$SIZESINGLEWARN bytes)${NC}\n"
printf "$SIZEWARNLINE"
fi

if [ $FILESUMERR -ne 0 ]; then
printf "${RED}The total file size exceeds the size limit ($SIZETOTALLIMIT bytes)${NC}\n"
fi

if [ $FILESUMWARN -ne 0 ]; then
printf "${YELLOW}Total file size to be committed ($FILESUM bytes)${NC}\n"
fi

if [ "$EXEERR" -ne 0 ]; then
printf "${RED}The following file(s) are executable, which is not allowed. Remove the execute permission via 'chmod -x filename' and try again.${NC}\n"
printf "$EXELINE"
fi

if [ "$TABERR" -ne 0 ]; then
printf "${RED}The following file(s) contain tabs, which is not allowed. Remove the tab stops and try again.${NC}\n"
printf "$TABLINE"
fi

# Abort if hook encountered any error
if [ "$ERROR" -ne 0 ]; then
echo '------------------------------------------------------------------------------------------------------------------------------------'
Expand Down

0 comments on commit d6156b8

Please sign in to comment.