3

I am trying to create git hook on update in main git repository. I want to prevent pushes that break any tests. How to get in bare repository code of project that will be after update to run tests for it?

P.S.: It's possible, of course to use pre-commit hook instead of update, in that case hook should be on commiters PCs, but I believe that should be a way to make verification on the server.

Alex
  • 185
  • 10

2 Answers2

4

Use this approach: Disable pushing to the main repo by developers. Instead, force people to push to a build repo. Point a CI server to that build repo. If a build succeeds, let that build server push to the main repo.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
4

A CI server can do it for you, but a simple script can also do the trick. David Gageot introduced an very easy solution for private building with git on his blog awhile ago (the idea is to build with the tests on a secondary local repo before pushing on the remote server) :

http://blog.javabien.net/tag/git/page/2/

Unfortunately, it is written in french, but you can still have a look on the script that is used to do it :

#!/bin/bash
if [ 0 -eq `git remote -v | grep -c push` ]; then
    REMOTE_REPO=`git remote -v | sed 's/origin//'`
else
    REMOTE_REPO=`git remote -v | grep "(push)" | sed 's/origin//' | sed 's/(push)//'`
fi

if [ ! -z "$1" ]; then
    git add .
    git commit -a -m "$1"
fi

git pull

if [ ! -d ".privatebuild" ]; then
    git clone . .privatebuild
fi

cd .privatebuild
git clean -df
git pull

if [ -e "pom.xml" ]; then
    mvn clean install

    if [ $? -eq 0 ]; then
        echo "Publishing to: $REMOTE_REPO"
        git push $REMOTE_REPO master
    else
        echo "Unable to build"
        exit $?
    fi
fi

EDIT for non java users

In the script above, replace mvn clean install by the command line allowing you to build your project (and remove the if statement that tests the existance of a maven pom.xml file).

Yanflea
  • 3,876
  • 1
  • 14
  • 14