dev: format code using "garden fmt" (black)
[git-cola.git] / cola / version.py
blob280aba03accf314bb16e1220d132065674adbde4
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 from .git import STDOUT
7 from .decorators import memoize
8 from ._version import VERSION
10 try:
11 if sys.version_info < (3, 8):
12 import importlib_metadata as metadata
13 else:
14 from importlib import metadata
15 except (ImportError, OSError):
16 metadata = None
19 if __name__ == '__main__':
20 srcdir = os.path.dirname(os.path.dirname(__file__))
21 sys.path.insert(1, srcdir)
24 # minimum version requirements
25 _versions = {
26 # git diff learned --patience in 1.6.2
27 # git mergetool learned --no-prompt in 1.6.2
28 # git difftool moved out of contrib in git 1.6.3
29 'git': '1.6.3',
30 'python': '2.6',
31 # new: git cat-file --filters --path=<path> SHA1
32 # old: git cat-file --filters blob SHA1:<path>
33 'cat-file-filters-path': '2.11.0',
34 # git diff --submodule was introduced in 1.6.6
35 'diff-submodule': '1.6.6',
36 # git check-ignore was introduced in 1.8.2, but did not follow the same
37 # rules as git add and git status until 1.8.5
38 'check-ignore': '1.8.5',
39 # git push --force-with-lease
40 'force-with-lease': '1.8.5',
41 # git submodule update --recursive was introduced in 1.6.5
42 'submodule-update-recursive': '1.6.5',
43 # git include.path pseudo-variable was introduced in 1.7.10.
44 'config-includes': '1.7.10',
45 # git config --show-scope was introduced in 2.26.0
46 'config-show-scope': '2.26.0',
47 # git config --show-origin was introduced in 2.8.0
48 'config-show-origin': '2.8.0',
49 # git for-each-ref --sort=version:refname
50 'version-sort': '2.7.0',
51 # Qt support for QT_AUTO_SCREEN_SCALE_FACTOR and QT_SCALE_FACTOR
52 'qt-hidpi-scale': '5.6.0',
53 # git rev-parse --show-superproject-working-tree was added in 2.13.0
54 'show-superproject-working-tree': '2.13.0',
55 # git rebase --update-refs was added in 2.38.0
56 'rebase-update-refs': '2.38.0',
60 def get(key):
61 """Returns an entry from the known versions table"""
62 return _versions.get(key)
65 def version():
66 """Returns the current version"""
67 pkg_version = VERSION
68 if metadata is not None:
69 try:
70 pkg_version = metadata.version('git-cola')
71 except (ImportError, OSError):
72 pass
73 return pkg_version
76 def builtin_version():
77 """Returns the version recorded in cola/_version.py"""
78 return VERSION
81 @memoize
82 def check_version(min_ver, ver):
83 """Check whether ver is greater or equal to min_ver"""
84 min_ver_list = version_to_list(min_ver)
85 ver_list = version_to_list(ver)
86 return min_ver_list <= ver_list
89 @memoize
90 def check(key, ver):
91 """Checks if a version is greater than the known version for <what>"""
92 return check_version(get(key), ver)
95 def check_git(context, key):
96 """Checks if Git has a specific feature"""
97 return check(key, git_version(context))
100 def version_to_list(value):
101 """Convert a version string to a list of numbers or strings"""
102 ver_list = []
103 for part in value.split('.'):
104 try:
105 number = int(part)
106 except ValueError:
107 number = part
108 ver_list.append(number)
109 return ver_list
112 @memoize
113 def git_version_str(context):
114 """Returns the current GIT version"""
115 git = context.git
116 return git.version(_readonly=True)[STDOUT].strip()
119 @memoize
120 def git_version(context):
121 """Returns the current GIT version"""
122 parts = git_version_str(context).split()
123 if parts and len(parts) >= 3:
124 result = parts[2]
125 else:
126 # minimum supported version
127 result = '1.6.3'
128 return result
131 def cola_version():
132 """A version string for consumption by humans"""
133 suffix = version()
134 return 'cola version %s' % suffix
137 def print_version(builtin=False, brief=False):
138 if builtin:
139 msg = builtin_version()
140 elif brief:
141 msg = version()
142 else:
143 msg = cola_version()
144 sys.stdout.write('%s\n' % msg)