Skip to content

git.Repo.clone_from() not working correctly in WSL #1902

Open
@paolzpk

Description

@paolzpk

I am using GitPython both in windows and WSL and I noticed that paths are not handled coherently between the two (i.e. C:/ vs /mnt/c). It is possible to go around it most of the time using wslpath and converting from Windows to WSL and vice versa but it looks that the clone_from() ends up raising an exception regardless if the to_path is in WSL format or Windows format.

Steps to reproduce:

from pathlib import Path
import git
import traceback
import subprocess

def to_windows_path(wsl_path: Path) -> Path:
    """ Converts a WSL path to windows path - works only if the WSL path actually exists """
    wslpath_process = subprocess.run(['wslpath', '-w', wsl_path], text=True, capture_output=True)
    return Path(wslpath_process.stdout.strip())

try:
  git.Repo.clone_from('git@github.com:gitpython-developers/GitPython.git', Path.cwd() / 'gitpython')
except git.GitCommandError:
  print(traceback.format_exc())

try:
  git.Repo.clone_from('git@github.com:gitpython-developers/GitPython.git', to_windows_path(Path.cwd()) / 'gitpython')
except git.NoSuchPathError:
  print(traceback.format_exc())

Expected behaviour:
When the script is launched from a python instance running on WSL the repo is cloned in $PWD/gitpython.

Actual behaviour:

[if (Path.cwd() / 'gitpython').exists() == False before running the script]
[git.version == '3.1.40']
python the_script.py

File ~/anaconda3/lib/python3.11/site-packages/git/repo/base.py:1328, in Repo.clone_from(cls, url, to_path, progress, env, multi_options, allow_unsafe_protocols, allow_unsafe_options, **kwargs)
1326 if env is not None:
1327 git.update_environment(**env)
-> 1328 return cls._clone(
1329 git,
1330 url,
1331 to_path,
1332 GitCmdObjectDB,
1333 progress,
1334 multi_options,
1335 allow_unsafe_protocols=allow_unsafe_protocols,
1336 allow_unsafe_options=allow_unsafe_options,
1337 **kwargs,
1338 )

File ~/anaconda3/lib/python3.11/site-packages/git/repo/base.py:1244, in Repo._clone(cls, git, url, path, odb_default_type, progress, multi_options, allow_unsafe_protocols, allow_unsafe_options, **kwargs)
1241 if not osp.isabs(path):
1242 path = osp.join(git._working_dir, path) if git._working_dir is not None else path
-> 1244 repo = cls(path, odbt=odbt)
1246 # retain env values that were passed to _clone()
1247 repo.git.update_environment(**git.environment())

File ~/anaconda3/lib/python3.11/site-packages/git/repo/base.py:215, in Repo.init(self, path, odbt, search_parent_directories, expand_vars)
213 if epath is not None:
214 if not os.path.exists(epath):
--> 215 raise NoSuchPathError(epath)
217 ## Walk up the path to find the .git dir.
218 #
219 curpath = epath
NoSuchPathError: /mnt/c/Users/user.name/Documents/C:\Users\user.name\Documents/gitpython

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions