Last active
March 1, 2024 12:07
-
-
Save vsyrovat/29578610bcaa6c1a794547db01d13fa1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
PRE_COMMIT_HOOK_FILE=".git/hooks/pre-commit" | |
if [[ -f $PRE_COMMIT_HOOK_FILE ]]; then | |
echo "File .git/hooks/pre-commit already exists" | |
ls -lsah $PRE_COMMIT_HOOK_FILE | |
cat $PRE_COMMIT_HOOK_FILE | |
exit 1 | |
else | |
case $1 in | |
elixir) | |
tee -a $PRE_COMMIT_HOOK_FILE > /dev/null <<EOF | |
#!/usr/bin/env bash | |
set -e | |
BRANCH_NAME=\$(git branch | grep '*' | sed 's/* //') | |
if [ "\$BRANCH_NAME" != '(no branch)' ] | |
then | |
mix format --check-formatted --dry-run | |
fi | |
EOF | |
;; | |
rust) | |
tee -a $PRE_COMMIT_HOOK_FILE > /dev/null <<EOF | |
#!/usr/bin/env bash | |
set -e | |
BRANCH_NAME=\$(git branch | grep '*' | sed 's/* //') | |
if [ "\$BRANCH_NAME" != '(no branch)' ] | |
then | |
cargo fmt -- --check | |
fi | |
EOF | |
;; | |
v) | |
tee -a $PRE_COMMIT_HOOK_FILE > /dev/null <<EOF | |
#!/usr/bin/env bash | |
set -e | |
BRANCH_NAME=\$(git branch | grep '*' | sed 's/* //') | |
if [ "\$BRANCH_NAME" != '(no branch)' ] | |
then | |
v fmt -c . | |
fi | |
EOF | |
;; | |
go) | |
tee -a $PRE_COMMIT_HOOK_FILE > /dev/null <<EOF | |
#!/usr/bin/env bash | |
set -e | |
BRANCH_NAME=\$(git branch | grep '*' | sed 's/* //') | |
if [ "\$BRANCH_NAME" != '(no branch)' ] | |
then | |
go vet ./... | |
test -z $(gofmt -l .) | |
fi | |
EOF | |
;; | |
make-lint) | |
tee -a $PRE_COMMIT_HOOK_FILE > /dev/null <<EOF | |
#!/usr/bin/env bash | |
set -e | |
BRANCH_NAME=\$(git branch | grep '*' | sed 's/* //') | |
if [ "\$BRANCH_NAME" != '(no branch)' ] | |
then | |
make lint | |
fi | |
EOF | |
;; | |
*) | |
echo "Error: argument not given or unknown. Expected one of: elixir, rust, v, go, make-lint" | |
exit 1 | |
;; | |
esac | |
chmod u+x $PRE_COMMIT_HOOK_FILE | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment