Skip to content

import git fails when current working directory doesn't exist #1319

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

Closed
bluenote10 opened this issue Aug 10, 2021 · 3 comments
Closed

import git fails when current working directory doesn't exist #1319

bluenote10 opened this issue Aug 10, 2021 · 3 comments

Comments

@bluenote10
Copy link

bluenote10 commented Aug 10, 2021

Steps to reproduce:

$ mkdir /tmp/dir_to_be_deleted
$ cd /tmp/dir_to_be_deleted
$ rmdir /tmp/dir_to_be_deleted
$ python -c "import git"

Crashes with

Traceback (most recent call last):
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/__init__.py", line 87, in <module>
    refresh()
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/__init__.py", line 76, in refresh
    if not Git.refresh(path=path):
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/cmd.py", line 243, in refresh
    cls().version()
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/cmd.py", line 585, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/cmd.py", line 1124, in _call_process
    return self.execute(call, **exec_kwargs)
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/cmd.py", line 782, in execute
    cwd = self._working_dir or os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/me/.virtualenvs/default/lib/python3.8/site-packages/git/__init__.py", line 89, in <module>
    raise ImportError('Failed to initialize: {0}'.format(exc)) from exc
ImportError: Failed to initialize: [Errno 2] No such file or directory

In general os.getcwd() needs to guarded against FileNotFoundError because of the possibility that there is no valid working directory.

GitPython version: 3.1.18

This is certainly not a big issue, but it can be very confusing, and there is (probabaly) not theoretical reason why GitPython cannot work in this case.

@Byron
Copy link
Member

Byron commented Aug 11, 2021

Thanks for the report. I understand that it's probably a bit strange that it needs the CWD to just be imported.
AFAIK it tries to get the git version string while initializing some global state. Maybe that could be done lazily instead to avoid this issue.

@bluenote10
Copy link
Author

Yes lazily would probably be the most elegant solution, because it is in the Python-best-practices-spirit of not executing anything that can throw at import time.

If you want a quick-and-dirty solution though, it should also be sufficient to replace

cwd = self._working_dir or os.getcwd()

with

try:
    cwd = self._working_dir or os.getcwd()
except FileNotFoundError:
    cwd = None

This should work, because cwd is only used in subprocess.Popen and passing cwd=None would be fine. If running other git commands than version that actually require a cwd, git would error anyway because of the "broken" working directory.

Byron added a commit that referenced this issue Aug 11, 2021

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
Byron added a commit that referenced this issue Aug 11, 2021

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
@Byron
Copy link
Member

Byron commented Aug 11, 2021

Thanks a lot for the suggestion. Since it was added as is I assume this particular issue will be resolved. Maybe there are other stumbling blocks though, and if so please feel free to post here so the issue can be reopened.

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