@@ -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,42 @@ 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+     def  _get_git (self ):
213+         working_dir  =  self ._working_tree_dir  or  self .common_dir 
214+         if  self ._git :
215+             if  self ._git ._working_dir  !=  expand_path (working_dir ):
216+                 self .close ()
217+                 self ._git  =  None 
218+ 
219+         if  not  self ._git :
220+             self ._git  =  self .GitCommandWrapperType (working_dir )
221+         return  self ._git 
222+ 
223+     def  _del_git (self ):
224+         if  self ._git :
225+             self ._git .clear_cache ()
226+             self ._git  =  None 
227+             # Tempfiles objects on Windows are holding references to 
228+             # open files until they are collected by the garbage 
229+             # collector, thus preventing deletion. 
230+             # TODO: Find these references and ensure they are closed 
231+             # and deleted synchronously rather than forcing a gc 
232+             # collection. 
233+             if  is_win :
234+                 gc .collect ()
235+             gitdb .util .mman .collect ()
236+             if  is_win :
237+                 gc .collect ()
238+ 
239+     git  =  property (fget = _get_git , fdel = _del_git )
240+ 
213241    def  __enter__ (self ):
214242        return  self 
215243
@@ -223,19 +251,7 @@ def __del__(self):
223251            pass 
224252
225253    def  close (self ):
226-         if  self .git :
227-             self .git .clear_cache ()
228-             # Tempfiles objects on Windows are holding references to 
229-             # open files until they are collected by the garbage 
230-             # collector, thus preventing deletion. 
231-             # TODO: Find these references and ensure they are closed 
232-             # and deleted synchronously rather than forcing a gc 
233-             # collection. 
234-             if  is_win :
235-                 gc .collect ()
236-             gitdb .util .mman .collect ()
237-             if  is_win :
238-                 gc .collect ()
254+         del  self .git 
239255
240256    def  __eq__ (self , rhs ):
241257        if  isinstance (rhs , Repo ):
@@ -431,7 +447,15 @@ def _get_config_path(self, config_level):
431447        elif  config_level  ==  "global" :
432448            return  osp .normpath (osp .expanduser ("~/.gitconfig" ))
433449        elif  config_level  ==  "repository" :
434-             return  osp .normpath (osp .join (self ._common_dir  or  self .git_dir , "config" ))
450+             try :
451+                 config_path  =  self .git .rev_parse ("config" , git_path = True )
452+             except  GitCommandError :
453+                 return  osp .normpath (osp .join (self ._common_dir  or  self .git_dir , "config" ))
454+             else :
455+                 if  self .git ._working_dir :
456+                     return  osp .normpath (osp .join (self .git ._working_dir , config_path ))
457+                 else :
458+                     return  config_path 
435459
436460        raise  ValueError ("Invalid configuration level: %r"  %  config_level )
437461
0 commit comments