Skip to content

Commit

Permalink
Remove optional from two member variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Sineaggi committed Jan 27, 2023
1 parent 63a60b3 commit 737ec27
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ class Repo(object):
DAEMON_EXPORT_FILE = "git-daemon-export-ok"

git = cast("Git", None) # Must exist, or __del__ will fail in case we raise on `__init__()`
working_dir: Optional[PathLike] = None
working_dir: PathLike
_working_tree_dir: Optional[PathLike] = None
git_dir: PathLike = ""
git_dir: PathLike
_common_dir: PathLike = ""

# precompiled regex
Expand Down Expand Up @@ -215,13 +215,14 @@ def __init__(
## Walk up the path to find the `.git` dir.
#
curpath = epath
_git_dir = None
while curpath:
# ABOUT osp.NORMPATH
# It's important to normalize the paths, as submodules will otherwise initialize their
# repo instances with paths that depend on path-portions that will not exist after being
# removed. It's just cleaner.
if is_git_dir(curpath):
self.git_dir = curpath
_git_dir = curpath
# from man git-config : core.worktree
# Set the path to the root of the working tree. If GIT_COMMON_DIR environment
# variable is set, core.worktree is ignored and not used for determining the
Expand All @@ -230,7 +231,7 @@ def __init__(
# directory, which is either specified by GIT_DIR, or automatically discovered.
# If GIT_DIR is specified but none of GIT_WORK_TREE and core.worktree is specified,
# the current working directory is regarded as the top level of your working tree.
self._working_tree_dir = os.path.dirname(self.git_dir)
self._working_tree_dir = os.path.dirname(_git_dir)
if os.environ.get("GIT_COMMON_DIR") is None:
gitconf = self.config_reader("repository")
if gitconf.has_option("core", "worktree"):
Expand All @@ -242,14 +243,14 @@ def __init__(
dotgit = osp.join(curpath, ".git")
sm_gitpath = find_submodule_git_dir(dotgit)
if sm_gitpath is not None:
self.git_dir = osp.normpath(sm_gitpath)
_git_dir = osp.normpath(sm_gitpath)

sm_gitpath = find_submodule_git_dir(dotgit)
if sm_gitpath is None:
sm_gitpath = find_worktree_git_dir(dotgit)

if sm_gitpath is not None:
self.git_dir = expand_path(sm_gitpath, expand_vars)
_git_dir = expand_path(sm_gitpath, expand_vars)
self._working_tree_dir = curpath
break

Expand All @@ -260,8 +261,9 @@ def __init__(
break
# END while curpath

if self.git_dir is None:
if _git_dir is None:
raise InvalidGitRepositoryError(epath)
self.git_dir = _git_dir

self._bare = False
try:
Expand All @@ -282,7 +284,7 @@ def __init__(
self._working_tree_dir = None
# END working dir handling

self.working_dir: Optional[PathLike] = self._working_tree_dir or self.common_dir
self.working_dir: PathLike = self._working_tree_dir or self.common_dir
self.git = self.GitCommandWrapperType(self.working_dir)

# special handling, in special times
Expand Down Expand Up @@ -320,7 +322,7 @@ def close(self) -> None:
gc.collect()

def __eq__(self, rhs: object) -> bool:
if isinstance(rhs, Repo) and self.git_dir:
if isinstance(rhs, Repo):
return self.git_dir == rhs.git_dir
return False

Expand All @@ -332,14 +334,12 @@ def __hash__(self) -> int:

# Description property
def _get_description(self) -> str:
if self.git_dir:
filename = osp.join(self.git_dir, "description")
filename = osp.join(self.git_dir, "description")
with open(filename, "rb") as fp:
return fp.read().rstrip().decode(defenc)

def _set_description(self, descr: str) -> None:
if self.git_dir:
filename = osp.join(self.git_dir, "description")
filename = osp.join(self.git_dir, "description")
with open(filename, "wb") as fp:
fp.write((descr + "\n").encode(defenc))

Expand All @@ -357,13 +357,7 @@ def common_dir(self) -> PathLike:
"""
:return: The git dir that holds everything except possibly HEAD,
FETCH_HEAD, ORIG_HEAD, COMMIT_EDITMSG, index, and logs/."""
if self._common_dir:
return self._common_dir
elif self.git_dir:
return self.git_dir
else:
# or could return ""
raise InvalidGitRepositoryError()
return self._common_dir or self.git_dir

@property
def bare(self) -> bool:
Expand Down

0 comments on commit 737ec27

Please sign in to comment.