Generate a Diff from a plugin

Patrick Dobbs patrick.dobbs at supplierselect.com
Thu Jun 18 20:40:19 BST 2009


 > John - thanks very much! that worked very well

 > Forest - well spotted, saved me some time there, thank you

In case anyone browses this list for something similar, I've pasted 
rough code that seems to work.

-----------------------------------------------------------

import re, cStringIO
from bzrlib import branch,diff

def post_push_hook(push_result):
    rev_range = range(push_result.old_revno, push_result.new_revno)
    for rev_no in rev_range:
        process_one_revision(push_result.source_branch,rev_no)

def process_one_revision(source,rev_no):
    rev_id = source.get_rev_id(rev_no)
    next_rev_id = source.get_rev_id(rev_no+1)
    message = source.repository.get_revision(next_rev_id).message
    bug_message = re.search("(\s|^)(\#)(\d+)",message) # look for a bug 
id string like #2998
    if bug_message:
        bug_id = bug_message.group(3)
        diff = get_diff(source,rev_id,next_rev_id)
        do_soap_bug_thing(message + "\n\n"+diff,bug_id) #not shown here

def get_diff(source_branch,rev_id1,rev_id2):
    source_branch.lock_read()
    tree1, tree2 = source_branch.repository.revision_trees([rev_id1, 
rev_id2])
    out = cStringIO.StringIO()
    diff.show_diff_trees(tree1, tree2, out, old_label='', new_label='')
    return out.getvalue()


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

--------------------------------------------------------------------------



More information about the bazaar mailing list