Skip to content

Commit 0b6b90f

Browse files
bdauvergneByron
authored andcommitted
use git rev-parse to look for config file
1 parent daa3f35 commit 0b6b90f

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

git/repo/base.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

git/test/lib/helper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ def setUpClass(cls):
366366

367367
@classmethod
368368
def tearDownClass(cls):
369-
cls.rorepo.git.clear_cache()
370-
cls.rorepo.git = None
369+
del cls.rorepo.git
371370

372371
def _make_file(self, rela_path, data, repo=None):
373372
"""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy