git-cola v4.0.0
[git-cola.git] / cola / version.py
blob9b20b6c2f5885946edf7b97dd3aa7a75cb846b73
1 """Provide git-cola's version number"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 import os
4 import sys
5 try:
6 if sys.version_info < (3, 8):
7 import importlib_metadata as metadata
8 else:
9 from importlib import metadata
10 except (ImportError, OSError):
11 metadata = None
14 if __name__ == '__main__':
15 srcdir = os.path.dirname(os.path.dirname(__file__))
16 sys.path.insert(1, srcdir)
18 from .git import STDOUT # noqa
19 from .decorators import memoize # noqa
20 from ._version import VERSION # noqa
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 for-each-ref --sort=version:refname
44 'version-sort': '2.7.0',
45 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
46 'qt-hidpi-scale': '5.6.0',
47 # git rev-parse --show-superproject-working-tree was added in 2.13.0
48 'show-superproject-working-tree': '2.13.0',
52 def get(key):
53 """Returns an entry from the known versions table"""
54 return _versions.get(key)
57 def version():
58 """Returns the current version"""
59 pkg_version = VERSION
60 if metadata is not None:
61 try:
62 pkg_version = metadata.version('git-cola')
63 except (ImportError, OSError):
64 pass
65 return pkg_version
68 @memoize
69 def check_version(min_ver, ver):
70 """Check whether ver is greater or equal to min_ver"""
71 min_ver_list = version_to_list(min_ver)
72 ver_list = version_to_list(ver)
73 return min_ver_list <= ver_list
76 @memoize
77 def check(key, ver):
78 """Checks if a version is greater than the known version for <what>"""
79 return check_version(get(key), ver)
82 def check_git(context, key):
83 """Checks if Git has a specific feature"""
84 return check(key, git_version(context))
87 def version_to_list(value):
88 """Convert a version string to a list of numbers or strings"""
89 ver_list = []
90 for p in value.split('.'):
91 try:
92 n = int(p)
93 except ValueError:
94 n = p
95 ver_list.append(n)
96 return ver_list
99 @memoize
100 def git_version_str(context):
101 """Returns the current GIT version"""
102 git = context.git
103 return git.version()[STDOUT].strip()
106 @memoize
107 def git_version(context):
108 """Returns the current GIT version"""
109 parts = git_version_str(context).split()
110 if parts and len(parts) >= 3:
111 result = parts[2]
112 else:
113 # minimum supported version
114 result = '1.6.3'
115 return result
118 def cola_version():
119 suffix = version()
120 return 'cola version %s' % suffix
123 def print_version(brief=False):
124 if brief:
125 msg = version()
126 else:
127 msg = cola_version()
128 sys.stdout.write('%s\n' % msg)