Redeye
Redeye

Reputation: 1602

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?

Upvotes: 9

Views: 7210

Answers (4)

aawins
aawins

Reputation: 31

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.

Upvotes: 3

Dirk
Dirk

Reputation: 19

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

Upvotes: 1

nzeemin
nzeemin

Reputation: 961

Probably, in simple cases, the following command line will be enough:

git rev-list HEAD --count

-- prints number of revisions, like "123".

Upvotes: 0

Redeye
Redeye

Reputation: 1602

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

Upvotes: 6

Related Questions