startup: open selected repository when "Enter" is pressed
[git-cola.git] / cola / version.py
blobcf66dd7e4f2c639b8004a592f1f8f00e481d375f
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 if __name__ == '__main__':
7 srcdir = os.path.dirname(os.path.dirname(__file__))
8 sys.path.insert(1, srcdir)
10 from .git import STDOUT # noqa
11 from .decorators import memoize # noqa
12 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 # new: git cat-file --filters --path=<path> SHA1
27 # old: git cat-file --filters blob SHA1:<path>
28 'cat-file-filters-path': '2.11.0',
29 # git diff --submodule was introduced in 1.6.6
30 'diff-submodule': '1.6.6',
31 # git check-ignore was introduced in 1.8.2, but did not follow the same
32 # rules as git add and git status until 1.8.5
33 'check-ignore': '1.8.5',
34 # git push --force-with-lease
35 'force-with-lease': '1.8.5',
36 # git submodule update --recursive was introduced in 1.6.5
37 'submodule-update-recursive': '1.6.5',
38 # git include.path pseudo-variable was introduced in 1.7.10
39 'config-includes': '1.7.10',
40 # git for-each-ref --sort=version:refname
41 'version-sort': '2.7.0',
42 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
43 'qt-hidpi-scale': '5.6.0',
44 # git rev-parse --show-superproject-working-tree was added in 2.13.0
45 'show-superproject-working-tree': '2.13.0',
49 def get(key):
50 """Returns an entry from the known versions table"""
51 return _versions.get(key)
54 def version():
55 """Returns the current version"""
56 return VERSION
59 def build_version():
60 """Return the build version, which includes the Git ID"""
61 return BUILD_VERSION
64 @memoize
65 def check_version(min_ver, ver):
66 """Check whether ver is greater or equal to min_ver"""
67 min_ver_list = version_to_list(min_ver)
68 ver_list = version_to_list(ver)
69 return min_ver_list <= ver_list
72 @memoize
73 def check(key, ver):
74 """Checks if a version is greater than the known version for <what>"""
75 return check_version(get(key), ver)
78 def check_git(context, key):
79 """Checks if Git has a specific feature"""
80 return check(key, git_version(context))
83 def version_to_list(value):
84 """Convert a version string to a list of numbers or strings"""
85 ver_list = []
86 for p in value.split('.'):
87 try:
88 n = int(p)
89 except ValueError:
90 n = p
91 ver_list.append(n)
92 return ver_list
95 @memoize
96 def git_version_str(context):
97 """Returns the current GIT version"""
98 git = context.git
99 return git.version()[STDOUT].strip()
102 @memoize
103 def git_version(context):
104 """Returns the current GIT version"""
105 parts = git_version_str(context).split()
106 if parts and len(parts) >= 3:
107 result = parts[2]
108 else:
109 # minimum supported version
110 result = '1.6.3'
111 return result
114 def cola_version(build=False):
115 if build:
116 suffix = build_version() or version()
117 else:
118 suffix = version()
119 return 'cola version %s' % suffix
122 def print_version(brief=False, build=False):
123 if brief:
124 if build:
125 msg = build_version()
126 else:
127 msg = version()
128 else:
129 msg = cola_version(build=build)
130 sys.stdout.write('%s\n' % msg)