Thanks, Konstantin,

I'll give your suggestion a try. One followup question, though: Is it
possible for me to distinguish between a "bare repository" and the
"root of a working tree"?

Jochen

On Wed, Jul 11, 2018 at 2:44 PM Konstantin Khomoutov <[email protected]> wrote:
>
> On Wed, Jul 11, 2018 at 04:37:25AM -0700, Jochen Wiedmann wrote:
>
> > I have studied a lot of Git hooks, both simple ones (like the ones, that
> > can be found in the "hooks" directory), as well as more complex ones (like
> > those running checkstyle), but one question remains unanswered:
> >
> > Assuming, my git hooks directory is "<PROJECT_DIR/.git/hooks>": Can I
> > simply use "../../relative_path_to_file" to access a file in my project
> > directory? (The intention being to validate the projects contents in a
> > pre-commit, or pre-push hook.)
>
> Depends on the kind of hook.
>
> Some hooks are defined to run in the work tree of a non-bare repository.
> Namely, hooks which have "commit", "rebase", "patch" etc in their names
> are of this kind.
>
> Still, you should not assume where your Git worktree is located the way
> you supposed: Git does some setup before calling its hooks - to cite the
> githooks(5) manual:
>
> | 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.
>
> So, when a work-tree related hook runs, the current directory of the
> process executing the hook will be the work tree already.
>
>
> More hard-core hooks — such as "[pre-]receive", "[pre-|post-]update" etc
> typically run in a server-side bare repository which does not have a
> work tree attached.  For this kind of hooks, inspecting individual files
> is still possible but requires usage of low-level (so-called "plumbing")
> Git commands which are able to access the object database of a
> repository directly.  If you need this, ask a more precise question, and
> we will try to help.
>
> --
> You received this message because you are subscribed to a topic in the Google 
> Groups "Git for human beings" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/git-users/3sHmGC0IpC8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to