Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic version updating using git on commit

I have a python project I'm working on using git for version control I want to set up a unique version number for every commit based on PEP440.

I.e I would use the following version numbering N(.N)*[{a|b|rc}N][.postN][.devN] for the master branch, were for every commit for which a higher level version number isn't incremented devN will be auto incremented by git on commit, the rest of the version would hopefully be controlled by a special git command: git increment-version {level}

where level would be one of major, minor, patch, patch-{level} (patch is the same as patch-0), alpha, beta, release, final, post would map to the version string as {major}[.{minor}[(.{patch-N})*]], alpha => aN, beta => bN, release => rcN, post => postN, with beta replacing alpha and release replacing beta, final removes pre-release component, if the earlier one is used than the current this should produce a error, When a higher level version is incremented all lower levels are rest to zero, and any patch level set to zero is not included in the string, incrementing major, minor or patch will set the per-release component to a0.

For a branch branching off of the master branch the same numbers will be used but the version applies to the branch it self, with the version string being

{master_version}+{branch_name}.{branch_version} where branch version undertakes the same format as for the master version

For branches off a non master branch the local version is extended as follows {branchN_name}.{branchN_version}(.{branchN_name}.{branchN_version})* where branch names must start with a letter and not match any of [{a|b|rc}N][.postN][.devN], each {branchN_name}.{branchN_version} is treated like a extended form of .postN component

The location of the version string should be specified as a file that is part of the repository, and a line pattern as follows using a string with escape sequences \\ = \, \" = ", \' = ', \v = version string i.e. "__version__ = \"\v\"".

On a commit git should search the specified file for this line load the version string, update it and write out a new version string to the file, intermediately before committing the changes including the updated version string, if the file has be otherwise change, but not added to the commit, the version, string should be update, in the version from the previous commit, and this change propagated to the modified file. If a on the master branch, and a version number has be incremented by a call to git increment-version {level} than git should also generate a tag for this commit matching the version string.

Ideally I would like to increment this extension to git by way of a python script that is part of the repository it self. However I'm open to other ways of solving this problem.

EDIT:

I believe the best solution is to make git command i.e. git-autoversion executable.

Then to enable said versioning for a repository I would run git autoversion --init --style=pep440 --template=python-info --versionfile=XXX/_info.py

which would create a file .gitautoversion which would contain

[autoversion]
version=1.0
local=
style=pep440
template=python-info
file=XXX/_info.py

it would also have a hook git autoversion --commit which should be run as a pre-commit hook.

finally it could be called as git autoversion --[major|minor|patch{-level}|alpha|beta|release|final|post|dev] which can be used to mark the version number to increment, only the last call would count this would add the line increment=xxx to the .gitautoversion configuration file, the command git autoversion --commit would read this and increment the version, if no increment is specified it defaults to dev, finally it invokes the command git-autoversion-tpl-{template} --version={version} --local={local} --file={file} to update the version file.

QUESTION: is their a way to install a global git hook when autoversion is installed to call git autoversion --commit for a commit to any repository this command can then check for .gitautoversion if found update the version otherwise do nothing. If not this would require manual installing of the hook.

QUESTION 2: is their a way to hook an extra argument to git commit, say --autoversion=[major|minor|patch{-level}|alpha|beta|release|final|post|dev], which would invoke the autoversion command before running commit hooks?

like image 999
Glen Fletcher Avatar asked Nov 07 '22 18:11

Glen Fletcher


1 Answers

You may be able to use version_scheme option in the following pypi project to do what you want

https://pypi.python.org/pypi/setuptools_scm

like image 126
TomDotTom Avatar answered Nov 15 '22 06:11

TomDotTom