我怎样才能重写python __version__与git? [英] How can I rewrite python __version__ with git?

查看:90
本文介绍了我怎样才能重写python __version__与git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模块中定义一个 __ version __ 变量,它应该在git commit上自动更新,类似于 SVN关键词。有没有办法在Git中实现它?有没有人有一个工作的例子?



我考虑使用 GitPython ,但我不想引入另一个依赖关系,我希望从SVN repo下载模块的用户或作为zip包使用相同的版本方案(我不关心这是一些难以理解的散列)。

>

编辑:我特别的问题是我必须运行仿真,仿真的结果取决于仿真脚本的确切版本。因此,每次我必须将版本号与模拟结果一起存储。如果两者都不同步,它可能会有非常可怕的结果。 解决方案

可能会更好地做到这一点,作为您的一部分打包,而不是在每次提交之后。

有两个主要选项:


  • 使用 git-存档 打包,然后使用 export-subst 属性。不幸的是,你可以替代的东西仅限于 git log --format = ... 的占位符。例如,你可以在你的文件中写 __ version__ = $ Format:%H $ ,把< filename> export-subst 在你的.gitattributes中,当你运行 git archive 时,它会被更改为提交的完整散列与...一起归档。这只是你要求的,但我更喜欢下一个选项。

  • 自己做一个打包过程的一部分(通常是一个构建编译软件包的过程),并使用 git describe 。这会给你一个漂亮的字符串,比如 v1.7.4.1-59-ge3d3f7d ,意思是59通过标签 v1.7.4.1 ,在提交 ge3d3f7d 中,您可以在打包/构建时以某种方式将其插入代码中的正确位置。这就是Git自己做的事情;结果被转储到一个文件中,其内容被读入makefile,然后通过 -D 预处理器选项传递到构建中,并放入各种文件名中(例如,发行版如果你确实想在每次提交之后执行此操作,那么你可以通过发布一个帖子来实现这一点。 - 提交钩子,但只有你(以及你勾选钩子的人)拥有它,并且非常有可能失去同步 - 你还必须拥有一个post-checkout钩子,等等上。对于任何需要此版本号的进程来说,它都是更好的选择。



    您也可以使用涂抹/清洁过滤器,这更像是您实际上需要(而不是简单地在每次提交之后)。


    I would like to define a __version__ variable in my module which should be automatically updated on git commit similarly to what SVN keywords do. Is there a way to achieve it in Git? Does anyone have a working example?

    I considered using GitPython, but I would not like to introduce another dependency and I want users who download the module from SVN repo or as a zip package to have the same versioning scheme (I do not care that is some illegible hash).

    Edit: My particular problem is that I have to run simulations whose result depend on the exact version of the simulation script. Therefore each time I have to store version number together with the simulation results. If both run out of sync, it may have very dire outcomes.

    解决方案

    It might be better to do this as part of your packaging, rather than after every commit.

    There are two primary options:

    • Use git-archive to package, and use the export-subst attribute. Unfortunately, the things you can substitute in are limited to the placeholders from git log --format=.... For example, you could write __version__ = $Format:%H$ in your file, put <filename> export-subst in your .gitattributes, and when you run git archive, that'd be changed to the full hash of the commit you're archiving with. This is just about what you're asking for, but I do prefer the next option.

    • Do it yourself as part of a packaging process (often a build process for compiled packages), and use git describe. That will get you a nice pretty string like v1.7.4.1-59-ge3d3f7d, meaning "59 commits past the tag v1.7.4.1, at commit ge3d3f7d" which you can then insert somehow into the right place in your code as you package/build. This is what Git itself does; the result is dumped to a file, whose contents are read into the makefile and then passed into the build via a -D preprocessor option, and placed into various filenames (e.g. the release tarball) directly.

    If you really, really want to do this after every commit, you could, with a post-commit hook, but then only you (and those you give the hook to) will have it, and it's very very possible to get out of sync - you'll also have to have a post-checkout hook, and so on and so on. It's really better for whatever processes that create something needing this version number to get it themselves.

    You could also use a smudge/clean filter, which would be more like what you actually want (rather than simply after every commit).

    这篇关于我怎样才能重写python __version__与git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