Why does git-bisect have to be run from the top level directory of the working tree?

Parker Coates picture Parker Coates · Sep 18, 2012 · Viewed 9.5k times · Source

If one tries to run any of the git-bisect commands from anywhere other than the root directory of the repository, one is told:

You need to run this command from the toplevel of the working tree.

Why is that? I know of no other git command that has this requirement, and I see no obvious reason that bisect should be special. The man page makes no mention of this restriction, either.

It's really not a big deal. I'm mostly just curious.

Answer

willoller picture willoller · Sep 22, 2012

Looking at some commits in the project, I see one by Marcel M. Cary ([email protected])

He says in a commit (it happens to be about git-pull but I think it is relevant)

"git pull" fails because POSIX shells have a notion of current working directory that is different from getcwd(). The shell stores this path in PWD. As a result, "cd ../" can be interpreted differently in a shell script than chdir("../") in a C program. The shell interprets "../" by essentially stripping the last textual path component from PWD, whereas C chdir() follows the ".." link in the current directory on the filesystem. When PWD is a symlink, these are different destinations. As a result, Git's C commands find the correct top-level working tree, and shell scripts do not.

https://github.com/git/git/commit/08fc0608657ee91bc85276667804c36a93138c7d

SO I'd say part of the reason is because git-bisect is a shell script which can't be trusted to find the toplevel on its own (when symlinks are involved).