How can I make git reject the click of code that will not compile?

I would like to add an update binding to git that prevents people from clicking java code that will not compile. Ideally, it will call javac, see the result, and enable or reject push.

The most common example of what I want to prevent is someone who does not commit all his changes, thereby breaking the assembly. However, I understand that on the client (and not on the server) git hooks are executed, so if this happens, the hook will allow you to click anyway.

What is the best way to prevent people breaking assembly with incomplete commits?

UPDATE:

Got a primitive version of the hook, thanks for all the help!

Excerpt from the update hook:

### make sure code compiles ## currently does this by copying the state of the repository as of the pushed code and attempting to build it # for now, hard coded as C:\Windows\Temp copydir="/c/Windows/Temp/git_hook_compile_copy" echo "making copy of $newrev to $copydir" >&2 rm -rf "$copydir" mkdir "$copydir" git archive $newrev | tar -x -C $copydir/ if [ "$?" != "0" ]; then echo "*** unable to make copy of code" >&2 exit 1 fi echo "attempting to build $newrev" >&2 "$ANT_HOME/bin/ant" -file "$copydir/appropriatePath/build.xml" if [ "$?" != "0" ]; then echo "*** code does not compile" >&2 exit 1 fi 

(note that this is for the Windows environment and depends on the specific ANT_HOME environment variables (and thus JAVA_HOME))

+7
source share
2 answers

Git server and client are not much different ;-). Thus, all interceptions launched on the "client" will be performed on the "server", unless, of course, the same events occur on the server.

For example, update hook, completely falls into this category. It is called after updating the branch, and if this hook returns a non-zero status, the update is canceled. Therefore, you may want to put the compilation there and return its result. All messages that you print using the hook will be shown to the user who commits on his console. This is very convenient as it can see error messages from your build script and fix it.

Even if you run two compilations at the same time, the git repository will not lose commit due to the use of the "old refname" argument in its update hook. However, it may happen that the committer is waiting for compilation, and its reflector is not pressed, because someone else made it click.

The default git repository contains a good example of the update hook (named update.sample ). Refer to it if you need a final example.

However, if the compilation is too long, and the commit speed exceeds the frequency with which you can compile your code, you can use more complex solutions. Browsers suggest looking for "continuous integration" on google.

+4
source

We do this, gerrit in the way of the work that the developer does and what everyone else sees. It takes two people to make a mistake that breaks things down for everyone else.

We also use buildbot (for continuous integration) so that we can run the assembly on all machines before the code is "public".

+1
source

All Articles