Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Binary file added docs/social-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions hooks/forbid-binary.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions hooks/shellcheck.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
32 changes: 32 additions & 0 deletions hooks/shfmt.sh
Original file line number Diff line number Diff line change
@@ -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