2

With RCS, I can create a perl file like:

#!/usr/bin/perl
my $version = '$Id: mycode,v 1.1 2019/01/26 13:54:27 jhnc Exp jhnc $';
print( join(" ", (split /[,\s]+/, $version)[1,3]), "\n");

Then, whenever I check the file in, the version will be updated. The aim is to avoid manual update of the version string.

I'm a git newbie. Is anything similar possible with git?

If I use a pre-commit hook I don't see how one gets the new git hash before the commit happens. And if I use post-commit, the change won't be committed. Is my only option to set up a parallel versioning scheme and use that in a pre-commit hook?

jhnc
  • 11,310
  • 1
  • 9
  • 26
  • Git doesn't use numeric versions but hashes to identify the commits (and not only the commits). If you manage to configure it to replace the value between the two `$` signs, the value it would put there is meaningless. The only information you get this way is the fact that two versions of the file belong to different commits. They might be identical except for the value of `$version` but there is no way to know from a commit hash which version is newer. – axiac Jan 26 '19 at 14:39

1 Answers1

1

When I searched before posting I didn't find anything relevant. Of course, when I searched again after posting, I found several relevant posts including:

which lead me to believe that the best solution is to set the version while building, packaging or installing, and not to try to store it in the revision control system itself.

So I am now doing:

  • git tag X.Y abcdefg on released commits
  • during packaging, patching output of git describe --tags into the code
jhnc
  • 11,310
  • 1
  • 9
  • 26
  • Usually the version is stored as a tag, added manually. During development, you add the base version number appended with a + and the first six or so digits of the commit. – Mad Physicist Jan 26 '19 at 15:46
  • @MadPhysicist It's during development that I'll be most likely to forget to update the version string. However, it seems that `git describe` helpfully does appending similar to what you suggest if the commit isn't tagged: `X.Y-N-gHHHHHHH` – jhnc Jan 26 '19 at 23:42