6

I am planning to write a few git hooks as a project which logs the user's actions in a database. This database can then be used for querying for all his activities. The actions I am trying to log are

  • commit
  • pull
  • push
  • merge
  • branch

I want to get this packaged in distros which can be installed via package manager. Git allows global hooks by placing any such in $PREFIX/share/templates/hooks

It currently has some hooks which are disabled (.sample is appended to their name). When a new repository is created, these hooks are copied in the .git folder of the repository

Now if a user installs the package for these hooks and hooks like post-commit and post-update are already enabled. In this case the script file will be overwritten! This sounds bad

This means git has only one hook file per action. If I need to keep three hooks for one action, it means it is not possible. This means automated install from package manager can introduce conflicts.

Now think that we packaged the hooks to overwrite the default enabled file. If a user wants to add some more actions to those file and then decided to uninstall my package, then his custom command would also be gone?

I thought that git was pretty clever in this regard and I was wrong :(

There needs to be a folder named post-commit and post-update or whatever actions and git should run all the scripts inside that folder. I am still hunting a way to deal with the current situation.

Manish Sinha
  • 2,092
  • 2
  • 22
  • 33
  • 1
    You might have a look at this [question about tracking git hooks](http://stackoverflow.com/questions/3462955/putting-git-hooks-into-repository) - I posted a barebones implementation of installing symlinks into the hooks directory, which run the appropriate hooks - it's easily extensible to multiple hooks for the same task, and also provides for users leaving behind their own local hooks. – Cascabel Nov 13 '10 at 15:31
  • 1
    Note: A simple “run a list of scripts” will not work properly for all the hook types because some of them expect to receive data on stdin (e.g. pre-receive, post-receive, post-rewrite); in most naïve implementations the first script to run would grab all the data and the rest would get nothing (EOFs). It might works to capture the data once and feed it to each script, but that may not be appropriate for all scripts/situations. – Chris Johnsen Nov 14 '10 at 06:10
  • Jefromi, thanks. This looks good that symlinking can be a solution, but again the problem is that how to patch git on every machine so that git init copies hooks symlinks to a folder? Any good examples? – Manish Sinha Nov 15 '10 at 15:38
  • Chris, I think the taking the stdin and passing it to all the scripts can be a better solution. – Manish Sinha Nov 15 '10 at 15:39

1 Answers1

3

Why not write a post-commit hook (for instance) that will look for a "post-commit-hooks" subdirectory and will list and execute all scripts find in it?
(the first one that doesn't run successfully will fail the all post-commit hook)

If the users initialize their Git repo from a predefined template directory, you can then make sure they all get those special scripts in their new repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Well, I think that if the first script fails, the process should not halt. Any special reason for stopping all other scripts if one fail? – Manish Sinha Nov 14 '10 at 08:35
  • Yeah the problem comes if the user initializes their templates from a predefines directory. In this case my personal scripts or installed from the package cant be included in such. – Manish Sinha Nov 14 '10 at 08:36