Skip to content

Instantly share code, notes, and snippets.

@oyeb
Last active February 12, 2018 10:15
Show Gist options
  • Select an option

  • Save oyeb/832f1b4c94886215f23c54df52aeeb6b to your computer and use it in GitHub Desktop.

Select an option

Save oyeb/832f1b4c94886215f23c54df52aeeb6b to your computer and use it in GitHub Desktop.
A git-hook that runs credo and optionally the formatter on commited changes
#!/bin/sh
#
# Runs credo and the formatter on the staged files, after the commit is made
# This is purely for notification and will not halt/change your commit.
# You must add the "path to Elixir 1.6 repository" to `ELIXIR_16` environment variable,
# Add this to your shell's `.rc`:
# export ELIXIR_16=path/to/elixir1.6
# List all .ex files staged in the previous commit
# Also pick any .exs from tests, except mock files.
RED='\033[1;31m'
LGRAY='\033[1;30m'
NC='\033[0m' # No Color
# define your filter patterns for credo
LIB="lib/.*.ex$"
TEST="test/.*.exs$"
TEST_SUPPORT="test/support/.*.ex$"
MIGRATIONS="priv/repo/migrations/.*.exs$"
# Compose them into 1 filter
FILTER=$LIB"|"$TEST"|"$MIGRATIONS
# Compute the set of staged files that match your filter pattern.
staged=$(git diff-tree --no-commit-id -r --name-only HEAD |
egrep $FILTER)
printf "${RED}Running 'mix credo --format=oneline' on staged files...${NC}\n"
echo
for blob in $staged; do
echo $blob
mix credo $blob --format=oneline
done
echo
@oyeb

oyeb commented Feb 12, 2018

Copy link
Copy Markdown
Author

Place this file here .git/hooks/post-commit

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