40 lines
1.5 KiB
Python
Raw Normal View History

2014-07-21 00:02:45 +02:00
#!/usr/bin/env python
import os
2015-10-21 17:01:31 +02:00
import subprocess
from ..version import __version__
2014-07-21 00:02:45 +02:00
def get_head(repo_path):
"""Get (branch, commit) from HEAD of a git repo."""
2014-07-21 00:25:26 +02:00
try:
ref = open(os.path.join(repo_path, '.git', 'HEAD'), 'r').read().strip()[5:].split('/')
branch = ref[-1]
commit = open(os.path.join(repo_path, '.git', *ref), 'r').read().strip()[:7]
return branch, commit
except:
return None
2015-10-21 17:01:31 +02:00
def get_version(repo_path):
try:
version = __version__.split('.')
2015-10-24 03:47:21 +02:00
major, minor, cn = [int(i) for i in version]
p = subprocess.Popen(['git',
'--git-dir', os.path.join(repo_path, '.git'),
'--work-tree', repo_path,
'rev-list', 'HEAD', '--count'],
2015-10-21 17:01:31 +02:00
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
raw, err = p.communicate()
c_head = int(raw.decode('ascii'))
2015-10-24 03:47:21 +02:00
q = subprocess.Popen(['git',
'--git-dir', os.path.join(repo_path, '.git'),
'--work-tree', repo_path,
'rev-list', 'master', '--count'],
2015-10-21 17:01:31 +02:00
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
raw, err = q.communicate()
c_master = int(raw.decode('ascii'))
cc = c_head - c_master
2015-10-23 05:33:31 +02:00
assert cc
2015-10-24 03:47:21 +02:00
return '%s.%s.%s' % (major, minor, cn + cc)
2015-10-21 17:01:31 +02:00
except:
return __version__