40

According to the manual, the post-checkout hook is run after a git checkout (just as expected) but also after a git clone (unless you pass --no-checkout).

Very well, now, considering the following:

  • you don't have a local repository before a git clone
  • hooks are not synced between remotes
  • hooks stored in a custom template directory used with --template are copied non-executable and therefore not executed after git clone (that is actually not true as stated by Jefromi in his answer, but the hook is still not executed)

It seems impossible that a post-checkout hook ever gets executed after a git clone. Still, the githooks man page at http://git-scm.com/docs/githooks explicitely states a way to avoid it being executed, and also parameters passed in case it is executed, which would indicate it is possible to execute a custom hook after a git-clone.

So, how is it possible? I am obviously missing something here.

Turns out

Geoffrey Bachelet
  • 4,047
  • 2
  • 21
  • 17

2 Answers2

20

I suppose you could make a custom installation - rename the hooks in .../share/git-core/templates/hooks to remove the .sample suffix.

You could also make a template directory full of symlinks to a hooks directory inside the repository, (e.g. post-checkout -> ../../hooks/post-checkout). Then if the cloned repo contained that particular hook, it'd get executed.

You're right, though, in most cases it will not happen.

Edit: I just tested it, and the --template option does appear to preserve permissions, so that's a much more straight-forward way to make it happen. What'd you see to indicate that it stripped that bit?

The final say on versions: You're looking at documentation online for a newer version of git than you're using. This feature was added in dfa7a6c (clone: run post-checkout hook when checking out); git describe says this wasn't included until v1.6.2-rc2.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    Right, I just tested, and it turns out they are actually copied with the executable bit (not sure why I though it was stripped off, must have read that somewhere on the internet). The hook is still not executed though, it's weird because if I do a `git checkout` right after the `git clone`, it is executed properly. What version of git do you have ? I use 1.6.0.4 here. I've posted the exact procedure used in a gist: http://gist.github.com/287084 – Geoffrey Bachelet Jan 26 '10 at 18:39
  • Also, I just tested putting the hook in `/usr/share/git-core/template` and it produces the exact same result – Geoffrey Bachelet Jan 26 '10 at 18:45
  • 1
    I did a quick test with `clone --template` - an executable post-checkout hook is definitely executed for me. I use the current git.git version (right now, git 1.7.0.rc0.8.ge3f67d). I'm not the only one on my current system, so trying it with older versions would have to wait until later. – Cascabel Jan 26 '10 at 19:43
  • Ok, it seems obvious now that I should upgrade my installation of Git! I'm going to try that tomorrow. Thank you all for your answers and time :-) – Geoffrey Bachelet Jan 26 '10 at 23:47
  • so when we clone a repo we have to specify the templates path , is their no way to update the --template after we have pulled the repo. – Ciasto piekarz Aug 13 '15 at 09:44
  • Is this answer still valid today? I'm don't get it. I do not see .../share/git-core/templates/hooks anywhere on my system.I am on Windows 10. I too have a similar requirement, where I want to symlink git hooks after the clone is successfully completed. I tried creating post-checkout hook in mylocalrepo/hooks directory. I want to create symlink of this hook under/.git/hooks after clone .But how will git get to know about the existence of the original post-checkout hook under mylocalrepo/hooks directory when no symlink relation exists in the first place, when we clone the repo. – Asif Kamran Malick Mar 23 '20 at 01:35
  • Firstly, is there a way I can implement a hook that would trigger after the clone. Secondly, if at all there is a post-clone-like-hook, then how does one go about making git aware of it, as hooks are not inherited during clone,and a cloned repo has only sample hooks, and for custom org-enforced hooks(hooks already hosted on github repo under a different directory) to work, we need to get the symlink relation established first. Creating symlinks can be done manually by each developer, but is there any way to automate this as well so it gets executed after the clone is successful. – Asif Kamran Malick Mar 23 '20 at 01:47
  • 1
    @AsifKamranMalick You can write an installation script so that people only have to run a single one-time command, but in general, no, a post-clone hook does not exist and I wouldn't expect it to. Imagine if someone pushed malicious code into a public repo, and simply cloning it caused that code to run. – Cascabel Mar 23 '20 at 18:20
  • @Cascabel Thanks. That makes sense. And I had the script thing in my mind,but was just curious if there existed a way that could automatically do sth post-clone. But how do organization's deal with such requirements. Is having a script and then asking developers to run them the only way. Just curious. – Asif Kamran Malick Mar 23 '20 at 19:47
  • @AsifKamranMalick, you can use the `post-checkout` for triggering some action after you clone a repository. There are exceptions, but it is possible. – SgtPooki Dec 19 '20 at 04:49
15

From the githooks documentation:

When git-init is run, a handful of example hooks are copied into the hooks directory of the new repository, but by default they are all disabled. To enable a hook, rename it by removing its .sample suffix.

This initialization takes place as part of creating a clone—note the call to init_db in builtin-clone.c.

For example:

$ cat /tmp/my-git-template/hooks/post-checkout 
#! /bin/bash
echo "Hello from $0"

$ git clone --template=/tmp/my-git-template file:///tmp/repo.git my-clone
Initialized empty Git repository in /tmp/my-clone/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Hello from .git/hooks/post-checkout
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • 2
    The documentation here is more talking about enabling it within your own repository, and the OP is obviously aware of this. Of course, you can remove the suffix from the templates in the installation directory (which is exactly what I already posted). – Cascabel Jan 26 '10 at 18:33
  • So it works properly with yours too - either the OP's made a silly mistake in testing, or a bug has been fixed since 1.6.0 - since you link to the source, I suspect you're using a pretty new version too? – Cascabel Jan 26 '10 at 19:45
  • I'm running 1.6.5.5. I have access to another box with 1.5.5.6 which copies the hook and retains the execute bits but does not seem to execute it. – Greg Bacon Jan 26 '10 at 20:10