How to trace through FetchContent code?

I am debugging an issue where FetchContent instead of checking out a requested branch leaves a repository in a detached HEAD state (where HEAD is pointing to the requested branch though).

To debug this, I am looking at the CMake code FetchContent is executing while performing the update step, and from what I can tell it should do the right thing. I would love to be able to trace through this code, but running my CMake command with --trace does not trace through the CMake code of FetchContent unfortunately.

Is it possible to trace the CMake code that is executed by FetchContent?

For now I found a crude work-around by adding debug prints to:

“…/Modules/ExternalProject/gitupdate.cmake.in”

relative to the CMake installation share directory.

To answer the problem I encountered; FetchContent defaults to checkout <remote>/GIT_TAG it seems like, causing the local repository to end up in a detached HEAD state (even in case the local branch points to the same commit). I am still researching whether there is any way to avoid that.