diff --git a/.circleci/config.yml b/.circleci/config.yml index e59bc72..05d12f0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,4 +6,8 @@ jobs: docker: - image: circleci/golang steps: - - run: echo "@TODO:" + - checkout + - run: go get -u mvdan.cc/sh/cmd/shfmt + - run: sudo apt-get -y install python-pip + - run: sudo pip install pre-commit + - run: pre-commit install diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 8e515d3..a7e71e8 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -42,3 +42,27 @@ entry: hooks/go-golangci-lint.sh language: script files: \.go$ + +- id: forbid-binary + name: Forbid binaries + description: Forbid binary files from being committed + entry: hooks/forbid-binary.sh + language: script + types: ['binary'] + +- id: shellcheck + name: Test shell scripts with shellcheck + description: Shell scripts conform to shellcheck + entry: hooks/shellcheck.sh + language: script + types: [shell] + args: [-e, SC1091] + additional_dependencies: [shellcheck] + +- id: shfmt + name: Check shell style with shfmt + language: script + entry: hooks/shfmt.sh + types: [shell] + args: ['-l', '-i', '2', '-ci'] + additional_dependencies: [shfmt] diff --git a/README.md b/README.md index 282d70d..dc0bafc 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,40 @@ -# git-hooks +# ![git-hooks](./docs/social-preview.png) [pre-commit]: https://pre-commit.com/ A collection of useful Git hooks for use with [pre-commit][]. -## Usage - -Create or append to your `.pre-commit-config.yaml` configuration: - -```yaml -- repo: https://github.com/syntaqx/git-hooks - rev: v0.0.14 - hooks: - - id: circleci-config-validate - - id: go-fmt - - id: go-test - - id: go-mod-tidy - - id: go-generate - - id: go-golangci-lint -``` - ## Available hooks * `circleci-config-validate` - Test if the CircleCI config file is well formed. +* `forbid-binary` - Prevent binary files from being committed. * `go-fmt` - Runs `go fmt` and asserts no changes are needed. * `go-test` - Runs `go test` and asserts no tests are failing. * `go-mod-tidy` - Runs `go mod tidy` and asserts all dependencies have been added. * `go-generate` - Runs `go generate` aginst the projects go files. * `go-golangci-lint` - Runs `golangci-lint`, requires golangci-lint. +* `shellcheck` - Run `shellcheck` against scripts. +* `shfmt` - Run `shfmt` against scripts. + +## Configure `pre-commit` + +Create or append to your `.pre-commit-config.yaml` configuration: + +```yaml +- repo: https://github.com/syntaqx/git-hooks + rev: v0.0.15 + hooks: + - id: circleci-config-validate + - id: forbid-binary + - id: go-fmt + - id: go-test + - id: go-mod-tidy + - id: go-generate + - id: go-golangci-lint + - id: forbid-binary + - id: shellcheck + - id: shfmt +``` ## License diff --git a/docs/social-preview.png b/docs/social-preview.png new file mode 100644 index 0000000..c87bd23 Binary files /dev/null and b/docs/social-preview.png differ diff --git a/hooks/forbid-binary.sh b/hooks/forbid-binary.sh new file mode 100755 index 0000000..28cdcad --- /dev/null +++ b/hooks/forbid-binary.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# Original: https://github.com/jumanjihouse/pre-commit-hooks#forbid-binary +# Forked to change runtime to /usr/bin/env on win10 +set -eu + +# Forbid binary files. +# pre-commit uses the "identify" pure python library to detect binary files. + +if [ $# -gt 0 ]; then + for filename in "${@}"; do + echo "[ERROR] ${filename} appears to be a binary file" + done + exit 1 +fi diff --git a/hooks/shellcheck.sh b/hooks/shellcheck.sh new file mode 100755 index 0000000..38bd08e --- /dev/null +++ b/hooks/shellcheck.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# Original: https://github.com/jumanjihouse/pre-commit-hooks#shellcheck +# Forked to change runtime to /usr/bin/env on win10 +set -eu + +# Ensure shell scripts conform to shellcheck. +# https://www.shellcheck.net/ + +readonly DEBUG=${DEBUG:-unset} +if [ "${DEBUG}" != unset ]; then + set -x +fi + +shellcheck "$@" diff --git a/hooks/shfmt.sh b/hooks/shfmt.sh new file mode 100755 index 0000000..b85eae3 --- /dev/null +++ b/hooks/shfmt.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# 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 + echo 'This check needs shfmt from https://github.com/mvdan/sh/releases' + 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