将颠覆存储库编号放入代码中
我想实现一种在代码中记录项目版本的方法,以便在测试时使用它并帮助跟踪错误。 看起来最好使用的版本号就是 Subversion 的当前修订版号。 有没有一种简单的方法可以将这个数字挂接到(在我的例子中是C++)头文件或其他文件中,然后我可以在代码中获取它? 我想这是一个提交后钩子什么的?
有谁有实现此功能的经验(请分享代码?),或者可以建议更好的替代方案吗? 谢谢。
I'd like to implement a way of recording the version of a project within code, so that it can be used when testing and to help track bugs.
It seems the best version number to use would just be the current revision number from Subversion. Is there an easy way to hook this number into a (C++ in my case) header file or something, which I can then get at in code? I guess this is a post commit hook or something?
Does anyone have any experience of implementing this (with code to share, please?), or can suggest a better alternative?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
虽然很漂亮,但修订版关键字技巧仅在该修订版中发生更改时更新文件 - 如果您不更改文件,那么它将继续反映旧修订版。
如果您希望软件始终反映总体修订号,那么您必须深入研究 相关 SVN 条目文件 并提取它,这并不太困难(它是一个 XML 文件)。
维基百科在其版本页面上执行此操作,以指示正在运行的软件的修订版本; 代码在这里 - 查找 <强>getSvnRevision()方法。
While nifty, the revision keyword trick only updates the file when it's changed in that revision - if you don't change the file, then it will continue to reflect the old revision.
If you want the software to always reflect the overall revision number, then you'll have to delve into the relevant SVN entries file and extract it, which isn't too difficult (it's an XML file).
Wikipedia does this on their version page to indicate the revision of the software that's running live; the code is here - look for the getSvnRevision() method.
您还可以使用 SubWCRev,它是 TortoiseSVN 的一部分。
SubWCRev 是 Windows 控制台程序,可用于读取 Subversion 工作副本的状态并可选择在模板文件中执行关键字替换。 这通常用作构建过程的一部分,作为将工作副本信息合并到正在构建的对象中的一种方法。 通常,它可用于在“关于”框中包含修订号。
http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html
You can also use SubWCRev which is part of TortoiseSVN.
SubWCRev is Windows console program which can be used to read the status of a Subversion working copy and optionally perform keyword substitution in a template file. This is often used as part of the build process as a means of incorporating working copy information into the object you are building. Typically it might be used to include the revision number in an “About” box.
http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html
将以下内容添加到文件顶部应该可以解决问题
Adding the following to the top of the file should do the trick
在你的Makefile中添加:
然后你可以在代码中的任何地方使用宏
SVN_REV
,例如:in your Makefile, add:
then you can use macro
SVN_REV
anywhere in your code, eg:您可以使用
svn:keywords
属性来启用Rev
关键字。然后,您可以在代码中使用
$Rev$
,SVN 在更新到$Rev: 256 $
时会自动扩展它,然后可以解析...有关 Subversion 手册的更多信息
You can use the
svn:keywords
property to enable theRev
keyword.You can then use
$Rev$
in your code and SVN will expand it automatically when updating to$Rev: 256 $
which can then parse...More info on the Subversion manual
一个很好的最新解决方案:
创建一个包含以下行的
Makefile
(与YourFile.dox
位于同一文件夹中):以及
YourFile.dox 应包含以下内容:
现在:
sed
将 .dox 中的RevNumber
替换为svnversion
的输出(在存储库的主文件夹中执行) )并将修改后的文件保存到YourFileDummy.dox
YourFileDummy.dox
上执行doxygen
以生成文档A good up-to-date solution:
Create a
Makefile
containing the following line (in the same folder asYourFile.dox
):And
YourFile.dox
should contain this:Now:
sed
replacesRevNumber
in the .dox with the output ofsvnversion
(executed in the main folder of your repository) and saves the modified file toYourFileDummy.dox
doxygen
is executed onYourFileDummy.dox
to generate the documentation两种方法:
在代码中嵌入 $Id$ 或 $Revision$。 然后设置文件的 svn:keywords="Id Revision" 属性。 这将为您提供该源文件的最后修改版本。 适合较小的项目和脚本。
或者,使用 Makefile 驱动的进程和命令行工具 svnversion。 (特定于语言 - 这应该适用于 C/C++)
或一些带有 sed 和 version.h.in 的更复杂的构建脚本。 然后只需 #include version.h
这将为您提供存储库版本号,该版本号将随着每次提交/更新而变化,并且对于大多数项目来说可能是更合适的版本号。
注意:我还使用了手动更新的人类可读版本字符串。 该示例将给出: 版本:1.0.1-r13445
~J
Two ways:
Embed $Id$ or $Revision$ within the code. Then set svn:keywords="Id Revision" property on the file. This will give you the last modified revision of that source file. Good for smaller projects and scripts.
Alternatively, use a Makefile driven process and the command line tool svnversion. (Language specific - this should work for C/C++)
Or some more complex build script with sed and version.h.in. Then just #include version.h
That will give you the repository version number, which will change with every commit / update, and is probably a more appropriate version number for most projects.
Note: I also used a human readable version string that I manually update. The example would give: Version: 1.0.1-r13445
~J