post_commit hook write a file?

Chris Hecker checker at d6.com
Sat May 21 19:05:58 UTC 2011


Awesome, thanks everybody.  I'll try this out.

Chris


On 2011/05/21 07:26, John Arbash Meinel wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 5/21/2011 7:19 AM, Chris Hecker wrote:
>>
>> Hi, I've never tried to write a plugin before, and my python skills are
>> at the barely-literate level, so I'm wondering if someone who knows the
>> bzr api inside and out can tell me if I'm doing this right.
>>
>> I'm trying to write a post_commit hook that will put the new revno in a
>> file at the root of the branch, if that file already exists.  Here's
>> what I have so far, does this look right?  It seems to work.
>>
>> This is to update a version resource in an executable.  Visual Studio is
>> totally broken in the way it handles build events and custom build
>> steps, so I can't use bzr revno or version-info because I need a file
>> that changes on every commit, sadly.
>>
>> Thanks,
>> Chris
>
> To start with, I would probably use a post_changed_branch_tip hook,
> since that also triggers when you do "bzr pull".
>
> Second, Transport knows about how to create a local path if the URL is
> actually local.
>
>>
>>
>> revno_commit_file.py:
>>
>> import bzrlib.branch
>>
>> def post_commit_hook(local, master, old_revno, old_revid, new_revno,
>> new_revid):
>>      import bzrlib.urlutils
>>      import os.path
>>      p = bzrlib.urlutils.local_path_from_url(master.base) + 'revno'
>>      if os.path.isfile(p):
>>          f = open(p,'w')
>>          f.write(str(new_revno))
>>          f.close()
>>
>> # my version of bzr doesn't have install_lazy_named_hook, sadly
>> bzrlib.branch.Branch.hooks.install_named_hook('post_commit',
>>                                                post_commit_hook,
>>                                                'Update revno file on
>> commit, if it exists.')
>>
>
>
> from bzrlib import branch, errors
>
> def update_revno_file(params):
>      try:
> 	trans = params.branch.bzrdir.root_transport
>          path = trans.local_abspath('revno')
>      except errors.NotLocalURL:
> 	return # Nothing to do for a remote URL
>      if os.path.isfile(path):
> 	with open(path, 'wb') as f:
> 	    f.write('%s\n' % (params.new_revno,))
>
> branch.Branch.hooks.install_named_hook('post_changed_branch_tip',
>      update_revno_file, 'update "revno"')
>
> John
> =:->
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (Cygwin)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk3Xy6wACgkQJdeBCYSNAAPGVwCfRuSUxG4PZ4kFeDMQMV2T8ZVo
> 5PMAoLHC54Ym9A8aGpjBMLa8ZRgdbjKb
> =DnDc
> -----END PGP SIGNATURE-----
>



More information about the bazaar mailing list