Skip to content

feat: ensure code is formatted and imports are sorted before commit #3381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/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 -eo pipefail

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

# 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
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<mockito.version>5.15.2</mockito.version>
<mssql-jdbc.version>12.8.1.jre11</mssql-jdbc.version>
<testcontainers.version>1.20.4</testcontainers.version>
<git-build-hook.version>3.5.0</git-build-hook.version>

<!-- Add slow test groups here and annotate classes similar to @Tag('groupName'). -->
<!-- Excluded groups are ran on github ci, to force here, pass -d"excludedGroups=" -->
Expand Down Expand Up @@ -419,6 +420,24 @@
</rules>
</configuration>
</plugin>

<plugin>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll pull this up to the mybatis parent before the next release at least in general configuration so other projects can benefit from it as many of the projects already have some of the exact same formatting enabled. I think this plugin can possibly use the hook from a jar and will look into that just in case that is possible so we don't need to repeat any of it. For now this is fine.

<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>${git-build-hook.version}</version>
<configuration>
<installHooks>
<pre-commit>hooks/pre-commit.sh</pre-commit>
</installHooks>
</configuration>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Loading