How to add pre-receive hook in GitLab

Recently we decided to move our git repo to local git server so we chose GitLab.

During weeks use GitLab it was really convenient. Until one day we have a thought about checking code style before push. 

Basically Git support 2 kinds of hooks. One is Client side and the other is from Server side
http://git-scm.com/book/en/Customizing-Git-Git-Hooks
In terms of checking code style we can do both on Client and Server side.  However we want this rules be more STRICT :D, so we decided to make it from Server side .

So here is problem! After investigation we found GitLab actually changed quite a lot from Gitorious. Especially Gitlab uses Gitlab-Shell to wrapper the Git commands. So when we found "Hooks" folder what we can get is like this:


 ONLY ONE "UPDATE" . If you open it you will found quite simple codes:

 So check GitlabUpdate in ../lib/



 And if you change the codes to exit with "1", all the push request will be rejected. Seems like we have to write some ruby codes to check Java codes style :P. And I do tried ... and failed.

I found "checkstyle" http://checkstyle.sourceforge.net/index.html
And I tried to call this tool from Ruby (never write Ruby codes...), it works. But I failed to read out git blob and files from push request. 

Then I changed my mind. I went back to follow the standard Git hooks. So I found for each repo I created there is a folder from Server. That's it.

http://bluec0re.blogspot.fr/2012/05/git-pre-receive-hook-with-checkstyle.html
follow this tutorial to create your own pre-receive in hooks
the path should be like :
/home/git/repositories/root/helloworld.git/hooks/

And also specify your checkstyle.jar and checks_style.xml files locations.

Tried to push from your local repo. You will find a bunch of error coming from GitLab. But the limitation is for each git repo you have to do this manually . Have not found better solution yet. Please let me know if there is better ones.

Enjoy it.

PS: The tutorial I mentioned did not consider remove files from git server . 
So add one more line

# create temporary dir and save a copy of the new files
tempdir = tempfile.mkdtemp('git_hook')
for ref, files in files.iteritems():
    for filename, data in files.iteritems():
        if len(data) <= 1: continue
        dname = os.path.dirname(filename)
        bname = os.path.basename(filename)
        try:
            os.makedirs(os.path.join(tempdir, dname))
        except OSError, exc:
            if exc.errno == errno.EEXIST:  # directory exists already
                pass
            else:
                raise

        with open(os.path.join(tempdir, dname, bname), 'w') as fp:
            fp.write(call_git(['cat-file', data[1], data[2]]))




Comments

Popular posts from this blog

A wired issue of MediaPlayer on Android

Problem when using Jackson json parser in Android.

Gradle issue - peer not authenticated