Web lists-archives.com

Re: Running 'git worktree add' in 'pre-commit' hook




On Thu, May 16, 2019 at 7:25 AM Duy Nguyen <pclouds@xxxxxxxxx> wrote:
> pre-commit hook sets GIT_INDEX_FILE to this "index.lock" so you have
> the latest index content (which is not the same as from
> $GIT_DIR/index). This variable will interfere with any commands that
> work on a different worktree.
>
> So you probably can still make it work by backing up $GIT_INDEX_FILE
> (in case you need it), then unset it before you use "git worktree" (or
> cd to it if you keep a permanent separate worktree for pre-commit
> activities). To make sure you don't have similar problems, you
> probably should do "env | grep GIT" from the hook and see if any other
> variables are set.

I researched this also and concluded that it's a bug in git-commit.
You run afoul of it in other situations, as well. For instance, say
you have your index file in a non-standard location:

    $ export GIT_INDEX_FILE=../storage/index
    $ git worktree add --detach other
    Preparing worktree (detached HEAD c9156d2)
    fatal: Unable to create '/.../foo/other/../storage/index.lock': No
such file or directory
    $

I think the correct fix is for git-commit to assign GIT_INDEX_FILE
with the absolute path to the index file, not a relative path.