Subject: Re: `git rev-parse --is-inside-work-tree` and $GIT_WORK_TREE

From: John Keeping <[email-masked]>
Date: Tue, 29 Mar 2016 21:52:08 +0100

On Tue, Mar 29, 2016 at 04:34:25PM -0400, Jeff King wrote:
> On Tue, Mar 29, 2016 at 06:42:44AM -0500, Elliott Cable wrote:
> 
> > So, I find this behaviour a little strange; I can't determine if it's
> > a subtle bug, or intentionally undefined/‘fuzzy’ behaviour:
> > 
> >     $ cd a-repo/.git/
> >     $ pwd
> >     /path/to/a-repo/.git
> >     $ git rev-parse --is-inside-work-tree
> >     false
> >     $ export GIT_WORK_TREE=/path/to/a-repo
> >     $ git rev-parse --is-inside-work-tree
> >     true
> > 
> > i.e. when within the repository (the `.git` directory), and when that
> > directory is a sub-directory of the working-tree, `rev-parse
> > --is-inside-work-tree` reports *false* (reasonable enough, I suppose);
> > but then if `$GIT_WORK_TREE` is set to precisely the directory that
> > git was *already* assuming was the working-directory, then the same
> > command, in the same location, reports *true*.
> > 
> > This should probably be made consistent: either `rev-parse
> > --is-inside-work-tree` should report “true”, even inside the `.git`
> > dir, as long as that directory is a sub-directory of the working-tree
> > … or repository-directories / `$GIT_DIR` / `.git` directories should
> > be excluded from truthy responses to `rev-parse
> > --is-inside-work-tree`.
> 
> Yeah, I think this is a bug. Presumably what is happening is that we are
> too eager to "cd $GIT_WORK_TREE" inside git-rev-parse, and by the time
> we ask "are we in a work tree", the answer has become yes. But the
> caller really wants to know "am _I_ inside the work tree".

I don't think that's what's happening.  Try:

	$ cd .git/
	$ GIT_WORK_TREE=.. git rev-parse --is-inside-work-tree
	true

so I think it's that we refuse to assume that the directory above a Git
directory is a working tree (something similar happens when the
"core.worktree" config variable is set).  I'm not convinced that's
unreasonable.

However, the case above also gives:

	$ GIT_WORK_TREE=.. git rev-parse --is-inside-git-dir
	false
	$ test $(pwd) = $(GIT_WORK_TREE=.. git rev-parse --git-dir); echo $?
	0

so even though $PWD *is* the Git directory, we're not in the Git
directory!  Setting GIT_DIR=$(pwd) makes no different to that.