From 34ed136d21f2f43a09907b3e9bbd679e94e3e748 Mon Sep 17 00:00:00 2001 From: Jad El Kik <64258942+ls-jad-elkik@users.noreply.github.com> Date: Mon, 8 Feb 2021 14:59:52 +0100 Subject: [PATCH] Add shellcheck and shfmt hooks (#6) * Add shellcheck and shfmt hooks * Fork shfmt pre-commit hook script * Improve usage of shfmt pre-commit hook script * Remove deprecated additional_dependencies for script hooks --- .gitmodules | 3 +++ .pre-commit-hooks.yaml | 16 ++++++++++++++++ pre_commit_hooks/shfmt.sh | 38 +++++++++++++++++++++++++++++++++++++ submodule_syntaqx_git_hooks | 1 + 4 files changed, 58 insertions(+) create mode 100755 pre_commit_hooks/shfmt.sh create mode 160000 submodule_syntaqx_git_hooks diff --git a/.gitmodules b/.gitmodules index f5ad99d..6629ef0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "pre-commit-hooks"] path = submodule_pre_commit_hooks url = git@github.com:pre-commit/pre-commit-hooks.git +[submodule "submodule_syntaqx_git_hooks"] + path = submodule_syntaqx_git_hooks + url = git@github.com:syntaqx/git-hooks.git diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 866e4a7..aa6c2a8 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -68,3 +68,19 @@ language: python types: [file, yaml] +- id: shellcheck + name: Test shell scripts with shellcheck + description: Shell scripts conform to shellcheck + entry: submodule_syntaqx_git_hooks/hooks/shellcheck.sh + language: script + types: [shell] + exclude_types: [csh, perl, python, ruby, tcsh, zsh] + args: [-e, SC1091] + +- id: shfmt + name: Check shell style with shfmt + language: script + entry: submodule_syntaqx_git_hooks/hooks/shfmt.sh + types: [shell] + exclude_types: [csh, perl, python, ruby, tcsh, zsh] + args: ['-l', '-i', '2', '-ci'] diff --git a/pre_commit_hooks/shfmt.sh b/pre_commit_hooks/shfmt.sh new file mode 100755 index 0000000..8968ce1 --- /dev/null +++ b/pre_commit_hooks/shfmt.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# Original: https://github.com/syntaqx/git-hooks +# Forked to auto-install shfmt and give better command suggestions +# Original: https://github.com/jumanjihouse/pre-commit-hooks#shfmt +# Forked to change runtime to /usr/bin/env on win10 +set -eu + +readonly DEBUG=${DEBUG:-unset} +if [ "${DEBUG}" != unset ]; then + set -x +fi + +if ! command -v shfmt >/dev/null 2>&1; then + if command -v brew &>/dev/null; then + brew install shfmt + else + >&2 echo 'This check needs shfmt from https://github.com/mvdan/sh/releases or brew install shfmt' + fi + exit 1 +fi + +readonly cmd=(shfmt "$@") +echo "[RUN] ${cmd[@]}" +output="$("${cmd[@]}" 2>&1)" +readonly output + +if [ -n "${output}" ]; then + echo '[FAIL]' + echo + echo "${output}" + echo + echo 'The above files have style errors.' + echo "Use 'shfmt -d $@' option to show diff." + echo "Use 'shfmt -w $@' option to write (autocorrect)." + exit 1 +else + echo '[PASS]' +fi diff --git a/submodule_syntaqx_git_hooks b/submodule_syntaqx_git_hooks new file mode 160000 index 0000000..fa20773 --- /dev/null +++ b/submodule_syntaqx_git_hooks @@ -0,0 +1 @@ +Subproject commit fa207736bb79c0db20b32568d8b34369375b0484