DerWeh
DerWeh

Reputation: 1829

Git use tag-name in code

I want to manage my code version using tags in git. If I have a new release I create a new annotated tag, e.g. 1.3.0. Now I would like to also have this version number available in the code itself. To do this I tried using:

git describe --tags

And put the result in a file. Obviously I am always one tag behind. If I create a new tag 1.3.1. the code will still refer to version 1.3.0, only the following commits will refer to the new number.

Is there a clean workaround? With regular commits I could just use --amend to overwrite the content once, but for tags this doesn't seem to exist.


Some context: I am using python with setuptools. In my setup-file I use git describe --tags and safe this as my version. Furthermore it is written to a file, if git is not installed the version is read from this file.
My python code then reads this file, to be able to print it's on version via ./script.py --version.

Upvotes: 0

Views: 451

Answers (1)

torek
torek

Reputation: 490078

There is a fundamental chicken-and-egg problem here: if the version string is, itself, under version control, how can you get the next version string into the controlled file before you commit that file into the version control system and create the version?

None of the answers are completely satisfactory (and in Mercurial this problem is unavoidable, as the tags are in a file, .hgtags, that is then version controlled, so that when you retrieve version 1.3.0, the tag name 1.3.0 is no longer available: it's only in the .hgtags file in the next version). The usual trick with Git, though, is to make sure that the version string is not encoded directly in any file that is version controlled.

In my answer to How can I add git version info into cython-built .so file? I give an outline for a general method of doing this, plus a specific way to do it in a setup.py file. You do not mention how your system is built—i.e., how releases are made—but the general method may still apply.

A less satisfactory, but still quite workable, method is to hard-code the tag in a file, write the tag you intend to assign into the file, commit the file, and then tag the commit. You can (and probably should) automate this process.

Upvotes: 2

Related Questions