git-cola v4.9.0
[git-cola.git] / cola / version.py
blob58847c9dd7f3c29dfc6e4ea1c875bf1ab9259c03
1 """Provide git-cola's version number"""
2 import sys
4 try:
5 if sys.version_info < (3, 8):
6 import importlib_metadata as metadata
7 else:
8 from importlib import metadata
9 except (ImportError, OSError):
10 metadata = None
12 from .git import STDOUT
13 from .decorators import memoize
14 from ._version import VERSION
16 try:
17 from ._scm_version import __version__ as SCM_VERSION
18 except ImportError:
19 SCM_VERSION = None
22 # minimum version requirements
23 _versions = {
24 # git diff learned --patience in 1.6.2
25 # git mergetool learned --no-prompt in 1.6.2
26 # git difftool moved out of contrib in git 1.6.3
27 'git': '1.6.3',
28 'python': '2.6',
29 # new: git cat-file --filters --path=<path> SHA1
30 # old: git cat-file --filters blob SHA1:<path>
31 'cat-file-filters-path': '2.11.0',
32 # git diff --submodule was introduced in 1.6.6
33 'diff-submodule': '1.6.6',
34 # git check-ignore was introduced in 1.8.2, but did not follow the same
35 # rules as git add and git status until 1.8.5
36 'check-ignore': '1.8.5',
37 # git push --force-with-lease
38 'force-with-lease': '1.8.5',
39 # git submodule update --recursive was introduced in 1.6.5
40 'submodule-update-recursive': '1.6.5',
41 # git include.path pseudo-variable was introduced in 1.7.10.
42 'config-includes': '1.7.10',
43 # git config --show-scope was introduced in 2.26.0
44 'config-show-scope': '2.26.0',
45 # git config --show-origin was introduced in 2.8.0
46 'config-show-origin': '2.8.0',
47 # git for-each-ref --sort=version:refname
48 'version-sort': '2.7.0',
49 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
50 'qt-hidpi-scale': '5.6.0',
51 # git rebase --rebase-merges was added in 2.18.0
52 'rebase-merges': '2.18.0',
53 # git rebase --update-refs was added in 2.38.0
54 'rebase-update-refs': '2.38.0',
55 # git rev-parse --show-superproject-working-tree was added in 2.13.0
56 'show-superproject-working-tree': '2.13.0',
60 def get(key):
61 """Returns an entry from the known versions table"""
62 return _versions.get(key)
65 def version():
66 """Returns the current version"""
67 if SCM_VERSION:
68 return SCM_VERSION
70 pkg_version = VERSION
71 if metadata is None:
72 return pkg_version
74 try:
75 metadata_version = metadata.version('git-cola')
76 except (ImportError, OSError):
77 return pkg_version
79 # Building from a tarball can end up reporting "0.0.0" or "0.1.dev*".
80 # Use the fallback version in these scenarios.
81 if not metadata_version.startswith('0.'):
82 return metadata_version
83 return pkg_version
86 def builtin_version():
87 """Returns the version recorded in cola/_version.py"""
88 return VERSION
91 @memoize
92 def check_version(min_ver, ver):
93 """Check whether ver is greater or equal to min_ver"""
94 min_ver_list = version_to_list(min_ver)
95 ver_list = version_to_list(ver)
96 return min_ver_list <= ver_list
99 @memoize
100 def check(key, ver):
101 """Checks if a version is greater than the known version for <what>"""
102 return check_version(get(key), ver)
105 def check_git(context, key):
106 """Checks if Git has a specific feature"""
107 return check(key, git_version(context))
110 def version_to_list(value):
111 """Convert a version string to a list of numbers or strings"""
112 ver_list = []
113 for part in value.split('.'):
114 try:
115 number = int(part)
116 except ValueError:
117 number = part
118 ver_list.append(number)
119 return ver_list
122 @memoize
123 def git_version_str(context):
124 """Returns the current GIT version"""
125 git = context.git
126 return git.version(_readonly=True)[STDOUT].strip()
129 @memoize
130 def git_version(context):
131 """Returns the current GIT version"""
132 parts = git_version_str(context).split()
133 if parts and len(parts) >= 3:
134 result = parts[2]
135 else:
136 # minimum supported version
137 result = get('git')
138 return result
141 def cola_version(builtin=False):
142 """A version string for consumption by humans"""
143 if builtin:
144 suffix = builtin_version()
145 else:
146 suffix = version()
147 return 'cola version %s' % suffix
150 def print_version(builtin=False, brief=False):
151 if builtin and brief:
152 msg = builtin_version()
153 elif brief:
154 msg = version()
155 else:
156 msg = cola_version(builtin=builtin)
157 print(msg)