Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
paolzpk opened this issue Apr 15, 2024 · 2 comments
Open

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

paolzpk opened this issue Apr 15, 2024 · 2 comments

Comments

@paolzpk
Copy link

paolzpk commented Apr 15, 2024

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

@Byron
Copy link
Member

Byron commented Apr 15, 2024

Thanks for reporting.

Even though I couldn't reproduce it, I wouldn't be surprised if something is going wrong here. It might be that it doesn't recognise the path as absolute, and makes it absolute by pre-pending the CWD all by itself (which it shouldn't have to do at all).

@paolzpk
Copy link
Author

paolzpk commented Apr 16, 2024

Indeed with relative paths it looks like it is working correctly, but still it would be nice if it worked correctly with absolute paths as well. In my project I think I will be able to get around this issue, but if I have time I will look into your repo and propose some changes!
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants