Skip to content

Commit 24abf10

Browse files
committed
Allow Pathlike urls and destinations when cloning
1 parent e3f38ff commit 24abf10

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

git/repo/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,8 +1362,10 @@ def _clone(
13621362
odbt = kwargs.pop("odbt", odb_default_type)
13631363

13641364
# When pathlib.Path or other class-based path is passed
1365+
if not isinstance(url, str):
1366+
url = url.__fspath__()
13651367
if not isinstance(path, str):
1366-
path = str(path)
1368+
path = path.__fspath__()
13671369

13681370
## A bug win cygwin's Git, when `--bare` or `--separate-git-dir`
13691371
# it prepends the cwd or(?) the `url` into the `path, so::
@@ -1380,7 +1382,7 @@ def _clone(
13801382
multi = shlex.split(" ".join(multi_options))
13811383

13821384
if not allow_unsafe_protocols:
1383-
Git.check_unsafe_protocols(str(url))
1385+
Git.check_unsafe_protocols(url)
13841386
if not allow_unsafe_options:
13851387
Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=cls.unsafe_git_clone_options)
13861388
if not allow_unsafe_options and multi_options:

test/test_clone.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This module is part of GitPython and is released under the
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

4+
from dataclasses import dataclass
45
import os
56
import os.path as osp
67
import pathlib
@@ -45,7 +46,20 @@ def test_checkout_in_non_empty_dir(self, rw_dir):
4546
def test_clone_from_pathlib(self, rw_dir):
4647
original_repo = Repo.init(osp.join(rw_dir, "repo"))
4748

48-
Repo.clone_from(original_repo.git_dir, pathlib.Path(rw_dir) / "clone_pathlib")
49+
Repo.clone_from(pathlib.Path(original_repo.git_dir), pathlib.Path(rw_dir) / "clone_pathlib")
50+
51+
@with_rw_directory
52+
def test_clone_from_pathlike(self, rw_dir):
53+
original_repo = Repo.init(osp.join(rw_dir, "repo"))
54+
55+
@dataclass
56+
class PathLikeMock:
57+
path: str
58+
59+
def __fspath__(self) -> str:
60+
return self.path
61+
62+
Repo.clone_from(PathLikeMock(original_repo.git_dir), PathLikeMock(os.path.join(rw_dir, "clone_pathlike")))
4963

5064
@with_rw_directory
5165
def test_clone_from_pathlib_withConfig(self, rw_dir):

0 commit comments

Comments
 (0)