Skip to content

Commit c65100f

Browse files
committed
Adds directory/package-based versions of hooks
1 parent 6ea0ea5 commit c65100f

13 files changed

+307
-3
lines changed

.pre-commit-hooks.yaml

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
# ==============================================================================
2+
# go-build-dir
3+
# * Directory-Based
4+
# * Executes if any .go files modified
5+
# ==============================================================================
6+
- id: go-build-dir
7+
name: 'go-build-dir'
8+
entry: go-build-dir.sh
9+
types: [go]
10+
language: 'script'
11+
description: "Run 'go build ./$(dirname $FILE)' for each staged .go file"
12+
pass_filenames: true
13+
require_serial: true
14+
15+
# ==============================================================================
16+
# go-build-pkg
17+
# * Directory-Based
18+
# * Executes if any .go files modified
19+
# ==============================================================================
20+
- id: go-build-pkg
21+
name: 'go-build-pkg'
22+
entry: go-build-pkg.sh
23+
types: [go]
24+
language: 'script'
25+
description: "Run 'go build $(go list)/$(dirname $FILE)' for each staged .go file"
26+
pass_filenames: true
27+
require_serial: true
28+
129
# ==============================================================================
230
# go-build-repo
331
# * Repo-based
@@ -39,6 +67,34 @@
3967
description: "Run 'go build $(go list)/...' in repo root folder"
4068
pass_filenames: false
4169

70+
# ==============================================================================
71+
# go-test-dir
72+
# * Directory-Based
73+
# * Executes if any .go files modified
74+
# ==============================================================================
75+
- id: go-test-dir
76+
name: 'go-test-dir'
77+
entry: go-test-dir.sh
78+
types: [go]
79+
language: 'script'
80+
description: "Run 'go test ./$(dirname $FILE)' for each staged .go file"
81+
pass_filenames: true
82+
require_serial: true
83+
84+
# ==============================================================================
85+
# go-test-pkg
86+
# * Directory-Based
87+
# * Executes if any .go files modified
88+
# ==============================================================================
89+
- id: go-test-pkg
90+
name: 'go-test-pkg'
91+
entry: go-test-pkg.sh
92+
types: [go]
93+
language: 'script'
94+
description: "Run 'go test $(go list)/$(dirname $FILE)' for each staged .go file"
95+
pass_filenames: true
96+
require_serial: true
97+
4298
# ==============================================================================
4399
# go-test-repo
44100
# * Repo-based
@@ -94,6 +150,34 @@
94150
description: "Run 'go vet $FILE' for each staged .go file"
95151
pass_filenames: true
96152

153+
# ==============================================================================
154+
# go-vet-dir
155+
# * Directory-Based
156+
# * Executes if any .go files modified
157+
# ==============================================================================
158+
- id: go-vet-dir
159+
name: 'go-vet-dir'
160+
entry: go-vet-dir.sh
161+
types: [go]
162+
language: 'script'
163+
description: "Run 'go vet ./$(dirname $FILE)' for each staged .go file"
164+
pass_filenames: true
165+
require_serial: true
166+
167+
# ==============================================================================
168+
# go-vet-pkg
169+
# * Directory-Based
170+
# * Executes if any .go files modified
171+
# ==============================================================================
172+
- id: go-vet-pkg
173+
name: 'go-vet-pkg'
174+
entry: go-vet-pkg.sh
175+
types: [go]
176+
language: 'script'
177+
description: "Run 'go vet $(go list)/$(dirname $FILE)' for each staged .go file"
178+
pass_filenames: true
179+
require_serial: true
180+
97181
# ==============================================================================
98182
# go-vet-repo
99183
# * Repo-based
@@ -233,6 +317,64 @@
233317
description: "Run 'golangci-lint run --fix $FILE' for each staged .go file"
234318
pass_filenames: true
235319

