Skip to content

Commit 922b4cd

Browse files
committed
Simplify: run generation hooks first, then trailing-whitespace cleanup
Instead of manually stripping trailing whitespace in each generation script, reorder pre-commit hooks so: 1. Generation hooks run FIRST (generate-manifests, update-values, etc.) 2. trailing-whitespace hook runs AFTER to clean up generated files This removes the strip_trailing_whitespace() function and all its calls, making the code simpler and letting pre-commit handle formatting.
1 parent 823a09d commit 922b4cd

File tree

5 files changed

+52
-96
lines changed

5 files changed

+52
-96
lines changed

.pre-commit-config.yaml

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,54 @@
1313

1414
repos:
1515
# =============================================================================
16-
# Standard pre-commit hooks
16+
# Generation hooks (run FIRST so formatters can clean up generated files)
17+
# =============================================================================
18+
- repo: local
19+
hooks:
20+
- id: generate-manifests
21+
name: Generate CRD manifests
22+
entry: scripts/dev/generate_files.sh generate_manifests
23+
language: script
24+
files: ^api/.*\.go$
25+
pass_filenames: false
26+
27+
- id: update-release-json
28+
name: Update release.json
29+
entry: scripts/dev/generate_files.sh update_release
30+
language: script
31+
files: (release\.json|scripts/evergreen/release/)
32+
pass_filenames: false
33+
34+
- id: update-values-yaml
35+
name: Update Helm values files
36+
entry: scripts/dev/generate_files.sh update_values
37+
language: script
38+
files: ^helm_chart/values.*\.yaml$
39+
pass_filenames: false
40+
41+
- id: generate-standalone-yaml
42+
name: Generate standalone YAML files
43+
entry: scripts/dev/generate_files.sh generate_standalone_yaml
44+
language: script
45+
files: ^helm_chart/
46+
pass_filenames: false
47+
48+
- id: regenerate-mco-tests
49+
name: Regenerate MCO evergreen tests
50+
entry: scripts/dev/generate_files.sh update_mco_tests
51+
language: script
52+
files: ^scripts/evergreen/e2e/mco/
53+
pass_filenames: false
54+
55+
- id: regenerate-multicluster-rbac
56+
name: Regenerate multi-cluster RBAC samples
57+
entry: scripts/dev/regenerate_multicluster_rbac.sh
58+
language: script
59+
files: ^(cmd/kubectl-mongodb|pkg/kubectl-mongodb)/
60+
pass_filenames: false
61+
62+
# =============================================================================
63+
# Standard pre-commit hooks (run AFTER generation to clean up generated files)
1764
# =============================================================================
1865
- repo: https://github.com/pre-commit/pre-commit-hooks
1966
rev: v5.0.0
@@ -128,13 +175,10 @@ repos:
128175
)
129176
130177
# =============================================================================
131-
# Local/custom hooks for project-specific tasks
178+
# Local/custom linting hooks
132179
# =============================================================================
133180
- repo: local
134181
hooks:
135-
# -------------------------------------------------------------------------
136-
# Linting hooks
137-
# -------------------------------------------------------------------------
138182
- id: golangci-lint
139183
name: Go linting
140184
entry: scripts/evergreen/lint_code.sh
@@ -188,51 +232,6 @@ repos:
188232
files: ^helm_chart/
189233
pass_filenames: false
190234

191-
# -------------------------------------------------------------------------
192-
# Generation hooks (run files generation and stage changes)
193-
# -------------------------------------------------------------------------
194-
- id: generate-manifests
195-
name: Generate CRD manifests
196-
entry: scripts/dev/generate_files.sh generate_manifests
197-
language: script
198-
files: ^api/.*\.go$
199-
pass_filenames: false
200-
201-
- id: update-release-json
202-
name: Update release.json
203-
entry: scripts/dev/generate_files.sh update_release
204-
language: script
205-
files: (release\.json|scripts/evergreen/release/)
206-
pass_filenames: false
207-
208-
- id: update-values-yaml
209-
name: Update Helm values files
210-
entry: scripts/dev/generate_files.sh update_values
211-
language: script
212-
files: ^helm_chart/values.*\.yaml$
213-
pass_filenames: false
214-
215-
- id: generate-standalone-yaml
216-
name: Generate standalone YAML files
217-
entry: scripts/dev/generate_files.sh generate_standalone_yaml
218-
language: script
219-
files: ^helm_chart/
220-
pass_filenames: false
221-
222-
- id: regenerate-mco-tests
223-
name: Regenerate MCO evergreen tests
224-
entry: scripts/dev/generate_files.sh update_mco_tests
225-
language: script
226-
files: ^scripts/evergreen/e2e/mco/
227-
pass_filenames: false
228-
229-
- id: regenerate-multicluster-rbac
230-
name: Regenerate multi-cluster RBAC samples
231-
entry: scripts/dev/regenerate_multicluster_rbac.sh
232-
language: script
233-
files: ^(cmd/kubectl-mongodb|pkg/kubectl-mongodb)/
234-
pass_filenames: false
235-
236235
# CI mode settings (set via environment variable PRE_COMMIT_FROM_REF/PRE_COMMIT_TO_REF)
237236
# In CI, pre-commit will check all files: pre-commit run --all-files
238237
ci:

