Skip to content

Commit

Permalink
fix(cmd): throw GitCommandNotFoundError ...
Browse files Browse the repository at this point in the history
... if it is not found. Previously, especially on windows, this wasn't
explicit.

Fixes #248, affects #126
  • Loading branch information
Byron committed Apr 8, 2015
1 parent e9f8f15 commit 1c2dd54
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
5 changes: 5 additions & 0 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Changelog
these operations to never raise. However, that behavious is undesirable as it would effectively hide the fact that there
was an error. See `this issue <https://github.com/gitpython-developers/GitPython/issues/271>`_ for more information.

* If the git executable can't be found in the PATH or at the path provided by `GIT_PYTHON_GIT_EXECUTABLE`, this is made
obvious by throwing `GitCommandNotFound`, both on unix and on windows.

- Those who support **GUI on windows** will now have to set `git.Git.USE_SHELL = True` to get the previous behaviour.

0.3.6 - Features
================
* **DOCS**
Expand Down
46 changes: 33 additions & 13 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
stream_copy,
WaitGroup
)
from .exc import GitCommandError
from .exc import (
GitCommandError,
GitCommandNotFound
)
from git.compat import (
string_types,
defenc,
Expand Down Expand Up @@ -241,6 +244,12 @@ class Git(LazyMixin):
_git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
GIT_PYTHON_GIT_EXECUTABLE = os.environ.get(_git_exec_env_var, git_exec_name)

# If True, a shell will be used when executing git commands.
# This should only be desirable on windows, see https://github.com/gitpython-developers/GitPython/pull/126
# for more information
# Override this value using `Git.USE_SHELL = True`
USE_SHELL = False

class AutoInterrupt(object):

"""Kill/Interrupt the stored process instance once this instance goes out of scope. It is
Expand Down Expand Up @@ -543,18 +552,29 @@ def execute(self, command,
env["LC_MESSAGES"] = "C"
env.update(self._environment)

proc = Popen(command,
env=env,
cwd=cwd,
stdin=istream,
stderr=PIPE,
stdout=PIPE,
# Prevent cmd prompt popups on windows by using a shell ... .
# See https://github.com/gitpython-developers/GitPython/pull/126
shell=sys.platform == 'win32',
close_fds=(os.name == 'posix'), # unsupported on windows
**subprocess_kwargs
)
if sys.platform == 'win32':
cmd_not_found_exception = WindowsError
else:
if sys.version_info[0] > 2:
cmd_not_found_exception = FileNotFoundError # NOQA # this is defined, but flake8 doesn't know
else:
cmd_not_found_exception = OSError
# end handle

try:
proc = Popen(command,
env=env,
cwd=cwd,
stdin=istream,
stderr=PIPE,
stdout=PIPE,
shell=self.USE_SHELL,
close_fds=(os.name == 'posix'), # unsupported on windows
**subprocess_kwargs
)
except cmd_not_found_exception as err:
raise GitCommandNotFound(str(err))

if as_process:
return self.AutoInterrupt(proc, command)

Expand Down
6 changes: 6 additions & 0 deletions git/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class NoSuchPathError(OSError):
""" Thrown if a path could not be access by the system. """


class GitCommandNotFound(Exception):
"""Thrown if we cannot find the `git` executable in the PATH or at the path given by
the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
pass


class GitCommandError(Exception):
""" Thrown if execution of the git command fails with non-zero status code. """

Expand Down

0 comments on commit 1c2dd54

Please sign in to comment.