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

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

From
Jeff King <peff@peff.net>
Date
May 17, 2019, 00:19 UTC
Message-ID
<20190517001926.GA13525@sigill.intra.peff.net>
In-Reply-To
<CAPig+cQQ5svrDKRPenL2+bJHGjddUPnAXCFSsQN+WBHqzGpDow@mail.gmail.com>
On Thu, May 16, 2019 at 07:16:54PM -0400, Eric Sunshine wrote:
Show 23 quoted lines
> On Thu, May 16, 2019 at 6:17 PM Jeff King <peff@peff.net> wrote:
> > On Thu, May 16, 2019 at 06:25:24PM +0700, Duy Nguyen wrote:
> > > 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). [...]
> >
> > The case of actually _adding_ a new work tree (before we enter it) is
> > weirder, though. We definitely want to stay in the same repository, and
> > clearing all of that would not make sense. I do wonder if worktree-add
> > should be handling GIT_INDEX_FILE (ignoring it when we want to be
> > dealing with the index of the new worktree we added, and handling any
> > relative fixups if we chdir inside the worktree code).
> 
> Ignoring GIT_INDEX_FILE was indeed the conclusion reached earlier in
> this thread. Addressing your other point, "git worktree add" does
> chdir() into the new worktree if a post-checkout hook exists since
> that hook needs to run in the new worktree, not in the worktree in
> which the "git worktree add" command itself was invoked. For the hook
> invocation, it already sanitizes the environment of GIT_DIR and
> GIT_WORK_TREE, and GIT_INDEX_FILE ought to be cleaned too. Is there
> any existing code in Git for doing the relative fixups you mention for
> other Git environment variables?

You can assign local_repo_env to child_process.env (or push it individually to env_array if you have to mix with other variables). See git_connect() for an example.

-Peff
Previous: Eric SunshineNext: Eric Sunshine
Message 15 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.