8

After I push my local git repo to the server:

$ git push origin master

I want the bare repo on the server (after it received the push from me) to:

$ cd /Users/me/Sites
$ git pull
$ exit

I've looked at some questions here and they mention hooks in $HOME/gitolite/hooks,

but here's what I'm working with:

git@mm:gitolite $ pwd
/Users/git/gitolite
git@mm:gitolite $ ls
.git/                 README.txt            install*
CHANGELOG             check-g2-compat*      src/
COPYING               convert-gitosis-conf* t/

How do I add a post-receive hook for a specific bare repo?

If I go into my bare repo:

git@mm:bare-repo.git $ ls hooks
applypatch-msg.sample*     pre-rebase.sample*
commit-msg.sample*         prepare-commit-msg.sample*
post-update.sample*        update@
pre-applypatch.sample*     update.sample*
pre-commit.sample*

I don't see a post-receive hook. What gives?

jshawl
  • 3,315
  • 2
  • 22
  • 34

2 Answers2

7

Update August 2013, with the latest gitolite: You now have official specific repo hook:

it's basically just creating a symlink in <repo.git>/hooks pointing to some file inside $rc{LOCAL_CODE}/hooks/repo-specific (except the gitolite-admin repo)


Original answer (

First, if you are using gitolite V3, you can define any hook, including a post-receive hook (except the update hook, see using hooks) : previously, with gitolite V2, pre-receive hook was reserved.

Now you can add a hook by copying it in the gitolite-admin/common/hooks local clone directory, and pushing gitolite-admin back to the gitolite server: gitolite will make sure that hook is declared for all bare repos it manages.

You can also add directly your hook on the server at a separate location designed by the $LOCAL_CODE "rc" variable ("rc" means defined in your gitolite.rc config file): $LOCAL_CODE/hooks/common. See "customizing gitolite".
The idea is to make sure a gitolite upgrade doesn't erase any of your custom programs.

Simply define a 'post-receive' file, executable (chmod 755), and copy it in the common/hooks directory of your choice (gitolite-admin local repo plus git push, or .gitolite on the server, or $LOCAL_CODE on the server).
Note: that fact that you don't see a 'post-receive.sample' file doesn't prevent you to define that hook.
If done directly on the server, you need then to run gitolite setup --hooks-only in order for your custom hooks to be setup on all bare repos.

What you would never do is to copy it directly on one of your bare-repo.git/hooks directory: that is the job of gitolite to publish "common" hooks to all your bare repo.
That way, you can manage them directly through any clone of gitolite-admin repo (pushing back that repo will update any hook that you might have changed).

"All bare repo" means your post-receive hook must know what bare repo it operates on:
You can do that by checking the value of $GIT_DIR (set to the root .git directory of the bare repo on which this hook is running).

Finally, for this kind of post-receive hook, see "Git checkout in post-receive hook: “Not a git repository '.'":
You need to define GIT_DIR and GIT_WORK_TREE explicitly to the destination repo in order for your git pull to succeed.

 GIT_WORK_TREE=/Users/me/Sites GIT_DIR=/Users/me/Sites/.git git pull
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I tried to setup repo-specific-hooks(v3) using the following: **1** I created `LOCAL_CODE => "$ENV{HOME}/.gitolite/local",` in rc **2** I add `'repo-specific-hooks',` to ENABLE array in rc, **3** I created dir `local/hooks/repo-specific/` in my gitolite-admin, commited and pushed that **4** I add string: `option hook.post-receive = deploy` in conf/gitolite.conf for `testing` repo, created file `local/hooks/repo-specific/deploy` in conf file with 0755, and commited and pushed them... **5** I made commit/push to testing repo, hook was not fired, what I did wrong ? – zb' Sep 27 '13 at 23:14
  • symlink also not here, tried to run `gitolite setup --hooks-only` without any success – zb' Sep 27 '13 at 23:15
  • @eicto I have edited tha answer with a link to repo-specific hook in gitolite: make sure you have the latest gitolite though. – VonC Sep 28 '13 at 08:19
  • just not understand, how can I place \t in commandline here: `gitolite git-config gitolite-options.hook=reponame hookname scriptname` I readed and rereaded docs many times, and still not understand why it just ignores all configs here. Can you give any non abstract sample of configuration, when we have git controlled hook directory(in local), and simple repo-specific hook saying "Hello world" on post-receive ? **gitolite version 3.5.2** – zb' Sep 28 '13 at 09:03
  • 1
    @eicto 3.5.2 is too old to contains repo-specific hook. You need to clone https://github.com/sitaramc/gitolite and update your current gitolite installation. – VonC Sep 28 '13 at 09:07
  • @eicto the installation or update is very easy: http://stackoverflow.com/a/18503812/6309 – VonC Sep 28 '13 at 09:27
0

To get a repo-specific post-commit hook (gitolite v3.6 and above):

  1. Edit gitolite.rc, comment in the line 'repo-specific-hooks' (which should be commented out by default).

  2. Run gitolite setup. It will complain about a directory path that's missing. Go and create it. mkdir -p /git/local/hooks/repo-specific.

  3. Add your post-receive hook to that dir:

    cd /git/local/hooks/repo-specific

    echo "stuff to do" > post-receive

    chmod o+x post-receive

  4. Add the post-receive hook to the desired repo(s) in gitolite-admin/conf/gitolite.conf:

    repo fooBar

    option hook.post-receive = post-receive

  5. Re-run gitolite setup. There shouldn't be any error messages.

  6. Commit something to foorBar and watch the magic happen.

If you want to debug why it's not working, have a look in ~/.gitolite/logs. Also check that your version is above 3.6.

If you want a post-receive action to happen to any repo that is under gitolite control, and you don't care which one, then simply add your post-receive script to ~/.gitolite/hooks/common.

Andy J
  • 1,479
  • 6
  • 23
  • 40