Skip to content

Commit 3430bde

Browse files
committed
Expanded ability of import
Renamed GIT_PYTHON_NOWARN to GIT_PYTHON_INITERR and added values for quiet import, warning import, and raise import. These respectively mean that no message or error is printed if git is non-existent, a plain warning is printed but the import succeeds, and an ImportError exception is raised.
1 parent b56d677 commit 3430bde

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

Diff for: git/cmd.py

+27-5
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,40 @@ def refresh(cls, path=None):
232232
# executable
233233
cls.GIT_PYTHON_GIT_EXECUTABLE = cls.git_exec_name
234234

235-
# test if the user didn't want a warning
236-
nowarn = os.environ.get("GIT_PYTHON_NOWARN", "false")
237-
nowarn = nowarn.lower() in ["t", "true", "y", "yes"]
238-
239-
if not nowarn:
235+
# determine what the user wanted to happen
236+
# we expect GIT_PYTHON_INITERR to either be unset or be one of
237+
# the following values:
238+
# q|quiet|s|silence
239+
# w|warn|warning
240+
# r|raise|e|error
241+
initerr_quiet = ["q", "quiet", "s", "silence"]
242+
initerr_warn = ["w", "warn", "warning"]
243+
initerr_raise = ["r", "raise", "e", "error"]
244+
245+
initerr = os.environ.get("GIT_PYTHON_INITERR", "warn").lower()
246+
if initerr in initerr_quiet:
247+
pass
248+
elif initerr in initerr_warn:
240249
print(dedent("""\
241250
WARNING: %s
242251
All git commands will error until this is rectified.
243252
244253
This initial warning can be silenced in the future by setting the environment variable:
245254
export GIT_PYTHON_NOWARN=true
246255
""") % err)
256+
elif initerr in initerr_raise:
257+
raise ImportError(err)
258+
else:
259+
err = dedent("""\
260+
GIT_PYTHON_INITERR environment variable has been set but it has been set with an invalid value.
261+
262+
Use only the following values:
263+
(1) q|quiet|s|silence: for no warning or exception
264+
(2) w|warn|warning: for a printed warning
265+
(3) r|raise|e|error: for a raised exception
266+
""")
267+
raise ImportError(err)
268+
247269
else:
248270
# after the first setup (when GIT_PYTHON_GIT_EXECUTABLE
249271
# is no longer None) we raise an exception and reset the

0 commit comments

Comments
 (0)