@@ -115,17 +115,17 @@ class Repo(object):
115
115
'working_dir' is the working directory of the git command, which is the working tree
116
116
directory if available or the .git directory in case of bare repositories
117
117
118
- 'working_tree_dir' is the working tree directory, but will raise AssertionError
118
+ 'working_tree_dir' is the working tree directory, but will return None
119
119
if we are a bare repository.
120
120
121
121
'git_dir' is the .git repository directory, which is always set."""
122
122
123
123
DAEMON_EXPORT_FILE = "git-daemon-export-ok"
124
124
125
125
git = cast ("Git" , None ) # Must exist, or __del__ will fail in case we raise on `__init__()`
126
- working_dir : Optional [ PathLike ] = None
126
+ working_dir : PathLike
127
127
_working_tree_dir : Optional [PathLike ] = None
128
- git_dir : PathLike = ""
128
+ git_dir : PathLike
129
129
_common_dir : PathLike = ""
130
130
131
131
# precompiled regex
@@ -215,13 +215,14 @@ def __init__(
215
215
## Walk up the path to find the `.git` dir.
216
216
#
217
217
curpath = epath
218
+ git_dir = None
218
219
while curpath :
219
220
# ABOUT osp.NORMPATH
220
221
# It's important to normalize the paths, as submodules will otherwise initialize their
221
222
# repo instances with paths that depend on path-portions that will not exist after being
222
223
# removed. It's just cleaner.
223
224
if is_git_dir (curpath ):
224
- self . git_dir = curpath
225
+ git_dir = curpath
225
226
# from man git-config : core.worktree
226
227
# Set the path to the root of the working tree. If GIT_COMMON_DIR environment
227
228
# variable is set, core.worktree is ignored and not used for determining the
@@ -230,9 +231,9 @@ def __init__(
230
231
# directory, which is either specified by GIT_DIR, or automatically discovered.
231
232
# If GIT_DIR is specified but none of GIT_WORK_TREE and core.worktree is specified,
232
233
# the current working directory is regarded as the top level of your working tree.
233
- self ._working_tree_dir = os .path .dirname (self . git_dir )
234
+ self ._working_tree_dir = os .path .dirname (git_dir )
234
235
if os .environ .get ("GIT_COMMON_DIR" ) is None :
235
- gitconf = self .config_reader ("repository" )
236
+ gitconf = self ._config_reader ("repository" , git_dir )
236
237
if gitconf .has_option ("core" , "worktree" ):
237
238
self ._working_tree_dir = gitconf .get ("core" , "worktree" )
238
239
if "GIT_WORK_TREE" in os .environ :
@@ -242,14 +243,14 @@ def __init__(
242
243
dotgit = osp .join (curpath , ".git" )
243
244
sm_gitpath = find_submodule_git_dir (dotgit )
244
245
if sm_gitpath is not None :
245
- self . git_dir = osp .normpath (sm_gitpath )
246
+ git_dir = osp .normpath (sm_gitpath )
246
247
247
248
sm_gitpath = find_submodule_git_dir (dotgit )
248
249
if sm_gitpath is None :
249
250
sm_gitpath = find_worktree_git_dir (dotgit )
250
251
251
252
if sm_gitpath is not None :
252
- self . git_dir = expand_path (sm_gitpath , expand_vars )
253
+ git_dir = expand_path (sm_gitpath , expand_vars )
253
254
self ._working_tree_dir = curpath
254
255
break
255
256
@@ -260,8 +261,9 @@ def __init__(
260
261
break
261
262
# END while curpath
262
263
263
- if self . git_dir is None :
264
+ if git_dir is None :
264
265
raise InvalidGitRepositoryError (epath )
266
+ self .git_dir = git_dir
265
267
266
268
self ._bare = False
267
269
try :
@@ -282,7 +284,7 @@ def __init__(
282
284
self ._working_tree_dir = None
283
285
# END working dir handling
284
286
285
- self .working_dir : Optional [ PathLike ] = self ._working_tree_dir or self .common_dir
287
+ self .working_dir : PathLike = self ._working_tree_dir or self .common_dir
286
288
self .git = self .GitCommandWrapperType (self .working_dir )
287
289
288
290
# special handling, in special times
@@ -320,7 +322,7 @@ def close(self) -> None:
320
322
gc .collect ()
321
323
322
324
def __eq__ (self , rhs : object ) -> bool :
323
- if isinstance (rhs , Repo ) and self . git_dir :
325
+ if isinstance (rhs , Repo ):
324
326
return self .git_dir == rhs .git_dir
325
327
return False
326
328
@@ -332,14 +334,12 @@ def __hash__(self) -> int:
332
334
333
335
# Description property
334
336
def _get_description (self ) -> str :
335
- if self .git_dir :
336
- filename = osp .join (self .git_dir , "description" )
337
+ filename = osp .join (self .git_dir , "description" )
337
338
with open (filename , "rb" ) as fp :
338
339
return fp .read ().rstrip ().decode (defenc )
339
340
340
341
def _set_description (self , descr : str ) -> None :
341
- if self .git_dir :
342
- filename = osp .join (self .git_dir , "description" )
342
+ filename = osp .join (self .git_dir , "description" )
343
343
with open (filename , "wb" ) as fp :
344
344
fp .write ((descr + "\n " ).encode (defenc ))
345
345
@@ -357,13 +357,7 @@ def common_dir(self) -> PathLike:
357
357
"""
358
358
:return: The git dir that holds everything except possibly HEAD,
359
359
FETCH_HEAD, ORIG_HEAD, COMMIT_EDITMSG, index, and logs/."""
360
- if self ._common_dir :
361
- return self ._common_dir
362
- elif self .git_dir :
363
- return self .git_dir
364
- else :
365
- # or could return ""
366
- raise InvalidGitRepositoryError ()
360
+ return self ._common_dir or self .git_dir
367
361
368
362
@property
369
363
def bare (self ) -> bool :
@@ -532,7 +526,9 @@ def delete_remote(self, remote: "Remote") -> str:
532
526
"""Delete the given remote."""
533
527
return Remote .remove (self , remote )
534
528
535
- def _get_config_path (self , config_level : Lit_config_levels ) -> str :
529
+ def _get_config_path (self , config_level : Lit_config_levels , git_dir : Optional [PathLike ] = None ) -> str :
530
+ if git_dir is None :
531
+ git_dir = self .git_dir
536
532
# we do not support an absolute path of the gitconfig on windows ,
537
533
# use the global config instead
538
534
if is_win and config_level == "system" :
@@ -546,7 +542,7 @@ def _get_config_path(self, config_level: Lit_config_levels) -> str:
546
542
elif config_level == "global" :
547
543
return osp .normpath (osp .expanduser ("~/.gitconfig" ))
548
544
elif config_level == "repository" :
549
- repo_dir = self ._common_dir or self . git_dir
545
+ repo_dir = self ._common_dir or git_dir
550
546
if not repo_dir :
551
547
raise NotADirectoryError
552
548
else :
@@ -575,15 +571,21 @@ def config_reader(
575
571
you know which file you wish to read to prevent reading multiple files.
576
572
:note: On windows, system configuration cannot currently be read as the path is
577
573
unknown, instead the global path will be used."""
578
- files = None
574
+ return self ._config_reader (config_level = config_level )
575
+
576
+ def _config_reader (
577
+ self ,
578
+ config_level : Optional [Lit_config_levels ] = None ,
579
+ git_dir : Optional [PathLike ] = None ,
580
+ ) -> GitConfigParser :
579
581
if config_level is None :
580
582
files = [
581
- self ._get_config_path (cast (Lit_config_levels , f ))
583
+ self ._get_config_path (cast (Lit_config_levels , f ), git_dir )
582
584
for f in self .config_level
583
585
if cast (Lit_config_levels , f )
584
586
]
585
587
else :
586
- files = [self ._get_config_path (config_level )]
588
+ files = [self ._get_config_path (config_level , git_dir )]
587
589
return GitConfigParser (files , read_only = True , repo = self )
588
590
589
591
def config_writer (self , config_level : Lit_config_levels = "repository" ) -> GitConfigParser :
0 commit comments