Skip to content

Commit

Permalink
Fix handling of network locations
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Dec 19, 2024
1 parent a232638 commit 6512b9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
11 changes: 2 additions & 9 deletions dulwich/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@
ObjectID = bytes


# url2pathname is lazily imported
url2pathname = None


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -2868,7 +2864,6 @@ def _win32_url_to_path(parsed) -> str:
https://datatracker.ietf.org/doc/html/rfc8089
"""
assert sys.platform == "win32" or os.name == "nt"
assert parsed.scheme == "file"

_, netloc, path, _, _, _ = parsed
Expand All @@ -2886,10 +2881,8 @@ def _win32_url_to_path(parsed) -> str:
else:
raise NotImplementedError("Non-local file URLs are not supported")

global url2pathname
if url2pathname is None:
from urllib.request import url2pathname # type: ignore
return url2pathname(netloc + path) # type: ignore
from nturl2path import url2pathname
return url2pathname(netloc + path)


def get_transport_and_path_from_url(
Expand Down
52 changes: 28 additions & 24 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
TraditionalGitClient,
_extract_symrefs_and_agent,
_remote_error_from_stderr,
_win32_url_to_path,
check_wants,
default_urllib3_manager,
get_credentials_from_store,
Expand Down Expand Up @@ -671,33 +672,36 @@ def test_file(self) -> None:
self.assertIsInstance(c, LocalGitClient)
self.assertEqual("/home/jelmer/foo", path)

def test_win32_url_to_path(self):
def check(url, expected):
parsed = urlparse(url)
self.assertEqual(_win32_url_to_path(parsed), expected)
check("file:C:/foo.bar/baz", "C:\\foo.bar\\baz")
check("file:/C:/foo.bar/baz", "C:\\foo.bar\\baz")
check("file://C:/foo.bar/baz", "C:\\foo.bar\\baz")
check("file:///C:/foo.bar/baz", "C:\\foo.bar\\baz")

@patch("os.name", "nt")
@patch("sys.platform", "win32")
def test_file_win(self) -> None:
# `_win32_url_to_path` uses urllib.request.url2pathname, which is set to
# `ntutl2path.url2pathname` when `os.name==nt`
from nturl2path import url2pathname

with patch("dulwich.client.url2pathname", url2pathname):
expected = "C:\\foo.bar\\baz"
for file_url in [
"file:C:/foo.bar/baz",
"file:/C:/foo.bar/baz",
"file://C:/foo.bar/baz",
"file://C://foo.bar//baz",
"file:///C:/foo.bar/baz",
]:
c, path = get_transport_and_path(file_url)
self.assertIsInstance(c, LocalGitClient)
self.assertEqual(path, expected)

for remote_url in [
"file://host.example.com/C:/foo.bar/baz"
"file://host.example.com/C:/foo.bar/baz"
"file:////host.example/foo.bar/baz",
]:
with self.assertRaises(NotImplementedError):
c, path = get_transport_and_path(remote_url)
expected = "C:\\foo.bar\\baz"
for file_url in [
"file:C:/foo.bar/baz",
"file:/C:/foo.bar/baz",
"file://C:/foo.bar/baz",
"file:///C:/foo.bar/baz",
]:
c, path = get_transport_and_path(file_url)
self.assertIsInstance(c, LocalGitClient)
self.assertEqual(path, expected)

for remote_url in [
"file://host.example.com/C:/foo.bar/baz"
"file://host.example.com/C:/foo.bar/baz"
"file:////host.example/foo.bar/baz",
]:
with self.assertRaises(NotImplementedError):
c, path = get_transport_and_path(remote_url)


class TestSSHVendor:
Expand Down

0 comments on commit 6512b9b

Please sign in to comment.