Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial keywords extension to expand on every commit

I need to use the hg keyword extension to embed the build date and revision into a source file. Leaving aside the whole "you really don't want to be doing that" argument, how can I do this?

Here's what my source file (lib/foo/version.rb) looks like (which happens to be Ruby, but that's only relevant from the point of view that I don't have a "compile" step in my build which I could do a -DREVISION="$(hg id)" in):

module Foo
  VERSION = {
    :date => "$Date$",
    :changeset => "$Revision$"
  }
end

The problem is that $Revision$ and $Date$ are expanded with the changeset and commit date of that file, whereas what I need is the tip changeset and commit date of the whole repository.

I don't see an obvious template I can use in hg help templates, nor does the keyword extension mention anything with global scope. Is what I'm trying to do possible?

like image 961
regularfry Avatar asked Apr 05 '11 10:04

regularfry


1 Answers

You can install a post-commit hook that updates the file:

[hooks]
post-commit = sed -i lib/foo/version.rb \
  -e "s|\$Date.*\$|\$Date: $(date)\$|" \
  -e "s|\$Version.*\$|\$Version: $(hg id -i)\$|"

You should then probably add the version file to the .hgignore file -- it will change after every commit and thus always be dirty. You could also add a encode filter that will clean up the version file:

[encode]
lib/foo/version.rb =  sed -e "s|\$Date.*\$|\$Date\$|" \
                          -e "s|\$Version.*\$|\$Version\$|"

This script will make Mercurial see the file as clean -- no matter what date and changeset has it really contains, Mercurial will see it as containing un-expanded $Date$ and $Version$ keywords:

$ hg commit -m test
$ hg tip
changeset:   7:df81c9ddc9ad
tag:         tip
user:        Martin Geisler 
date:        Wed Apr 06 14:39:26 2011 +0200
summary:     test

$ hg status
$ hg cat version.py
date = "$Date$"
version = "$Version$"
$ cat version.py
date = "$Date: Wed Apr  6 14:39:26 CEST 2011$"
version = "$Version: df81c9ddc9ad$"
like image 151
Martin Geisler Avatar answered Nov 03 '22 23:11

Martin Geisler