git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: "Crabtree, Andrew" <andrew.crabtree@hpe.com>
Cc: Junio C Hamano <gitster@pobox.com>,
	"git@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: Inconsistent results from git rev-parse --show-toplevel
Date: Thu, 30 Jan 2020 05:29:33 -0500	[thread overview]
Message-ID: <20200130102933.GE840531@coredump.intra.peff.net> (raw)
In-Reply-To: <TU4PR8401MB1117E8EEB853728B0392D83CF9090@TU4PR8401MB1117.NAMPRD84.PROD.OUTLOOK.COM>

On Sat, Jan 25, 2020 at 10:22:45PM +0000, Crabtree, Andrew wrote:

> > But the bigger thing, I think, is: who is setting GIT_DIR but not setting GIT_WORK_TREE to match? Because IMHO that's the situation that is causing the confusion.
> Pre-commit hook when worktrees are used? 
> 
> pre-commit
> #!/bin/bash
> env | grep GIT
> 
> /tmp/pre_commit_test_worktree  (new_branch)$ git add frob
> /tmp/pre_commit_test_worktree  (new_branch)$ git commit -m "frob"
> GIT_DIR=/tmp/pre_commit_test/.git/worktrees/pre_commit_test_worktree
> GIT_EDITOR=:
> GIT_INDEX_FILE=/tmp/pre_commit_test/.git/worktrees/pre_commit_test_worktree/index
> GIT_PREFIX=
> GIT_AUTHOR_DATE=@1579990789 -0800
> GIT_AUTHOR_NAME=Andrew Crabtree
> GIT_EXEC_PATH=/usr/local/libexec/git-core
> GIT_AUTHOR_EMAIL=andrew.crabtree@hpe.com
> [new_branch 7b1b747] frob
>  1 file changed, 0 insertions(+), 0 deletions(-)
>  create mode 100644 frob

I dug into this a little bit. I think the culprit is actually that our
internal set_git_dir() puts GIT_DIR into the environment, but the
matching setup_work_tree() doesn't touch GIT_WORK_TREE unless it's
already set.

This is mostly fine because we chdir() to the top-level of the working
tree, meaning that any sub-processes would see the correct environment.
But if we execute some arbitrary script (like a hook) that does a chdir,
the results are surprising.

Something like this seems like it would be an improvement:

diff --git a/setup.c b/setup.c
index e2a479a64f..75e2d1393c 100644
--- a/setup.c
+++ b/setup.c
@@ -394,12 +394,7 @@ void setup_work_tree(void)
 	if (!work_tree || chdir_notify(work_tree))
 		die(_("this operation must be run in a work tree"));
 
-	/*
-	 * Make sure subsequent git processes find correct worktree
-	 * if $GIT_WORK_TREE is set relative
-	 */
-	if (getenv(GIT_WORK_TREE_ENVIRONMENT))
-		setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
+	setenv(GIT_WORK_TREE_ENVIRONMENT, work_tree, 1);
 
 	initialized = 1;
 }

but it fails a test in t5601 around git-clone. So there may be some
weird subtle interaction here (or possibly just a bug in git-clone, if
it isn't careful enough to clean its environment when moving into the
newly created repo).

-Peff

  reply	other threads:[~2020-01-30 10:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-24 18:07 Inconsistent results from git rev-parse --show-toplevel Crabtree, Andrew
2020-01-24 19:59 ` Junio C Hamano
2020-01-25 19:31   ` Crabtree, Andrew
2020-01-25 19:53     ` Jeff King
2020-01-25 22:22       ` Crabtree, Andrew
2020-01-30 10:29         ` Jeff King [this message]
2020-01-30 21:59           ` Crabtree, Andrew
2020-02-14  6:36             ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200130102933.GE840531@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=andrew.crabtree@hpe.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).