Skip to content

Commit 0e6bbb7

Browse files
authored
Ensure pre-commit hooks are kept up-to-date (#3887)
1 parent 120922e commit 0e6bbb7

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

make/buf/all.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ endif
190190
$(SED_I) "s/golang:1\.[0-9][0-9]*/golang:$(GOVERSION)/g" $(shell git-ls-files-unstaged | grep Dockerfile)
191191
$(SED_I) "s/golang:1\.[0-9][0-9]*/golang:$(GOVERSION)/g" $(shell git-ls-files-unstaged | grep \.mk$)
192192
$(SED_I) "s/go-version: '1\.[0-9][0-9].x'/go-version: '$(GOVERSION).x'/g" $(shell git-ls-files-unstaged | grep \.github\/workflows | grep -v previous.yaml)
193+
$(MAKE) checkandupdateprecommithooks
193194

194195
.PHONY: bufimageutilupdateexpectations
195196
bufimageutilupdateexpectations:
@@ -203,3 +204,9 @@ newtodos:
203204
.PHONY: newtodofiles
204205
newtodofiles:
205206
@bash make/buf/scripts/newtodos.bash | grep -v FUTURE | cut -f 1 -d : | sort | uniq
207+
208+
.PHONY: checkandupdateprecommithooks
209+
checkandupdateprecommithooks:
210+
@bash make/buf/scripts/checkandupdateprecommithooks.bash
211+
212+
postupgrade:: checkandupdateprecommithooks
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
DIR="$(CDPATH= cd "$(dirname "${0}")/../../.." && pwd)"
6+
cd "${DIR}"
7+
8+
GO_VERSION=$(grep '^go ' go.mod | awk '{print $2}')
9+
# If the version in go.mod does not include the patch version, e.g. go 1.24, for the
10+
# .pre-commit-hook.yaml configuration, we'll need to add a 0.
11+
if [[ ! "${GO_VERSION}" =~ '^([0-9]+\.){3}$' ]]; then
12+
GO_VERSION="${GO_VERSION}.0"
13+
fi
14+
15+
function lesser_version() {
16+
echo -e "$1\n$2" | sort -V | head -n 1
17+
}
18+
19+
function update_pre_commit_hooks() {
20+
yq .[].language_version=\"${GO_VERSION}\" .pre-commit-hooks.yaml > .pre-commit-hooks.yaml.tmp
21+
mv .pre-commit-hooks.yaml.tmp .pre-commit-hooks.yaml
22+
}
23+
24+
for version in $(yq ".[].language_version" .pre-commit-hooks.yaml)
25+
do
26+
LESSER_VERSION=$(lesser_version "${GO_VERSION}" "${version}")
27+
if [ ${version} == "${LESSER_VERSION}" ]; then
28+
echo "found lower pre-commit hook version ${version} compared to go.mod version ${GO_VERSION}"
29+
update_pre_commit_hooks
30+
break
31+
fi
32+
done

0 commit comments

Comments
 (0)