Repair `stg log` with patches from subdir
[stgit.git] / stgit / version.py
blobd0263493a97ff6313ada509b4b6bc024bfd5f8aa
1 import os
2 import re
3 import sys
5 from stgit.run import Run, RunException
8 class VersionUnavailable(Exception):
9 pass
12 def git_describe_version():
13 root = sys.path[0] if sys.path[0] else None
14 try:
15 v = (
16 Run('git', 'describe', '--tags', '--abbrev=4')
17 .cwd(root)
18 .discard_stderr()
19 .output_one_line()
21 except RunException as e:
22 raise VersionUnavailable(str(e))
23 m = re.match(r'^v([0-9].*)', v)
24 if m:
25 v = m.group(1)
26 else:
27 raise VersionUnavailable('bad version: %s' % v)
28 try:
29 dirty = (
30 Run('git', 'diff-index', '--name-only', 'HEAD')
31 .cwd(root)
32 .discard_stderr()
33 .raw_output()
35 except RunException as e:
36 raise VersionUnavailable(str(e))
37 if dirty:
38 v += '-dirty'
39 return v
42 def git_archival_version():
43 archival_path = os.path.join(sys.path[0], '.git_archival.txt')
44 if not os.path.isfile(archival_path):
45 # The archival file will not be present in sdist archives.
46 raise VersionUnavailable('%s does not exist' % archival_path)
47 tag_re = re.compile(r'(?<=\btag: )([^,]+)\b')
48 with open(archival_path) as f:
49 for line in f:
50 if line.startswith('ref-names:'):
51 for tag in tag_re.findall(line):
52 if tag.startswith('v'):
53 return tag[1:]
54 else:
55 raise VersionUnavailable('no tags found in %s' % archival_path)
58 def get_builtin_version():
59 try:
60 import stgit.builtin_version
61 except ImportError:
62 raise VersionUnavailable('could not import stgit.builtin_version')
63 else:
64 return stgit.builtin_version.version
67 def get_version():
68 for v in [get_builtin_version, git_describe_version, git_archival_version]:
69 try:
70 return v()
71 except VersionUnavailable:
72 pass
73 return 'unknown-version'
76 # minimum version requirements
77 git_min_ver = '2.2.0'
78 python_min_ver = '3.5'