Explorar o código

don't hard-code protocol v2 and tweak version comparisons to be future-proof

Based on a suggestion by Jelmer
Stefan Sperling hai 9 meses
pai
achega
5c1a5ecedb
Modificáronse 3 ficheiros con 27 adicións e 11 borrados
  1. 8 4
      dulwich/client.py
  2. 4 2
      tests/compat/test_client.py
  3. 15 5
      tests/test_client.py

+ 8 - 4
dulwich/client.py

@@ -1923,8 +1923,10 @@ class SubprocessSSHVendor(SSHVendor):
         if key_filename:
             args.extend(["-i", str(key_filename)])
 
-        if protocol_version is None or protocol_version == 2:
-            args.extend(["-o", "SetEnv GIT_PROTOCOL=version=2"])
+        if protocol_version is None:
+            protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_FETCH
+        if protocol_version > 0:
+            args.extend(["-o", f"SetEnv GIT_PROTOCOL=version={protocol_version}"])
 
         if username:
             host = f"{username}@{host}"
@@ -1992,8 +1994,10 @@ class PLinkSSHVendor(SSHVendor):
         # does not work then the server should behave as if we had requested
         # protocol version 0.
         env = copy.deepcopy(os.environ)
-        if protocol_version is None or protocol_version == 2:
-            env["GIT_PROTOCOL"] = "version=2"
+        if protocol_version is None:
+            protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_FETCH
+        if protocol_version > 0:
+            env["GIT_PROTOCOL"] = f"version={protocol_version}"
 
         proc = subprocess.Popen(
             [*args, command],

+ 4 - 2
tests/compat/test_client.py

@@ -435,8 +435,10 @@ class TestSSHVendor:
         cmd = cmd.split("-", 1)
         path = path.replace("'", "")
         env = dict(os.environ)
-        if protocol_version is None or protocol_version == 2:
-            env["GIT_PROTOCOL"] = "version=2"
+        if protocol_version is None:
+            protocol_version = protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH
+        if protocol_version > 0:
+            env["GIT_PROTOCOL"] = f"version={protocol_version}"
 
         p = subprocess.Popen(
             [*cmd, path],

+ 15 - 5
tests/test_client.py

@@ -58,7 +58,7 @@ from dulwich.client import (
 from dulwich.config import ConfigDict
 from dulwich.objects import Commit, Tree
 from dulwich.pack import pack_objects_to_data, write_pack_data, write_pack_objects
-from dulwich.protocol import TCP_GIT_PORT, Protocol
+from dulwich.protocol import DEFAULT_GIT_PROTOCOL_VERSION_FETCH, TCP_GIT_PORT, Protocol
 from dulwich.repo import MemoryRepo, Repo
 from dulwich.tests.utils import open_repo, setup_warning_catcher, tear_down_repo
 
@@ -1539,8 +1539,13 @@ class SubprocessSSHVendorTests(TestCase):
             "2200",
             "-i",
             "/tmp/id_rsa",
-            "-o",
-            "SetEnv GIT_PROTOCOL=version=2",
+        ]
+        if DEFAULT_GIT_PROTOCOL_VERSION_FETCH:
+            expected += [
+                "-o",
+                f"SetEnv GIT_PROTOCOL=version={DEFAULT_GIT_PROTOCOL_VERSION_FETCH}",
+            ]
+        expected += [
             "user@host",
             "git-clone-url",
         ]
@@ -1564,8 +1569,13 @@ class SubprocessSSHVendorTests(TestCase):
             "-o",
             "Option=Value",
             "-x",
-            "-o",
-            "SetEnv GIT_PROTOCOL=version=2",
+        ]
+        if DEFAULT_GIT_PROTOCOL_VERSION_FETCH:
+            expected += [
+                "-o",
+                f"SetEnv GIT_PROTOCOL=version={DEFAULT_GIT_PROTOCOL_VERSION_FETCH}",
+            ]
+        expected += [
             "host",
             "git-clone-url",
         ]