Hook git command into visual studio pre build step

I’m using git as version control and I’m trying to hook the SHA1 of the current commit into a visual studio build as a define.

So basically I want to turn the output of the command

git rev-parse HEAD

into a define that I can embed in the output binary.

Thanks in advance.

  • Change remote HEAD branch in git hosted on visual studio online
  • Is there any way to get my changes back after I have done “Undo Pending changes”
  • git sub modules visual studio 2017 not downloading
  • Visual Studio ignores *.jfm entry in the gitIgnore file
  • How can I use Git locally in an SVN+Visual Studio environment
  • Upload a project to github C# Visual Studio
  • GitHub Enterprise vs. Team Foundation Server (TFS)
  • How to connect github with visual studio code?
  • 2 Solutions collect form web for “Hook git command into visual studio pre build step”

    Using a post-commit githook, you can put it into a file. I think git on windows comes with a bourne shell, so the following would work in a post-commit hook.

    echo -n '#define GIT_SHA ' > $GIT_SHA_INCLUDE_FILE
    git rev-parse HEAD >> $GIT_SHA_INCLUDE_FILE

    And save that as .git/hooks/post-commit

    The file should remain untracked since it’s (nearly) impossible to include the hash of the commit within the commit itself.

    Frankly, I prefer a pre-build events in Visual Studio over Git post-commit. There is additional benefit of knowing if you building from a dirty repository or not. Did the write-up on that today. Here are the general steps from build to execution:

    The build process has a pre-build script, which gathers version and repository status information. In our case, VC++ uses the Windows PowerShell version_info.ps1 script.

    The pre-build script generates a source code file, which is expected by the rest of the codebase. The generated file has everything needed to allow or deny running the version. In our case it is a C++ header file version.h.

    A code contains function which checks if the build is legitimate to run, and logs and stops the process as needed. That example code is in the versionLogAndVet function of the version.cpp example file.

    At run time, the versionLogAndVet function allows to run only permitted combinations of repository status / build configuration. It also logs version information.

    The specifics and code examples are at the full post.

    Git Baby is a git and github fan, let's start git clone.