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

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

From
Duy Nguyen <pclouds@gmail.com>
Date
Mar 30, 2016, 00:53 UTC
Message-ID
<CACsJy8BAW0E36qKjJqvLL0ZHKdh3+7G1axt1jD46Yv3atfL7fw@mail.gmail.com>
In-Reply-To
<20160329203425.GA24027@sigill.intra.peff.net>
On Wed, Mar 30, 2016 at 3:34 AM, Jeff King <peff@peff.net> wrote:
Show 27 quoted lines
> 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`.

No. Once you set GIT_WORK_TREE you tell git that worktree exists. That overrides the "bare repo" attribute (i.e. no worktree) that's automatically set when we try to find .git directory.

> 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".

On relative GIT_WORK_TREE some mail down this thread, there's this note in t1510 that you might find interesting

5. GIT_WORK_TREE/core.worktree was originally meant to work only if
   GIT_DIR is set, but earlier git didn't enforce it, and some scripts
   depend on the implementation that happened to first discover .git by
   going up from the users $cwd and then using the specified working tree
   that may or may not have any relation to where .git was found in.  This
   historical behaviour must be kept.

Basically if you set GIT_WORK_TREE you better set GIT_DIR too to keep things sane.

-- 
Duy
Previous: Junio C HamanoNext: Elliott Cable
Message 15 of 16 in “`git rev-parse --is-inside-work-tree` and $GIT_WORK_TREE”
  1. Elliott CableMar 29, 2016
  2. Elliott CableMar 29, 2016
  3. John KeepingMar 29, 2016
  4. Junio C HamanoMar 29, 2016
  5. Jeff KingMar 29, 2016
  6. Junio C HamanoMar 29, 2016
  7. Jeff KingMar 29, 2016
  8. Jeff KingMar 29, 2016
  9. John KeepingMar 29, 2016
  10. Jeff KingMar 29, 2016
  11. John KeepingMar 29, 2016
  12. John KeepingMar 29, 2016
  13. Jeff KingMar 29, 2016
  14. Junio C HamanoMar 29, 2016
  15. Duy NguyenMar 30, 2016
  16. Elliott CableApr 1, 2016

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.