tags:

views:

440

answers:

2

Not sure if this is possible in git (I haven't found it but I may be using the wrong vocabulary in my searches), but it be would useful to be able to modify and enable hooks as the defaults for all new repositories (at the time of creation I mean) so these don't have to be customized each time a new repository is created. It seems the easy way to do this is write a wrapper that sets my hooks and chmods them when I create a new repository, but if there's a way built into git I would rather use that instead of having unnecessary wrapper scripts however little lying around.


Clarification copied up from a comment to a now-deleted answer:

My question is whether the default behavior for ALL new repositories can be changed, so they don't need to be customized in the same way each time for each new repository. The easy answer is write a wrapper for creating and customizing the repos (it generates the hook scripts and chmods them), but it seems like this default behavior should also be customizable without having to do that.

A: 

With git 1.6.5.3 (and some earlier versions), you get sample hooks delivered in your .git/hooks directory:

$ ls .git/hooks
applypatch-msg.sample     post-update.sample        prepare-commit-msg.sample
commit-msg.sample         pre-applypatch.sample     update.sample
post-commit.sample        pre-commit.sample
post-receive.sample       pre-rebase.sample
$ 

They are all executable on my system. To make use of one of the hooks, either copy or rename the file removing the '.sample' suffix. Edit it as appropriate to suit your requirements.


Addressing the question in a comment - to change the default sample hooks installed, you need to find the directory where git is installed. On my machine, that is $HOME/git - so the binary is in $HOME/git/bin/git. Then the directory containing the sample hooks is:

$HOME/git/share/git-core/templates/hooks

If you edit those templates (be careful), then that is what will be copied to new git repositories. They'll still be samples, but they'll be your samples.

I've not experimented with creating a non-sample file in the directory; it might or might not be copied. Be wary of changing the defaults, though - when you next upgrade, you'll have to redo the changes.

Jonathan Leffler
This also doesn't answer the question I asked... See clarification in my response to John Feminella's reply, which he responded to.
rplevy
@rplevy: I can see the clarification since I have enough rep to see deleted answers; other people cannot (including you). Edit your question to match. I think my answer does address your question, but I'm biassed. Specifically, one way to change the default templates installed is to edit the files that are copied by 'git init', which is what I said. Brian Campbell also pointed out that there is a way to configure 'git init' to use a non-standard location, but ensuring that the non-standard option is applied could be interesting. You might need to investigate the system configuration.
Jonathan Leffler
Thanks, either I misread your answer at first pass or it was elaborated on since. This does indeed answer my question. Re: "when you next upgrade, you'll have to redo the changes" that depends, I think apt may prompt you. And also the files can be maintained separately. Symbolically linked I suppose would be a good way to do it.
rplevy
@rplevy: OK - there was a small window when the material below the rule was not present; I guess you looked at the answer then, and didn't see the update which was done within 5 minutes of the posting (so it didn't count as an edit of the posting). I don't know whether apt would prompt you or not. Symlinks might help; I wouldn't rely on it without experimentation because I can see a number of ways in which upgrades could clobber your modifications (and I would keep the templates under git control!).
Jonathan Leffler
+2  A: 

From the git-init man page (also works with git-clone if you are cloning an existing repo instead of creating a new one from scratch):

       --template=<template_directory>
           Provide the directory from which templates will be used. The
           default template directory is /usr/share/git-core/templates.

           When specified, <template_directory> is used as the source of the
           template files rather than the default. The template files include
           some directory structure, some suggested "exclude patterns", and
           copies of non-executing "hook" files. The suggested patterns and
           hook files are all modifiable and extensible.

So, you can either modify the system-wide template directory (which defaults to /usr/share/git-core/templates, but may be in a different location on your machine), or you can supply a template directory on the command line when you create or clone the repo.

Brian Campbell
Oh ok, it is possible, great!
rplevy
But the hook files will end up non-executable on fresh clones, which is reassuring to know.
ndim
But, if someone uses their own git installation or clones from a remote machine (say, over ssh) then the hooks won't be copied.
Pat Notz
Right. This is only useful for setting up a default set of hooks for yourself on your own machine. If you want to have everyone on the same team using the same set of hooks, you're going to need a wrapper script or something of the sort.
Brian Campbell