git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Unexpected working directory in post-receive hook in non-bare repository

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 10, 2017, 11:13 UTC
Message-ID
<CACBZZX4uBL99y=ZaKZ7dqyP9Ne-cx=kYkh8p51p3VYOr3PQGSw@mail.gmail.com>
In-Reply-To
<20170409130126.uqmjop25jidhblhd@ruderich.org>
On Sun, Apr 9, 2017 at 3:01 PM, Simon Ruderich <simon@ruderich.org> wrote:
Show 40 quoted lines
> The following snippet reproduces the issue for me (note the
> remote: line in its output):
>
>     git --version
>
>     rm -rf a b
>
>     git init a
>     cd a
>     echo first >data
>     git add data
>     git commit -m initial
>     cat >>.git/hooks/post-receive <<EOF
>     #!/bin/sh
>     pwd
>     EOF
>     chmod +x .git/hooks/post-receive
>     cd ..
>
>     git clone a b
>     cd b
>     echo second >>data
>     git add data
>     git commit -m test
>     git push origin master:not-master
>
> According to man githooks "Before Git invokes a hook, it changes
> its working directory to either the root of the working tree in a
> non-bare repository, [...]". In this case "a" is non-bare and I
> expected the command to be run in the working tree; but instead
> it's run inside .git. (This caused some confusion in my case
> because I ran "git merge" in the hook which put files in the .git
> directory and I didn't notice it at first. I know running merge
> in receive-hooks is "bad practice" but it works fine in my
> setup.)
>
> The same happens for all hooks executed by git-receive-pack:
> pre-receive, update, post-receive, post-update.
>
> Is this a documentation issue or unexpected behavior?

It's a documentation issue I think. I added this change to the githooks manpage last year in 49fa52fd00, but didn't think about the case of pushing into non-bare repositories. The behavior itself hasn't changed in a long time.

I wonder how to phrase this so that it's unambiguous & simply states a general rule. I.e. instead of:

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

Can we say as we do now that:
* All hooks regardless of type in bare repos execute in the bare repo
* If you have a working tree hooks use that
But add:
* Working trees are ignored by any hooks invoked on your behalf during a push.

Some ad-hoc testing reveals that this rule also goes for the push-to-checkout hook. Should it? Wouldn't it be more useful if it broke the pattern, since it's dealing with the working tree on the other side? Junio?

Previous: Simon RuderichNext: Simon Ruderich
Message 2 of 4 in “Unexpected working directory in post-receive hook in non-bare repository”
  1. Simon RuderichApr 9, 2017
  2. Ævar Arnfjörð BjarmasonApr 10, 2017
  3. githooks.txt: clarify push hooks are always executed in $GIT_DIRSimon Ruderich, Apr 29, 2017
  4. Ævar Arnfjörð BjarmasonApr 29, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.