From d2aa051da63c67c97cfd97a005b40f18e8a257e3 Mon Sep 17 00:00:00 2001 From: Willie Scholtz Date: Sat, 4 Jan 2025 12:19:13 +0100 Subject: [PATCH 1/2] feat: ensure code is formatted and imports are sorted before commit --- hooks/pre-commit.sh | 21 +++++++++++++++++++++ pom.xml | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100755 hooks/pre-commit.sh diff --git a/hooks/pre-commit.sh b/hooks/pre-commit.sh new file mode 100755 index 00000000000..d7b3cf8e70b --- /dev/null +++ b/hooks/pre-commit.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# +# Copyright 2009-2025 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -e + +# ensure code is formatted correctly and that imports are sorted as some IDE's automatically change it before commit +mvn formatter:format impsort:sort \ No newline at end of file diff --git a/pom.xml b/pom.xml index 121a826dc3f..2179976db98 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,7 @@ 5.15.2 12.8.1.jre11 1.20.4 + 3.5.0 @@ -419,6 +420,24 @@ + + + com.rudikershaw.gitbuildhook + git-build-hook-maven-plugin + ${git-build-hook.version} + + + hooks/pre-commit.sh + + + + + + install + + + + From d38acfff01604d6af55c7a681eeec552d6c5318e Mon Sep 17 00:00:00 2001 From: Willie Scholtz Date: Sat, 4 Jan 2025 13:47:04 +0100 Subject: [PATCH 2/2] First check if there would be any failures before we format and sort If there are, automatically sort and format and fail the commit as we cannot reliably tell which files to add back to staging --- hooks/pre-commit.sh | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/hooks/pre-commit.sh b/hooks/pre-commit.sh index d7b3cf8e70b..c1d01e5a494 100755 --- a/hooks/pre-commit.sh +++ b/hooks/pre-commit.sh @@ -14,8 +14,22 @@ # See the License for the specific language governing permissions and # limitations under the License. # +set -eo pipefail -set -e +function format_and_sort() { + # Run the validate and import check commands, suppressing all output and errors + if ! mvn formatter:validate impsort:check > /dev/null 2>&1; then + # If validation failed, fix it by ensuring code is formatted correctly + # and that imports are sorted as some IDEs automatically change it on save + mvn formatter:format impsort:sort -# ensure code is formatted correctly and that imports are sorted as some IDE's automatically change it before commit -mvn formatter:format impsort:sort \ No newline at end of file + # Fail the commit, so the author can re-select what to commit + echo "Formatting and/or import sorting were required. Please check and make another commit." + exit 1 + fi + + # If no error occurred, print a success message + echo "All files are properly formatted and imports are sorted." +} + +format_and_sort \ No newline at end of file