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

Re: [PATCH v1] worktree: set worktree environment in post-checkout hook

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Feb 12, 2018, 03:27 UTC
Message-ID
<CAPig+cQ7MRQVT-1yNF8t6RMHNrgMtYvzOToamaLO==ymxTtLYg@mail.gmail.com>
In-Reply-To
<20180210010132.33629-1-lars.schneider@autodesk.com>
On Fri, Feb 9, 2018 at 8:01 PM,  <lars.schneider@autodesk.com> wrote:
Show 14 quoted lines
> In ade546be47 (worktree: invoke post-checkout hook (unless
> --no-checkout), 2017-12-07) we taught Git to run the post-checkout hook
> in worktrees. Unfortunately, the environment of the hook was not made
> aware of the worktree. Consequently, a 'git rev-parse --show-toplevel'
> call in the post-checkout hook would return a wrong result.
>
> Fix this by setting the 'GIT_WORK_TREE' environment variable to make
> Git calls within the post-checkout hook aware of the worktree.
>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> ---
> I think this is a bug in Git 2.16. We noticed it because it caused a
> problem in Git LFS [1]. The modified test case fails with Git 2.16 and
> succeeds with this patch.
Thanks for reporting and diagnosing the problem.

I have some concerns about this patch's fix of setting GIT_WORK_TREE unconditionally. In particular, such unconditional setting of GIT_WORK_TREE might cause unforeseen problems. Although the circumstances may not be quite the same, but the tale told by 86d26f240f (setup.c: re-fix d95138e (setup: set env $GIT_WORK_TREE when .., 2015-12-20) makes me cautious.

More significantly, though, setting GIT_WORK_TREE seems too specialized a solution. While it may "fix" Git commands invoked by the hook, it does nothing for other commands ('cp', 'mv', etc.) which the hook may employ.

As a review comment, I was going to suggest that you chdir() to the new worktree directory instead of messing with GIT_WORK_TREE, but when I tested it myself before making the suggestion, I discovered that the issue is a bit more involved. The result is that I ended up posting a patch series[1] to replace this one, with what I believe is a more correct fix.

[1]: https://public-inbox.org/git/20180212031526.40039-1-sunshine@sunshineco.com/
Previous: Junio C Hamano
Message 24 of 24 in “worktree: set worktree environment in post-checkout hook”
  1. worktree: set worktree environment in post-checkout hooklars.schneider@autodesk.com, Feb 10, 2018
  2. Lars SchneiderFeb 10, 2018
  3. 0/2 worktree: change to new worktree dir before running hook(s)Eric Sunshine, Feb 12, 2018
  4. 2/2 worktree: add: change to new worktree directory before running hookEric Sunshine, Feb 12, 2018
  5. Junio C HamanoFeb 12, 2018
  6. Eric SunshineFeb 12, 2018
  7. Lars SchneiderFeb 12, 2018
  8. Eric SunshineFeb 13, 2018
  9. Eric SunshineFeb 13, 2018
  10. Johannes SixtFeb 13, 2018
  11. Eric SunshineFeb 13, 2018
  12. 1/2 run-command: teach 'run_hook' about alternate worktreesEric Sunshine, Feb 12, 2018
  13. Lars SchneiderFeb 12, 2018
  14. Eric SunshineFeb 12, 2018
  15. worktree: add: fix 'post-checkout' not knowing new worktree locationEric Sunshine, Feb 15, 2018
  16. Junio C HamanoFeb 15, 2018
  17. Eric SunshineFeb 15, 2018
  18. Junio C HamanoFeb 15, 2018
  19. Eric SunshineFeb 15, 2018
  20. worktree: add: fix 'post-checkout' not knowing new worktree locationEric Sunshine, Feb 15, 2018
  21. Lars SchneiderFeb 16, 2018
  22. Eric SunshineFeb 16, 2018
  23. Junio C HamanoFeb 16, 2018
  24. Eric SunshineFeb 12, 2018

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.