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.