|
5 | 5 | import re
|
6 | 6 | import sys
|
7 | 7 |
|
8 |
| -from stgit import run, utils |
9 |
| -from stgit.exception import StgException |
10 | 8 | from stgit.run import Run, RunException
|
11 | 9 |
|
12 | 10 |
|
13 |
| -class VersionUnavailable(StgException): |
| 11 | +class VersionUnavailable(Exception): |
14 | 12 | pass
|
15 | 13 |
|
| 14 | + |
16 | 15 | def git_describe_version():
|
17 |
| - path = sys.path[0] |
| 16 | + root = sys.path[0] |
18 | 17 | try:
|
19 |
| - v = Run('git', 'describe', '--tags', '--abbrev=4' |
20 |
| - ).cwd(path).output_one_line() |
| 18 | + v = Run( |
| 19 | + 'git', 'describe', '--tags', '--abbrev=4' |
| 20 | + ).cwd(root).output_one_line() |
21 | 21 | except RunException as e:
|
22 | 22 | raise VersionUnavailable(str(e))
|
23 |
| - if not re.match(r'^v[0-9]', v): |
24 |
| - raise VersionUnavailable('%s: bad version' % v) |
| 23 | + m = re.match(r'^v([0-9].*)', v) |
| 24 | + if m: |
| 25 | + v = m.group() |
| 26 | + else: |
| 27 | + raise VersionUnavailable('bad version: %s' % v) |
25 | 28 | try:
|
26 |
| - dirty = Run('git', 'diff-index', '--name-only', 'HEAD' |
27 |
| - ).cwd(path).raw_output() |
| 29 | + dirty = Run( |
| 30 | + 'git', 'diff-index', '--name-only', 'HEAD' |
| 31 | + ).cwd(root).raw_output() |
28 | 32 | except RunException as e:
|
29 | 33 | raise VersionUnavailable(str(e))
|
30 | 34 | if dirty:
|
31 | 35 | v += '-dirty'
|
32 |
| - return utils.strip_prefix('v', v) |
| 36 | + return v |
| 37 | + |
| 38 | + |
| 39 | +def git_archival_version(): |
| 40 | + tag_re = re.compile(r'(?<=\btag: )([^,]+)\b') |
| 41 | + archival_path = os.path.join(sys.path[0], '.git_archival.txt') |
| 42 | + with open(archival_path) as f: |
| 43 | + for line in f: |
| 44 | + if line.startswith('ref-names:'): |
| 45 | + tags = tag_re.findall(line) |
| 46 | + if tags: |
| 47 | + return tags[0] |
| 48 | + else: |
| 49 | + raise VersionUnavailable('no tags found in %s' % archival_path) |
| 50 | + |
33 | 51 |
|
34 | 52 | def builtin_version():
|
35 | 53 | try:
|
36 |
| - import stgit.builtin_version as bv |
| 54 | + import stgit.builtin_version |
37 | 55 | except ImportError:
|
38 |
| - raise VersionUnavailable() |
| 56 | + raise VersionUnavailable('could not import stgit.builtin_version') |
39 | 57 | else:
|
40 |
| - return bv.version |
41 |
| - |
42 |
| -def _builtin_version_file(ext = 'py'): |
43 |
| - return os.path.join(sys.path[0], 'stgit', 'builtin_version.%s' % ext) |
| 58 | + return stgit.builtin_version.version |
44 | 59 |
|
45 |
| -def write_builtin_version(): |
46 |
| - try: |
47 |
| - v = git_describe_version() |
48 |
| - except VersionUnavailable: |
49 |
| - return |
50 |
| - with open(_builtin_version_file(), 'w') as f: |
51 |
| - f.write( |
52 |
| - '# This file was generated automatically. Do not edit by hand.\n' |
53 |
| - 'version = %r\n' % v) |
54 | 60 |
|
55 | 61 | def get_version():
|
56 |
| - for v in [builtin_version, git_describe_version]: |
| 62 | + for v in [builtin_version, git_describe_version, git_archival_version]: |
57 | 63 | try:
|
58 | 64 | return v()
|
59 | 65 | except VersionUnavailable:
|
60 | 66 | pass
|
61 | 67 | return 'unknown-version'
|
62 | 68 |
|
63 |
| -version = get_version() |
64 | 69 |
|
65 | 70 | # minimum version requirements
|
66 | 71 | git_min_ver = '1.5.2'
|
|
0 commit comments