Skip to content

Commit 79a36a5

Browse files
committed
Minor additional cleanup
Added additional information in the import warning/error that tells the user how to silence the warning/error. Also added a GIT_OK variable that allows for a quick check whether the refresh has succeeded instead of needing to test an actual git command.
1 parent 2b3e769 commit 79a36a5

File tree

3 files changed

+59
-28
lines changed

3 files changed

+59
-28
lines changed

‎git/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ def _init_externals():
6060

6161

6262
#{ Initialize git executable path
63+
GIT_OK = None
64+
6365
def refresh(path=None):
6466
"""Convenience method for setting the git executable path."""
67+
global GIT_OK
68+
GIT_OK = False
69+
6570
if not Git.refresh(path=path):
6671
return
6772
if not FetchInfo.refresh():
6873
return
74+
75+
GIT_OK = True
6976
#} END initialize git executable path
7077

7178
#################

‎git/cmd.py

+50-27
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,15 @@ def __setstate__(self, d):
191191

192192
# Provide the full path to the git executable. Otherwise it assumes git is in the path
193193
_git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
194+
_refresh_env_var = "GIT_PYTHON_REFRESH"
194195
GIT_PYTHON_GIT_EXECUTABLE = None
195196
# note that the git executable is actually found during the refresh step in
196197
# the top level __init__
197198

198199
@classmethod
199200
def refresh(cls, path=None):
200-
"""This gets called by the refresh function (see the top level __init__).
201+
"""This gets called by the refresh function (see the top level
202+
__init__).
201203
"""
202204
# discern which path to refresh with
203205
if path is not None:
@@ -214,17 +216,21 @@ def refresh(cls, path=None):
214216
try:
215217
cls().version()
216218
has_git = True
217-
except GitCommandNotFound:
219+
except (GitCommandNotFound, PermissionError):
220+
# - a GitCommandNotFound error is spawned by ourselves
221+
# - a PermissionError is spawned if the git executable provided
222+
# cannot be executed for whatever reason
218223
pass
219224

220225
# warn or raise exception if test failed
221226
if not has_git:
222227
err = dedent("""\
223-
Bad git executable. The git executable must be specified in one of the following ways:
224-
(1) be included in your $PATH, or
225-
(2) be set via $GIT_PYTHON_GIT_EXECUTABLE, or
226-
(3) explicitly set via git.refresh.
227-
""")
228+
Bad git executable.
229+
The git executable must be specified in one of the following ways:
230+
- be included in your $PATH
231+
- be set via $%s
232+
- explicitly set via git.refresh()
233+
""") % cls._git_exec_env_var
228234

229235
# revert to whatever the old_git was
230236
cls.GIT_PYTHON_GIT_EXECUTABLE = old_git
@@ -241,36 +247,53 @@ def refresh(cls, path=None):
241247
# 1|w|warn|warning
242248
# 2|r|raise|e|error
243249

244-
mode = os.environ.get("GIT_PYTHON_REFRESH", "raise").lower()
250+
mode = os.environ.get(cls._refresh_env_var, "raise").lower()
245251

246-
quiet = ["0", "q", "quiet", "s", "silence", "n", "none"]
247-
warn = ["1", "w", "warn", "warning"]
248-
error = ["2", "e", "error", "r", "raise"]
252+
quiet = ["quiet", "q", "silence", "s", "none", "n", "0"]
253+
warn = ["warn", "w", "warning", "1"]
254+
error = ["error", "e", "raise", "r", "2"]
249255

250256
if mode in quiet:
251257
pass
252-
elif mode in warn:
253-
print(dedent("""\
254-
WARNING: %s
258+
elif mode in warn or mode in error:
259+
err = dedent("""\
260+
%s
255261
All git commands will error until this is rectified.
256262
257-
This initial warning can be silenced in the future by setting the environment variable:
258-
export GIT_PYTHON_REFRESH=quiet
259-
""") % err)
260-
elif mode in error:
261-
raise ImportError(err)
263+
This initial warning can be silenced or aggravated in the future by setting the
264+
$%s environment variable. Use one of the following values:
265+
- %s: for no warning or exception
266+
- %s: for a printed warning
267+
- %s: for a raised exception
268+
269+
Example:
270+
export %s=%s
271+
""") % (
272+
err,
273+
cls._refresh_env_var,
274+
"|".join(quiet),
275+
"|".join(warn),
276+
"|".join(error),
277+
cls._refresh_env_var,
278+
quiet[0])
279+
280+
if mode in warn:
281+
print("WARNING: %s" % err)
282+
else:
283+
raise ImportError(err)
262284
else:
263285
err = dedent("""\
264-
GIT_PYTHON_REFRESH environment variable has been set but it has been set with an invalid value.
286+
%s environment variable has been set but it has been set with an invalid value.
265287
266288
Use only the following values:
267-
(1) {quiet}: for no warning or exception
268-
(2) {warn}: for a printed warning
269-
(3) {error}: for a raised exception
270-
""").format(
271-
quiet="|".join(quiet),
272-
warn="|".join(warn),
273-
error="|".join(error))
289+
- %s: for no warning or exception
290+
- %s: for a printed warning
291+
- %s: for a raised exception
292+
""") % (
293+
cls._refresh_env_var,
294+
"|".join(quiet),
295+
"|".join(warn),
296+
"|".join(error))
274297
raise ImportError(err)
275298

276299
# we get here if this was the init refresh and the refresh mode

‎git/remote.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ class FetchInfo(object):
221221

222222
@classmethod
223223
def refresh(cls):
224-
"""This gets called by the setup function (see the top level __init__).
224+
"""This gets called by the refresh function (see the top level
225+
__init__).
225226
"""
226227
# clear the old values in _flag_map
227228
try:

0 commit comments

Comments
 (0)