320+
# ==============================================================================
321+
# golangci-lint-dir
322+
# * Directory-Based
323+
# * Executes if any .go files modified
324+
# ==============================================================================
325+
- id: golangci-lint-dir
326+
name: 'golangci-lint-dir'
327+
entry: golangci-lint-dir.sh
328+
types: [go]
329+
language: 'script'
330+
description: "Run 'golangci-lint run ./$(dirname $FILE)' for each staged .go file"
331+
pass_filenames: true
332+
require_serial: true
333+
334+
# ==============================================================================
335+
# golangci-lint-dir-fix
336+
# * Directory-Based
337+
# * Modifies files
338+
# * Executes if any .go files modified
339+
# ==============================================================================
340+
- id: golangci-lint-dir-fix
341+
name: 'golangci-lint-dir-fix'
342+
entry: golangci-lint-dir-fix.sh
343+
types: [go]
344+
language: 'script'
345+
description: "Run 'golangci-lint run --fix ./$(dirname $FILE)' for each staged .go file"
346+
pass_filenames: true
347+
require_serial: true
348+
349+
# ==============================================================================
350+
# golangci-lint-pkg
351+
# * Directory-Based
352+
# * Executes if any .go files modified
353+
# ==============================================================================
354+
- id: golangci-lint-pkg
355+
name: 'golangci-lint-pkg'
356+
entry: golangci-lint-pkg.sh
357+
types: [go]
358+
language: 'script'
359+
description: "Run 'golangci-lint run $(go list)/$(dirname $FILE)' for each staged .go file"
360+
pass_filenames: true
361+
require_serial: true
362+
363+
# ==============================================================================
364+
# golangci-lint-pkg-fix
365+
# * Directory-Based
366+
# * Modifies files
367+
# * Executes if any .go files modified
368+
# ==============================================================================
369+
- id: golangci-lint-pkg-fix
370+
name: 'golangci-lint-pkg-fix'
371+
entry: golangci-lint-pkg-fix.sh
372+
types: [go]
373+
language: 'script'
374+
description: "Run 'golangci-lint run --fix $(go list)/$(dirname $FILE)' for each staged .go file"
375+
pass_filenames: true
376+
require_serial: true
377+
236378
# ==============================================================================
237379
# golangci-lint-repo
238380
# * Repo-based

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,25 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
2828
#
2929
# Go Build
3030
#
31+
- id: go-build-dir
32+
- id: go-build-pkg
3133
- id: go-build-repo
3234
- id: go-build-repo-dir
3335
- id: go-build-repo-pkg
3436
#
3537
# Go Test
3638
#
39+
- id: go-test-dir
40+
- id: go-test-pkg
3741
- id: go-test-repo
3842
- id: go-test-repo-dir
3943
- id: go-test-repo-pkg
4044
#
4145
# Go Vet
4246
#
4347
- id: go-vet
48+
- id: go-vet-dir
49+
- id: go-vet-pkg
4450
- id: go-vet-repo
4551
- id: go-vet-repo-dir
4652
- id: go-vet-repo-pkg
@@ -64,6 +70,10 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
6470
#
6571
- id: golangci-lint
6672
- id: golangci-lint-fix
73+
- id: golangci-lint-dir
74+
- id: golangci-lint-dir-fix
75+
- id: golangci-lint-pkg
76+
- id: golangci-lint-pkg-fix
6777
- id: golangci-lint-repo
6878
- id: golangci-lint-repo-fix
6979
```
@@ -75,12 +85,28 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
7585
Unless configured to `"always_run"` (see below), hooks ONLY run when
7686
matching file types (usually `*.go`) are staged.
7787

88+
--------------------
7889
### File-Based Hooks
7990
By default, hooks run against matching staged files individually.
8091

8192
#### No User Args
8293
Currently, file-based hooks DO NOT accept user-args.
8394

95+
-------------------------
96+
### Directory-Based Hooks
97+
Some hooks work on a per-directory basis. The hooks run against the directory containing one or more matching staged files.
98+
99+
#### No User Args
100+
Currently, directory-based hooks DO NOT accept user-args.
101+
102+
#### Directory-Hook Suffixes
103+
- ``*-dir-*`` : Hook runs using `./$(dirname $FILE)` as target.
104+
- ``*-pkg-*`` : Hook runs using `'$(go list)/$(dirname $FILE)` as target.
105+
106+
#### Multiple Hook Invocations
107+
By design, the directory-based hooks only execute against a given directory once per hook invocation. HOWEVER, due to OS command-line length limits, Pre-Commit can invoke a hook multiple times if a large number of files are staged.
108+
109+
--------------------
84110
### Repo-Based Hooks
85111
Hooks named `'*-repo-*'` only run once (if any matching files are staged). They are NOT provided the list of staged files and are more full-repo oriented.
86112

