From cd2aaf04f91c1b21f2bf0617d323c9d427bc571c Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sun, 25 Jul 2021 20:39:04 -0500 Subject: [PATCH] bash/pre-commit: git hook to lint files before committing --- bash/pre-commit | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 bash/pre-commit diff --git a/bash/pre-commit b/bash/pre-commit new file mode 100755 index 0000000..058d039 --- /dev/null +++ b/bash/pre-commit @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +has() { + if (( $# )); then + hash "$1" 2>/dev/null + fi +} + +is_a() { + if (( $# > 1 )); then + head -1 "$1" | grep -qE '^#!.*'"$2"'$' + fi +} + +find . -name .DS_Store -exec rm -f -- {} + + +git status -zu | while read -rd '' xy filename; do + if [[ $xy = "??" || $xy = D ]]; then + continue + fi + + if [[ $filename = *.sh ]] || is_a "$filename" bash; then + if has shellcheck; then + shellcheck -s bash "$filename" + fi + elif [[ $filename = *.rb ]] || is_a "$filename" ruby; then + if has rubocop; then + rubocop -c "$(git rev-parse --show-toplevel)" "$filename" + fi + fi +done