How good is my method of embedding version numbers in my application using Mercurial hooks?

This is not a very specific question, and I like the criticism of my current approach more.

I would like to include the version number of the program in the program that I am developing. This is not a commercial product, but a research application, so it is important to know which version generated the results.

My method works as follows:

  • My .hg / hgrc link has a "pre-commit" link in the version_gen.sh file
  • version_gen.sh consists solely of: hg parent --template "r{rev}_{date|shortdate}" > version.num
  • In the makefile, the line version="%__VERSION__% in the main script is replaced by the contents of version.num.

Are there any better ways to do this? The only real short coming that I see is that if you just commit the specfic file, version.num will be updated, but it will not be perfect, and if I tried to add, always committing this file, it would lead to an infite loop (unless I created some temporary file to indicate that I was already in a commit, but that seems ugly ...).

+7
mercurial build version makefile
source share
4 answers

What you are trying to do is called a keyword extension that is not supported in the Mercurial kernel .

You can integrate this extension into a make file or (easier) with a keyword extension .

This extension allows you to extend RCS / CVS-like and user keys in text files tracked by Mercurial.
The extension occurs in the working directory and / or when creating the distribution using the " hg archive "

+1
source share

Problem

As you have already determined, you really created the Catch-22 here .

You cannot put significant information into the version.num file until the changes are committed, and since you save version.num in the repository, you cannot commit the changes to the repository until you complete the version.num file.

My decision

I would suggest the following:

  • Get rid of the "pre-commit" hook and hg forget the version.num file.
  • Add version.num to your .hgignore file.
  • Adjust version_gen.sh to consist of:

    hg parent --template "r{node|short}_{date|shortdate}" > version.num

  • In the makefile, verify that version_gen.sh running before version.num used to set the version parameter.

My reasons

As @ Ry4an suggests that the build system insert revision information into the software during build, using version control information is a much better option. The only problem with this is to try to compile the code from the hg archive repository, where the build system cannot extract the relevant information.

I would be inclined to prevent this, however - in my own build system, the assembly failed if the revision information could not be extracted.

Also, as @Kai Inkinen suggests , using version numbers is not portable. Rev 21 on one machine may be rev 22 on another. Although this may not be a problem right now, it may be in the future if you start working with other people.

Finally, I explain the reasons why I do not like the Keyword extension in question regarding similar questions to your own question:

I looked at the Mercurials Keyword extension, as this seemed like an obvious solution. Nevertheless, the more I looked at him and read the opinions of people, the more I came to the conclusion that this is not quite what needs to be done.

I also remember the problems associated with replacing keywords in projects of previous companies ....

Also, I don’t really want to include Mercurial extensions to complete the build. I want the solution to be taken for granted, so that the application is not just accidentally compiled without built-in version information just because the extension is not included or the correct supporting software was not installed.

Then in the comments to the answer that suggested using the keyword extension:

... I rejected the use of the keyword extension, as it would be too easy to end up with the string "$ Id $" being compiled into an executable. If the keyword extension was built into the mercurial and not the extension, and by default I could consider it, but since it is worth it, it just would not be reliable. - Mark Booth

And do not think that there may be a more reliable solution. What if someone accidentally damages .hg or builds not from a clone, but from an archive? - Mr.Cat

@ Mr.Cat - I don’t think there can be a less reliable solution than expanding keywords. Anywhere where you explicitly did not enable the extension (or someone turned it off), you will get the literal string "$ID$" compiled into an object file without complaints. If the mercury or repo is damaged (not sure what you meant), you still need to fix it. As for the hg archive , my original solution will not compile if you try to create it from the archive! This is exactly what I want. I do not want any source to be compiled into our applications without a source controlled by the audit! - Mark Booth

+6
source share

That you use the pre-commit hook is relevant. You should not put the rest of version_gen.sh in thesemves source files, just in build / release artifacts, which you can do more accurately with the help of "upgrade".

You do not want the Makefile to really change in the repo with each commit, which only makes hell merged. You want to insert the version after checking the files before assembly, which is what the update hook does.

+1
source share

On distributed systems such as Mercurial, the actual "version number" does not necessarily mean the same thing in every environment. Even if this is a one-person project, and you are very careful about having only your central repo, you will probably still want to use sha1-sum instead, since it is really unique to this state of the repository. Shay can be retrieved through the {node} pattern

As a suggestion, I believe that the best workflow would be to use tags instead, which are also local to your repository until you push them upstream. Do not write your number to a file, but instead tag your release code with a meaningful tag, for example
RELEASE_2
or
RELEASE_2010-04-01
or maybe a script, and use a template to create a tag?

You can then add the tag to your version.num file without the version (in .hgignore) that will be added to the assembly. This way you can give meaningful names to releases and associate the release with a unique identifier.

0
source share

All Articles