tests: use pytest fixtures in browse_model_test
[git-cola.git] / cola / version.py
blob14630d612450ac9d8a3f5a037c357e48858a9a61
1 # Copyright (C) 2007-2018 David Aguilar and contributors
2 """Provide git-cola's version number"""
3 from __future__ import division, absolute_import, unicode_literals
4 import os
5 import sys
7 if __name__ == '__main__':
8 srcdir = os.path.dirname(os.path.dirname(__file__))
9 sys.path.insert(1, srcdir)
11 from .git import STDOUT # noqa
12 from .decorators import memoize # noqa
13 from ._version import VERSION # noqa
15 try:
16 from ._build_version import BUILD_VERSION
17 except ImportError:
18 BUILD_VERSION = ''
20 # minimum version requirements
21 _versions = {
22 # git diff learned --patience in 1.6.2
23 # git mergetool learned --no-prompt in 1.6.2
24 # git difftool moved out of contrib in git 1.6.3
25 'git': '1.6.3',
26 'python': '2.6',
27 # new: git cat-file --filters --path=<path> SHA1
28 # old: git cat-file --filters blob SHA1:<path>
29 'cat-file-filters-path': '2.11.0',
30 # git diff --submodule was introduced in 1.6.6
31 'diff-submodule': '1.6.6',
32 # git check-ignore was introduced in 1.8.2, but did not follow the same
33 # rules as git add and git status until 1.8.5
34 'check-ignore': '1.8.5',
35 # git push --force-with-lease
36 'force-with-lease': '1.8.5',
37 # git submodule update --recursive was introduced in 1.6.5
38 'submodule-update-recursive': '1.6.5',
39 # git include.path pseudo-variable was introduced in 1.7.10
40 'config-includes': '1.7.10',
41 # git for-each-ref --sort=version:refname
42 'version-sort': '2.7.0',
43 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
44 'qt-hidpi-scale': '5.6.0',
45 # git rev-parse --show-superproject-working-tree was added in 2.13.0
46 'show-superproject-working-tree': '2.13.0',
50 def get(key):
51 """Returns an entry from the known versions table"""
52 return _versions.get(key)
55 def version():
56 """Returns the current version"""
57 return VERSION
60 def build_version():
61 """Return the build version, which includes the Git ID"""
62 return BUILD_VERSION
65 @memoize
66 def check_version(min_ver, ver):
67 """Check whether ver is greater or equal to min_ver"""
68 min_ver_list = version_to_list(min_ver)
69 ver_list = version_to_list(ver)
70 return min_ver_list <= ver_list
73 @memoize
74 def check(key, ver):
75 """Checks if a version is greater than the known version for <what>"""
76 return check_version(get(key), ver)
79 def check_git(context, key):
80 """Checks if Git has a specific feature"""
81 return check(key, git_version(context))
84 def version_to_list(value):
85 """Convert a version string to a list of numbers or strings"""
86 ver_list = []
87 for p in value.split('.'):
88 try:
89 n = int(p)
90 except ValueError:
91 n = p
92 ver_list.append(n)
93 return ver_list
96 @memoize
97 def git_version_str(context):
98 """Returns the current GIT version"""
99 git = context.git
100 return git.version()[STDOUT].strip()
103 @memoize
104 def git_version(context):
105 """Returns the current GIT version"""
106 parts = git_version_str(context).split()
107 if parts and len(parts) >= 3:
108 result = parts[2]
109 else:
110 # minimum supported version
111 result = '1.6.3'
112 return result
115 def cola_version(build=False):
116 if build:
117 suffix = build_version() or version()
118 else:
119 suffix = version()
120 return 'cola version %s' % suffix
123 def print_version(brief=False, build=False):
124 if brief:
125 if build:
126 msg = build_version()
127 else:
128 msg = version()
129 else:
130 msg = cola_version(build=build)
131 sys.stdout.write('%s\n' % msg)