@@ -123,9 +123,9 @@ class Repo(object):
123123DAEMON_EXPORT_FILE = "git-daemon-export-ok"
124124
125125git = 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
127127_working_tree_dir : Optional [PathLike ] = None
128- git_dir : PathLike = ""
128+ git_dir : PathLike
129129_common_dir : PathLike = ""
130130
131131# precompiled regex
@@ -215,13 +215,14 @@ def __init__(
215215## Walk up the path to find the `.git` dir.
216216#
217217curpath = epath
218+ _git_dir = None
218219while curpath :
219220# ABOUT osp.NORMPATH
220221# It's important to normalize the paths, as submodules will otherwise initialize their
221222# repo instances with paths that depend on path-portions that will not exist after being
222223# removed. It's just cleaner.
223224if is_git_dir (curpath ):
224- self . git_dir = curpath
225+ _git_dir = curpath
225226# from man git-config : core.worktree
226227# Set the path to the root of the working tree. If GIT_COMMON_DIR environment
227228# variable is set, core.worktree is ignored and not used for determining the
@@ -230,7 +231,7 @@ def __init__(
230231# directory, which is either specified by GIT_DIR, or automatically discovered.
231232# If GIT_DIR is specified but none of GIT_WORK_TREE and core.worktree is specified,
232233# 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 )
234235if os .environ .get ("GIT_COMMON_DIR" ) is None :
235236gitconf = self .config_reader ("repository" )
236237if gitconf .has_option ("core" , "worktree" ):
@@ -242,14 +243,14 @@ def __init__(
242243dotgit = osp .join (curpath , ".git" )
243244sm_gitpath = find_submodule_git_dir (dotgit )
244245if sm_gitpath is not None :
245- self . git_dir = osp .normpath (sm_gitpath )
246+ _git_dir = osp .normpath (sm_gitpath )
246247
247248sm_gitpath = find_submodule_git_dir (dotgit )
248249if sm_gitpath is None :
249250sm_gitpath = find_worktree_git_dir (dotgit )
250251
251252if sm_gitpath is not None :
252- self . git_dir = expand_path (sm_gitpath , expand_vars )
253+ _git_dir = expand_path (sm_gitpath , expand_vars )
253254self ._working_tree_dir = curpath
254255break
255256
@@ -260,8 +261,9 @@ def __init__(
260261break
261262# END while curpath
262263
263- if self . git_dir is None :
264+ if _git_dir is None :
264265raise InvalidGitRepositoryError (epath )
266+ self .git_dir = _git_dir
265267
266268self ._bare = False
267269try :
@@ -282,7 +284,7 @@ def __init__(
282284self ._working_tree_dir = None
283285# END working dir handling
284286
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
286288self .git = self .GitCommandWrapperType (self .working_dir )
287289
288290# special handling, in special times
@@ -320,7 +322,7 @@ def close(self) -> None:
320322gc .collect ()
321323
322324def __eq__ (self , rhs : object ) -> bool :
323- if isinstance (rhs , Repo )and self . git_dir :
325+ if isinstance (rhs , Repo ):
324326return self .git_dir == rhs .git_dir
325327return False
326328
@@ -332,14 +334,12 @@ def __hash__(self) -> int:
332334
333335# Description property
334336def _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" )
337338with open (filename , "rb" ) as fp :
338339return fp .read ().rstrip ().decode (defenc )
339340
340341def _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" )
343343with open (filename , "wb" ) as fp :
344344fp .write ((descr + "\n " ).encode (defenc ))
345345
@@ -357,13 +357,7 @@ def common_dir(self) -> PathLike:
357357"""
358358 :return: The git dir that holds everything except possibly HEAD,
359359 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
367361
368362@property
369363def bare (self ) -> bool :
0 commit comments