@@ -66,7 +66,7 @@ class Repo(object):
6666 'git_dir' is the .git repository directory, which is always set."""
6767 DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
6868
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__()`
7070 working_dir = None
7171 _working_tree_dir = None
7272 git_dir = None
@@ -202,14 +202,29 @@ def __init__(self, path=None, odbt=GitCmdObjectDB, search_parent_directories=Fal
202202 # END working dir handling
203203
204204 self .working_dir = self ._working_tree_dir or self .common_dir
205- self .git = self .GitCommandWrapperType (self .working_dir )
206205
207206 # special handling, in special times
208207 args = [osp .join (self .common_dir , 'objects' )]
209208 if issubclass (odbt , GitCmdObjectDB ):
210209 args .append (self .git )
211210 self .odb = odbt (* args )
212211
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+
213228 def __enter__ (self ):
214229 return self
215230
@@ -223,8 +238,8 @@ def __del__(self):
223238 pass
224239
225240 def close (self ):
226- if self .git :
227- self .git .clear_cache ()
241+ if self ._git :
242+ self ._git .clear_cache ()
228243 # Tempfiles objects on Windows are holding references to
229244 # open files until they are collected by the garbage
230245 # collector, thus preventing deletion.
@@ -431,7 +446,15 @@ def _get_config_path(self, config_level):
431446 elif config_level == "global" :
432447 return osp .normpath (osp .expanduser ("~/.gitconfig" ))
433448 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
435458
436459 raise ValueError ("Invalid configuration level: %r" % config_level )
437460
0 commit comments