Commit cf363628 authored by Christoph Groth's avatar Christoph Groth
Browse files

improve version extraction from git

* Only extract the version from Git when the top level directory of the Git
  repository coincides with the root directory of the Kwant distribution

* Disregard tags on branches that were merged-in (with Git >= 1.8.4).
parent b0f16464
...@@ -11,25 +11,44 @@ import os ...@@ -11,25 +11,44 @@ import os
__all__ = ['version'] __all__ = ['version']
distr_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# When changing this function, remember to also change its twin in ../setup.py. # When changing this function, remember to also change its twin in ../setup.py.
def get_version_from_git(): def get_version_from_git():
kwant_dir = os.path.dirname(os.path.abspath(__file__))
try: try:
p = subprocess.Popen(['git', 'describe'], cwd=kwant_dir, p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError: except OSError:
return return
if p.wait() != 0: if p.wait() != 0:
return return
version = p.communicate()[0].strip() # TODO: use os.path.samefile once we depend on Python >= 3.3.
if os.path.normpath(p.communicate()[0].rstrip('\n')) != distr_root:
# The top-level directory of the current Git repository is not the same
# as the root directory of the Kwant distribution: do not extract the
# version from Git.
return
# git describe --first-parent does not take into account tags from branches
# that were merged-in.
for opts in [['--first-parent'], []]:
try:
p = subprocess.Popen(['git', 'describe'] + opts, cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() == 0:
break
else:
return
version = p.communicate()[0].rstrip('\n')
if version[0] == 'v': if version[0] == 'v':
version = version[1:] version = version[1:]
try: try:
p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=kwant_dir) p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=distr_root)
except OSError: except OSError:
version += '-confused' # This should never happen. version += '-confused' # This should never happen.
else: else:
...@@ -37,6 +56,7 @@ def get_version_from_git(): ...@@ -37,6 +56,7 @@ def get_version_from_git():
version += '-dirty' version += '-dirty'
return version return version
version = get_version_from_git() version = get_version_from_git()
if version is None: if version is None:
try: try:
......
...@@ -49,7 +49,7 @@ if cythonize and cython_version: ...@@ -49,7 +49,7 @@ if cythonize and cython_version:
else: else:
from distutils.command.build_ext import build_ext from distutils.command.build_ext import build_ext
kwant_dir = os.path.dirname(os.path.abspath(__file__)) distr_root = os.path.dirname(os.path.abspath(__file__))
class kwant_build_ext(build_ext): class kwant_build_ext(build_ext):
...@@ -135,7 +135,7 @@ class test(Command): ...@@ -135,7 +135,7 @@ class test(Command):
def git_lsfiles(): def git_lsfiles():
try: try:
p = subprocess.Popen(['git', 'ls-files'], cwd=kwant_dir, p = subprocess.Popen(['git', 'ls-files'], cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError: except OSError:
return return
...@@ -159,7 +159,7 @@ class kwant_sdist(distutils_sdist): ...@@ -159,7 +159,7 @@ class kwant_sdist(distutils_sdist):
# Check that MANIFEST exists and has not been generated by # Check that MANIFEST exists and has not been generated by
# distutils. # distutils.
try: try:
with open(kwant_dir + '/MANIFEST', 'r') as f: with open(distr_root + '/MANIFEST', 'r') as f:
line = f.read() line = f.read()
except IOError: except IOError:
print >>sys.stderr, "error: MANIFEST file is missing and " \ print >>sys.stderr, "error: MANIFEST file is missing and " \
...@@ -168,7 +168,7 @@ class kwant_sdist(distutils_sdist): ...@@ -168,7 +168,7 @@ class kwant_sdist(distutils_sdist):
trustworthy = not line.strip().startswith('#') trustworthy = not line.strip().startswith('#')
else: else:
# Generate MANIFEST file. # Generate MANIFEST file.
with open(kwant_dir + '/MANIFEST', 'w') as f: with open(distr_root + '/MANIFEST', 'w') as f:
for name in names: for name in names:
a, sep, b = name.rpartition('/') a, sep, b = name.rpartition('/')
if b == '.gitignore': if b == '.gitignore':
...@@ -200,20 +200,39 @@ with a comment). It may well be incomplete.""" ...@@ -200,20 +200,39 @@ with a comment). It may well be incomplete."""
# include a copy. # include a copy.
def get_version_from_git(): def get_version_from_git():
try: try:
p = subprocess.Popen(['git', 'describe'], cwd=kwant_dir, p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError: except OSError:
return return
if p.wait() != 0: if p.wait() != 0:
return return
version = p.communicate()[0].strip() # TODO: use os.path.samefile once we depend on Python >= 3.3.
if os.path.normpath(p.communicate()[0].rstrip('\n')) != distr_root:
# The top-level directory of the current Git repository is not the same
# as the root directory of the Kwant distribution: do not extract the
# version from Git.
return
# git describe --first-parent does not take into account tags from branches
# that were merged-in.
for opts in [['--first-parent'], []]:
try:
p = subprocess.Popen(['git', 'describe'] + opts, cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() == 0:
break
else:
return
version = p.communicate()[0].rstrip('\n')
if version[0] == 'v': if version[0] == 'v':
version = version[1:] version = version[1:]
try: try:
p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=kwant_dir) p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=distr_root)
except OSError: except OSError:
version += '-confused' # This should never happen. version += '-confused' # This should never happen.
else: else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment