maxmelbin
maxmelbin

Reputation: 2095

git hooks : is there a clone hook?

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?

Upvotes: 43

Views: 31999

Answers (5)

larsks
larsks

Reputation: 311506

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.

Upvotes: 13

ZachB
ZachB

Reputation: 15366

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

Upvotes: 5

kskkskksk
kskkskksk

Reputation: 101

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.

Upvotes: 3

maxmelbin
maxmelbin

Reputation: 2095

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!

Upvotes: 32

ralphtheninja
ralphtheninja

Reputation: 133008

No, there isn't any clone hook.

Upvotes: 10

Related Questions