status: add support for "git checkout --theirs/--ours" on unmerge files
[git-cola.git] / cola / version.py
blobde58577f1773e13e926e2bd85c158f5ba8de0b19
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 @memoize
77 def check_version(min_ver, ver):
78 """Check whether ver is greater or equal to min_ver"""
79 min_ver_list = version_to_list(min_ver)
80 ver_list = version_to_list(ver)
81 return min_ver_list <= ver_list
84 @memoize
85 def check(key, ver):
86 """Checks if a version is greater than the known version for <what>"""
87 return check_version(get(key), ver)
90 def check_git(context, key):
91 """Checks if Git has a specific feature"""
92 return check(key, git_version(context))
95 def version_to_list(value):
96 """Convert a version string to a list of numbers or strings"""
97 ver_list = []
98 for p in value.split('.'):
99 try:
100 n = int(p)
101 except ValueError:
102 n = p
103 ver_list.append(n)
104 return ver_list
107 @memoize
108 def git_version_str(context):
109 """Returns the current GIT version"""
110 git = context.git
111 return git.version(_readonly=True)[STDOUT].strip()
114 @memoize
115 def git_version(context):
116 """Returns the current GIT version"""
117 parts = git_version_str(context).split()
118 if parts and len(parts) >= 3:
119 result = parts[2]
120 else:
121 # minimum supported version
122 result = '1.6.3'
123 return result
126 def cola_version():
127 suffix = version()
128 return 'cola version %s' % suffix
131 def print_version(brief=False):
132 if brief:
133 msg = version()
134 else:
135 msg = cola_version()
136 sys.stdout.write('%s\n' % msg)