text: defer calls to setStyleSheet()
[git-cola.git] / cola / version.py
blob6946371162bad2993092a1b918bed4e7e249f44c
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
14 try:
15 from ._build_version import BUILD_VERSION
16 except ImportError:
17 BUILD_VERSION = ''
19 # minimum version requirements
20 _versions = {
21 # git diff learned --patience in 1.6.2
22 # git mergetool learned --no-prompt in 1.6.2
23 # git difftool moved out of contrib in git 1.6.3
24 'git': '1.6.3',
25 'python': '2.6',
26 # git diff --submodule was introduced in 1.6.6
27 'diff-submodule': '1.6.6',
28 # git check-ignore was introduced in 1.8.2, but did not follow the same
29 # rules as git add and git status until 1.8.5
30 'check-ignore': '1.8.5',
31 # git for-each-ref --sort=version:refname
32 'version-sort': '2.7.0',
33 # new: git cat-file --filters --path=<path> SHA1
34 # old: git cat-file --filters blob SHA1:<path>
35 'cat-file-filters-path': '2.11.0',
39 def get(key):
40 """Returns an entry from the known versions table"""
41 return _versions.get(key)
44 def version():
45 """Returns the current version"""
46 return VERSION
49 def build_version():
50 """Return the build version, which includes the Git ID"""
51 return BUILD_VERSION
54 @memoize
55 def check_version(min_ver, ver):
56 """Check whether ver is greater or equal to min_ver
57 """
58 min_ver_list = version_to_list(min_ver)
59 ver_list = version_to_list(ver)
60 return min_ver_list <= ver_list
63 @memoize
64 def check(key, ver):
65 """Checks if a version is greater than the known version for <what>"""
66 return check_version(get(key), ver)
69 def check_git(context, key):
70 """Checks if Git has a specific feature"""
71 return check(key, git_version(context))
74 def version_to_list(value):
75 """Convert a version string to a list of numbers or strings
76 """
77 ver_list = []
78 for p in value.split('.'):
79 try:
80 n = int(p)
81 except ValueError:
82 n = p
83 ver_list.append(n)
84 return ver_list
87 @memoize
88 def git_version_str(context):
89 """Returns the current GIT version"""
90 git = context.git
91 return git.version()[STDOUT].strip()
94 @memoize
95 def git_version(context):
96 """Returns the current GIT version"""
97 parts = git_version_str(context).split()
98 if parts and len(parts) >= 3:
99 result = parts[2]
100 else:
101 # minimum supported version
102 result = '1.6.3'
103 return result
106 def cola_version(build=False):
107 if build:
108 suffix = build_version() or version()
109 else:
110 suffix = version()
111 return 'cola version %s' % suffix
114 def print_version(brief=False, build=False):
115 if brief:
116 if build:
117 msg = build_version()
118 else:
119 msg = version()
120 else:
121 msg = cola_version(build=build)
122 sys.stdout.write('%s\n' % msg)