Skip to content

Commit e926d05

Browse files
committed
Added a make check-generate target to the makefile
1 parent 95884c4 commit e926d05

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

.ci/check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ echo "Running golangci-lint..."
4949
# golangci-lint can't be run from outside the directory
5050
(cd ${SOURCE_PATH} && golangci-lint run -c .golangci.yaml --timeout 10m)
5151

52+
echo "Running check-generate..."
53+
make check-generate
54+
5255
# Run Static Application Security Testing (SAST) using gosec
56+
echo "Running SAST..."
5357
make sast-report

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ generate: $(VGOPATH) $(DEEPCOPY_GEN) $(DEFAULTER_GEN) $(CONVERSION_GEN) $(OPENAP
176176
@./hack/generate-code
177177
@./hack/api-reference/generate-spec-doc.sh
178178

179+
.PHONY: check-generate
180+
check-generate:
181+
@./hack/check-generate.sh
182+
179183
.PHONY: add-license-headers
180184
add-license-headers: $(GO_ADD_LICENSE)
181185
@./hack/add_license_headers.sh

hack/check-generate.sh

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env bash
2+
#
3+
# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
set -e
8+
9+
echo "> Generate"
10+
11+
check_branch="__check"
12+
stashed=false
13+
checked_out=false
14+
generated=false
15+
16+
function delete-check-branch {
17+
git rev-parse --verify "$check_branch" &>/dev/null && git branch -q -D "$check_branch" || :
18+
}
19+
20+
function cleanup {
21+
if [[ "$generated" == true ]]; then
22+
if ! clean_err="$(make clean && git reset --hard -q && git clean -qdf)"; then
23+
echo "Could not clean: $clean_err"
24+
fi
25+
fi
26+
27+
if [[ "$checked_out" == true ]]; then
28+
if ! checkout_err="$(git checkout -q -)"; then
29+
echo "Could not checkout to previous branch: $checkout_err"
30+
fi
31+
fi
32+
33+
if [[ "$stashed" == true ]]; then
34+
if ! stash_err="$(git stash pop -q)"; then
35+
echo "Could not pop stash: $stash_err"
36+
fi
37+
fi
38+
39+
delete-check-branch
40+
}
41+
42+
trap cleanup EXIT SIGINT SIGTERM
43+
44+
if which git &>/dev/null; then
45+
if ! git rev-parse --git-dir &>/dev/null; then
46+
echo "Not a git repo, aborting"
47+
exit 1
48+
fi
49+
50+
if [[ "$(git rev-parse --abbrev-ref HEAD)" == "$check_branch" ]]; then
51+
echo "Already on check branch, aborting"
52+
exit 1
53+
fi
54+
delete-check-branch
55+
56+
if [[ "$(git status -s)" != "" ]]; then
57+
stashed=true
58+
git stash --include-untracked -q
59+
git stash apply -q &>/dev/null
60+
fi
61+
62+
checked_out=true
63+
git checkout -q -b "$check_branch"
64+
git add --all
65+
git commit -q --allow-empty -m 'checkpoint'
66+
67+
old_status="$(git status -s)"
68+
69+
if ! out=$(rm -rf hack/tools/bin/ 2>&1); then
70+
echo "Error while cleaning hack/tools/bin/: $out"
71+
exit 1
72+
fi
73+
74+
echo ">> make generate"
75+
generated=true
76+
if ! out=$(make generate 2>&1); then
77+
echo "Error during calling make generate: $out"
78+
exit 1
79+
fi
80+
new_status="$(git status -s)"
81+
82+
if [[ "$old_status" != "$new_status" ]]; then
83+
echo "make generate needs to be run:"
84+
echo "$new_status"
85+
exit 1
86+
fi
87+
88+
echo ">> make tidy"
89+
if ! out=$(make tidy 2>&1); then
90+
echo "Error during calling make tidy: $out"
91+
exit 1
92+
fi
93+
new_status="$(git status -s)"
94+
95+
if [[ "$old_status" != "$new_status" ]]; then
96+
echo "make tidy needs to be run:"
97+
echo "$new_status"
98+
exit 1
99+
fi
100+
else
101+
echo "No git detected, cannot run make check-generate"
102+
fi
103+
exit 0

0 commit comments

Comments
 (0)