Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rewrite python __version__ with git?

Tags:

git

python

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.

like image 263
btel Avatar asked Apr 07 '11 13:04

btel


People also ask

How do I change the Python Package version?

How do I Install a Specific Version of a Python Package? To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .

How do you write Git commands in Python?

If you need to perform some logic on the output of the commands then you would use the following subprocess call format. import subprocess PIPE = subprocess. PIPE branch = 'my_branch' process = subprocess. Popen(['git', 'pull', branch], stdout=PIPE, stderr=PIPE) stdoutput, stderroutput = process.

How do I run a git clone in Python?

Git(). clone("git://git.xyz.com/platform/manifest.git") will also tell you how to install it. But most likely, all you need is something like pip install pygit2 or pip install GitPython or something like that.

How do I create a Git version?

Another solution can be to use the Git tags to automatically generate the version number. A script can find the closest tag starting with v such as v0. 1.2 and use the tag name as the version number plus the first n digits of the current commit ( v0. 1.2 (build 4acd21) ).


1 Answers

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).

like image 72
Cascabel Avatar answered Oct 01 '22 13:10

Cascabel