Skip to content

Commit 1c2dd54

Browse files
committed
fix(cmd): throw GitCommandNotFoundError ...
... if it is not found. Previously, especially on windows, this wasn't explicit. Fixes #248, affects #126
1 parent e9f8f15 commit 1c2dd54

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

doc/source/changes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Changelog
1212
these operations to never raise. However, that behavious is undesirable as it would effectively hide the fact that there
1313
was an error. See `this issue <https://github.com/gitpython-developers/GitPython/issues/271>`_ for more information.
1414

15+
* If the git executable can't be found in the PATH or at the path provided by `GIT_PYTHON_GIT_EXECUTABLE`, this is made
16+
obvious by throwing `GitCommandNotFound`, both on unix and on windows.
17+
18+
- Those who support **GUI on windows** will now have to set `git.Git.USE_SHELL = True` to get the previous behaviour.
19+
1520
0.3.6 - Features
1621
================
1722
* **DOCS**

git/cmd.py

+33-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
stream_copy,
2727
WaitGroup
2828
)
29-
from .exc import GitCommandError
29+
from .exc import (
30+
GitCommandError,
31+
GitCommandNotFound
32+
)
3033
from git.compat import (
3134
string_types,
3235
defenc,
@@ -241,6 +244,12 @@ class Git(LazyMixin):
241244
_git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
242245
GIT_PYTHON_GIT_EXECUTABLE = os.environ.get(_git_exec_env_var, git_exec_name)
243246

247+
# If True, a shell will be used when executing git commands.
248+
# This should only be desirable on windows, see https://github.com/gitpython-developers/GitPython/pull/126
249+
# for more information
250+
# Override this value using `Git.USE_SHELL = True`
251+
USE_SHELL = False
252+
244253
class AutoInterrupt(object):
245254

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

546-
proc = Popen(command,
547-
env=env,
548-
cwd=cwd,
549-
stdin=istream,
550-
stderr=PIPE,
551-
stdout=PIPE,
552-
# Prevent cmd prompt popups on windows by using a shell ... .
553-
# See https://github.com/gitpython-developers/GitPython/pull/126
554-
shell=sys.platform == 'win32',
555-
close_fds=(os.name == 'posix'), # unsupported on windows
556-
**subprocess_kwargs
557-
)
555+
if sys.platform == 'win32':
556+
cmd_not_found_exception = WindowsError
557+
else:
558+
if sys.version_info[0] > 2:
559+
cmd_not_found_exception = FileNotFoundError # NOQA # this is defined, but flake8 doesn't know
560+
else:
561+
cmd_not_found_exception = OSError
562+
# end handle
563+
564+
try:
565+
proc = Popen(command,
566+
env=env,
567+
cwd=cwd,
568+
stdin=istream,
569+
stderr=PIPE,
570+
stdout=PIPE,
571+
shell=self.USE_SHELL,
572+
close_fds=(os.name == 'posix'), # unsupported on windows
573+
**subprocess_kwargs
574+
)
575+
except cmd_not_found_exception as err:
576+
raise GitCommandNotFound(str(err))
577+
558578
if as_process:
559579
return self.AutoInterrupt(proc, command)
560580

git/exc.py

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class NoSuchPathError(OSError):
1818
""" Thrown if a path could not be access by the system. """
1919

2020

21+
class GitCommandNotFound(Exception):
22+
"""Thrown if we cannot find the `git` executable in the PATH or at the path given by
23+
the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
24+
pass
25+
26+
2127
class GitCommandError(Exception):
2228
""" Thrown if execution of the git command fails with non-zero status code. """
2329

0 commit comments

Comments
 (0)