cmds: use version.check_git(...) in OpenParent
[git-cola.git] / cola / version.py
blobb90a8a5e2197ba964c868459a9623751de2b2816
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',
36 # git rev-parse --show-superproject-working-tree was added in 2.13.0
37 'show-superproject-working-tree': '2.13.0',
41 def get(key):
42 """Returns an entry from the known versions table"""
43 return _versions.get(key)
46 def version():
47 """Returns the current version"""
48 return VERSION
51 def build_version():
52 """Return the build version, which includes the Git ID"""
53 return BUILD_VERSION
56 @memoize
57 def check_version(min_ver, ver):
58 """Check whether ver is greater or equal to min_ver
59 """
60 min_ver_list = version_to_list(min_ver)
61 ver_list = version_to_list(ver)
62 return min_ver_list <= ver_list
65 @memoize
66 def check(key, ver):
67 """Checks if a version is greater than the known version for <what>"""
68 return check_version(get(key), ver)
71 def check_git(context, key):
72 """Checks if Git has a specific feature"""
73 return check(key, git_version(context))
76 def version_to_list(value):
77 """Convert a version string to a list of numbers or strings
78 """
79 ver_list = []
80 for p in value.split('.'):
81 try:
82 n = int(p)
83 except ValueError:
84 n = p
85 ver_list.append(n)
86 return ver_list
89 @memoize
90 def git_version_str(context):
91 """Returns the current GIT version"""
92 git = context.git
93 return git.version()[STDOUT].strip()
96 @memoize
97 def git_version(context):
98 """Returns the current GIT version"""
99 parts = git_version_str(context).split()
100 if parts and len(parts) >= 3:
101 result = parts[2]
102 else:
103 # minimum supported version
104 result = '1.6.3'
105 return result
108 def cola_version(build=False):
109 if build:
110 suffix = build_version() or version()
111 else:
112 suffix = version()
113 return 'cola version %s' % suffix
116 def print_version(brief=False, build=False):
117 if brief:
118 if build:
119 msg = build_version()
120 else:
121 msg = version()
122 else:
123 msg = cola_version(build=build)
124 sys.stdout.write('%s\n' % msg)