3

I have installed mvn java-formatter:format task in pre-commit hook. This task will format the java source codes.

pushd src/ > /dev/null

mvn java-formatter:format
RETVAL=$?
if (($RETVAL == 1)); then
        exit 1
fi

popd > /dev/null

I need to append newly formatted source files to commit. How can I do this?

Milad Khajavi
  • 2,586
  • 8
  • 36
  • 64

2 Answers2

5

The pre-commit hook should be able to include a git add, to modify the index about to be committed.

See for example "git pre-commit hook, add file into index".

Try a git add -A :/. (see "Add as if from the root folder of the repository")

If you need to add only the files which were already staged, then you need:

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
0

It is possible to do that, but requires a tricky script.

Here you can find the same problem solved. There, it is updating the file version on every commit, instead of formatting the code. It is fully working: https://github.com/addonszz/Galileo/tree/master/githooks

Then you just replace the 'Version File Replacement' algorithm on the file 'updateVersion.sh', by your 'Formatting Code' algorithm. Maybe you need to change a few things like, remove the branch limitation, because there, the script only runs if you are on the 'develop' branch.

Also, it will only change the file, if is staged. If the file is not staged, then it will do nothing. More precisely, it print out what it is doing on every step.

user
  • 5,816
  • 7
  • 53
  • 105