Skip to content

import git fails when current working directory is not accessible #1334

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
comzyh opened this issue Sep 4, 2021 · 1 comment
Closed

import git fails when current working directory is not accessible #1334

comzyh opened this issue Sep 4, 2021 · 1 comment

Comments

@comzyh
Copy link

comzyh commented Sep 4, 2021

Very similar to #1319
Steps to reproduce:

$ mkdir /tmp/foo
$ cd /tmp/foo
$ chmod -x .
$ python -c "import git"

Crash with:

Traceback (most recent call last):
  File "/home/comzyh/.local/lib/python3.8/site-packages/git/__init__.py", line 87, in <module>
    refresh()
  File "/home/comzyh/.local/lib/python3.8/site-packages/git/__init__.py", line 76, in refresh
    if not Git.refresh(path=path):
  File "/home/comzyh/.local/lib/python3.8/site-packages/git/cmd.py", line 305, in refresh
    raise ImportError(err)
ImportError: Bad git executable.
The git executable must be specified in one of the following ways:
    - be included in your $PATH
    - be set via $GIT_PYTHON_GIT_EXECUTABLE
    - explicitly set via git.refresh()

All git commands will error until this is rectified.

This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
    - quiet|q|silence|s|none|n|0: for no warning or exception
    - warn|w|warning|1: for a printed warning
    - error|e|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet


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

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/comzyh/.local/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: Bad git executable.
The git executable must be specified in one of the following ways:
    - be included in your $PATH
    - be set via $GIT_PYTHON_GIT_EXECUTABLE
    - explicitly set via git.refresh()

All git commands will error until this is rectified.

This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
    - quiet|q|silence|s|none|n|0: for no warning or exception
    - warn|w|warning|1: for a printed warning
    - error|e|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet

GitPython: 3.1.20

GitPython will run a git version when importing, but if the current workding dir is not accessible, the Popen clause will fail.

cwd=cwd,

This can be fixed by adding 2 lines after 5b3669e, but I'm not sure if this fix will break some of your assumption.

        # Allow the user to have the command executed in their working dir.
        try:
            cwd = self._working_dir or os.getcwd()  # type: Union[None, str]            
        except FileNotFoundError:
            cwd = None
        if not os.access(cwd, os.X_OK):
            cwd = None

I found this issue when I'm a previliedged user and try to switch user using sudo su <user> and then run a Python script which need a package depending on GitPython.

Byron added a commit that referenced this issue Sep 5, 2021
Byron added a commit that referenced this issue Sep 5, 2021
@Byron
Copy link
Member

Byron commented Sep 5, 2021

Thanks a lot, I have added the additional check assuming this fixes the issue described here.

@Byron Byron added this to the v3.1.21 - Bugfixes milestone Sep 5, 2021
@Byron Byron closed this as completed in 5da76e8 Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants