41

We want to store some meta-information about the commit in an external database. During a clone or a checkout, this database should be referred and we copy the meta information to a file in the repo which is cloned. The database is required rather than just using a file is for the sake of indexing and searches etc ...

I thought if there is a clone hook, we could trigger this. I couldn't find the clone hooks in the sample in .git/hooks. is there one? is post-checkout hook the only possibility at client side?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
maxmelbin
  • 2,045
  • 3
  • 21
  • 29

5 Answers5

32

ok, one way to do this is to use the clone --template option.

Specify the location where the client side hooks will be stored as value to the --template switch. The hooks are copied to the clone, and the post-checkout hook is fired immediately!

CharlesB
  • 86,532
  • 28
  • 194
  • 218
maxmelbin
  • 2,045
  • 3
  • 21
  • 29
  • 9
    But the template directory cannot be inside the repository you are cloning - it must already exist on the filesystem before you clone. – chrishiestand Mar 28 '13 at 19:45
  • 1
    http://stackoverflow.com/a/2141577/1183537 helped me a lot, more clarification, and addresses what you said @chrishiestand – mswieboda Jan 23 '14 at 18:11
13

When you clone a remote repository, you can't run any client-side hooks because hooks are local to your working copy, and you're creating one from scratch. When you pull new changes from a remote repository, git will run your local post-merge hook if it exists.

There is nothing run on the server as the result of a pull operation. A push operation will trigger the servers's update and post-update hooks.

See the Git Book for more information.

larsks
  • 277,717
  • 41
  • 399
  • 399
10

No, there isn't any clone hook.

rtn
  • 127,556
  • 20
  • 111
  • 121
4

Since git version 1.6.3, the post-checkout hook runs on git-clone (when run without --no-checkout).

It is also run after git-clone[1], unless the --no-checkout (-n) option is used. The first parameter given to the hook is the null-ref, the second the ref of the new HEAD and the flag is always 1.

https://git-scm.com/docs/githooks#_post_checkout

ZachB
  • 13,051
  • 4
  • 61
  • 89
2

I'm late but there is a workaround:

  1. Capture git clone execution and set a variable using trap set_var_on_git_clone DEBUG where set_var_on_git_clone is a user-defined function. (e.g. https://gist.github.com/KeyAmam/a6afcabc3a724fc4a541aca7629c3ff6)

  2. Check the variable in post-checkout script and do some stuff in the case. Clear the variable at the end of the script. (e.g. https://gist.github.com/KeyAmam/6a0e8805c0b6a662adb6bcf8118a089a)

This only works in Bash but you can do a similar thing in other shells.

kskkskksk
  • 91
  • 2
  • 6
  • Nice - I can add this to our machine setup Ansible script, and it will be easy enough to get it to detect and source .../.git_clone_hook, so the hook's under source control like .gitignore. – Tim Baverstock Sep 30 '22 at 09:02