Skip to content

Commit afcb85d

Browse files
zimegmwbrooks
andauthored
chore: add githooks to guard against unexpected commits (#546)
Co-authored-by: Michael Brooks <mbrooks@slack-corp.com>
1 parent 3720e4f commit afcb85d

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

.claude/CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ See [`.github/STYLE_GUIDE.md`](../.github/STYLE_GUIDE.md) for conventions on com
207207
- `go.mod` - Go module dependencies and minimum Go version (see `go.mod` for current version)
208208
- `.circleci/config.yml` - CircleCI workflows for CI/CD pipeline
209209
- `.github/workflows/` - GitHub Actions for automated testing and releases
210+
- `.githooks/` - Opt-in Git hooks (enable with `git config core.hooksPath .githooks`)
210211

211212
## Commit Message Format
212213

.githooks/pre-commit

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2022-2026 Salesforce, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -e
17+
18+
# Guard against committing directly to main
19+
branch="$(git rev-parse --abbrev-ref HEAD)"
20+
if [ "$branch" = "main" ]; then
21+
echo "error: commits directly to main are not allowed. Use a development branch."
22+
exit 1
23+
fi
24+
25+
# Run linter on staged changes
26+
cd "$(git rev-parse --show-toplevel)" || exit 1
27+
go tool golangci-lint run

.github/MAINTAINERS_GUIDE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Certain things are common during development and require a few commands.
207207
**Task outline:**
208208

209209
- [Cloning the project](#cloning-the-project)
210+
- [Git hooks](#git-hooks)
210211
- [Initializing the project](#initializing-the-project)
211212
- [Testing](#testing)
212213
- [Module and unit tests](#module-and-unit-tests)
@@ -233,6 +234,19 @@ git clone https://github.com/slackapi/slack-cli.git
233234
cd slack-cli/
234235
```
235236

237+
#### Git hooks
238+
239+
Opt-in Git hooks are available in the `.githooks/` directory. To enable them:
240+
241+
```zsh
242+
git config core.hooksPath .githooks
243+
```
244+
245+
The pre-commit hook will:
246+
247+
- Block commits directly to the `main` branch
248+
- Run `go tool golangci-lint run` to lint before committing
249+
236250
### Initializing the project
237251

238252
When you first clone the project or after you major updates to the project, it's

0 commit comments

Comments
 (0)