prefs: return the resolved editor variable
[git-cola.git] / cola / version.py
blobbcb47915c2220f682774b7fb786b30b8217f2a18
1 """Provide git-cola's version number"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 import os
4 import sys
6 try:
7 if sys.version_info < (3, 8):
8 import importlib_metadata as metadata
9 else:
10 from importlib import metadata
11 except (ImportError, OSError):
12 metadata = None
15 if __name__ == '__main__':
16 srcdir = os.path.dirname(os.path.dirname(__file__))
17 sys.path.insert(1, srcdir)
19 from .git import STDOUT # noqa
20 from .decorators import memoize # noqa
21 from ._version import VERSION # noqa
23 # minimum version requirements
24 _versions = {
25 # git diff learned --patience in 1.6.2
26 # git mergetool learned --no-prompt in 1.6.2
27 # git difftool moved out of contrib in git 1.6.3
28 'git': '1.6.3',
29 'python': '2.6',
30 # new: git cat-file --filters --path=<path> SHA1
31 # old: git cat-file --filters blob SHA1:<path>
32 'cat-file-filters-path': '2.11.0',
33 # git diff --submodule was introduced in 1.6.6
34 'diff-submodule': '1.6.6',
35 # git check-ignore was introduced in 1.8.2, but did not follow the same
36 # rules as git add and git status until 1.8.5
37 'check-ignore': '1.8.5',
38 # git push --force-with-lease
39 'force-with-lease': '1.8.5',
40 # git submodule update --recursive was introduced in 1.6.5
41 'submodule-update-recursive': '1.6.5',
42 # git include.path pseudo-variable was introduced in 1.7.10
43 'config-includes': '1.7.10',
44 # git for-each-ref --sort=version:refname
45 'version-sort': '2.7.0',
46 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
47 'qt-hidpi-scale': '5.6.0',
48 # git rev-parse --show-superproject-working-tree was added in 2.13.0
49 'show-superproject-working-tree': '2.13.0',
53 def get(key):
54 """Returns an entry from the known versions table"""
55 return _versions.get(key)
58 def version():
59 """Returns the current version"""
60 pkg_version = VERSION
61 if metadata is not None:
62 try:
63 pkg_version = metadata.version('git-cola')
64 except (ImportError, OSError):
65 pass
66 return pkg_version
69 @memoize
70 def check_version(min_ver, ver):
71 """Check whether ver is greater or equal to min_ver"""
72 min_ver_list = version_to_list(min_ver)
73 ver_list = version_to_list(ver)
74 return min_ver_list <= ver_list
77 @memoize
78 def check(key, ver):
79 """Checks if a version is greater than the known version for <what>"""
80 return check_version(get(key), ver)
83 def check_git(context, key):
84 """Checks if Git has a specific feature"""
85 return check(key, git_version(context))
88 def version_to_list(value):
89 """Convert a version string to a list of numbers or strings"""
90 ver_list = []
91 for p in value.split('.'):
92 try:
93 n = int(p)
94 except ValueError:
95 n = p
96 ver_list.append(n)
97 return ver_list
100 @memoize
101 def git_version_str(context):
102 """Returns the current GIT version"""
103 git = context.git
104 return git.version(_readonly=True)[STDOUT].strip()
107 @memoize
108 def git_version(context):
109 """Returns the current GIT version"""
110 parts = git_version_str(context).split()
111 if parts and len(parts) >= 3:
112 result = parts[2]
113 else:
114 # minimum supported version
115 result = '1.6.3'
116 return result
119 def cola_version():
120 suffix = version()
121 return 'cola version %s' % suffix
124 def print_version(brief=False):
125 if brief:
126 msg = version()
127 else:
128 msg = cola_version()
129 sys.stdout.write('%s\n' % msg)