From 0e314103b828bfa52d04d7c063353eba76ae7e9f Mon Sep 17 00:00:00 2001 From: Zaran Date: Tue, 20 Sep 2011 15:47:03 +0200 Subject: Add post-receive script Depends on irclib (included). The misc directory should be used for files related to project maintenance or for configurations files (e.g. ejabberd.conf) --- misc/post-receive | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 misc/post-receive (limited to 'misc/post-receive') diff --git a/misc/post-receive b/misc/post-receive new file mode 100755 index 0000000..04853c1 --- /dev/null +++ b/misc/post-receive @@ -0,0 +1,9 @@ +#!/bin/sh +# post-receive hook for notifications +# arguments received +# + +# irc notification +# change the path of the python script +a=`git log --format=format:"%an | %s" $1..$2` +python git-post-commit.py "irc.freenode.net" "AliasCommit" "#alias" "$a" -- cgit v1.2.3-70-g09d2 From 4264d6c6360c234d789df2ea513bb83022f41ab3 Mon Sep 17 00:00:00 2001 From: Zaran Date: Wed, 21 Sep 2011 00:40:17 +0200 Subject: Fix a bug in post-recieve Values are passed to the hook's standard input and not to the commandline arguments. The Git doc is unclear on this subject. --- misc/post-receive | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'misc/post-receive') diff --git a/misc/post-receive b/misc/post-receive index 04853c1..09ccf95 100755 --- a/misc/post-receive +++ b/misc/post-receive @@ -5,5 +5,6 @@ # irc notification # change the path of the python script -a=`git log --format=format:"%an | %s" $1..$2` +read oldrev newrev refname +a=`git log --format=format:"%an | %s" $oldrev..$newrev` python git-post-commit.py "irc.freenode.net" "AliasCommit" "#alias" "$a" -- cgit v1.2.3-70-g09d2 From c607a0177e6950e098d0f655dc4b8d6008d50710 Mon Sep 17 00:00:00 2001 From: Zaran Date: Wed, 21 Sep 2011 01:34:23 +0200 Subject: Improve irc notification to support multiple commits. post-recieve hooks print all the commit lines to standard output which is piped to git-post-commit.py. git-post-commit.py now reads on standard input, one line at a time and prints in the channel. --- misc/git-post-commit.py | 14 +++++++++----- misc/post-receive | 11 ++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'misc/post-receive') diff --git a/misc/git-post-commit.py b/misc/git-post-commit.py index 775fe3b..07eeb60 100755 --- a/misc/git-post-commit.py +++ b/misc/git-post-commit.py @@ -1,10 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- """ -Simple script to send a message to an IRC channel +Simple script to send messages to an IRC channel in Fire and Forget mode (non persistent connection) -Usage: git-post-commit +Usage: git-post-commit + +Read lines one by one on the standard input and print them in +in the channel """ import irclib @@ -19,15 +22,16 @@ class IRCFire(irclib.SimpleIRCClient): connection.join(self.target) def on_join(self, connection, event): - self.connection.privmsg(self.target, sys.argv[4]) + for line in sys.stdin: + self.connection.privmsg(self.target, line) self.connection.quit("Commit sent") def on_disconnect(self, connection, event): sys.exit(0) def main(): - if len(sys.argv) != 5: - print "Usage: " + sys.argv[0] + " " + if len(sys.argv) != 4: + print "Usage: " + sys.argv[0] + " " sys.exit(1) s = sys.argv[1].split(":", 1) diff --git a/misc/post-receive b/misc/post-receive index 09ccf95..b44916a 100755 --- a/misc/post-receive +++ b/misc/post-receive @@ -5,6 +5,11 @@ # irc notification # change the path of the python script -read oldrev newrev refname -a=`git log --format=format:"%an | %s" $oldrev..$newrev` -python git-post-commit.py "irc.freenode.net" "AliasCommit" "#alias" "$a" +{ + while read oldrev newrev refname + do + a=`git log --format=format:"%an | %s" $oldrev..$newrev` + printf "$a\n" + done +} | python git-post-commit.py "irc.freenode.net" "AliasCommit" "#alias" + -- cgit v1.2.3-70-g09d2 From 73b2c72961544b42229dd334fc75a20d52acff9b Mon Sep 17 00:00:00 2001 From: Zaran Date: Wed, 21 Sep 2011 03:09:31 +0200 Subject: Add trac update hook. Called from the post-receive hook --- misc/post-receive | 3 +- misc/trac-update.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100755 misc/trac-update.py (limited to 'misc/post-receive') diff --git a/misc/post-receive b/misc/post-receive index b44916a..1041c89 100755 --- a/misc/post-receive +++ b/misc/post-receive @@ -4,11 +4,12 @@ # # irc notification -# change the path of the python script +# change the path of the python scripts { while read oldrev newrev refname do a=`git log --format=format:"%an | %s" $oldrev..$newrev` + printf "$oldrev $newrev $refname\n" | python trac-update.py printf "$a\n" done } | python git-post-commit.py "irc.freenode.net" "AliasCommit" "#alias" diff --git a/misc/trac-update.py b/misc/trac-update.py new file mode 100755 index 0000000..352abce --- /dev/null +++ b/misc/trac-update.py @@ -0,0 +1,82 @@ +#! /usr/bin/python +# -*- coding: utf-8 -*- +# +# Copyright (c) 2011 Grzegorz SobaƄski +# +# Version: 2.0 +# +# Git post receive script developed for mlabs +# - adds the commits to trac +# based on post-receive-email from git-contrib +# + +import re +import os +import sys +from subprocess import Popen, PIPE, call + +# config +TRAC_ENV = '/home/www/alias' +GIT_PATH = '/usr/bin/git' +TRAC_ADMIN = '/usr/bin/trac-admin' +REPO_NAME = '(default)' + +# communication with git + +def call_git(command, args, input=None): + return Popen([GIT_PATH, command] + args, stdin=PIPE, stdout=PIPE).communicate(input)[0] + + +def get_new_commits(ref_updates): + """ Gets a list uf updates from git running post-receive, + we want the list of new commits to the repo, that are part + of the push. Even if the are in more then one ref in the push. + + Basically, we are running: + git rev-list new1 ^old1 new2 ^old2 ^everything_else + + It returns a list of commits""" + + all_refs = set(call_git('for-each-ref', ['--format=%(refname)']).splitlines()) + commands = [] + for old, new, ref in ref_updates: + # branch delete, skip it + if re.match('0*$', new): + continue + + commands += [new] + all_refs.discard(ref) + + if not re.match('0*$', old): + # update + commands += ["^%s" % old] + # else: new - do nothing more + + for ref in all_refs: + commands += ["^%s" % ref] + + new_commits = call_git('rev-list', ['--stdin', '--reverse'], '\n'.join(commands)).splitlines() + return new_commits + + +def handle_trac(commits): + if not (os.path.exists(TRAC_ENV) and os.path.isdir(TRAC_ENV)): + print "Trac path (%s) is not a directory." % TRAC_ENV + + if len(commits) == 0: + return + + args = [TRAC_ADMIN, TRAC_ENV, 'changeset', 'added', REPO_NAME] + commits + call(args) + + +# main +if __name__ == '__main__': + # gather all commits, to call trac-admin only once + lines = sys.stdin.readlines() + updates = [line.split() for line in lines] + commits = get_new_commits(updates) + + # call trac-admin + handle_trac(commits) + -- cgit v1.2.3-70-g09d2