at_yasu's blog

ロード的なことを

Bazaar の Hook

Bazaarでhookを使う方法。結論的に言えば、Pluginとしてスクリプトを書くようです。

作成

BazaarのPluginなので、Pythonで書いてやる必要があります。例として、pushした時に起動するhookで、pushされたらそれぞれの情報を出力します。

BazaarのPluginは、「bzrlib/plugins」や「~/.bazaar/plugins」、ないしは環境変数「BZR_PLUGIN_PATH」で指定された場所になります。

#!/usr/bin/python
# -*- coding:utf-8 -*-

from bzrlib import branch

def post_push_hook (p):
    print "source_branch: %s" % (p.source_branch)
    print "target_branch: %s" % (p.target_branch)
    print "master_branch: %s" % (p.master_branch)
    print "local_branch:  %s" % (p.local_branch)
    print "old_revno:     %d" % (p.old_revno)
    print "old_revid:     %s" % (p.old_revid)
    print "new_revno:     %d" % (p.new_revno)
    print "new_revid:     %s" % (p.new_revid)

branch.Branch.hooks.install_named_hook('post_push', post_push_hook, 'My Post_push hook')


実行例

[yasui@negro: ~/tmp/hook-aaa][13:02] $ bzr push
cannot import name filters
Unable to load plugin 'keywords' from '/Volumes/home/Users/yasui/.bazaar/plugins'
Using saved push location: /Volumes/home/Users/yasui/tmp/hook-test/
source_branch: BzrBranch6('file:///Volumes/home/Users/yasui/tmp/hook-aaa/')
target_branch: BzrBranch6('file:///Volumes/home/Users/yasui/tmp/hook-test/')
master_branch: BzrBranch6('file:///Volumes/home/Users/yasui/tmp/hook-test/')
local_branch:  None
old_revno:     1
old_revid:     a.yasui@gmail.com-20090306040137-eexjtm38fyjsjspy
new_revno:     2
new_revid:     a.yasui@gmail.com-20090306040240-v242pm9ibjz3b52i
All changes applied successfully.
Pushed up to revision 2.
[yasui@negro: ~/tmp/hook-aaa][13:02] $

作成

hookのタイプは色々あるようで、代表的な例として。

  • open
  • post_push
  • post_pull
  • post_commit
  • commit_message_template


一つのファイルで全て済ます場合は、「プラグイン名.py」、複数のファイルを使う場合は「プラグイン名/__init__.py」に書けというとの事。

注意点

だいたい何かをミスしていれば警告してくれます。ただしファイル名に「-」を付けるのはだめなようで。作成したら「bzr hook」でテストをするのが良いようです。

それと私が読んでいるサイトは、hazaarでhazaar-ngとの違いが私自身がついていません。ご了承をば。