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