Code formatting will be done using pre-commit hook, which runs first when user do a commit.
Code for the hook is given below. It formats java source code using astyle.
For this hook to work as expected astyle command needs to be in the path. Astyle can be downloaded from http://astyle.sourceforge.net/
For more information on git hooks please visit https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
Code for the hook is given below. It formats java source code using astyle.
#!/bin/bash # don't do anything if astyle command is not available command -v astyle >/dev/null 2>&1 || { exit 0; } # get the root directory for the repository RootDir=`git rev-parse --show-toplevel` # get all the files that are staged for v in `git diff --name-only --cached` do # if it is java file if [[ $v == *.java ]] then astyle --style=java "$RootDir/$v" > /dev/null 2>&1 # remove the .orig file created by astyle [ -f "$RootDir/$v"".orig" ] && rm -f "$RootDir/$v"".orig" # stage the modified file # its ok if the file is unchanged after running astyle git add "$RootDir/$v" fi done exit 0Copy the above code in .git/hooks with file name as pre-commit.
For this hook to work as expected astyle command needs to be in the path. Astyle can be downloaded from http://astyle.sourceforge.net/
For more information on git hooks please visit https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
Hey,
ReplyDeleteGrazie! Grazie! Grazie! Your blog is indeed quite interesting around It's all fundamental Java code formatting hook for git just follows it! agree with you on lot of points!
Getting memory violation error when I use string function "strlwr", what wrong in step 3, please help, I am new to c language
Very useful article, if I run into challenges along the way, I will share them here.
Kind Regards,
Yamini