summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2021-05-06 00:28:32 -0400
committerMike Frysinger <vapier@google.com>2021-05-06 18:36:25 +0000
commit8e768eaaa722a99405f6542ac718880c8c22f060 (patch)
tree652e0b64c3b1ac810162acc4b9d35f4f265bb6a2
parent2f8fdbecde985e2a5ecf498d97d0d3ea9d1a6865 (diff)
downloadgit-repo-8e768eaaa722a99405f6542ac718880c8c22f060.tar.gz
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>
-rw-r--r--git_command.py32
-rw-r--r--tests/test_git_command.py9
2 files changed, 20 insertions, 21 deletions
diff --git a/git_command.py b/git_command.py
index d06fc77c..f8cb280c 100644
--- a/git_command.py
+++ b/git_command.py
@@ -12,6 +12,7 @@
12# See the License for the specific language governing permissions and 12# See the License for the specific language governing permissions and
13# limitations under the License. 13# limitations under the License.
14 14
15import functools
15import os 16import os
16import re 17import re
17import sys 18import sys
@@ -45,7 +46,6 @@ LAST_CWD = None
45_ssh_proxy_path = None 46_ssh_proxy_path = None
46_ssh_sock_path = None 47_ssh_sock_path = None
47_ssh_clients = [] 48_ssh_clients = []
48_ssh_version = None
49 49
50 50
51def _run_ssh_version(): 51def _run_ssh_version():
@@ -64,16 +64,14 @@ def _parse_ssh_version(ver_str=None):
64 return () 64 return ()
65 65
66 66
67@functools.lru_cache(maxsize=None)
67def ssh_version(): 68def ssh_version():
68 """return ssh version as a tuple""" 69 """return ssh version as a tuple"""
69 global _ssh_version 70 try:
70 if _ssh_version is None: 71 return _parse_ssh_version()
71 try: 72 except subprocess.CalledProcessError:
72 _ssh_version = _parse_ssh_version() 73 print('fatal: unable to detect ssh version', file=sys.stderr)
73 except subprocess.CalledProcessError: 74 sys.exit(1)
74 print('fatal: unable to detect ssh version', file=sys.stderr)
75 sys.exit(1)
76 return _ssh_version
77 75
78 76
79def ssh_sock(create=True): 77def ssh_sock(create=True):
@@ -125,18 +123,14 @@ def terminate_ssh_clients():
125 _ssh_clients = [] 123 _ssh_clients = []
126 124
127 125
128_git_version = None
129
130
131class _GitCall(object): 126class _GitCall(object):
127 @functools.lru_cache(maxsize=None)
132 def version_tuple(self): 128 def version_tuple(self):
133 global _git_version 129 ret = Wrapper().ParseGitVersion()
134 if _git_version is None: 130 if ret is None:
135 _git_version = Wrapper().ParseGitVersion() 131 print('fatal: unable to detect git version', file=sys.stderr)
136 if _git_version is None: 132 sys.exit(1)
137 print('fatal: unable to detect git version', file=sys.stderr) 133 return ret
138 sys.exit(1)
139 return _git_version
140 134
141 def __getattr__(self, name): 135 def __getattr__(self, name):
142 name = name.replace('_', '-') 136 name = name.replace('_', '-')
diff --git a/tests/test_git_command.py b/tests/test_git_command.py
index 912a9dbe..76c092f4 100644
--- a/tests/test_git_command.py
+++ b/tests/test_git_command.py
@@ -29,8 +29,8 @@ import wrapper
29class SSHUnitTest(unittest.TestCase): 29class SSHUnitTest(unittest.TestCase):
30 """Tests the ssh functions.""" 30 """Tests the ssh functions."""
31 31
32 def test_ssh_version(self): 32 def test_parse_ssh_version(self):
33 """Check ssh_version() handling.""" 33 """Check parse_ssh_version() handling."""
34 ver = git_command._parse_ssh_version('Unknown\n') 34 ver = git_command._parse_ssh_version('Unknown\n')
35 self.assertEqual(ver, ()) 35 self.assertEqual(ver, ())
36 ver = git_command._parse_ssh_version('OpenSSH_1.0\n') 36 ver = git_command._parse_ssh_version('OpenSSH_1.0\n')
@@ -40,6 +40,11 @@ class SSHUnitTest(unittest.TestCase):
40 ver = git_command._parse_ssh_version('OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017\n') 40 ver = git_command._parse_ssh_version('OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017\n')
41 self.assertEqual(ver, (7, 6)) 41 self.assertEqual(ver, (7, 6))
42 42
43 def test_ssh_version(self):
44 """Check ssh_version() handling."""
45 with mock.patch('git_command._run_ssh_version', return_value='OpenSSH_1.2\n'):
46 self.assertEqual(git_command.ssh_version(), (1, 2))
47
43 def test_ssh_sock(self): 48 def test_ssh_sock(self):
44 """Check ssh_sock() function.""" 49 """Check ssh_sock() function."""
45 with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'): 50 with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'):