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

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

From
Eric Sunshine <sunshine@sunshineco.com>
Date
May 16, 2019, 11:59 UTC
Message-ID
<CAPig+cQ5T1yj0t1gehXYKN7=rEJ15feOUEJEtAVK565=VwdUrg@mail.gmail.com>
In-Reply-To
<CACsJy8DnkjuZD-9pbhAsFo16jHKt8U831LLxb3-nCQP5_FOmtA@mail.gmail.com>
On Thu, May 16, 2019 at 7:25 AM Duy Nguyen <pclouds@gmail.com> 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.

I think that this is not quite accurate. The problem isn't that git-commit is pointing GIT_INDEX_FILE at a temporary index; that works fine. The problem is when it is using the normal .git/index file, in which case the value it assigns to GIT_INDEX_FILE is relative.

For instance, with pre-commit hook:
    #!/bin/sh
    echo "GIT_INDEX_FILE=$GIT_INDEX_FILE"
    git worktree add --detach other

"git-worktree add" with a normal git-commit breaks due to the relative location specified by GIT_INDEX_FILE:

    $ git add file
    $ git commit -m foo
    GIT_INDEX_FILE=.git/index
    Preparing worktree (detached HEAD 0857fef)
    fatal: Unable to create '/.../other/.git/index.lock': Not a directory
    $

whereas, the "git commit <file>" form succeeds just fine because it assigns an absolute path for the temporary index:

    $ git commit -m foo file
    GIT_INDEX_FILE=/.../.git/next-index-23675.lock
    Preparing worktree (detached HEAD 0857fef)
    HEAD is now at 0857fef first
    [master 2fa5413] foo
    $
Previous: Duy NguyenNext: Jeff King
Message 12 of 17 in “Running 'git worktree add' in 'pre-commit' hook”
  1. Cosmin PolifronieMay 14, 2019
  2. Bryan TurnerMay 15, 2019
  3. Cosmin PolifronieMay 15, 2019
  4. Duy NguyenMay 16, 2019
  5. Eric SunshineMay 16, 2019
  6. Eric SunshineMay 16, 2019
  7. Duy NguyenMay 16, 2019
  8. Eric SunshineMay 16, 2019
  9. Duy NguyenMay 16, 2019
  10. Eric SunshineMay 16, 2019
  11. Duy NguyenMay 16, 2019
  12. Eric SunshineMay 16, 2019
  13. Jeff KingMay 16, 2019
  14. Eric SunshineMay 16, 2019
  15. Jeff KingMay 17, 2019
  16. Eric SunshineMay 17, 2019
  17. Jeff KingMay 17, 2019

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.