@@ -121,9 +147,13 @@ Consider adding aliases to longer-named hooks for easier CLI usage.
121147
- [go-critic](#go-critic)
122148
- [GoLangCI-Lint](#golangci-lint)
123149
124-
------------------
150+
-------------------
125151
### go-build-repo-*
126152
153+
#### Directory-Based Hooks
154+
- `go-build-dir`
155+
- `go-build-pkg`
156+
127157
#### Repo-Based Hooks
128158
- `go-build-repo`
129159
- `go-build-repo-dir`
@@ -139,6 +169,10 @@ Comes with Golang ( [golang.org](https://golang.org/) )
139169
------------------
140170
### go-test-repo-*
141171
172+
#### Directory-Based Hooks
173+
- `go-test-dir`
174+
- `go-test-pkd`
175+
142176
#### Repo-Based Hooks
143177
- `go-test-repo`
144178
- `go-test-repo-dir`
@@ -157,6 +191,10 @@ Comes with Golang ( [golang.org](https://golang.org/) )
157191
#### File-Based Hooks
158192
- `go-vet` - Runs against staged `.go` files
159193
194+
#### Directory-Based Hooks
195+
- `go-vet-dir`
196+
- `go-vet-pkg`
197+
160198
#### Repo-Based Hooks
161199
- `go-vet-repo`
162200
- `go-vet-repo-dir`
@@ -288,16 +326,20 @@ go get -u golang.org/x/lint/golint
288326
- `golangci-lint`
289327
- `golangci-lint-fix`
290328
329+
#### Directory-Based Hooks
330+
- `golangci-lint-dir`
331+
- `golangci-lint-dir-fix`
332+
- `golangci-lint-pkg`
333+
- `golangci-lint-pkg-fix`
334+
291335
#### Repo-Based Hooks
292336
- `golangci-lint-repo`
293337
- `golangci-lint-repo-fix`
294338
295-
296339
#### Install
297340
```
298341
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
299342
```
300-
301343
#### Useful Args
302344
```
303345
--fast : Run only fast linters (from enabled linters sets)

go-build-dir.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
4+
go build "./${sub}"
5+
if [ $? -ne 0 ]; then
6+
errCode=1
7+
fi
8+
done
9+
exit $errCode

go-build-pkg.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
pkg=$(go list) # repo root package
4+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
5+
go build "${pkg}/${sub}"
6+
if [ $? -ne 0 ]; then
7+
errCode=1
8+
fi
9+
done
10+
exit $errCode

go-test-dir.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
4+
go test "./${sub}"
5+
if [ $? -ne 0 ]; then
6+
errCode=1
7+
fi
8+
done
9+
exit $errCode

go-test-pkg.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
pkg=$(go list) # repo root package
4+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
5+
go test "${pkg}/${sub}"
6+
if [ $? -ne 0 ]; then
7+
errCode=1
8+
fi
9+
done
10+
exit $errCode

go-vet-dir.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
4+
go vet "./${sub}"
5+
if [ $? -ne 0 ]; then
6+
errCode=1
7+
fi
8+
done
9+
exit $errCode

go-vet-pkg.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
pkg=$(go list) # repo root package
4+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
5+
go vet "${pkg}/${sub}"
6+
if [ $? -ne 0 ]; then
7+
errCode=1
8+
fi
9+
done
10+
exit $errCode

golangci-lint-dir-fix.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
4+
golangci-lint run --fix "./${sub}"
5+
if [ $? -ne 0 ]; then
6+
errCode=1
7+
fi
8+
done
9+
exit $errCode

golangci-lint-dir.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
errCode=0
3+
for sub in $(echo "$@" | xargs -n1 dirname | sort -u); do
4+
golangci-lint run "./${sub}"
5+
if [ $? -ne 0 ]; then
6+
errCode=1
7+
fi
8+
done
9+
exit $errCode

0 commit comments

Comments
 (0)