KanonConductor

changeset 59:a9d0b61f3260

support git hook.
author chris
date Fri, 13 May 2011 13:43:21 +0900
parents 768f26ad37c5
children 45215326ffa4
files etc/opt/kanon/vcs-template/git/post-receive opt/kanon/bin/kanon-create-project
diffstat 2 files changed, 70 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/etc/opt/kanon/vcs-template/git/post-receive	Fri May 13 13:43:21 2011 +0900
     1.3 @@ -0,0 +1,69 @@
     1.4 +#! /opt/kanon/bin/python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +#
     1.7 +# Copyright (c) 2010 Grzegorz SobaƄski
     1.8 +#
     1.9 +# Git post receive script developed for mlabs
    1.10 +# - adds the commits to trac
    1.11 +# based on post-receive-email from git-contrib
    1.12 +#
    1.13 +
    1.14 +import re
    1.15 +import os
    1.16 +import sys
    1.17 +from subprocess import Popen, PIPE, call
    1.18 +
    1.19 +# config
    1.20 +TRAC_ENV = '/var/opt/kanon/trac/%{PROJECT_NAME}'
    1.21 +GIT_PATH = '/usr/bin/git'
    1.22 +TRAC_ADMIN = '/opt/kanon/bin/trac-admin'
    1.23 +REPO_NAME = '(default)'
    1.24 +# if you are using gitolite or sth similar, you can get the repo name from environemt
    1.25 +# REPO_NAME = os.getenv('GL_REPO')
    1.26 +
    1.27 +# communication with git
    1.28 +
    1.29 +def call_git(command, args, input=None):
    1.30 +    return Popen([GIT_PATH, command] + args, stdin=PIPE, stdout=PIPE).communicate(input)[0]
    1.31 +
    1.32 +
    1.33 +def handle_ref_trac(old, new, ref):
    1.34 +    # branch delete, skip it
    1.35 +    if re.match('0*$', new):
    1.36 +        return []
    1.37 +
    1.38 +    if re.match('0*$', old):
    1.39 +        # create
    1.40 +        revspec = "%s" % new
    1.41 +    else:
    1.42 +        # update
    1.43 +        revspec = "%s..%s" % (old, new)
    1.44 +
    1.45 +    all_branches = call_git('for-each-ref', ['--format=%(refname)', 'refs/heads/']).splitlines()
    1.46 +    other_branches = [branch for branch in all_branches if not branch == ref]
    1.47 +    not_other_branches = call_git('rev-parse', ['--not'] + other_branches)
    1.48 +    new_commits = call_git('rev-list', ['--stdin', '--reverse', revspec], not_other_branches).splitlines()
    1.49 +    return new_commits
    1.50 +
    1.51 +
    1.52 +def handle_trac(commits):
    1.53 +    if not (os.path.exists(TRAC_ENV) and os.path.isdir(TRAC_ENV)):
    1.54 +        print "Trac path (%s) is not a directory." % TRAC_ENV
    1.55 +
    1.56 +    if len(commits) == 0:
    1.57 +        return
    1.58 +
    1.59 +    args = [TRAC_ADMIN, TRAC_ENV, 'changeset', 'added', REPO_NAME] + commits 
    1.60 +    call(args)
    1.61 +
    1.62 +
    1.63 +# main
    1.64 +if __name__ == '__main__':
    1.65 +    # gather all commits, to call trac-admin only once
    1.66 +    commits = []
    1.67 +    for line in sys.stdin:
    1.68 +        commits += handle_ref_trac(*line.split())
    1.69 +
    1.70 +    # call trac-admin
    1.71 +    handle_trac(commits)
    1.72 +
     2.1 --- a/opt/kanon/bin/kanon-create-project	Thu May 12 19:19:27 2011 +0900
     2.2 +++ b/opt/kanon/bin/kanon-create-project	Fri May 13 13:43:21 2011 +0900
     2.3 @@ -193,6 +193,7 @@
     2.4  then
     2.5     git init --bare $KANON_VAR/git/$PROJECT_NAME
     2.6     git --git-dir=$KANON_VAR/git/$PROJECT_NAME update-server-info  
     2.7 +   cat $KANON_CONFIG/vcs-template/git/post-receive |sed 's/%{PROJECT_NAME}/'$PROJECT_NAME'/' > $KANON_VAR/git/$PROJECT_NAME/hooks/post-receive
     2.8     chown $APACHE_USER.$APACHE_USER -R $KANON_VAR/git/$PROJECT_NAME
     2.9  elif [ "$REPO_TYPE" != '' ]
    2.10  then