4

Can anyone recommend an approach to have my Git repo reject commits if the Java code being committed doesn't conform to a predefined coding standard. I'm fed up of seeing a mix of tabs and spaces and a commit log with entries such as 'Converting tabs to spaces'.

I have Eclipse configured to automatically perform formatting on save, but I realised that I had a different set-up across two machines, so I decided I wanted this built into my version control to save me from having to check my Eclipse configuration each time. I am also open for debate as to whether or not the formatting should just be applied automatically on commit rather than rejecting the commit.

Also interested in opinions on whether or not I should include coding styles in this too, such as rejecting for loops that don't use the for-each syntax etc.

DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
  • 2
    You can create a git pre-commit hook that invokes CheckStyle, FindBugs, or your favorite static analyzer to verify the Java source files being added. [This GitHub Gist](https://gist.github.com/davetron5000/37350) has an example script you can base your hook on. – Perception Mar 07 '13 at 15:38
  • 1
    Or use a filter driver: http://stackoverflow.com/questions/2316677/can-git-automatically-switch-between-spaces-and-tabs - Beyond that, your question is a bit "not constructive" as you're basically asking a bunch of broad open-ended questions to which there's no definitive answer beyond what people's own personal preferences are. – Brian Roach Mar 07 '13 at 15:44

0 Answers0