Skip to content

Pre Commit Hook (for this project)

Benjamin Smith edited this page Jun 13, 2019 · 5 revisions

Copy the test below and paste into a file with this name

.../dex-contracts/.git/hooks/pre-commit

and make it executable chmod +x .git/hooks/pre-commit

#!/bin/sh

STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
STAGED_SOL_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".sol$")
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"

if [[ ("$STAGED_JS_FILES" = "") && ("$STAGED_SOL_FILES" = "") ]]; then
  exit 0
fi

PASS=true

printf "\nValidating Javascript:\n"

# Check for eslint
if [[ ! -x "$ESLINT" ]]; then
  printf "\t\033[41mPlease install ESlint\033[0m (npm i --save-dev eslint)"
  exit 1
fi

for FILE in $STAGED_JS_FILES
do
  "$ESLINT" "$FILE"

  if [[ "$?" == 0 ]]; then
    printf "\t\033[32mESLint Passed: $FILE\033[0m"
  else
    printf "\t\033[41mESLint Failed: $FILE\033[0m"
    PASS=false
  fi
done

printf "\nJavascript validation completed!\n"

printf "\nValidating Solidity:\n"

# Check for eslint
which solhint &> /dev/null
if [[ "$?" == 1 ]]; then
  echo "\t\033[41mPlease install solhint\033[0m"
  exit 1
fi

for FILE in $STAGED_SOL_FILES
do
  solhint "$FILE"

  if [[ "$?" == 0 ]]; then
    printf "\t\033[32msolhint Passed: $FILE\033[0m"
  else
    printf "\t\033[41msolhint Failed: $FILE\033[0m"
    PASS=false
  fi
done

for FILE in $STAGED_SOL_FILES
do
  solium --file "$FILE"

  if [[ "$?" == 0 ]]; then
    printf "\t\033[32msolium Passed: $FILE\033[0m"
  else
    printf "\t\033[41msolium Failed: $FILE\033[0m"
    PASS=false
  fi
done

printf "\nSolidity validation completed!\n"

if ! $PASS; then
  printf "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass Linting but do not. Please fix the errors and try again.\n"
  exit 1
else
  printf "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi

exit $?
Clone this wiki locally