Gabriel
Gabriel

Reputation: 9432

Why does Git execute hooks from an other repository?

With this MWE Git executes hooks in a the wrong repository (A):

# Make simple repo
git init "/tmp/A" && cd "/tmp/A" || exit 1

# Make hook to demonstrate the problem
mkdir -p ".git/hooks" &&
    echo "echo 'EXECUTED!!!'" >.git/hooks/reference-transaction &&
    chmod +x ".git/hooks/reference-transaction" &&
    git clone -c core.hooksPath=".git/hooks" --template= "https://github.com/gabyx/Githooks.git" /tmp/Githooks

Results in

Initialized empty Git repository in /home/test/tmp/A/.git/
Cloning into 'Githooks'...
EXECUTED!!!
EXECUTED!!!
EXECUTED!!!
EXECUTED!!!

Which is totally weird: I expect git clone to execute the hook in /tmp/Githooks/.git/hooks because from

core.hooksPath

The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run (see the "DESCRIPTION" section of githooks[5]).

and further in Doc :

Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository.

Why is this, and is this a Bug which should be reported?

Upvotes: 3

Views: 101

Answers (1)

torek
torek

Reputation: 488183

I think this is a bug.

The git clone command is kind of a special case, because it makes a new Git repository. Subsequent operations should happen in this new repository. Relative paths should probably be relative to the new repository. However, there should be no hooks there unless the template provides them.

(It's not clear to me that a template can be a Git repository: the template should be a local file tree.)

As a general rule, it's unwise to run any hook that has not had a chance to be verified first.

This is one of those cases that will generate arguments.

Upvotes: 2

Related Questions