Skip to content

Commit

Permalink
Add shellcheck and shfmt hooks (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ls-jad-elkik authored Feb 8, 2021
1 parent a1c3065 commit 34ed136
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
38 changes: 38 additions & 0 deletions pre_commit_hooks/shfmt.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions submodule_syntaqx_git_hooks

0 comments on commit 34ed136

Please sign in to comment.