- Install pre-commit which is a package manager for installing pre-commit hooks.
NOTE: This particular step is already done and is only here for your information. No action is required from new users, since Hook file is already copied in the project. I am adding this step here since it is good to know how this was done and so that if needed in future you can add (new) hooks here.
-
Search for the pre commit hook you are interested in from the list of available hooks.
-
If you search with word "java" in the search window of above webpage, you will find (at least) 3 different pre commit hooks.
-
All of these hooks use Google Java Format program that reformats Java source code to comply with Google's Java codestyle.
-
The hook we have chosen for our project is named
google-style-javafrom user matltz - on Github. -
Click on the above GitHub URL to go to see how to use this hook. You will see an example as below
- repo: https://github.com/maltzj/google-style-precommit-hook sha: b7e9e7fcba4a5aea463e72fe9964c14877bd8130 hooks: - id: google-style-java- Add a file in root of this repository named
.pre-commit-config.yamland paste above content in it (already done. Listed for fyi only). - Change tag
shatorevsince that is how it used in latest versions (already done). - Add any other hooks you are interested in here (as you will see in file
.pre-commit-config.yaml).
- Add a file in root of this repository named
- Each user need to run below step (Only one time ever for a project repo):
- To install the git hook script on your local git repository:
-
run
pre-commit installfrom the root of this repository. -
Should give result as below if successful.
$ pre-commit install pre-commit installed at .git/hooks/pre-commit
-
- To install the git hook script on your local git repository:
-
That's it you are now all set!
-
Pre-commit will now run on every commit that you make and your code will be auto-magically formatted as per Google's Java codestyle
-
If there are any formatting violations, which they will be almost "All-of-the-time" (as below). Not only it will tell you what the failures are but it will also fix it for you (of course you would need to add those changes to stage and commit)
2023-02-25 20:40:19.553 [info] Google Java Code Style for Java......................(no files to check)Skipped Trim Trailing Whitespace.................................................Failed hook id: trailing-whitespace exit code: 1 files were modified by this hook Fixing README.md ```bash - You can see failures here: - `Trim Trailing Whitespace.................................................Failed` - Here it tells you that the hook is fixing these issues for you: - ``` files were modified by this hook Fixing README.md ``` - Now all you need to do is stage and add those changes and that's it! - Once you stage and commit, you should see now a message as below where everything should pass. - ``` 2023-02-25 20:42:57.360 [info] Google Java Code Style for Java......................(no files to check)Skipped Trim Trailing Whitespace.................................................Passed Check Yaml...........................................(no files to check)Skipped Check JSON...........................................(no files to check)Skipped Pretty format JSON...................................(no files to check)Skipped Check Xml............................................(no files to check)Skipped Fix End of Files.........................................................Passed Don't commit to branch...................................................Passed ```
When pre-commit is first introduced in a project it is advised to format all the files at least one time to avoid seeing formatting changes going forward. The way you can do this is by running the below command in a powershell or a git terminal
Note: It would not work from intellij or normal terminals.
java -jar "C:\Users\Pramod Yadav\.cache\pre-commit\google-java-formatter1.16.0.jar" --replace $(git ls-files *.java)
Here you to replace the "Pramod Yadav" user with your user directory where this jar file was downloaded. Once formatted push all the changes in a PR and you can be assured that you dont have to see any formatting related issues anymore in your project PRs.