j4mie/pyooks — Issue #1: Implementation ideas to evolve pyooks
Author: daltonmatos
Created: 2011-11-08 17:31:08+00:00
State: open

Hello Jamie,

First, contratulations for your project. It's a very interesting idea. In fact, some time ago I almost started a very similar project but for svn hooks, as I stopped using svn for my personal projects git seems to be the next target.

So the first idea was to have pyooks to manage all git hooks for a repository. For example, currently we need do declare all hook Classes insite the `.git/hooks/post-commit` file. My idea was to have always *the same* code, for instance:

from pyooks import runhook

runhook()

and then `runhook`, internally, could know which kind of hook it is. It can do that looking at `sys.argv[0]`. Then, after discovering this it would dynamically load all hooks of that kind (post-commit, pre-commit, etc).

To do this I was thinking about using one of my projects, plugnplay (https://github.com/daltonmatos/plugnplay). With it we could have different API for each kind of Hook, something like this:

class PreCommitHook(plugnplay.Interface):

def each_changed_file(self, filenobject):
pass

And everytime we need to implement a PreCommitHook we would do:

class CheckPEP8(plugnplay.Plugin):
implements = [PreCommitHook]

def each_changed_file(self, fileobject):
#Here goes the code that checks for PEP8

p.s. A PostCommitHook implementor would receive the commit SHA, instead of the changed files, for example.

And finnaly our `unhook()` would be something like this:

def runhook()
hook_type = sys.argv[0]

if hook_type == 'pre-commit':
changed = _get_changed_files()
for hook in PreCommitHook.implementors():
for file in changed:
hook.each_changed_file(file)

Something like this (sorry for the bad indentation). This way we could have more than one Hook implementation per hook type. And also pyooks could have a command line script to "install" itself into a git repo, this would be done by creating the desired hook type with the code with `runhook()`. Something like: `pyooks install `

Plugnplay is capable of loading plugins from a directory on disk, so pyooks could have a configuration to know where to find external plugins, that is, plugins written by other people and that are not included on the official distribution.

Anyway, there are yet a lot of ideas, for example: The capability of choosing the order of executions of the hooks and to enable/disable individual hooks. All this would be saved on `.git/config` file.

I will provide a proof-of-concept implementation so you will be able to see more clearly what are the ideas. I just wanted to post here so we can have this registered.

Thank you very much!