I generate a Visual Studio project out of tree in some build directory from some source directory. The project uses Git so the source directory has a .git directory.
If I open the resultant Visual Studio solution it does not think the project is using Git. This is because it expects a .git directory in the build tree.
At present I have some generate time CMakeLists.txt stuff that checks the source tree for a .git directory and does a mklink in the build tree to create a symlink from the build directory to the source.git directory and things work fine when I open Visual Studio.
I am wondering if there is a better way to do this. Maybe built into CMake or there is some stuff I can get output into the generated MSBuildwhich can tell Visual Studio where the .git directory is.
There may be an issue for other generators but I’m only interested in Visual Studio.
It sounds odd that Visual Studio requires for the project to be rooted next to .git, since it’s almost never the case - even projects that maintain VS projects directly, without cmake, are typically having it in some subfolder, either because Windows is not the only available platform or because they need multiple projects (e.g. different ones for different VS versions).
And it’s very simple to to check whether folder is part of git repository - almost any git command can do that or git rev-parse --show-toplevel can show the root folder. So there must be some setting in Visual Studio that allows to search for git repository not just at the solution root.
Following up on @Andrej730‘s suggestions/questions. Is your build folder in the repository? For example, if you used Windows CMD shell to navigate to be in the folder that your .sln file lives and invoked the command git status, what happens?
If it says it’s not inside a .git repository then that’s your problem, you would need to move the build folder to be outside the source but still under root folder for the repository.
In my experience, the build folder does not need to be managed by the repository but it needs to be under repository’s root folder. You can use the .gitignore file to instruct git to ignore everything under the build folder so it does not appear in your git stats, git diffgit add -u, etc. commands.