diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 4282deb..492528d 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -16,6 +16,11 @@ # Check the number and total size of files about to be commited #=================================================================================================================================== +# Executables, command -v is safe with sh +DU_EXE=$(command -v du) +GIT_EXE=$(command -v git) +LS_EXE=$(command -v ls) + # Check if override is requested if [ "$GIT_OVERRIDE_LIMITS" = "1" ]; then echo 'Detected "GIT_OVERRIDE_LIMITS=1", overriding pre-commit check ...' @@ -30,7 +35,7 @@ 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 @@ -44,7 +49,7 @@ EXEWARN=0 # 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=$(type tput > /dev/null && tput colors) if test -n "$NbrOfColors" && test "$NbrOfColors" -ge 8; then NC="$(tput sgr0)" RED="$(tput setaf 1)" @@ -54,13 +59,13 @@ 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) + SORTED=$($LS_EXE -Shsr "$CHANGED" 2> /dev/null) + NUMBER=$($GIT_EXE diff --staged --numstat | wc -l) # Check the number of files if [ "$NUMBER" -ge "$NUMBERLIMIT" ]; then @@ -77,8 +82,8 @@ 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=$($DU_EXE -h "$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)) @@ -121,7 +126,7 @@ if [ -n "$CHANGED" ]; 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 fi - LINE=$(ls -alFhs "$file") + LINE=$($LS_EXE -alFhs "$file") printf "$LINE\n" ERROR=1 fi