{"thread":{"id":"55210","subject":"bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior","startedAt":"2021-02-25T15:03:35Z","lastAt":"2021-02-25T18:32:17Z","messageCount":2,"participants":["Martin Nicolay","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"417819","messageId":"alpine.LSU.2.20.2102251549330.20742@cpza.bfz-tzou.qr","threadId":"55210","inReplyTo":null,"subject":"bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior","fromName":"Martin Nicolay","fromEmail":"m.nicolay@osm-ag.de","sentAt":"2021-02-25T14:54:27Z","receivedAt":"2021-02-25T15:03:35Z","isPatch":false,"sender":{"key":"m.nicolay@osm-ag.de","avatar":null},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n\n$ env | grep GIT\n$ git --version\ngit version 2.30.1\n$ git init t\nInitialized empty Git repository in /tmp/t/.git/\n$ mkdir t/foo\n$ cd t/foo\n$ git rev-parse --show-toplevel\n/tmp/t\n$ GIT_DIR=$(git rev-parse --git-dir) git rev-parse --show-toplevel\n/tmp/t/foo\n$ git rev-parse --git-dir\n/tmp/t/.git\n\nWhat did you expect to happen? (Expected behavior)\n\n\"git rev-parse --show-toplevel\" should be the same in both invocation.\n\nWhat happened instead? (Actual behavior)\n\n\"git rev-parse --show-toplevel\" shows the current working directory if GIT_DIR is set.\n\nWhat's different between what you expected and what actually happened?\n\nSetting GIT_DIR to $(git rev-parse --git-dir) should not change anything.\n\nAnything else you want to add:\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.30.1\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 5.3.18-22-default #1 SMP Wed Jun 3 12:16:43 UTC 2020 (720aeba) x86_64\ncompiler info: gnuc: 9.1\nlibc info: glibc: 2.26\n$SHELL (typically, interactive shell): /opt/local/bin/bash\n\n\n[Enabled Hooks]\n"},{"id":"417832","messageId":"xmqqtuq0uhkg.fsf@gitster.g","threadId":"55210","inReplyTo":"alpine.LSU.2.20.2102251549330.20742@cpza.bfz-tzou.qr","subject":"Re: bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-02-25T18:29:35Z","receivedAt":"2021-02-25T18:32:17Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Martin Nicolay <m.nicolay@osm-ag.de> writes:\n\n> What did you do before the bug happened? (Steps to reproduce your issue)\n\nThanks for concise and clear descriptions for us to learn everything\nyou did.  I wish all the reports were written like this one.\n\n> $ env | grep GIT\n> $ git --version\n> git version 2.30.1\n\nShowed there is no funny environment variable involved and the\nversion.  Good.\n\n> $ git init t\n> Initialized empty Git repository in /tmp/t/.git/\n> $ mkdir t/foo\n> $ cd t/foo\n> $ git rev-parse --show-toplevel\n> /tmp/t\n\nThe root of the working tree is at /tmp/t/, the repository at\n/tmp/t/.git/, and you are in the foo/ subdirectory.  When you ask\n\"where is the top level of the working tree in this state, because\nyou do not have GIT_DIR or GIT_WORK_TREE environment variables, you\nare asking Git to \"discover\" both the .git/ directory and the top of\nthe working tree, by starting at the current directory,\ni.e. /tmp/t/foo, which is where you are.\n\nFirst Git looks to see /tmp/t/foo/.git/ exists and is a repository;\nit is not.  So it goes one level up and does so for /tmp/t/.git/ and\nit finds that it is the repository.  The directory in which the\nrepository was discovered is the top of the working tree, hence you\nget /tmp/t back.\n\n> $ GIT_DIR=$(git rev-parse --git-dir) git rev-parse --show-toplevel\n> /tmp/t/foo\n\nWhen you give the GIT_DIR environment variable, you are telling Git\nnot to perform the repository discovery, AND also you are telling\nGit that you are at the top-level of the working tree.  So this is\nquite expected (it is a feature that is used by folks who want to\nhave their repository data in a distant and unrelated directory from\ntheir working tree).\n\nIn other words, this is 100% expected behaviour.\n\nIf you want to also tell Git where the top-level of your working\ntree is, you can export GIT_WORK_TREE at the same time.\n\n    Side note: the latter environment variable was invented for this\n    exact reason.  Back when only GIT_DIR existed as a way to tell\n    Git where the distant repository is, those who wanted to use the\n    \"split\" layout had to stay at the top-level of their working\n    tree, and they got tired of not being able to work from a\n    subdirectory.  GIT_WORK_TREE was introduced to tell where the\n    top-level is separately from GIT_DIR when GIT_DIR environment\n    variable is in use.\n\n> $ git rev-parse --git-dir\n> /tmp/t/.git\n"}]}