# Copyright (C) 2009, Hendrik van Antwerpen # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """Changing trac bug status after commits. To have bzr change the status of bugs in trac, you need to configure the location of your trac. To do this set the configuration option ``trac_project``. If ``trac_project`` is empty, nothing will be done. """ version_info = (1, 0, 0, 'dev', 0) if version_info[3] == 'final': version_string = '%d.%d.%d' % version_info[:3] else: version_string = '%d.%d.%d%s%d' % version_info __version__ = version_string TRAC_PROJECT_PROP = 'trac_project' import trac trac_version = (int(trac.__version__[0]), int(trac.__version__[2:4])) if trac_version < (0,12): warning("bzr-trac hook needs Trac >= 0.12") """ BZR BranchTipChange Hook """ import os,sys,subprocess import bzrlib from bzrlib.bzrdir import BzrDir from bzrlib.branch import Branch from bzrlib.commands import Command, register_command from bzrlib.option import Option from bzrlib.trace import warning def get_component_path(component): url = component.bzrdir.root_transport.external_url() from urlparse import urlparse parts = urlparse(url) assert not parts.scheme or parts.scheme == 'file' return parts.path def branch_tip_hook(params): """This is the post_update_branch_tip hook that runs after branch updates.""" branch = params.branch old_revno = params.old_revno new_revno = params.new_revno config = branch.get_config(); project = config.get_user_option(TRAC_PROJECT_PROP) if not project: return repository = branch.repository repository_path = get_component_path(repository) # calculate actual new revisions, if none we stop to prevent double # comments at tickets revnos = map(str,range(old_revno+1,new_revno+1)) if not revnos: return # calculate prefixed revnos in shared repository enviroment if repository.is_shared(): branch_path = get_component_path(branch) common_path = os.path.commonprefix([repository_path,branch_path]) offset_path = os.path.relpath(branch_path,common_path) prefix = offset_path+',' revnos = map(lambda revno: prefix+revno, revnos) # fire changeset_added event in Trac from trac.env import open_environment from trac.versioncontrol.api import RepositoryManager env = open_environment(project) rm = RepositoryManager(env) rm.notify('changeset_added', repository_path, revnos) Branch.hooks.install_named_hook('post_change_branch_tip', branch_tip_hook, 'bzr-trac') """ BZR TracProject Command """ class cmd_trac_project(Command): """Print or set trac project to submit changes. """ takes_args = ['project?'] takes_options = [ Option('remove', "Remove trac project information.") ] def run(self, project=None, remove=False): bzrdir = BzrDir.open_containing('.')[0] branch = bzrdir.open_branch() config = branch.get_config() if remove: config.set_user_option(TRAC_PROJECT_PROP, None) else: if project: config.set_user_option(TRAC_PROJECT_PROP, project) repository = bzrdir.find_repository() if repository.is_shared(): warning("Detected a shared repository: " "trac project has to be set on every branch!") else: print config.get_user_option(TRAC_PROJECT_PROP) register_command(cmd_trac_project)