core: make getcwd() fail-safe
[git-cola.git] / cola / version.py
blob3b8b566d512f076ba100505a4a6a7ce6e6990d4a
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 # 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 for-each-ref --sort=version:refname
37 'version-sort': '2.7.0',
38 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
39 'qt-hidpi-scale': '5.6.0',
40 # git rev-parse --show-superproject-working-tree was added in 2.13.0
41 'show-superproject-working-tree': '2.13.0',
45 def get(key):
46 """Returns an entry from the known versions table"""
47 return _versions.get(key)
50 def version():
51 """Returns the current version"""
52 return VERSION
55 def build_version():
56 """Return the build version, which includes the Git ID"""
57 return BUILD_VERSION
60 @memoize
61 def check_version(min_ver, ver):
62 """Check whether ver is greater or equal to min_ver
63 """
64 min_ver_list = version_to_list(min_ver)
65 ver_list = version_to_list(ver)
66 return min_ver_list <= ver_list
69 @memoize
70 def check(key, ver):
71 """Checks if a version is greater than the known version for <what>"""
72 return check_version(get(key), ver)
75 def check_git(context, key):
76 """Checks if Git has a specific feature"""
77 return check(key, git_version(context))
80 def version_to_list(value):
81 """Convert a version string to a list of numbers or strings
82 """
83 ver_list = []
84 for p in value.split('.'):
85 try:
86 n = int(p)
87 except ValueError:
88 n = p
89 ver_list.append(n)
90 return ver_list
93 @memoize
94 def git_version_str(context):
95 """Returns the current GIT version"""
96 git = context.git
97 return git.version()[STDOUT].strip()
100 @memoize
101 def git_version(context):
102 """Returns the current GIT version"""
103 parts = git_version_str(context).split()
104 if parts and len(parts) >= 3:
105 result = parts[2]
106 else:
107 # minimum supported version
108 result = '1.6.3'
109 return result
112 def cola_version(build=False):
113 if build:
114 suffix = build_version() or version()
115 else:
116 suffix = version()
117 return 'cola version %s' % suffix
120 def print_version(brief=False, build=False):
121 if brief:
122 if build:
123 msg = build_version()
124 else:
125 msg = version()
126 else:
127 msg = cola_version(build=build)
128 sys.stdout.write('%s\n' % msg)