@@ -66,7 +66,7 @@ class Repo(object):
66
66
'git_dir' is the .git repository directory, which is always set."""
67
67
DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
68
68
69
- git = None # Must exist, or __del__ will fail in case we raise on `__init__()`
69
+ _git = None # Must exist, or __del__ will fail in case we raise on `__init__()`
70
70
working_dir = None
71
71
_working_tree_dir = None
72
72
git_dir = None
@@ -202,14 +202,29 @@ def __init__(self, path=None, odbt=GitCmdObjectDB, search_parent_directories=Fal
202
202
# END working dir handling
203
203
204
204
self .working_dir = self ._working_tree_dir or self .common_dir
205
- self .git = self .GitCommandWrapperType (self .working_dir )
206
205
207
206
# special handling, in special times
208
207
args = [osp .join (self .common_dir , 'objects' )]
209
208
if issubclass (odbt , GitCmdObjectDB ):
210
209
args .append (self .git )
211
210
self .odb = odbt (* args )
212
211
212
+ class GitCommand (object ):
213
+ def __get__ (self , instance , owner ):
214
+ if instance is not None :
215
+ working_dir = instance ._working_tree_dir or instance .common_dir
216
+ if instance ._git :
217
+ if instance ._git ._working_dir != working_dir :
218
+ instance .close ()
219
+ instance ._git = None
220
+
221
+ if not instance ._git :
222
+ instance ._git = instance .GitCommandWrapperType (working_dir )
223
+ return instance ._git
224
+ raise AttributeError ('git' )
225
+
226
+ git = GitCommand ()
227
+
213
228
def __enter__ (self ):
214
229
return self
215
230
@@ -223,8 +238,8 @@ def __del__(self):
223
238
pass
224
239
225
240
def close (self ):
226
- if self .git :
227
- self .git .clear_cache ()
241
+ if self ._git :
242
+ self ._git .clear_cache ()
228
243
# Tempfiles objects on Windows are holding references to
229
244
# open files until they are collected by the garbage
230
245
# collector, thus preventing deletion.
@@ -431,7 +446,15 @@ def _get_config_path(self, config_level):
431
446
elif config_level == "global" :
432
447
return osp .normpath (osp .expanduser ("~/.gitconfig" ))
433
448
elif config_level == "repository" :
434
- return osp .normpath (osp .join (self ._common_dir or self .git_dir , "config" ))
449
+ try :
450
+ config_path = self .git .rev_parse ("config" , git_path = True )
451
+ except GitCommandError :
452
+ return osp .normpath (osp .join (self ._common_dir or self .git_dir , "config" ))
453
+ else :
454
+ if self .git ._working_dir :
455
+ return osp .normpath (osp .join (self .git ._working_dir , config_path ))
456
+ else :
457
+ return config_path
435
458
436
459
raise ValueError ("Invalid configuration level: %r" % config_level )
437
460
0 commit comments