git_command: switch version caches to functools
Simplifies the code a bit to use the stdlib cache helper.
Change-Id: I778e90100ce748a71cc3a5a5d67dda403334315e
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/305482
Reviewed-by: Raman Tenneti <rtenneti@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
diff --git a/git_command.py b/git_command.py
index d06fc77..f8cb280 100644
--- a/git_command.py
+++ b/git_command.py
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import functools
import os
import re
import sys
@@ -45,7 +46,6 @@
_ssh_proxy_path = None
_ssh_sock_path = None
_ssh_clients = []
-_ssh_version = None
def _run_ssh_version():
@@ -64,16 +64,14 @@
return ()
+@functools.lru_cache(maxsize=None)
def ssh_version():
"""return ssh version as a tuple"""
- global _ssh_version
- if _ssh_version is None:
- try:
- _ssh_version = _parse_ssh_version()
- except subprocess.CalledProcessError:
- print('fatal: unable to detect ssh version', file=sys.stderr)
- sys.exit(1)
- return _ssh_version
+ try:
+ return _parse_ssh_version()
+ except subprocess.CalledProcessError:
+ print('fatal: unable to detect ssh version', file=sys.stderr)
+ sys.exit(1)
def ssh_sock(create=True):
@@ -125,18 +123,14 @@
_ssh_clients = []
-_git_version = None
-
-
class _GitCall(object):
+ @functools.lru_cache(maxsize=None)
def version_tuple(self):
- global _git_version
- if _git_version is None:
- _git_version = Wrapper().ParseGitVersion()
- if _git_version is None:
- print('fatal: unable to detect git version', file=sys.stderr)
- sys.exit(1)
- return _git_version
+ ret = Wrapper().ParseGitVersion()
+ if ret is None:
+ print('fatal: unable to detect git version', file=sys.stderr)
+ sys.exit(1)
+ return ret
def __getattr__(self, name):
name = name.replace('_', '-')