Mercurial 关键字扩展可在每次提交时扩展

发布于 2024-10-30 00:47:44 字数 484 浏览 5 评论 0原文

我需要使用 hg 关键字扩展将构建日期和修订版本嵌入到源文件中。撇开“你真的不想这样做”的争论不谈,我该怎么做呢?

这是我的源文件 (lib/foo/version.rb) 的样子(恰好是 Ruby,但这只是从我没有“编译”步骤的角度来看相关的)在我的构建中,我可以执行 -DREVISION="$(hg id)" in):

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

问题是 $Revision$ 和 $Date$ 使用该文件的变更集和提交日期进行扩展 ,而我需要的是整个存储库的提示变更集和提交日期。

我没有看到可以在 hg help templates 中使用的明显模板,关键字扩展也没有提及任何具有全局范围的内容。我想做的事情可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-06 00:47:44

您可以安装更新文件的 post-commit 挂钩:

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

然后您应该将版本文件添加到 .hgignore 文件中 - 每次提交后它都会更改,因此总是很脏。您还可以添加一个编码过滤器来清理版本文件:

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

此脚本将使 Mercurial 将文件视为干净的 - 无论它实际包含什么日期和变更集,Mercurial 都会将其视为包含未扩展的 $Date$$Version$ 关键字:

$ 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$"

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$"
篱下浅笙歌 2024-11-06 00:47:44

如果您从结帐运行代码,则可以直接调用 hg 并缓存该值。类似于:

module Foo
  VERSION = {
    :version => system("hg log --template '{note|short}-{latesttag}-{latesttagdistance}' -r .")
  }
end

如果您没有在安装了 Mercurial 的系统上从结帐内部运行代码,那么您的部署脚本可以轻松获取/使用该值 - 也许可以通过使用 hg archive 来获取要发送的 tarball,然后自动包含 .hg_archive.txt

我向您保证,无论您的设置是什么,都有比关键字扩展更漂亮的方法。

If you're running your code from a checkout you can invoke hg directly and cache the value. Something like:

module Foo
  VERSION = {
    :version => system("hg log --template '{note|short}-{latesttag}-{latesttagdistance}' -r .")
  }
end

and if you're not running the code from inside a checkout on a system with Mercurial installed, then your deploy script can easily get/use the value -- perhaps by using hg archive to get the tarball to send which then automatically includes a .hg_archive.txt.

I guarantee you there's a prettier way to do this than the keywords extension no matter what your setup is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文