version_from_git.py 1.32 KB
Newer Older
Martin Bauer's avatar
Martin Bauer committed
1
import subprocess
2
from distutils.version import StrictVersion
Martin Bauer's avatar
Martin Bauer committed
3
4


5
def version_number_from_git(tag_prefix='release/', sha_length=10, version_format="{version}.dev{commits}+{sha}"):
Martin Bauer's avatar
Martin Bauer committed
6
7
8
9
10
11
12
13
14
15
16
17
18
    def get_released_versions():
        tags = sorted(subprocess.getoutput('git tag').split('\n'))
        versions = [t[len(tag_prefix):] for t in tags if t.startswith(tag_prefix)]
        return versions

    def tag_from_version(v):
        return tag_prefix + v

    def increment_version(v):
        parsed_version = [int(i) for i in v.split('.')]
        parsed_version[-1] += 1
        return '.'.join(str(i) for i in parsed_version)

19
20
21
    version_strings = get_released_versions()
    version_strings.sort(key=StrictVersion)
    latest_release = version_strings[-1]
Martin Bauer's avatar
Martin Bauer committed
22
23
24
25
26
27
28
29
30
31
32
33
34
    commits_since_tag = subprocess.getoutput('git rev-list {}..HEAD --count'.format(tag_from_version(latest_release)))
    sha = subprocess.getoutput('git rev-parse HEAD')[:sha_length]
    is_dirty = len(subprocess.getoutput("git status --untracked-files=no -s")) > 0

    if int(commits_since_tag) == 0:
        version_string = latest_release
    else:
        next_version = increment_version(latest_release)
        version_string = version_format.format(version=next_version, commits=commits_since_tag, sha=sha)

    if is_dirty:
        version_string += ".dirty"
    return version_string