Skip to content

Instantly share code, notes, and snippets.

@marconi
Created August 27, 2011 05:13
Show Gist options
  • Select an option

  • Save marconi/1175012 to your computer and use it in GitHub Desktop.

Select an option

Save marconi/1175012 to your computer and use it in GitHub Desktop.
Found this nice script for adding git commits to trac.
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010 Grzegorz Sobański
#
# 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 = '/path/to/trac/env'
GIT_PATH = '/usr/bin/git'
TRAC_ADMIN = '/usr/bin/trac-admin'
REPO_NAME = '(default)'
# if you are using gitolite or sth similar, you can get the repo name from environemt
# REPO_NAME = os.getenv('GL_REPO')
# communication with git
def call_git(command, args, input=None):
return Popen([GIT_PATH, command] + args, stdin=PIPE, stdout=PIPE).communicate(input)[0]
def handle_ref_trac(old, new, ref):
# branch delete, skip it
if re.match('0*$', new):
return []
if re.match('0*$', old):
# create
revspec = "%s" % new
else:
# update
revspec = "%s..%s" % (old, new)
all_branches = call_git('for-each-ref', ['--format=%(refname)', 'refs/heads/']).splitlines()
other_branches = [branch for branch in all_branches if not branch == ref]
not_other_branches = call_git('rev-parse', ['--not'] + other_branches)
new_commits = call_git('rev-list', ['--stdin', '--reverse', revspec], not_other_branches).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
commits = []
for line in sys.stdin:
commits += handle_ref_trac(*line.split())
# call trac-admin
handle_trac(commits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment