0

I know there are already resources of printing or returning a git commit hash in VS/C++ with the use of some kind of make based files and some script setup.

But I was wondering if there is a direct way of getting the hash number from git command to the C++ based codes in Visual Studio 2015?

I have created a header file which should return the string of git commit number from the CMakeLists.txt and that function from the header file is being called in a GUI .cpp file labeled as the version name.

But so far this approach is not working. And I was also thinking it is too much work to create a CMake file and a bash setup just to print a git hash.

Let me know if there are better ways to do it in visual studio 2015 in c++.

Thanks for the help.

Also I am trying to return the function value in the following way, which I am not sure if wrong or not.

***std::string functionname()
{ return "@value@";
}

CodeNoob
  • 3
  • 5
  • *I was also thinking it is too much work to create a CMake file and a bash setup just to print a git hash.* Why is that too much work? It would take fewer than five minutes. – torek Jun 09 '22 at 00:28
  • well, I did create a CMake file but why that is not working is a different question. I asked if there are other methods without a CMake for this? Thanks. – CodeNoob Jun 09 '22 at 02:56
  • Since the hash ID of the commit depends on the current commit (not on the file itself) you'll need to run some command to *get* the current commit. You'll then want to put this into a file, but you will want to be sure *not* to commit that file, as that will give you a new, different commit hash ID, requiring you to update the file and commit, which will get you a new, different commit hash ID, etc. So overall there's not a good way to put the right hash ID into a (committed) file. That's why we don't do that. – torek Jun 09 '22 at 05:34
  • You don't have to use CMake, but you do want to *dynamically* run `git describe` or similar to produce the appropriate identifier, at compile time, and feed it to the compilation command, perhaps as a `-D` macro or some such, or as a non-committed file you `#include`. That requires some kind of dynamic process: usually a simple one-line shell command, which is usually most easily embedded into a build system like make or CMake. – torek Jun 09 '22 at 05:36
  • That's the process in a nutshell: run `git describe` or `git rev-parse HEAD` or some such, massage it into something useful to the compiler, and compile it. You'll definitely want something along these lines. – torek Jun 09 '22 at 05:37
  • so a bash script should work then. Right? – CodeNoob Jun 09 '22 at 17:05
  • Sure. You'll need to store the bash commands somewhere: in a `.sh` or `.bash` file, or perhaps in a (C)Makefile. – torek Jun 09 '22 at 17:06
  • should there be a pre-commit-txt file within some gitHook path? – CodeNoob Jun 09 '22 at 19:00
  • No. A pre-commit hook is the wrong place to do this: it happens *before* the commit. The commit hash isn't known until the commit is *done*. – torek Jun 09 '22 at 19:01

0 Answers0