Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Versioning with Git in Visual C++

I have a C++ project in VS2013. In the past on similar projects I've used SubWCRev with Subversion to auto generate version numbers. I had a template file like this :

#define MAJOR_VERSION       2
#define MINOR_VERSION       2
#define MICRO_VERSION       0
#define BUILD_VERSION       $WCMODS?$WCREV$+1:$WCREV$$

#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)

#define BUILD_VERSION_STRING    QUOTE(MAJOR_VERSION.MINOR_VERSION.MICRO_VERSION.BUILD_VERSION)

Then I ran SubWCRev as a pre-build step to generate the header file which I included in the project to define the version numbers.

I'm now using Git and want to do something similar. I know that Git doesn't have an equivalent of revision number but the HEAD SHA would be fine.

It doesn't seem like there's an equivalent way to do this with Git and I need to do some scripting which isn't my strong point. Could a Git Guru point me in the right direction to achieve this?

like image 574
Redeye Avatar asked Apr 24 '14 08:04

Redeye


3 Answers

I've eventually found a really useful batch file which actually does way more than I need :

https://github.com/Thell/git-vs-versioninfo-gen

It basically does a very similar job to SubWCRev and generates a header file that I can include in my VS project to set all the version strings. After a bit of hacking around with the script I've got it to do exactly what I wanted.

There's a C# version here too :

https://github.com/jasperboot/git-vs-versioninfo

like image 52
Redeye Avatar answered Nov 19 '22 12:11

Redeye


With the latest TortoiseGit 2.7.0, you can simply replace SubWCRev with GitWCRev and get the same results. Revision will use git's SHA1.

like image 40
aawins Avatar answered Nov 19 '22 11:11

aawins


I'm using the output from git describe for the build string. I don't use windows.

This example works under Linux fine.

version.h

#ifndef GIT_VERSION_H
#define GIT_VERSION_H
extern const char* git_version;
#endif

The version.c is generated by awk

git describe | awk 'BEGIN { print "#include \"version.h\"" } { print "const char* git_version = \"" $$0 "\";"} END {}' > version.c

Dirk

like image 34
Dirk Avatar answered Nov 19 '22 10:11

Dirk