Author Topic: Show GIT version on my application  (Read 4619 times)

Offline GRighi

  • Single posting newcomer
  • *
  • Posts: 2
Show GIT version on my application
« on: August 26, 2024, 03:51:07 pm »
Hello,
I would like to show the gitversion of my repo on my applications.
The idea is to generate a version.h file containing the git information like this https://stackoverflow.com/questions/69194603/how-to-set-git-version-in-h-file-before-build.
Is there a way to retrive the gitversion info and generate the version.h file during the project build?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1644
Re: Show GIT version on my application
« Reply #1 on: August 26, 2024, 04:59:22 pm »
Add a script doing that to the prebuild steps (in project options).

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Show GIT version on my application
« Reply #2 on: August 27, 2024, 03:49:26 am »
This is the batch(GenerateHash.cmd) code I used to generate a hash source file, I used it for many years.
I used it as a pre-build steps in the Code::Blocks' project build options.

The source file(version.cpp) has some contents like below:

Code
const char* GITHASH="971ec0e821a9dd008c785a9b53e030e939dfef00"; 

Then you can link the version.cpp in your project.

In the pre build step, I wrote

Code
cmd /c GenerateHash.cmd

Here is the content of the batch file:

Code
@echo off

REM a way to get the command line response from the git command
REM windows batch (bat, cmd) - How to set command output to variable https://gist.github.com/pavelburov/4dd76f12ef2d08c401bf54ec344efae9
for /f %%i in ('git rev-parse HEAD') do set VARIABLE=%%i
echo %VARIABLE%

REM https://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
REM some special characters should be escaped

If not exist version.cpp (
echo const char* GITHASH=^"%VARIABLE%^"^; > version.cpp
goto EndOfFile
)

echo const char* GITHASH=^"%VARIABLE%^"^; > temp.version.cpp

REM echo abc>a.txt
REM echo abc1>b.txt

REM compare the two files using the FC command
REM FC - file compare - Windows CMD - SS64.com https://ss64.com/nt/fc.html
FC version.cpp temp.version.cpp >NUL
IF %ERRORLEVEL% == 0 (
REM the two files are the same
echo "Same commit hash, no need to update version.cpp"
goto EndOfFile
) ELSE (
REM something different, update the version.h file with new hash
copy temp.version.cpp version.cpp
echo "New commit hash found, version.cpp updated"
)

:EndOfFile

When the hash value changes(I'm using a compare command "FC" to compare the hash strings), I need to update the version.cpp file.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline GRighi

  • Single posting newcomer
  • *
  • Posts: 2
Re: Show GIT version on my application
« Reply #3 on: August 27, 2024, 10:43:28 am »
Ok I managed to set the pre-build steps to run a python script that I already use to retrive the git version and write on a version.h file.
Thank you for your help