how to write a project-aware post commit hook for trac?

Andrew Bennetts andrew.bennetts at canonical.com
Thu May 5 01:39:28 UTC 2011


Nagy Viktor - Vikti - Bika wrote:
>    hi,
> 
>    I would like to have a single post commit hook for all my trac-s
>    I've seen:
>    [1]http://doc.bazaar.canonical.com/bzr.dev/en/user-guide/hooks.html
> 
>    the trac hook should call
>    $ trac-admin $ENV changeset added "openerp-6" $REV
> 
>    where $ENV is a directory path to my trac instance.
> 
>    my first question is how could I get $REV?
> 
>    the second needs a bit of explanation
>    I have several projects, and my repo is organised the same way, e.g.
>    if $ENV is /var/tracprojects/myproj, then my bzr repo is at
>    /var/bzr/myproj
>    so how can I get the path of the current bzr repo the hook is called on?

I'm not quite sure how “openerp-6” should be derived in that example, so
this may need some adjusting, but you want a plugin along the lines of:

“““

import os.path
import subprocess

from bzrlib.branch import Branch
from bzrlib import urlutils

def add_tip_to_trac(params):
    trac_base = '/var/tracprojects/'
    repo_path = urlutils.local_path_from_url(params.branch.repository.user_url)
    branch_path = urlutils.local_path_from_url(params.branch.user_url)
    project_name = os.path.basename(repo_path)
    branch_name = os.path.basename(branch_path)
    stdout_and_err = subprocess.check_output(
    	['trac-admin',
	 os.path.join(trac_base, project_name),
	 'changeset',
	 'added',
	 branch_name,
	 params.new_revid,
	 ],
	 stderr=subprocess.STDOUT)
    # log the stdout_and_err somewhere?

Branch.hooks.install_named_hook(
    'post_change_branch_tip', add_tip_to_trac, 'add changeset to Trac')

”””

i.e. use the post_change_branch_tip hook, and the params object passed
to that has the details of the branch and new revision etc that you
need.

-Andrew.




More information about the bazaar mailing list