scripts/dev/generate_files.sh

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
#!/usr/bin/env bash
22
#
33
# File generation script for pre-commit hooks.
4-
# This script generates various files (YAML configs, manifests, etc.) and ensures
5-
# they are properly formatted without trailing whitespace.
4+
# This script generates various files (YAML configs, manifests, etc.).
5+
# Trailing whitespace cleanup is handled by the trailing-whitespace hook
6+
# which runs AFTER these generation hooks.
67
#
78

89
set -Eeou pipefail
910

1011
source scripts/dev/set_env_context.sh
1112
source scripts/funcs/printing
1213

13-
# Helper function to strip trailing whitespace from a file
14-
# This ensures generated files don't cause pre-commit hook cycles
15-
strip_trailing_whitespace() {
16-
local file="$1"
17-
if [[ -f "${file}" ]]; then
18-
# Use sed to remove trailing whitespace from each line
19-
# Note: sed -i '' (with space) is required for macOS, sed -i'' for Linux
20-
if [[ "$(uname)" == "Darwin" ]]; then
21-
sed -i '' 's/[[:space:]]*$//' "${file}"
22-
else
23-
sed -i 's/[[:space:]]*$//' "${file}"
24-
fi
25-
fi
26-
}
27-
2814
if [ -f "${PROJECT_DIR}/venv/bin/activate" ]; then
2915
source "${PROJECT_DIR}/venv/bin/activate"
3016
fi
@@ -34,7 +20,6 @@ mkdir -p "$(go env GOPATH)/bin"
3420
update_mco_tests() {
3521
echo "Regenerating MCO evergreen tests configuration"
3622
python scripts/evergreen/e2e/mco/create_mco_tests.py >.evergreen-mco.yml
37-
strip_trailing_whitespace .evergreen-mco.yml
3823
git add .evergreen-mco.yml
3924
}
4025

@@ -82,29 +67,11 @@ generate_standalone_yaml() {
8267
rm -rf "${charttmpdir:?}"/*
8368
helm template --namespace mongodb -f helm_chart/values.yaml helm_chart --output-dir "${charttmpdir}" --values helm_chart/values-multi-cluster.yaml "$@"
8469
cat "${FILES[@]}" >public/mongodb-kubernetes-multi-cluster.yaml
85-
86-
# Strip trailing whitespace from all generated files
87-
strip_trailing_whitespace public/mongodb-kubernetes.yaml
88-
strip_trailing_whitespace public/mongodb-kubernetes-openshift.yaml
89-
strip_trailing_whitespace public/mongodb-kubernetes-multi-cluster.yaml
90-
strip_trailing_whitespace public/crds.yaml
91-
strip_trailing_whitespace config/manager/manager.yaml
92-
strip_trailing_whitespace config/rbac/database-roles.yaml
93-
strip_trailing_whitespace config/rbac/operator-roles-base.yaml
94-
strip_trailing_whitespace config/rbac/operator-roles-clustermongodbroles.yaml
95-
strip_trailing_whitespace config/rbac/operator-roles-pvc-resize.yaml
96-
strip_trailing_whitespace config/rbac/operator-roles-telemetry.yaml
9770
}
9871

9972
generate_manifests() {
10073
make manifests
10174

102-
# Strip trailing whitespace from generated CRD files
103-
for f in config/crd/bases/*.yaml helm_chart/crds/*.yaml; do
104-
strip_trailing_whitespace "${f}"
105-
done
106-
strip_trailing_whitespace public/crds.yaml
107-
10875
git add config/crd/bases
10976
git add helm_chart/crds
11077
git add public/crds.yaml
@@ -115,10 +82,6 @@ update_values_yaml_files() {
11582
# shellcheck disable=SC2154
11683
python scripts/evergreen/release/update_helm_values_files.py
11784

118-
# Strip trailing whitespace from generated files
119-
strip_trailing_whitespace helm_chart/values.yaml
120-
strip_trailing_whitespace helm_chart/values-openshift.yaml
121-
12285
# commit any changes we made
12386
git add helm_chart/values.yaml
12487
git add helm_chart/values-openshift.yaml

scripts/dev/regenerate_multicluster_rbac.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bash
22
set -Eeou pipefail
33

4-
# Source the environment context to get go in PATH
54
# shellcheck disable=SC1091
65
source scripts/dev/set_env_context.sh
76

scripts/evergreen/check_precommit.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ initial_index_state=$(git diff --name-only --cached --diff-filter=AM)
66

77
export EVERGREEN_MODE=true
88

9-
# Source the environment context (sets up PATH for go, etc.)
10-
# This is created by clone->setup_context step in CI
119
# shellcheck disable=SC1091
1210
source scripts/dev/set_env_context.sh
1311

14-
# Activate the venv if it exists (CI creates this via python_venv setup step)
15-
# The venv contains pre-commit installed from requirements.txt
1612
if [[ -f "${PROJECT_DIR}/venv/bin/activate" ]]; then
1713
echo "Activating venv..."
1814
# shellcheck disable=SC1091

scripts/evergreen/lint_code.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bash
22
set -Eeou pipefail
33

4-
# Source the environment context to get go in PATH
54
# shellcheck disable=SC1091
65
source scripts/dev/set_env_context.sh
76

0 commit comments

Comments
 (0)