How to setup post-receive-email Git hook with Gitolite

21,544

Solution 1

You can look at the doc hook for starters:

where do I (the admin) put the hooks?

In general, all hooks go into the hooks/common directory. Only the special post-update hook meant for the admin repo goes into hooks/gitolite-admin.

But the GitoliteV3 doc on 'mirroring' provides an alternative to a custom hook.


For the second question:

I would like to avoid editing config file for each repository manually.
It would be much more fun if I could configure everything in the same, centralized, place for whole Gitolite.

The doc gitolite.conf is quite clear:

repo specific git config commands

Sometimes you want to specify git config settings for some of your repos.
For example, you may have a custom post-receive hook that sends an email when a push happens, and this hook needs to know whom to send the email to, etc.

You can set git config values by specifying something like this within a "repo" paragraph:

example usage: if you placed a hook in hooks/common that requires configuration information that is specific to each repo, you could do this:

repo gitolite
    config hooks.mailinglist = [email protected]
    config hooks.emailprefix = "[gitolite] "
    config foo.bar = ""
    config foo.baz =

The syntax is simple:

config sectionname.keyname = [optional value_string]

This does either a plain "git config section.key value" (for the first 3 examples above) or "git config --unset-all section.key" (for the last example).
Other forms (--add, the value_regex, etc) are not supported.

Note: this won't work unless the rc file has the right settings; please see comments around the variable $GL_GITCONFIG_KEYS $GIT_CONFIG_KEYS (now in GitoliteV3 or 'g3') in gitolite rc file for details and security information.

Solution 2

at the moment, this does not work:

repo @all
    config foo.bar = "baz"

I presume you would like it to work, but it's kinda low on my list right now due to other pressures, and the fact that there is a workaround:

@almostall = repo1 repo2 repo3
@almostall = repo4 repo5 repo6 [add as many more as you like]

[... later ...]
repo @almostall
    config foo.bar = "baz"

Hope that helps, and sorry about the oversight on the @all

Solution 3

The gitolite cookbook tells how to configure hooks:

1. Enable local non-core programs in gitolite

Edit the gitolite configuration file (usually ~git/.gitolite.rc) and uncomment the following line:

LOCAL_CODE => "$rc{GL_ADMIN_BASE}/local"

Ensure to read the security warnings.

2. Enable repository specific hooks

Uncommenting the repo-specific-hooks line in the gitolite configuration file.

3. Add the email hook

Put the correspondig post-receive hook executable (I use git-multimail) in your gitolite-admin repository as the file /local/hooks/repo-specific/git-multimail.

Commit and push it.

4. Configure settings for multimail hook

To allow adding config keys via the gitolite config file edit the gitolite config file ~git/.gitolite.rc and update the following line:

GIT_CONFIG_KEYS => ".*"

Ensure to read the security warning. You may want to narrow it down to GIT_CONFIG_KEYS => "multimailhook\..*".

5. Configure the multimail email hook

This is an example configuration of the gitolite.conf file in the gitolite-admin repository:

repo @all
    config multimailhook.environment      = gitolite
    config multimailhook.from             = [email protected]
    config multimailhook.mailinglist      = [email protected]

repo xyz
    option hook.post-receive = git-multimail

I decided to use repo specific hooks and store them in the gitolite-admin repository. Alternatively, you could use global hooks (/local/hooks/common) or store them somewhere else on the gitolite server and point LOCAL_CODE there.

Share:
21,544
Alexander Gladysh
Author by

Alexander Gladysh

:-) mailto: [email protected]

Updated on February 13, 2020

Comments

  • Alexander Gladysh
    Alexander Gladysh over 4 years

    I'm using post-receive-email hook from the Git distribution to send e-mails to certain users when Git repository is updated (hook invoked from post-receive).

    All my repositories were managed manually. Now, I get so many repos and so many users and groups that I have to upgrade to some Git repository management system. I picked Gitolite.

    But I am a bit at loss on how to configure the e-mail notifications.

    Update: I will elaborate the question a bit:

    First question is: Where should I put the hook and should I change it somehow so it would work with Gitolite?

    Second question:

    The standard post-receive-email hook depends on three parameters in *.git/config: hooks.envelopesender, hooks.emailprefix and hooks.mailinglist.

    These parameters are, in general, different for each repository that I move under Gitolite. In practice, they are the same for the same permission groups — users, which have access to the repository, receive notifications, others — not.

    I would like to avoid editing config file for each repository manually. It would be much more fun if I could configure everything in the same, centralized, place for whole Gitolite.

    So, any hints?

  • Alexander Gladysh
    Alexander Gladysh over 13 years
    OK, seems that it should work. Is there a nice way to centralize hook configuration parameters for all repositories (hooks.envelopesender, hooks.emailprefix, hooks.envelopesender)?
  • VonC
    VonC over 13 years
    @Alexander: all hooks are centralized in hooks/common within your local copy of gitolite. Script easy-install will update it to the remote gitolite-admin and copy those hooks to each repos.
  • Alexander Gladysh
    Alexander Gladysh over 13 years
    @VonC: That I understand. But post-receive-email hook has its own configuration parameters (living in *.git/config). I understand that now I have to manually edit config file for each repository. This does not sound like much fun. I would like to do this from the one central place, just like I do with repository permissions.
  • VonC
    VonC over 13 years
    @Alexander: git config`` has three levels of configuration: the global config (in the homedir of gitadmin: ~/.gitconfig) should be a natural spot for... 'global' (as in 'valid for all repos') config
  • Alexander Gladysh
    Alexander Gladysh over 13 years
    @VonC: No, that would not work: these parameters are, obviously, different for each gitolite access group — e-mail notifications are sent only to the participants.
  • VonC
    VonC over 13 years
    @Alexander: right, so that means you need to change the classic email notification hook in order to get the list of participants based on what gitolite has compiled in its own config admin file.
  • Alexander Gladysh
    Alexander Gladysh over 13 years
    @VonC: and that is the main point of my question (sorry for not being clear): how to do this?
  • Alexander Gladysh
    Alexander Gladysh over 13 years
    @VonC: Thank you! Almost there. Is there a way to configure this per access group and not per repository? (If not, I guess, I can always generate gitolite.conf)
  • VonC
    VonC over 13 years
    @Alexander: I didn't se that kind of level for gitconfig parameter.
  • Alexander Gladysh
    Alexander Gladysh over 13 years
    Yes, that would be cool and will make my config much prettier. But this does not solve all my problems, as I would like to be able to auto-configure hooks.mailinglist so it would be composed from e-mails of the users who have access to the each repo.