git-cola v2.11
[git-cola.git] / cola / version.py
blob1116a4927afbdd66b401ce6812a50806fc73570a
1 # Copyright (c) David Aguilar
2 """Provide git-cola's version number"""
3 from __future__ import division, absolute_import, unicode_literals
5 import os
6 import sys
8 if __name__ == '__main__':
9 srcdir = os.path.dirname(os.path.dirname(__file__))
10 sys.path.insert(1, srcdir)
12 from .git import git
13 from .git import STDOUT
14 from .decorators import memoize
15 from ._version import VERSION
17 # minimum version requirements
18 _versions = {
19 # git diff learned --patience in 1.6.2
20 # git mergetool learned --no-prompt in 1.6.2
21 # git difftool moved out of contrib in git 1.6.3
22 'git': '1.6.3',
23 'python': '2.6',
24 # git diff --submodule was introduced in 1.6.6
25 'diff-submodule': '1.6.6',
26 # git check-ignore was introduced in 1.8.2, but did not follow the same
27 # rules as git add and git status until 1.8.5
28 'check-ignore': '1.8.5',
29 # git for-each-ref --sort=version:refname
30 'version-sort': '2.0.0',
34 def get(key):
35 """Returns an entry from the known versions table"""
36 return _versions.get(key)
39 def version():
40 """Returns the current version"""
41 return VERSION
44 @memoize
45 def check_version(min_ver, ver):
46 """Check whether ver is greater or equal to min_ver
47 """
48 min_ver_list = version_to_list(min_ver)
49 ver_list = version_to_list(ver)
50 return min_ver_list <= ver_list
53 @memoize
54 def check(key, ver):
55 """Checks if a version is greater than the known version for <what>"""
56 return check_version(get(key), ver)
59 def check_git(key):
60 """Checks if Git has a specific feature"""
61 return check(key, git_version())
64 def version_to_list(version):
65 """Convert a version string to a list of numbers or strings
66 """
67 ver_list = []
68 for p in version.split('.'):
69 try:
70 n = int(p)
71 except ValueError:
72 n = p
73 ver_list.append(n)
74 return ver_list
77 @memoize
78 def git_version_str():
79 """Returns the current GIT version"""
80 return git.version()[STDOUT].strip()
83 @memoize
84 def git_version():
85 """Returns the current GIT version"""
86 parts = git_version_str().split()
87 if parts and len(parts) >= 3:
88 return parts[2]
89 else:
90 # minimum supported version
91 return '1.6.3'
94 def cola_version():
95 return 'cola version %s' % version()
98 def print_version(brief=False):
99 if brief:
100 msg = version()
101 else:
102 msg = cola_version()
103 sys.stdout.write('%s\n' % msg)
106 if __name__ == '__main__':
107 print_version(brief=True)