Răsfoiți Sursa

Factor out a parse_rsync_url function.

Jelmer Vernooij 7 ani în urmă
părinte
comite
482d93faa0
3 a modificat fișierele cu 44 adăugiri și 16 ștergeri
  1. 3 0
      NEWS
  2. 26 16
      dulwich/client.py
  3. 15 0
      dulwich/tests/test_client.py

+ 3 - 0
NEWS

@@ -12,6 +12,9 @@
   * Support the `http.sslVerify` and `http.sslCAInfo`
     configuration options. (Jelmer Vernooij)
 
+  * Factor out `dulwich.client.parse_rsync_url` function.
+    (Jelmer Vernooij)
+
  API CHANGES
 
   * Index.iterblobs has been renamed to Index.iterobjects.

+ 26 - 16
dulwich/client.py

@@ -1601,6 +1601,25 @@ def get_transport_and_path_from_url(url, config=None, **kwargs):
     raise ValueError("unknown scheme '%s'" % parsed.scheme)
 
 
+def parse_rsync_url(location):
+    """Parse a rsync-style URL."""
+    if ':' in location and '@' not in location:
+        # SSH with no user@, zero or one leading slash.
+        (host, path) = location.split(':', 1)
+        user = None
+    elif ':' in location:
+        # SSH with user@host:foo.
+        user_host, path = location.split(':', 1)
+        if '@' in user_host:
+            user, host = user_host.rsplit('@', 1)
+        else:
+            user = None
+            host = user_host
+    else:
+        raise ValueError('not a valid rsync-style URL')
+    return (user, host, path)
+
+
 def get_transport_and_path(location, **kwargs):
     """Obtain a git client from a URL.
 
@@ -1622,19 +1641,10 @@ def get_transport_and_path(location, **kwargs):
         # Windows local path
         return default_local_git_client_cls(**kwargs), location
 
-    if ':' in location and '@' not in location:
-        # SSH with no user@, zero or one leading slash.
-        (hostname, path) = location.split(':', 1)
-        return SSHGitClient(hostname, **kwargs), path
-    elif ':' in location:
-        # SSH with user@host:foo.
-        user_host, path = location.split(':', 1)
-        if '@' in user_host:
-            user, host = user_host.rsplit('@', 1)
-        else:
-            user = None
-            host = user_host
-        return SSHGitClient(host, username=user, **kwargs), path
-
-    # Otherwise, assume it's a local path.
-    return default_local_git_client_cls(**kwargs), location
+    try:
+        (username, hostname, path) = parse_rsync_url(location)
+    except ValueError:
+        # Otherwise, assume it's a local path.
+        return default_local_git_client_cls(**kwargs), location
+    else:
+        return SSHGitClient(hostname, username=username, **kwargs), path

+ 15 - 0
dulwich/tests/test_client.py

@@ -56,6 +56,7 @@ from dulwich.client import (
     default_urllib3_manager,
     get_transport_and_path,
     get_transport_and_path_from_url,
+    parse_rsync_url,
     )
 from dulwich.config import (
     ConfigDict,
@@ -1112,3 +1113,17 @@ class PuttySSHVendorTests(TestCase):
         args = command.proc.args
 
         self.assertListEqual(expected, args[0])
+
+
+class RsyncUrlTests(TestCase):
+
+    def test_simple(self):
+        self.assertEqual(
+                parse_rsync_url('foo:bar/path'),
+                (None, 'foo', 'bar/path'))
+        self.assertEqual(
+                parse_rsync_url('user@foo:bar/path'),
+                ('user', 'foo', 'bar/path'))
+
+    def test_path(self):
+        self.assertRaises(ValueError, parse_rsync_url, '/path')