Ask Your Question

can git tags be created for each of the releases into pypi?

asked 2011-03-16 22:38:07 -0600

tfoote gravatar image

updated 2011-10-10 03:00:48 -0600

Evgeny gravatar image

I was considering installing using git, but couldn't find any tags to use.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-03-16 22:47:48 -0600

Evgeny gravatar image

updated 2011-10-09 21:03:09 -0600

Added a post-commit hook, which adds a tag if version increments, so the future versions will have tags.

# place in .git/hooks/post-commit
"""Post-commit hook for git, which automatically adds tag
corresponding to the new version, to adapt it to your needs,
change the ``VERSION_FILE`` variable below.
VERSION_FILE = 'askbot/'

import commands, re

class Version(object):
    """basic implementation of version utility,
    only supports numeric triples like '0.1.3'
    def __init__(self, vstring = None):
        if vstring:

    def parse(self, vstring):
        major, minor, micro = vstring.split('.')
        self.major = int(major)
        self.minor = int(minor)
        self.micro = int(micro)

    def __str__(self):
        return "%s.%s.%s" % (self.major, self.minor, self.micro)

    def __cmp__(self, other):
        cmp_major = cmp(self.major, other.major)
        if cmp_major == 0:
            cmp_minor = cmp(self.minor, other.minor)
            if cmp_minor == 0:
                return cmp(self.micro, other.micro)
                return cmp_minor
            return cmp_major

def add_tag(tag):
    """tag the git repo with the new version"""
    status, output = commands.getstatusoutput('git tag -f %s' % tag)
    if status:
        raise Exception('tagging not successful: %s %s' % (output, status))

def get_version(version_file_string):
    """returns instance of :class:`Version`
    based on the string input,
    which is expected to be contents of some file or output"""
    regex_string = r"VERSION\s?\=\s?" + \
                    r"\((?P<major>[0-9]+)\s?\," + \
                    r"\s?(?P<minor>[0-9]+)\s?\," + \
    for line in version_file_string.split("\n"):
        regex = re.compile(regex_string)
        match  =
        if match:
            vstring = '.'.join(match.groups())
            return Version(vstring = vstring)
    return Version('0.0.0')

def get_versions():
    """returns name of a new version as string
    if there is a new version, otherwise
    status, output = commands.getstatusoutput(
        'git show HEAD:' + VERSION_FILE
    new_version = get_version(output)

    status, output = commands.getstatusoutput(
        'git show HEAD^:' + VERSION_FILE
    old_version = get_version(output)
    return new_version, old_version

if __name__ == '__main__':

    NEW, OLD = get_versions()
    if NEW > OLD:
        print "added new tag %s" % NEW
    elif NEW < OLD:
        raise ValueError("Error: new version is older than the previous")
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-03-16 22:38:07 -0600

Seen: 493 times

Last updated: Oct 09 '11