|
@@ -0,0 +1,59 @@
|
|
|
+"""
|
|
|
+Alternate to `Versioneer <https://pypi.python.org/pypi/versioneer/>`_ using
|
|
|
+`Dulwich <https://pypi.python.org/pypi/dulwich>`_ to sort tags by time from
|
|
|
+newest to oldest.
|
|
|
+
|
|
|
+Copy this file (``latest_git_tags.py``) into the package's top folder, import it
|
|
|
+into ``__init__.py`` and then set ::
|
|
|
+
|
|
|
+ from latest_git_tags import get_recent_tags
|
|
|
+
|
|
|
+ __version__ = get_recent_tags()[0][0][1:]
|
|
|
+
|
|
|
+
|
|
|
+This example assumes the tags have a leading "v" like "v0.3", and that the
|
|
|
+``.git`` folder is in the project folder that containts the package folder.
|
|
|
+"""
|
|
|
+
|
|
|
+from dulwich.repo import Repo
|
|
|
+import time
|
|
|
+import datetime
|
|
|
+import os
|
|
|
+
|
|
|
+
|
|
|
+DIRNAME = os.path.abspath(os.path.dirname(__file__))
|
|
|
+PROJDIR = os.path.dirname(DIRNAME)
|
|
|
+
|
|
|
+def get_recent_tags(projdir=PROJDIR):
|
|
|
+ """
|
|
|
+ Get list of recent tags in order from newest to oldest and their datetimes.
|
|
|
+
|
|
|
+ :param projdir: path to ``.git``
|
|
|
+ :returns: list of (tag, [datetime, commit, author]) sorted from new to old
|
|
|
+ """
|
|
|
+ project = Repo(projdir)
|
|
|
+ refs = project.get_refs()
|
|
|
+ tags = {}
|
|
|
+
|
|
|
+ for key, value in refs.iteritems():
|
|
|
+ obj = project.get_object(value)
|
|
|
+
|
|
|
+ if obj.type_name != 'tag':
|
|
|
+
|
|
|
+ continue
|
|
|
+
|
|
|
+ _, tag = key.rsplit('/', 1)
|
|
|
+
|
|
|
+ if obj.object[0].type_name == 'commit':
|
|
|
+ commit = project.get_object(obj.object[1])
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ tags[tag] = [
|
|
|
+ datetime.datetime(*time.gmtime(commit.commit_time)[:6]),
|
|
|
+ commit.id,
|
|
|
+ commit.author
|
|
|
+ ]
|
|
|
+
|
|
|
+
|
|
|
+ return sorted(tags.iteritems(), key=lambda tag: tag[1][0], reverse=True)
|