When searching for Git version control information, libBasic's CMake
checks for the path '.git/logs/HEAD'. However, when LLVM is included as
a Git submodule, this path does not exist. Instead, it contains a '.git'
file with the following:
gitdir: ../../.git/modules/external/llvm
Where '../..' is the relative path to the root repository that contains
the LLVM Git submodule.
Because of this discrepancy, clang --version does not output source
control information if built from a Git submodule.
To fix, check whether '.git' is a directory or a file. If it's a
directory, simply use the '.git/logs/HEAD' path. If it's a file, read it
and check for the tell-tale sign of a Git submodule: the text "gitdir:".
If it exists, follow that path and use the 'logs/HEAD' at that location
instead. This allows not only the correct revision information to be
retrieved, but also uses a file that will change with each source
control revision.
Test Plan:
- Before applying this change, build Clang as a Git submodule in a repository that places it in external/clang, and confirm no revision information is output when clang --version is invoked (just "clang 5.0.0" is output, no Git hashes).
- Apply these changes and build Clang as a Git repository nested under llvm/tools/clang, and confirm that clang --version displays correct version information.
- Apply these changes and build Clang as a Git submodule using the structure described in (1), and confirm version control information is output as in (2).
How about
and then no stripping or replacing later at all? This admittedly doesn't handle some future submodule format that doesn't put gitdir on the first line, but neither does the code that's there.
(This also doesn't handle absolute path submodule references, but looking around a bit makes it sound like those aren't supposed to occur in practice and are considered bugs when git does generate them. I could be wrong about that though.)