Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make it so git rejects pushing code that won't compile?

I would like to add an update hook to git that prevents people from pushing java code that won't compile. Ideally it would call javac, see the result, and either allow or reject the push.

The most common example of something I want to prevent is somebody not committing all their changes thus breaking the build. However, it is my understanding that git hooks run on the client (not the server) so if the above happens the hook will still allow the push.

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

UPDATE:

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

Excerpt from 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 this is for a windows environment and relies on ANT_HOME (and thus JAVA_HOME) environment variables being defined)

like image 505
Aaron Silverman Avatar asked Dec 27 '10 20:12

Aaron Silverman


2 Answers

Git server and client do not differ that much ;-). So all hooks that run on "client" will run on "server" if same events occur on the server, of course.

For example, update hook, totally falls into this category. It's called after a branch was updated, and if this hook returns nonzero status, the update is canceled. So you might want to put compilation there, and return its result. All the messages you print from hook will be shown to the user who commits on his console. This is quite handy, since he can see error messages from your build script, and fix it.

Even if you run two compilations concurrently, git repository won't lose commits, due to use of "old refname" argument in its update hook. However, it might then happen that a committer waits for compilation, and his ref isn't pushed because someone else got his one pushed instead.

A default git repository contains a nice example of an update hook (named update.sample). Refer to it if you need a definitive example.

However, if compilation is too long, and the rate of commits exceeds the frequency you can compile your code with, you might want to use more advanced solutions. Commenters suggest to look for "continuous integration" in google.

like image 64
P Shved Avatar answered Sep 26 '22 09:09

P Shved


We do this by having gerrit in the path of the work a developer does and what everyone else sees. It takes two people to make a mistake that breaks stuff for everyone else.

We also use buildbot (for continuous integration) in such a way that we can run a build across all machines before the code goes "public."

like image 38
Dustin Avatar answered Sep 26 '22 09:09

Dustin