copyright check pre-commit hook

John Arbash Meinel john at arbash-meinel.com
Thu Jan 7 23:16:58 GMT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Erik de Castro Lopo wrote:
> Hi all,
> 
> I asked on #bzr but this is probably a better place :-).
> 
> I'd like to have a bzr pre-commit hook that checks copyright notices
> in the files being commited for the current year and fail the commit
> if the current year is not included.
> 
> Eg if 'bzr whoami' says "Erik <erik at hotmail.com>" a commit of a file
> containing:
> 
>    Copyright (c) 2007-2009  Erik <erik at hotmail.com>
>    Copyright (c) 2010  Peter <peter at hotmail.com>
> 
> should fail while
> 
>    Copyright (c) 2007-2010  Erik <erik at hotmail.com>
>    Copyright (c) 2010  Peter <peter at hotmail.com>
> 
> should pass.
>   
> I know nothing of Bzr internals and rarely hack python so doing this
> myself is not really a possibility. If someone can hack up something
> that works, I can maintain it.
> 
> Cheers,
> Erik

It doesn't do everything you want but I just put together a plugin that
does the checking of copyright, etc.

 lp:~jameinel/+junk/bzr-update-copyright

If all you wanted was a pre-commit hook, then you probably just want a
plugin that does something like:

def inspect_file(path, file_id, future_tree):
    content = future_tree.get_file_lines(file_id)
    # do your inspection, returning [] if there are no problems, or
    # ['error_message'] if something is wrong (potentially multiple
    # errors)
    errors = []
    if something_is_wrong:
      errors.append('File %s is missing foobar' % (path,))
    return errors


def copyright_hook(local_branch, master_branch, old_revno, old_revid,
		   new_revno, new_revid, tree_delta, future_tree):
  # Inspect tree_delta for changes, and get the text content from
  # 'future_tree'

  errors = []
  for path, file_id, kind in tree_delta.added:
    if kind == 'file':
      errors.extend(inspect_file(path, file_id, future_tree))
  # Skip removed
  for (oldpath, newpath, file_id, kind, text_modified,
       _) in tree_delta.renamed:
    if kind == 'file' and text_modified:
      errors.extend(inspect_file(newpath, file_id, future_tree))
  for (path, file_id, old_kind, new_kind) in tree_delta.kind_changed:
    if new_kind == 'file':
      errors.extend(inspect_file(path, file_id, future_tree))
  for (path, file_id, kind, text_modified, _) in tree_delta.modified:
    if kind == 'file':
      errors.extend(inspect_file(path, file_id, future_tree))
  if errors:
    raise ValueError("There are errors in your copyright headers:\n%s"
                     % ('\n'.join(errors),))

bzrlib.branch.Branch.hooks.install_named_hook('pre_commit',
    copyright_hook, 'copyright-check')


John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAktGa2oACgkQJdeBCYSNAANi4gCcCcepyUMTUvi6wZcCbT+LAE9g
M1IAn0m6VpyAtSpDtPsYEuv4JOWBiK9j
=fY3B
-----END PGP SIGNATURE-----



More information about the bazaar mailing list