Skip to content

Commit 319a9b7

Browse files
authored
Port shellcheck, shfmt & forbid-binary (#2)
* porting shellcheck, shfmt & forbid-binary * chmod +x * add social-preview to docs * add base CircleCI testing
1 parent acdc6fa commit 319a9b7

File tree

7 files changed

+113
-18
lines changed

7 files changed

+113
-18
lines changed

.circleci/config.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ jobs:
66
docker:
77
- image: circleci/golang
88
steps:
9-
- run: echo "@TODO:"
9+
- checkout
10+
- run: go get -u mvdan.cc/sh/cmd/shfmt
11+
- run: sudo apt-get -y install python-pip
12+
- run: sudo pip install pre-commit
13+
- run: pre-commit install

.pre-commit-hooks.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,27 @@
4242
entry: hooks/go-golangci-lint.sh
4343
language: script
4444
files: \.go$
45+
46+
- id: forbid-binary
47+
name: Forbid binaries
48+
description: Forbid binary files from being committed
49+
entry: hooks/forbid-binary.sh
50+
language: script
51+
types: ['binary']
52+
53+
- id: shellcheck
54+
name: Test shell scripts with shellcheck
55+
description: Shell scripts conform to shellcheck
56+
entry: hooks/shellcheck.sh
57+
language: script
58+
types: [shell]
59+
args: [-e, SC1091]
60+
additional_dependencies: [shellcheck]
61+
62+
- id: shfmt
63+
name: Check shell style with shfmt
64+
language: script
65+
entry: hooks/shfmt.sh
66+
types: [shell]
67+
args: ['-l', '-i', '2', '-ci']
68+
additional_dependencies: [shfmt]

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
1-
# git-hooks
1+
# ![git-hooks](./docs/social-preview.png)
22

33
[pre-commit]: https://pre-commit.com/
44

55
A collection of useful Git hooks for use with [pre-commit][].
66

7-
## Usage
8-
9-
Create or append to your `.pre-commit-config.yaml` configuration:
10-
11-
```yaml
12-
- repo: https://github.com/syntaqx/git-hooks
13-
rev: v0.0.14
14-
hooks:
15-
- id: circleci-config-validate
16-
- id: go-fmt
17-
- id: go-test
18-
- id: go-mod-tidy
19-
- id: go-generate
20-
- id: go-golangci-lint
21-
```
22-
237
## Available hooks
248

259
* `circleci-config-validate` - Test if the CircleCI config file is well formed.
10+
* `forbid-binary` - Prevent binary files from being committed.
2611
* `go-fmt` - Runs `go fmt` and asserts no changes are needed.
2712
* `go-test` - Runs `go test` and asserts no tests are failing.
2813
* `go-mod-tidy` - Runs `go mod tidy` and asserts all dependencies have been added.
2914
* `go-generate` - Runs `go generate` aginst the projects go files.
3015
* `go-golangci-lint` - Runs `golangci-lint`, requires golangci-lint.
16+
* `shellcheck` - Run `shellcheck` against scripts.
17+
* `shfmt` - Run `shfmt` against scripts.
18+
19+
## Configure `pre-commit`
20+
21+
Create or append to your `.pre-commit-config.yaml` configuration:
22+
23+
```yaml
24+
- repo: https://github.com/syntaqx/git-hooks
25+
rev: v0.0.15
26+
hooks:
27+
- id: circleci-config-validate
28+
- id: forbid-binary
29+
- id: go-fmt
30+
- id: go-test
31+
- id: go-mod-tidy
32+
- id: go-generate
33+
- id: go-golangci-lint
34+
- id: forbid-binary
35+
- id: shellcheck
36+
- id: shfmt
37+
```
3138
3239
## License
3340

docs/social-preview.png

16.3 KB
Loading

hooks/forbid-binary.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Original: https://github.com/jumanjihouse/pre-commit-hooks#forbid-binary
3+
# Forked to change runtime to /usr/bin/env on win10
4+
set -eu
5+
6+
# Forbid binary files.
7+
# pre-commit uses the "identify" pure python library to detect binary files.
8+
9+
if [ $# -gt 0 ]; then
10+
for filename in "${@}"; do
11+
echo "[ERROR] ${filename} appears to be a binary file"
12+
done
13+
exit 1
14+
fi

hooks/shellcheck.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Original: https://github.com/jumanjihouse/pre-commit-hooks#shellcheck
3+
# Forked to change runtime to /usr/bin/env on win10
4+
set -eu
5+
6+
# Ensure shell scripts conform to shellcheck.
7+
# https://www.shellcheck.net/
8+
9+
readonly DEBUG=${DEBUG:-unset}
10+
if [ "${DEBUG}" != unset ]; then
11+
set -x
12+
fi
13+
14+
shellcheck "$@"

hooks/shfmt.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
# Original: https://github.com/jumanjihouse/pre-commit-hooks#shfmt
3+
# Forked to change runtime to /usr/bin/env on win10
4+
set -eu
5+
6+
readonly DEBUG=${DEBUG:-unset}
7+
if [ "${DEBUG}" != unset ]; then
8+
set -x
9+
fi
10+
11+
if ! command -v shfmt >/dev/null 2>&1; then
12+
echo 'This check needs shfmt from https://github.com/mvdan/sh/releases'
13+
exit 1
14+
fi
15+
16+
readonly cmd="shfmt $*"
17+
echo "[RUN] ${cmd}"
18+
output="$(${cmd} 2>&1)"
19+
readonly output
20+
21+
if [ -n "${output}" ]; then
22+
echo '[FAIL]'
23+
echo
24+
echo "${output}"
25+
echo
26+
echo 'The above files have style errors.'
27+
echo 'Use "shfmt -d" option to show diff.'
28+
echo 'Use "shfmt -w" option to write (autocorrect).'
29+
exit 1
30+
else
31+
echo '[PASS]'
32+
fi

0 commit comments

Comments
 (0)