Skip to content

Instantly share code, notes, and snippets.

@lukasni
Created January 18, 2019 16:16
Show Gist options
  • Save lukasni/821ad67679f27bf3d18e14b11dc816d2 to your computer and use it in GitHub Desktop.
Save lukasni/821ad67679f27bf3d18e14b11dc816d2 to your computer and use it in GitHub Desktop.
git pre-commit hook for Elixir running format check, tests and credo.
#!/bin/bash
#
# Linter Elixir files using Credo.
# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
#
# Config
# ------
# credo.terminate
# The credo exit status level to be considered as “not passed”—to prevent
# git commit until fixed.
# Config
terminate_on=$(git config --int credo.terminate)
if [[ -z $terminate_on ]]; then terminate_on=16; fi
# cd to project root
cd `git rev-parse --show-toplevel`
# format it :: format project before commit
mix format --check-formatted
FORMAT_RES=$?
if [ $FORMAT_RES == 1 ]; then
echo ""
echo "☆ ============================================= ☆"
echo "☆ There are formatting issues. ☆" >&2
echo "☆ Please format the project before committing ☆" >&2
echo "☆ ============================================= ☆"
echo ""
exit $CREDO_RES
fi
echo ""
echo "★ ========================================= ★"
echo "★ Formatting check passed successfully. ★"
echo "★ ========================================= ★"
echo ""
# test it :: run tests before commit (silently)
mix test 2>&1 >/dev/null
TEST_RES=$?
if [ $TEST_RES -ne 0 ]; then
echo ""
echo "☆ ==================================== ☆"
echo "☆ Some tests are failed. ☆" >&2
echo "☆ Please fix them before committing. ☆" >&2
echo "☆ ==================================== ☆"
echo ""
exit $TEST_RES
fi
echo ""
echo "★ ============================== ★"
echo "★ Tests passed successfully. ★"
echo "★ ============================== ★"
echo ""
# lint it :: credo checks before commit
mix credo
CREDO_RES=$?
if [ $CREDO_RES -ge $terminate_on ]; then
echo ""
echo "☆ ============================================= ☆"
echo "☆ Credo found critical problems with your code ☆" >&2
echo "☆ and commit can not proceed. Please examine ☆" >&2
echo "☆ log above and fix issues before committing. ☆" >&2
echo "☆ ============================================= ☆"
echo ""
exit $CREDO_RES
fi
if [ $CREDO_RES -le 9 ]; then CREDO_RES=" $CREDO_RES"; fi
echo ""
echo "★ ============================== ★"
echo "★ Credo passed successfully. ★"
echo "★ Exit value is: (total: $CREDO_RES). ★"
echo "★ ============================== ★"
echo ""
# Finished
exit 0
@roeeyn
Copy link

roeeyn commented Mar 20, 2024

Should line 29 be $FORMAT_RES instead of $CREDO_RES?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment