User forums > Using Code::Blocks

Show GIT version on my application

(1/1)

GRighi:
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?

Miguel Gimenez:
Add a script doing that to the prebuild steps (in project options).

ollydbg:
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";
--- End code ---

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

In the pre build step, I wrote


--- Code: ---cmd /c GenerateHash.cmd
--- End code ---

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
--- End code ---

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.

GRighi:
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

Navigation

[0] Message Index

Go to full version