@@ -190,61 +190,60 @@ def __setstate__(self, d):
190
190
USE_SHELL = False
191
191
192
192
# Provide the full path to the git executable. Otherwise it assumes git is in the path
193
- @ classmethod
194
- def refresh ( cls , path = None ):
195
- """Convenience method for refreshing the git executable path."""
196
- cls . setup ( path = path )
193
+ _git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
194
+ GIT_PYTHON_GIT_EXECUTABLE = None
195
+ # note that the git executable is actually found during the setup step in
196
+ # the top level __init__
197
197
198
198
@classmethod
199
- def setup (cls , path = None ):
200
- """Convenience method for setting the git executable path."""
199
+ def refresh (cls , path = None ):
200
+ """This gets called by the setup function (see the top level __init__).
201
+ """
202
+ # discern which path to refresh with
201
203
if path is not None :
202
- # use the path the user gave
203
- os .environ [cls ._git_exec_env_var ] = path
204
- elif cls ._git_exec_env_var in os .environ :
205
- # fall back to the environment variable that's already set
206
- pass
204
+ new_git = os .path .abspath (path )
207
205
else :
208
- # hope that git can be found on the user's $PATH
209
- pass
206
+ new_git = os .environ .get (cls ._git_exec_env_var , cls .git_exec_name )
210
207
208
+ # keep track of the old and new git executable path
211
209
old_git = cls .GIT_PYTHON_GIT_EXECUTABLE
212
- new_git = os .environ .get (cls ._git_exec_env_var , cls .git_exec_name )
213
210
cls .GIT_PYTHON_GIT_EXECUTABLE = new_git
214
211
212
+ # test if the new git executable path is valid
215
213
has_git = False
216
214
try :
217
215
cls ().version ()
218
216
has_git = True
219
217
except GitCommandNotFound :
220
218
pass
221
219
220
+ # warn or raise exception if test failed
222
221
if not has_git :
223
222
err = dedent ("""\
224
223
Bad git executable. The git executable must be specified in one of the following ways:
225
224
(1) be included in your $PATH, or
226
225
(2) be set via $GIT_PYTHON_GIT_EXECUTABLE, or
227
- (3) explicitly call git.cmd. setup with the full path .
226
+ (3) explicitly set via git.setup (or git.refresh) .
228
227
""" )
229
228
230
229
if old_git is None :
231
230
# on the first setup (when GIT_PYTHON_GIT_EXECUTABLE is
232
231
# None) we only warn the user and simply set the default
233
232
# executable
234
233
cls .GIT_PYTHON_GIT_EXECUTABLE = cls .git_exec_name
235
- print ("WARNING: %s" % err )
234
+ print (dedent ("""\
235
+ WARNING: %s
236
+ All git commands will error until this is rectified.
237
+ """ ) % err )
236
238
else :
237
239
# after the first setup (when GIT_PYTHON_GIT_EXECUTABLE
238
240
# is no longer None) we raise an exception and reset the
239
241
# GIT_PYTHON_GIT_EXECUTABLE to whatever the value was
240
242
# previously
241
- cls .GIT_PYTHON_GIT_EXECUTABLE = old_name
243
+ cls .GIT_PYTHON_GIT_EXECUTABLE = old_git
242
244
raise GitCommandNotFound ("git" , err )
243
245
244
- _git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
245
- # immediately set with the default value ("git")
246
- GIT_PYTHON_GIT_EXECUTABLE = None
247
- # see the setup performed below
246
+ return has_git
248
247
249
248
@classmethod
250
249
def is_cygwin (cls ):
@@ -1024,16 +1023,3 @@ def clear_cache(self):
1024
1023
self .cat_file_all = None
1025
1024
self .cat_file_header = None
1026
1025
return self
1027
-
1028
-
1029
-
1030
- # this is where the git executable is setup
1031
- def setup (path = None ):
1032
- Git .setup (path = path )
1033
-
1034
-
1035
- def refresh (path = None ):
1036
- Git .refresh (path = path )
1037
-
1038
-
1039
- setup ()
0 commit comments