From 5291eafa412117b80ebbf645fc51559dd0b2caaf Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 5 May 2021 15:53:03 -0400 Subject: ssh: move all ssh logic to a common place We had ssh logic sprinkled between two git modules, and neither was quite the right home for it. This largely moves the logic as-is to its new home. We'll leave major refactoring to followup commits. Bug: https://crbug.com/gerrit/12389 Change-Id: I300a8f7dba74f2bd132232a5eb1e856a8490e0e9 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/305483 Reviewed-by: Chris Mcdonald Tested-by: Mike Frysinger --- git_command.py | 91 ++++------------------------------------------------------ 1 file changed, 5 insertions(+), 86 deletions(-) (limited to 'git_command.py') diff --git a/git_command.py b/git_command.py index f8cb280c..fabad0e0 100644 --- a/git_command.py +++ b/git_command.py @@ -14,16 +14,14 @@ import functools import os -import re import sys import subprocess -import tempfile -from signal import SIGTERM from error import GitError from git_refs import HEAD import platform_utils from repo_trace import REPO_TRACE, IsTrace, Trace +import ssh from wrapper import Wrapper GIT = 'git' @@ -43,85 +41,6 @@ GIT_DIR = 'GIT_DIR' LAST_GITDIR = None LAST_CWD = None -_ssh_proxy_path = None -_ssh_sock_path = None -_ssh_clients = [] - - -def _run_ssh_version(): - """run ssh -V to display the version number""" - return subprocess.check_output(['ssh', '-V'], stderr=subprocess.STDOUT).decode() - - -def _parse_ssh_version(ver_str=None): - """parse a ssh version string into a tuple""" - if ver_str is None: - ver_str = _run_ssh_version() - m = re.match(r'^OpenSSH_([0-9.]+)(p[0-9]+)?\s', ver_str) - if m: - return tuple(int(x) for x in m.group(1).split('.')) - else: - return () - - -@functools.lru_cache(maxsize=None) -def ssh_version(): - """return ssh version as a tuple""" - 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): - global _ssh_sock_path - if _ssh_sock_path is None: - if not create: - return None - tmp_dir = '/tmp' - if not os.path.exists(tmp_dir): - tmp_dir = tempfile.gettempdir() - if ssh_version() < (6, 7): - tokens = '%r@%h:%p' - else: - tokens = '%C' # hash of %l%h%p%r - _ssh_sock_path = os.path.join( - tempfile.mkdtemp('', 'ssh-', tmp_dir), - 'master-' + tokens) - return _ssh_sock_path - - -def _ssh_proxy(): - global _ssh_proxy_path - if _ssh_proxy_path is None: - _ssh_proxy_path = os.path.join( - os.path.dirname(__file__), - 'git_ssh') - return _ssh_proxy_path - - -def _add_ssh_client(p): - _ssh_clients.append(p) - - -def _remove_ssh_client(p): - try: - _ssh_clients.remove(p) - except ValueError: - pass - - -def terminate_ssh_clients(): - global _ssh_clients - for p in _ssh_clients: - try: - os.kill(p.pid, SIGTERM) - p.wait() - except OSError: - pass - _ssh_clients = [] - class _GitCall(object): @functools.lru_cache(maxsize=None) @@ -256,8 +175,8 @@ class GitCommand(object): if disable_editor: env['GIT_EDITOR'] = ':' if ssh_proxy: - env['REPO_SSH_SOCK'] = ssh_sock() - env['GIT_SSH'] = _ssh_proxy() + env['REPO_SSH_SOCK'] = ssh.sock() + env['GIT_SSH'] = ssh.proxy() env['GIT_SSH_VARIANT'] = 'ssh' if 'http_proxy' in env and 'darwin' == sys.platform: s = "'http.proxy=%s'" % (env['http_proxy'],) @@ -340,7 +259,7 @@ class GitCommand(object): raise GitError('%s: %s' % (command[1], e)) if ssh_proxy: - _add_ssh_client(p) + ssh.add_client(p) self.process = p if input: @@ -352,7 +271,7 @@ class GitCommand(object): try: self.stdout, self.stderr = p.communicate() finally: - _remove_ssh_client(p) + ssh.remove_client(p) self.rc = p.wait() @staticmethod -- cgit v1.2.3-54-g00ecf